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 <cstdio>
00045 #include <w_pthread.h>
00046
00047 #ifdef __GNUG__
00048 #pragma interface
00049 #endif
00050
00051 class ErrLog;
00052 class logstream;
00053
00054 #ifndef _SYSLOG_H
00055 #define LOG_EMERG 0
00056 #define LOG_ALERT 1
00057 #define LOG_CRIT 2
00058 #define LOG_ERR 3
00059 #define LOG_WARNING 4
00060 #define LOG_NOTICE 5
00061 #define LOG_INFO 6
00062 #define LOG_DEBUG 7
00063 #define LOG_USER 8
00064 #endif
00065
00066
00067
00068 namespace shore_errlog {
00069 using namespace std;
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 enum LogPriority {
00083 log_none = -1,
00084 log_emerg = LOG_EMERG,
00085 log_fatal = LOG_ALERT,
00086 log_alert = log_fatal,
00087 log_internal = LOG_CRIT,
00088 log_error = LOG_ERR,
00089 log_warning = LOG_WARNING,
00090 log_info = LOG_INFO,
00091 log_debug=LOG_DEBUG,
00092 log_all,
00093 default_prio = log_error
00094 };
00095
00096 }
00097
00098 using namespace shore_errlog;
00099
00100 extern ostream& flushl(ostream& o);
00101 extern ostream& emerg_prio(ostream& o);
00102 extern ostream& fatal_prio(ostream& o);
00103 extern ostream& internal_prio(ostream& o);
00104 extern ostream& error_prio(ostream& o);
00105 extern ostream& warning_prio(ostream& o);
00106 extern ostream& info_prio(ostream& o);
00107 extern ostream& debug_prio(ostream& o);
00108 extern void setprio(ostream&, LogPriority);
00109 extern logstream *is_logstream(std::basic_ostream<char, std::char_traits<char > > &);
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 class logstream : public w_ostrstream {
00122 friend class ErrLog;
00123 friend ostream &flush_and_setprio(ostream& o, LogPriority p);
00124 friend ostream& emerg_prio(ostream& o);
00125 friend ostream& fatal_prio(ostream& o);
00126 friend ostream& internal_prio(ostream& o);
00127 friend ostream& error_prio(ostream& o);
00128 friend ostream& warning_prio(ostream& o);
00129 friend ostream& info_prio(ostream& o);
00130 friend ostream& debug_prio(ostream& o);
00131
00132 unsigned int __magic1;
00133 LogPriority _prio;
00134 ErrLog* _log;
00135 unsigned int __magic2;
00136
00137 public:
00138
00139 friend logstream *is_logstream(std::basic_ostream<char, std::char_traits<char > > &);
00140
00141 enum { LOGSTREAM__MAGIC = 0xad12bc45 };
00142 private:
00143
00144 public:
00145 logstream(char *buf, size_t bufsize = 1000)
00146 : w_ostrstream(buf, bufsize),
00147 __magic1(LOGSTREAM__MAGIC),
00148 _prio(log_none),
00149 __magic2(LOGSTREAM__MAGIC)
00150 {
00151
00152
00153
00154 assert(__magic1==LOGSTREAM__MAGIC);
00155 }
00156
00157 protected:
00158 void init_errlog(ErrLog* mine) { _log = mine; }
00159
00160
00161 };
00162
00163
00164
00165
00166 enum LoggingDestination {
00167 log_to_ether,
00168 log_to_unix_file,
00169 log_to_open_file,
00170 log_to_stderr
00171 };
00172
00173 typedef void (*ErrLogFunc)(ErrLog *, void *);
00174
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(std::basic_ostream<char, std::char_traits<char > > &);
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 unsigned int _magic;
00205 pthread_mutex_t* _errlog_mutex;
00206
00207 enum { ERRORLOG__MAGIC = 0xa2d29754 };
00208
00209 public:
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 ErrLog(
00222 const char *ident,
00223 LoggingDestination dest,
00224 const char *filename = 0,
00225 LogPriority level = default_prio,
00226 char *ownbuf = 0,
00227 int ownbufsz = 0
00228 );
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 ErrLog(
00239 const char *ident,
00240 LoggingDestination dest,
00241 FILE *file = 0,
00242 LogPriority level = default_prio,
00243 char *ownbuf = 0,
00244 int ownbufsz = 0
00245 );
00246
00247 ~ErrLog();
00248
00249
00250
00251
00252
00253 static LogPriority parse(const char *arg, bool *ok=0);
00254
00255
00256
00257
00258
00259
00260
00261
00262 logstream clog;
00263
00264
00265
00266
00267
00268 void log(enum LogPriority prio, const char *format, ...);
00269
00270
00271 const char * ident() {
00272 return _ident;
00273 }
00274 LoggingDestination destination() { return _destination; };
00275
00276
00277 LogPriority getloglevel() { return _level; }
00278
00279
00280 const char * getloglevelname() {
00281 switch(_level) {
00282 case log_none:
00283 return "log_none";
00284 case log_emerg:
00285 return "log_emerg";
00286 case log_fatal:
00287 return "log_fatal";
00288 case log_internal:
00289 return "log_internal";
00290 case log_error:
00291 return "log_error";
00292 case log_warning:
00293 return "log_warning";
00294 case log_info:
00295 return "log_info";
00296 case log_debug:
00297 return "log_debug";
00298 case log_all:
00299 return "log_all";
00300 default:
00301 return "error: unknown";
00302
00303 }
00304 }
00305
00306
00307 LogPriority setloglevel( LogPriority prio);
00308
00309 private:
00310 void _init1();
00311 void _init2();
00312 void _flush();
00313 void _openlogfile( const char *filename );
00314 void _closelogfile();
00315 NORET ErrLog(const ErrLog &);
00316
00317
00318 } ;
00319
00320
00321
00322 #endif