[omniORB] omniORB::log and omniORB::logger and syslog

Michael J. Accetta mja@laurelnetworks.com
Wed, 31 Oct 2001 20:13:59 -0500


This is a multi-part message in MIME format.
--------------3E36FCA9E3A9234EE589A07B
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

David Byron wrote:

> I'm using omniORB 3.0.4 and I'd like to change omniORB::log and
> omniORB::logger to use syslog instead of doing an fprintf on stderr.  I'm
> using a system without an MMU, so doing some redirection to a program that
> will call syslog for me is going to be complicated and take more memory than
> just changing the code to call syslog directory.
>
> Has anyone thought about doing this?  It looks like the changes are all
> happily local to src/lib/omniORB2/orbcore/logIOStream.cc.  Is that what
> other folks think as well?

...

We made a change similar to this last year, I've attached the diff in case it
is useful to you.  Basically, the change routes all log messages through a
single mechanism and then provides a hook  to override the default print to
stderr operation used in the flush operation which actually sends the data to
the log destination.

Mike Accetta
Laurel Networks

--------------3E36FCA9E3A9234EE589A07B
Content-Type: text/plain; charset=us-ascii;
 name="logging.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="logging.diff"

==== //depot/comp/CORBA/omniORB_300/include/omniORB3/omniORB.h#3 (text) - //depot/comp/CORBA/omniORB_300/include/omniORB3/omniORB.h#4 (text) ==== content
@@ -812,6 +812,9 @@
     // Flushes the logger -- it can then be re-used for another
     // message.
 
+    static void configure(void (*)(const char*), const char* prefix);
+    // set default logger and prefix
+
   private:
     logger(const logger&);
     logger& operator=(const logger&);
@@ -823,8 +826,20 @@
     char*       pd_buf;
     char*       pd_p;      // assert(*pd_p == '\0')
     char*       pd_end;    // assert(pd_p < pd_end)
+
+    void      (*pd_print)(const char*);
+    // print the current pd_buf
+
+    static const char* _configPrefix;
+    static void      (*_configPrint)(const char*);
+    // current print and prefix for new logger objects
+
+    static void        _stderrPrint(const char*);
+    // default print implementation
   };
 
+  _CORBA_MODULE_VAR _core_attr logger& defaultLogger;
+  // default logger
 
   _CORBA_MODULE_FN void logf(const char* fmt ...);
   // Writes log message with prefix, and appends '\n'.
==== //depot/comp/CORBA/omniORB_300/src/lib/omniORB2/orbcore/logIOstream.cc#3 (text) - //depot/comp/CORBA/omniORB_300/src/lib/omniORB2/orbcore/logIOstream.cc#4 (text) ==== content
@@ -76,6 +76,7 @@
 #include <ctype.h>
 
 static omniORB::logStream _log;
+static omniORB::logger _logger;
 
 #if defined(HAS_Cplusplus_Namespace) && defined(_MSC_VER)
 // MSVC++ does not give the variables external linkage otherwise. Its a bug.
@@ -90,6 +91,7 @@
 
 #endif
 
