errlog.h

00001 /*<std-header orig-src='shore' incl-file-exclusion='ERRLOG_H'>
00002 
00003  $Id: errlog.h,v 1.18 2010/05/26 01:20:21 nhall Exp $
00004 
00005 SHORE -- Scalable Heterogeneous Object REpository
00006 
00007 Copyright (c) 1994-99 Computer Sciences Department, University of
00008                       Wisconsin -- Madison
00009 All Rights Reserved.
00010 
00011 Permission to use, copy, modify and distribute this software and its
00012 documentation is hereby granted, provided that both the copyright
00013 notice and this permission notice appear in all copies of the
00014 software, derivative works or modified versions, and any portions
00015 thereof, and that both notices appear in supporting documentation.
00016 
00017 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
00018 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
00019 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
00020 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
00021 
00022 This software was developed with support by the Advanced Research
00023 Project Agency, ARPA order number 018 (formerly 8230), monitored by
00024 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
00025 Further funding for this work was provided by DARPA through
00026 Rome Research Laboratory Contract No. F30602-97-2-0247.
00027 
00028 */
00029 
00030 #ifndef ERRLOG_H
00031 #define ERRLOG_H
00032 
00033 #include "w_defines.h"
00034 
00035 /*  -- do not edit anything above this line --   </std-header>*/
00036 
00037 /* errlog.h -- facilities for logging errors */
00038 
00039 #include <cassert>
00040 #include <cstdlib>
00041 #include <cstddef>
00042 #include <w.h>
00043 #include <iostream>
00044 #include <w_strstream.h>
00045 #include <cstdio>    // XXX just needs a forward decl
00046 
00047 
00048 #ifdef __GNUG__
00049 #pragma interface
00050 #endif
00051 
00052 class ErrLog; // forward
00053 class logstream; // forward
00054 
00055 #ifndef    _SYSLOG_H
00056 #define LOG_EMERG 0 
00057 #define LOG_ALERT 1
00058 #define LOG_CRIT  2
00059 #define LOG_ERR   3
00060 #define LOG_WARNING 4
00061 #define LOG_NOTICE  5
00062 #define LOG_INFO  6
00063 #define LOG_DEBUG 7
00064 #define LOG_USER  8
00065 #endif
00066 
00067 /** \brief A namespace for errlog-related types.
00068  */
00069 namespace shore_errlog {
00070 using namespace std;
00071 
00072 /*!\enum LogPriority
00073  * \brief Enumeration that enables filtering of messages by priority.
00074  *
00075  * The following __omanip functions are defined to correspond 
00076  * to the LogPriority:
00077  * emerg_prio, fatal_prio, internal_prio, 
00078  * error_prio, warning_prio, info_prio, debug_prio
00079  *
00080  * Log messages must end with the new __omanip function
00081  * flushl.
00082  */
00083 enum LogPriority {
00084     log_none = -1,    // none (for global variable logging_level only)
00085     log_emerg = LOG_EMERG,        // no point in continuing (syslog's LOG_EMERG)
00086     log_fatal = LOG_ALERT,        // no point in continuing (syslog's LOG_ALERT)
00087     log_alert = log_fatal,        // alias
00088     log_internal = LOG_CRIT,    // internal error 
00089     log_error = LOG_ERR,        // client error 
00090     log_warning = LOG_WARNING,    // client error 
00091     log_info = LOG_INFO,        // just for yucks 
00092     log_debug=LOG_DEBUG,        // for debugging gory details 
00093     log_all,
00094     default_prio = log_error
00095 };
00096 
00097 } /* namespace syslog_compat */
00098 
00099 using namespace shore_errlog;
00100 
00101 extern ostream& flushl(ostream& o);
00102 extern ostream& emerg_prio(ostream& o);
00103 extern ostream& fatal_prio(ostream& o);
00104 extern ostream& internal_prio(ostream& o);
00105 extern ostream& error_prio(ostream& o);
00106 extern ostream& warning_prio(ostream& o);
00107 extern ostream& info_prio(ostream& o);
00108 extern ostream& debug_prio(ostream& o);
00109 extern void setprio(ostream&, LogPriority);
00110 extern logstream *is_logstream(ostream &);
00111 /** \brief A strstream-based log output stream. 
00112  * \details
00113  * Responds to these iomanip functions :
00114  *  emerg_prio, 
00115  *  fatal_prio, 
00116  *  internal_prio, 
00117  *  error_prio, 
00118  *  warning_prio, 
00119  *  info_prio, 
00120  *  debug_prio
00121  */
00122 class logstream : public w_ostrstream {
00123     friend class ErrLog;
00124     friend ostream &flush_and_setprio(ostream& o, LogPriority p);
00125     friend ostream& emerg_prio(ostream& o);
00126     friend ostream& fatal_prio(ostream& o);
00127     friend ostream& internal_prio(ostream& o);
00128     friend ostream& error_prio(ostream& o);
00129     friend ostream& warning_prio(ostream& o);
00130     friend ostream& info_prio(ostream& o);
00131     friend ostream& debug_prio(ostream& o);
00132 
00133     unsigned int     __magic1;
00134     LogPriority     _prio;
00135     ErrLog*        _log;
00136     unsigned int     __magic2;
00137 
00138 public:
00139 /** \cond skip */
00140     friend logstream *is_logstream(ostream &);
00141 
00142     enum { LOGSTREAM__MAGIC = 0xad12bc45 };
00143 private:
00144     static w_ostrstream static_stream;
00145 public:
00146     logstream(char *buf, size_t bufsize = 1000)
00147     : w_ostrstream(buf, bufsize),
00148       __magic1(LOGSTREAM__MAGIC),
00149       _prio(log_none), 
00150       __magic2(LOGSTREAM__MAGIC)
00151         {
00152             // tie this to a hidden stream; 
00153             tie(&static_stream);
00154             assert(tie() == &static_stream) ;
00155             assert(__magic1==LOGSTREAM__MAGIC);
00156         }
00157 
00158 protected:
00159     void  init_errlog(ErrLog* mine) { _log = mine; }
00160 /** \endcond skip
00161  */
00162 };
00163 
00164 /** \enum LoggingDestination Describes the possible log destinations,
00165  * used for the ErrLog constructor.
00166  */
00167 enum LoggingDestination {
00168     log_to_ether, /*! no logging */
00169     log_to_unix_file, /*! sent to a unix file identified by name */
00170     log_to_open_file, /*! sent to an open unix FILE* */
00171     log_to_stderr  /*! sent to stderr */
00172 }; 
00173 
00174 typedef void (*ErrLogFunc)(ErrLog *, void *);
00175 
00176 /** \brief A syslog-style output stream for logging errors, information, etc.
00177  *
00178  * This output stream is used for issuing errors, "information",
00179  * debugging tracing, etc. to the operator (e.g., stderr) or to
00180  * a file,  somewhat like syslog.
00181  * \attention This predates true multi-threading and is thus not thread-safe.
00182  * We have not yet replaced this code, with a thread-safe version.
00183  * It is still useful for debugging non-timing-dependent issues,
00184  * for issuing operator messages before multithreading really starts, e.g.,
00185  * during recovery.
00186  *
00187  * Example:
00188  * \code
00189  * ErrLog errlog(log_to_unix_file, "sm.errlog");
00190  * errlog->clog << info_prio << "Restart recovery : analysis phase " << flushl;
00191  * \endcode
00192  */
00193 class ErrLog {
00194     friend class logstream;
00195     friend logstream *is_logstream(ostream &);
00196     friend ostream &flush_and_setprio(ostream& o, LogPriority p);
00197 
00198     LoggingDestination    _destination;
00199     LogPriority         _level;
00200     FILE*             _file;        // if local file logging is used
00201     const char *         _ident; // identity for syslog & local use
00202     char *            _buffer; // default is static buffer
00203     size_t            _bufsize; 
00204 
00205     enum { ERRORLOG__MAGIC = 0xa2d29754 };
00206 
00207 public:
00208 
00209     /** Create a log.
00210      * @param[in] ident  The name of the log.
00211      * @param[in] dest   Indicates destination (unix file, stderr, etc).
00212      * @param[in] filename Name of destination or "-' .
00213      * @param[in] level  Minimum priority level of messages to be sent to the file. For filtering.
00214      * @param[in] ownbuf Buffer to use. Default is NULL.
00215      * @param[in] ownbufsz Size of given buffer. Default is 0.
00216      *
00217      * Using the name "-" is the same as specifying log_to_stderr
00218      */
00219     ErrLog(
00220         const char *ident,
00221         LoggingDestination dest, // required
00222         const char *filename = 0,             
00223         LogPriority level =  default_prio,
00224         char *ownbuf = 0,
00225         int  ownbufsz = 0  // length of ownbuf, if ownbuf is given
00226     );
00227 
00228     /** Create a log.
00229      * @param[in] ident  The name of the log.
00230      * @param[in] dest   Indicates destination (unix file, stderr, etc).
00231      * @param[in] file   Already open FILE*.  Default is NULL.
00232      * @param[in] level  Minimum priority level of messages to be sent to the file. For filtering.
00233      * @param[in] ownbuf Buffer to use. Default is NULL.
00234      * @param[in] ownbufsz Size of given buffer. Default is 0.
00235      */
00236     ErrLog(
00237         const char *ident,
00238         LoggingDestination dest, // required
00239         FILE *file = 0,             
00240         LogPriority level =  default_prio,
00241         char *ownbuf = 0,
00242         int  ownbufsz = 0  // length of ownbuf, if ownbuf is given
00243     );
00244 
00245     ~ErrLog();
00246 
00247     /** Convert a char string to an enumeration value.  
00248      * @param[in] arg The string to parse.
00249      * @param[out] ok Returns true/false if parse worked/not (optional)
00250      */
00251     static LogPriority parse(const char *arg, bool *ok=0);
00252 
00253     /** A stream that can be used with operator<<.
00254      * Example:
00255      * \code
00256      * ErrLog E("XXX", log_to_unix_file, "XXX.out");
00257      * E->clog << obj << endl;
00258      * \endcode
00259      */
00260     logstream    clog;
00261 
00262     /** Format and issue a message with the given priority, that
00263      * is, don't issue it unless this priority is equal to or higher 
00264      * than the priority of this error log.
00265      */
00266     void log(enum LogPriority prio, const char *format, ...);
00267 
00268     /** Return the name of the file if known */
00269     const char * ident() { 
00270         return _ident;
00271     }
00272     LoggingDestination    destination() { return _destination; };
00273 
00274     /** Return the current logging level */
00275     LogPriority getloglevel() { return _level; }
00276 
00277     /** Return a static string describing the current logging level */
00278     const char * getloglevelname() {
00279         switch(_level) {
00280             case log_none:
00281                 return "log_none";
00282             case log_emerg:
00283                 return "log_emerg";
00284             case log_fatal:
00285                 return "log_fatal";
00286             case log_internal:
00287                 return "log_internal";
00288             case log_error:
00289                 return "log_error";
00290             case log_warning:
00291                 return "log_warning";
00292             case log_info:
00293                 return "log_info";
00294             case log_debug:
00295                 return "log_debug";
00296             case log_all:
00297                 return "log_all";
00298             default:
00299                 return "error: unknown";
00300                 // w_assert1(0);
00301         }
00302     }
00303 
00304     /** Change the current logging level */
00305     LogPriority setloglevel( LogPriority prio) {
00306         LogPriority old = _level;
00307         _level =  prio;
00308         return old;
00309     }
00310 
00311 private:
00312     void _init1();
00313     void _init2();
00314     void _flush(bool in_crit);
00315     void _openlogfile( const char *filename );
00316     void _closelogfile();
00317     NORET ErrLog(const ErrLog &); // disabled
00318     
00319     unsigned int _magic;
00320 
00321 } /* ErrLog */;
00322 
00323 /*<std-footer incl-file-exclusion='ERRLOG_H'>  -- do not edit anything below this line -- */
00324 
00325 #endif          /*</std-footer>*/

Generated on Wed Jul 7 17:22:32 2010 for Shore Storage Manager by  doxygen 1.4.7