00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef ERRLOG_H
00031 #define ERRLOG_H
00032
00033 #include "w_defines.h"
00034
00035
00036
00037
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>
00046
00047
00048 #ifdef __GNUG__
00049 #pragma interface
00050 #endif
00051
00052 class ErrLog;
00053 class logstream;
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
00068
00069 namespace shore_errlog {
00070 using namespace std;
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 enum LogPriority {
00084 log_none = -1,
00085 log_emerg = LOG_EMERG,
00086 log_fatal = LOG_ALERT,
00087 log_alert = log_fatal,
00088 log_internal = LOG_CRIT,
00089 log_error = LOG_ERR,
00090 log_warning = LOG_WARNING,
00091 log_info = LOG_INFO,
00092 log_debug=LOG_DEBUG,
00093 log_all,
00094 default_prio = log_error
00095 };
00096
00097 }
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
00112
00113
00114
00115
00116
00117
00118
00119
00120
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
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
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
00161
00162 };
00163
00164
00165
00166
00167 enum LoggingDestination {
00168 log_to_ether,
00169 log_to_unix_file,
00170 log_to_open_file,
00171 log_to_stderr
00172 };
00173
00174 typedef void (*ErrLogFunc)(ErrLog *, void *);
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
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;
00201 const char * _ident;
00202 char * _buffer;
00203 size_t _bufsize;
00204
00205 enum { ERRORLOG__MAGIC = 0xa2d29754 };
00206
00207 public:
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 ErrLog(
00220 const char *ident,
00221 LoggingDestination dest,
00222 const char *filename = 0,
00223 LogPriority level = default_prio,
00224 char *ownbuf = 0,
00225 int ownbufsz = 0
00226 );
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 ErrLog(
00237 const char *ident,
00238 LoggingDestination dest,
00239 FILE *file = 0,
00240 LogPriority level = default_prio,
00241 char *ownbuf = 0,
00242 int ownbufsz = 0
00243 );
00244
00245 ~ErrLog();
00246
00247
00248
00249
00250
00251 static LogPriority parse(const char *arg, bool *ok=0);
00252
00253
00254
00255
00256
00257
00258
00259
00260 logstream clog;
00261
00262
00263
00264
00265
00266 void log(enum LogPriority prio, const char *format, ...);
00267
00268
00269 const char * ident() {
00270 return _ident;
00271 }
00272 LoggingDestination destination() { return _destination; };
00273
00274
00275 LogPriority getloglevel() { return _level; }
00276
00277
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
00301 }
00302 }
00303
00304
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 &);
00318
00319 unsigned int _magic;
00320
00321 } ;
00322
00323
00324
00325 #endif