/* *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* */ /* ** Copyright UCAR (c) 1990 - 2016 */ /* ** University Corporation for Atmospheric Research (UCAR) */ /* ** National Center for Atmospheric Research (NCAR) */ /* ** Boulder, Colorado, USA */ /* ** BSD licence applies - redistribution and use in source and binary */ /* ** forms, with or without modification, are permitted provided that */ /* ** the following conditions are met: */ /* ** 1) If the software is modified to produce derivative works, */ /* ** such modified software should be clearly marked, so as not */ /* ** to confuse it with the version available from UCAR. */ /* ** 2) Redistributions of source code must retain the above copyright */ /* ** notice, this list of conditions and the following disclaimer. */ /* ** 3) Redistributions in binary form must reproduce the above copyright */ /* ** notice, this list of conditions and the following disclaimer in the */ /* ** documentation and/or other materials provided with the distribution. */ /* ** 4) Neither the name of UCAR nor the names of its contributors, */ /* ** if any, may be used to endorse or promote products derived from */ /* ** this software without specific prior written permission. */ /* ** DISCLAIMER: THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS */ /* ** OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */ /* ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ /* *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* */ /* ERR.c */ #include #include #include #include #include #include #include #include #include #include #include #define MAX_LOGFILE_NAME 60 #define MAX_PRIORITIES 7 #define MAX_USERFUNCS 5 /* global variables */ static char *Application = "Application Name Unknown"; static char Logfile[MAX_LOGFILE_NAME] = "errlog"; static int Log = FALSE; static int Override = FALSE; static int Standard = TRUE; static int SysLog = FALSE; static int User = FALSE; static ERRuserFuncp UserFunc[ MAX_USERFUNCS] = { NULL, NULL, NULL, NULL, NULL}; static int UserOn[ MAX_USERFUNCS]; static int UserCount = 0; static char *PriorityName[ MAX_PRIORITIES] = {"EMERGENCY!!!", "ALERT!", "RESOURCE EXHAUSTED", "PROGRAM ERROR", "Warning", "Information Message", "Debug" }; static int PriorityLevel[ MAX_PRIORITIES] = { LOG_EMERG, LOG_ALERT, LOG_ERR, LOG_ERR, LOG_WARNING, LOG_INFO, LOG_DEBUG }; #define LOG_FACILITY LOG_LOCAL0 static char *GetTime( void) { time_t t; time( &t); return( ctime( &t)); } static void SysLogOn(void) { static int first = 1; if(first) { openlog(Application, LOG_PID, LOG_FACILITY); first = 0; } SysLog = TRUE; } void ERRinit( char *applic, int argc, char *argv[]) { int i; int process = FALSE; int on = TRUE; int from_main = TRUE; if (applic != NULL) { if (NULL != (Application = malloc( strlen( applic) + 1))) strcpy( Application, applic); STRncopy( Logfile, applic, MAX_LOGFILE_NAME); STRconcat( Logfile, ".errlog", MAX_LOGFILE_NAME); } else from_main = FALSE; for (i=0; i errno = %d\n", Logfile, errno); Standard = TRUE; Log = FALSE; } } if (Log) { fputs( buffer, Logf); fflush( Logf); } } /* send to standard error */ if (Standard) { fputs( buffer, stderr); fflush( stderr); } /* user processing */ if (User) { int i; for (i=0; i= MAX_PRIORITIES)) level = ERR_PROGRAM; /* construct the text to display, using the variable list of args */ va_start( args, format); vsprintf( buffer, format, args); va_end( args); /* call the outputter */ SendOut( level, buffer); } int ERRuser( ERRuserFuncp user) { if (UserCount < MAX_USERFUNCS) { UserFunc[ UserCount] = user; UserOn[ UserCount] = TRUE; UserCount++; } return (UserCount-1 ); } void ERRuserActive( int usr_idx, int turnon) { if ((usr_idx >= 0) && (usr_idx < MAX_USERFUNCS)) UserOn[ usr_idx] = turnon; } #ifdef TEST void Special( int priority, char *message) { printf("begin special\npriority %d <%s> end special\n", priority, message); } void main( argc, argv) int argc; char *argv[]; { ERRinit( "Test ERR", argc, argv); /* send only to stderr */ ERRprintf(ERR_PROGRAM, "Test this %d\n <%s>", 999, "stderr only"); /* send only to file errlog */ ERRcontrol( "OFF STD ON LOG"); ERRprintf(ERR_ALERT, "Test this %d\n <%s>", 999, "logger only"); /* also to special processing routine */ ERRuser( Special); ERRcontrol( "ON USR"); ERRprintf(ERR_RESOURCE, "Test this %d\n <%s>", 999, "logger and special"); /* send to system log */ ERRcontrol("OFF LOG USER ON STD SLOG"); ERRprintf(ERR_INFO, "Test this %d\n <%s>", 999, "standard and slogger"); } #endif