[Opensrf-commits] r981 - trunk/src/libopensrf

svn at svn.open-ils.org svn at svn.open-ils.org
Thu Jun 28 22:27:51 EDT 2007


Author: miker
Date: 2007-06-28 22:24:47 -0400 (Thu, 28 Jun 2007)
New Revision: 981

Modified:
   trunk/src/libopensrf/log.c
Log:
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.



Modified: trunk/src/libopensrf/log.c
===================================================================
--- trunk/src/libopensrf/log.c	2007-06-28 03:14:06 UTC (rev 980)
+++ trunk/src/libopensrf/log.c	2007-06-29 02:24:47 UTC (rev 981)
@@ -1,5 +1,8 @@
 #include <opensrf/log.h>
 
+#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 @@
         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 @@
    }
 }
 
-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 @@
    }
 }
 
-char* osrfLogGetXid() {
+char* osrfLogGetXid( void ) {
    return _osrfLogXid;
 }
 
@@ -93,6 +96,22 @@
 	}
 }
 
+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 @@
 
 	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;
 	}
 



More information about the opensrf-commits mailing list