From: miker Date: Fri, 29 Jun 2007 02:24:47 +0000 (+0000) Subject: arg ... Patch from Scott McKellar that: X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=95c9e63f1202ac90f1b05cadc98a18c8a99a55fd;p=working%2FOpenSRF.git arg ... Patch from Scott McKellar that: 1. When we log messages to a log file, we open and close the file for each message. If the open fails, we write a message to stderr about the failure to open, but we discard the original message that we were asked to issue. With this patch, after issuing the message about the failure to open, we write the original message to stderr. I believe that this change plugs the last message leak. Now if we're asked to issue a message, by golly we're going to issue it, one way or another. Of course the user may still not see the message, either through inattention or, for example, because stderr is redirected to /dev/null. In that case the user is just a poo-poo head, and I have no sympathy for him. 2. In an earlier post I proposed to change osrfLogSetType() so that it would return the previous log type. That way one could temporarily change the log type and then restore it later, without knowing in advance what to restore it to. Upon further reflection I decided that the only plausible use for this trick would be to reroute messages to standard error, and I might as well provide a mechanism limited to this purpose. Accordingly I created two new functions and prototyped them in log.h: osrfLogToStderr() reroutes messages to stderr, and saves the previous log type internally. osrfRestoreLogType() restores the log type previously saved, if any. This interface provides fewer ways for the calling code to mess up than what I had originally contemplated. First, the calling code doesn't have to keep track of the previous log type. Second, if the messages are already rerouted, osrfLogToStderr() will do nothing. One needn't worry about nested reroutings, or maintaining a stack of log types, or anything of the sort. If we ever need anything fancier we can build it, but I don't think we will. 3. Wherever a function takes no parameters, I coded "void" as the formal parameter list. Otherwise the compiler doesn't know whether the function takes parameters or not. git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@981 9efc2488-bf62-4759-914b-345cdb29e865 --- diff --git a/src/libopensrf/log.c b/src/libopensrf/log.c index 10f2d30..94c5359 100644 --- a/src/libopensrf/log.c +++ b/src/libopensrf/log.c @@ -1,5 +1,8 @@ #include +#define OSRF_NO_LOG_TYPE -1 + +static int _prevLogType = OSRF_NO_LOG_TYPE; static int _osrfLogType = OSRF_LOG_TYPE_STDERR; static int _osrfLogFacility = LOG_LOCAL0; static int _osrfLogActFacility = LOG_LOCAL1; @@ -22,7 +25,7 @@ static void _osrfLogSetXid( const char* xid ); VA_LIST_TO_STRING(m); \ _osrfLogDetail( l, f, li, VA_BUF ); -void osrfLogCleanup() { +void osrfLogCleanup( void ) { free(_osrfLogAppname); _osrfLogAppname = NULL; free(_osrfLogFile); @@ -46,12 +49,12 @@ static void _osrfLogSetXid( const char* xid ) { } } -void osrfLogClearXid() { _osrfLogSetXid(""); } +void osrfLogClearXid( void ) { _osrfLogSetXid(""); } void osrfLogSetXid(char* xid) { if(!_osrfLogIsClient) _osrfLogSetXid(xid); } -void osrfLogMkXid() { +void osrfLogMkXid( void ) { if(_osrfLogIsClient) { static int _osrfLogXidInc = 0; /* increments with each new xid for uniqueness */ char buf[32]; @@ -62,7 +65,7 @@ void osrfLogMkXid() { } } -char* osrfLogGetXid() { +char* osrfLogGetXid( void ) { return _osrfLogXid; } @@ -93,6 +96,22 @@ static void osrfLogSetType( int logtype ) { } } +void osrfLogToStderr( void ) +{ + if( OSRF_NO_LOG_TYPE == _prevLogType ) { + _prevLogType = _osrfLogType; + _osrfLogType = OSRF_LOG_TYPE_STDERR; + } +} + +void osrfRestoreLogType( void ) +{ + if( _prevLogType != OSRF_NO_LOG_TYPE ) { + _osrfLogType = _prevLogType; + _prevLogType = OSRF_NO_LOG_TYPE; + } +} + void osrfLogSetFile( const char* logfile ) { if(!logfile) return; if(_osrfLogFile) free(_osrfLogFile); @@ -237,7 +256,10 @@ static void _osrfLogToFile( const char* msg, ... ) { FILE* file = fopen(_osrfLogFile, "a"); if(!file) { - fprintf(stderr, "Unable to fopen log file %s for writing\n", _osrfLogFile); + fprintf(stderr, + "Unable to fopen log file %s for writing; logging to standard error\n", _osrfLogFile); + fprintf(stderr, "%s %s %s\n", _osrfLogAppname, datebuf, VA_BUF ); + return; }