+omniORB::logger& omniORB::defaultLogger = _logger;
 
 omniORB::logStream::logStream() : pd_state(0)
 {
@@ -102,7 +104,7 @@
 omniORB::logStream& 
 omniORB::logStream::operator<<(char c)
 {
-  fprintf(stderr,"%c",c);
+  defaultLogger << c;
   return *this;
 }
 
@@ -110,42 +112,42 @@
 omniORB::logStream&
 omniORB::logStream::operator<<(const char *s)
 {
-  fprintf(stderr,"%s",s);
+  defaultLogger <<s;
   return *this;
 }
 
 omniORB::logStream&
 omniORB::logStream::operator<<(const void *p)
 {
-  fprintf(stderr,"%p",p);
+  defaultLogger << p;
   return *this;
 }
 
 omniORB::logStream&
 omniORB::logStream::operator<<(int n)
 {
-  fprintf(stderr,"%d",n);
+  defaultLogger << n;
   return *this;
 }
 
 omniORB::logStream&
 omniORB::logStream::operator<<(unsigned int n)
 {
-  fprintf(stderr,"%u",n);
+  defaultLogger << n;
   return *this;
 }
 
 omniORB::logStream&
 omniORB::logStream::operator<<(long n)
 {
-  fprintf(stderr,"%ld",n);
+  defaultLogger << n;
   return *this;
 }
 
 omniORB::logStream&
 omniORB::logStream::operator<<(unsigned long n)
 {
-  fprintf(stderr,"%lu",n);
+  defaultLogger << n;
   return *this;
 }
 
@@ -169,7 +171,7 @@
 omniORB::logStream&
 omniORB::logStream::operator<<(double n)
 {
-  fprintf(stderr,"%g",n);
+  defaultLogger << n;
   return *this;
 }
 
@@ -187,7 +189,7 @@
 omniORB::logStream&
 omniORB::logStream::flush()
 {
-  fflush(stderr);
+  defaultLogger.flush();
   return *this;
 }
 
@@ -202,22 +204,64 @@
 
 #define PREFIX           "omniORB: "
 
+void
+omniORB::logger::_stderrPrint(const char* p) {
+   fprintf(stderr, "%s", p);
+}
+
+
+//
+// These are static data members used for logger configuration process-wide
+// and not intended to be altered after initial configuration in any
+// multi-threaded enviornment.
+//
+const char* omniORB::logger::_configPrefix = PREFIX;
+void      (*omniORB::logger::_configPrint)(const char*) = _stderrPrint;
+
 
 omniORB::logger::logger(const char* prefix)
   : pd_prefix(prefix), pd_buf(new char[INIT_BUF_SIZE])
 {
-  if( !pd_prefix )  pd_prefix = PREFIX;
+  if( !pd_prefix )  pd_prefix = _configPrefix;
 
   strcpy(pd_buf, pd_prefix);
   pd_p = pd_buf + strlen(pd_prefix);
   pd_end = pd_buf + INIT_BUF_SIZE;
+  pd_print = _configPrint;
 }
 
 
+//
+// Laurel needs to hook its own logging into this package.  We accomplish
+// this by altering the logStream methods to use a default logger object
+// so that all omniORB logging, whether it be through the logStream or logger,
+// interfaces, goes through the logger class.  Then we use this new configure
+// hook to adjust the prefix and print function associated with any logger
+// object.  The logf() and do_logs() functions are also adjusted to use the
+// default logger object.  If configure() is never invoked, logging remains
+// as before directed to stderr using the compile-time PREFIX.
+//
+// MJA 09/20/00
+//
+
+void
+omniORB::logger::configure(void (*print)(const char*), const char* prefix) {
+    _configPrint = print;
+    _configPrefix = prefix;
+
+    // adjust the parameters for the static default logger as well
+    defaultLogger.pd_print = print;
+    int len = strlen(prefix);
+    defaultLogger.reserve(len);
+    strcpy(defaultLogger.pd_buf, prefix);
+    defaultLogger.pd_p = defaultLogger.pd_buf + len;
+}
+
+
 omniORB::logger::~logger()
 {
   if( (size_t)(pd_p - pd_buf) != strlen(pd_prefix) )
-    fprintf(stderr, "%s", pd_buf);
+    pd_print(pd_buf);
 
   delete[] pd_buf;
 }
@@ -377,7 +421,7 @@
 omniORB::logger::flush()
 {
   if( (size_t)(pd_p - pd_buf) != strlen(pd_prefix) )
-    fprintf(stderr, "%s", pd_buf);
+    pd_print(pd_buf);
 
   pd_p = pd_buf + strlen(pd_prefix);
   *pd_p = '\0';
@@ -414,22 +458,22 @@
 void
 omniORB::logf(const char* fmt ...)
 {
-  char inlinebuf[INLINE_BUF_SIZE];
-  char* buf = inlinebuf;
-  size_t fmtlen = strlen(fmt) + sizeof(PREFIX) + 1;
-
-  if( fmtlen > INLINE_BUF_SIZE )  buf = new char[fmtlen];
-
-  strcpy(buf, PREFIX);
-  strcpy(buf + sizeof(PREFIX) - 1, fmt);
-  strcat(buf, "\n");
+  int vlen = 64;	// will start*2 (at 128)
+  char* vbuf=0;
 
   va_list args;
-  va_start(args, fmt);
-  vfprintf(stderr, buf, args);
-  va_end(args);
-
-  if( buf != inlinebuf )  delete[] buf;
+  int len;
+  do {
+      delete[] vbuf;
+      vlen *= 2;
+      vbuf = new char[vlen];
+      va_start(args, fmt);
+      len = vsnprintf(vbuf, vlen, fmt, args);
+      va_end(args);
+  } while (len >= vlen);
+  defaultLogger<<vbuf<<"\n";
+  defaultLogger.flush();
+  delete[] vbuf;
 }
 #endif
 
@@ -437,19 +481,8 @@
 void
 omniORB::do_logs(const char* fmt)
 {
-  char inlinebuf[INLINE_BUF_SIZE];
-  char* buf = inlinebuf;
-  size_t fmtlen = strlen(fmt) + sizeof(PREFIX) + 1;
-
-  if( fmtlen > INLINE_BUF_SIZE )  buf = new char[fmtlen];
-
-  strcpy(buf, PREFIX);
-  strcpy(buf + sizeof(PREFIX) - 1, fmt);
-  strcat(buf, "\n");
-
-  fprintf(stderr, "%s", buf);
-
-  if( buf != inlinebuf )  delete[] buf;
+  defaultLogger<<fmt<<"\n";
+  defaultLogger.flush();
 }
 
 //////////////////////////////////////////////////////////////////////

--------------3E36FCA9E3A9234EE589A07B--