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
00031
00032
00033
00034
00035
00042 #ifndef COMMONCPP_APPLOG_H_
00043 #define COMMONCPP_APPLOG_H_
00044
00045 #ifndef COMMONCPP_SLOG_H_
00046 #include <commoncpp/slog.h>
00047 #endif
00048
00049 #ifndef COMMONCPP_EXCEPTION_H_
00050 #include <commoncpp/exception.h>
00051 #endif
00052
00053 #include <string>
00054 #include <sstream>
00055 #include <iostream>
00056 #include <map>
00057
00058 NAMESPACE_COMMONCPP
00059 using namespace std;
00068 class __EXPORT HEXdump
00069 {
00070 protected:
00074 std::string _str;
00075
00076 public:
00077
00086 HEXdump(const unsigned char *buffer, int buff_len, int max_len = 200);
00087
00091 virtual ~HEXdump() { _str = string();}
00092
00097 const char * c_str() const
00098 {
00099 return _str.c_str();
00100 }
00101
00105 std::string str()
00106 {
00107 return _str;
00108 }
00109
00115 friend std::ostream& operator<< (std::ostream& out, const HEXdump &hd)
00116 {
00117 out << hd.c_str();
00118 return out;
00119 }
00120
00121 };
00122
00123 #ifdef CCXX_EXCEPTIONS
00124
00128 class __EXPORT AppLogException : public ost::Exception
00129 {
00130 public:
00135 AppLogException(const char *what_arg) : ost::Exception(what_arg) {};
00136
00137 };
00138 #endif
00139
00140 class AppLogPrivate;
00141
00170 class __EXPORT AppLog : protected streambuf, public ostream
00171 {
00172 protected:
00173
00174 AppLogPrivate *d;
00175 void writeLog(bool endOfLine = true);
00176 static std::map<string, Slog::Level> *assoc;
00177
00178 public:
00182 class __EXPORT Ident
00183 {
00184 private:
00185 std::string _ident;
00186 public:
00187
00191 Ident() {};
00192
00196 ~Ident() {};
00197
00201 Ident(Ident& id) {_ident = id._ident;}
00202
00206 Ident(const char *str) : _ident(str) {};
00207
00211 std::string& str() {return _ident;}
00212
00216 Ident& operator= (std::string &st) {_ident = st; return *this;}
00217
00221 Ident& operator= (const char str[]) {_ident = str; return *this;}
00222
00226 const char* c_str() {return _ident.c_str();}
00227 };
00228
00229 #ifndef _MSWINDOWS_
00230
00237 AppLog(const char* logFileName = NULL, bool logDirectly = false , bool usePipe = false);
00238 #else
00239
00245 AppLog(const char* logFileName = NULL, bool logDirectly = false);
00246 #endif
00247
00250 virtual ~AppLog();
00251
00256 void subscribe();
00257
00261 void unsubscribe();
00262
00263 #ifndef _MSWINDOWS_
00264
00271 void logFileName(const char* FileName, bool logDirectly = false, bool usePipe = false);
00272 #else
00273
00279 void logFileName(const char* FileName, bool logDirectly = false);
00280 #endif
00281
00284 void close(void);
00285
00290 void level(Slog::Level enable);
00291
00296 void clogEnable(bool en = true);
00297
00302 void slogEnable(bool en = true);
00303
00309 void identLevel(const char *ident, Slog::Level level);
00310
00315 void open(const char *ident);
00316
00322 virtual int overflow(int c);
00323
00327 virtual int sync();
00328
00333 void emerg(const char *format, ...);
00334
00339 void alert(const char *format, ...);
00340
00345 void critical(const char *format, ...);
00346
00351 void error(const char *format, ...);
00352
00357 void warn(const char *format, ...);
00358
00363 void notice(const char *format, ...);
00364
00369 void info(const char *format, ...);
00370
00375 void debug(const char *format, ...);
00376
00383 AppLog &operator()(const char *ident, Slog::Level level = Slog::levelError);
00384
00390 inline AppLog& operator()(Ident &ident)
00391 {
00392 open(ident.c_str());
00393 return *this;
00394 }
00395
00401 AppLog &operator()(Slog::Level level);
00402
00408 AppLog& operator<< (AppLog& (*pfManipulator)(AppLog&));
00409
00415 AppLog& operator<< (ostream& (*pfManipulator)(ostream&));
00416
00417 friend ostream& operator << (ostream &os, AppLog & al)
00418 {
00419 return al;
00420 }
00421
00427 inline AppLog& operator<< (Ident &ident)
00428 {
00429 open(ident.c_str());
00430 return *this;
00431 }
00432
00433
00438 inline AppLog &warn(void)
00439 {return operator()(Slog::levelWarning);}
00440
00445 AppLog &error(void)
00446 { return operator()(Slog::levelError);}
00447
00452 inline AppLog &debug(void)
00453 {return operator()(Slog::levelDebug);}
00454
00459 inline AppLog &emerg(void)
00460 {return operator()(Slog::levelEmergency);}
00461
00466 inline AppLog &alert(void)
00467 {return operator()(Slog::levelAlert);}
00468
00473 inline AppLog &critical(void)
00474 {return operator()(Slog::levelCritical);}
00475
00480 inline AppLog ¬ice(void)
00481 {return operator()(Slog::levelNotice);}
00482
00487 inline AppLog &info(void)
00488 {return operator()(Slog::levelInfo);}
00489
00505 static Slog::Level levelTranslate(string name)
00506 {
00507 std::map<string, Slog::Level>::iterator it = assoc->find(name);
00508 return (it != assoc->end()) ? it->second : Slog::levelEmergency;
00509 }
00510
00511 };
00512
00518 __EXPORT inline AppLog &debug(AppLog& sl)
00519 {return sl.operator()(Slog::levelDebug);}
00520
00526 __EXPORT inline AppLog &warn(AppLog& sl)
00527 {return sl.operator()(Slog::levelWarning);}
00528
00534 __EXPORT inline AppLog &error(AppLog& sl)
00535 { return sl.operator()(Slog::levelError);}
00536
00542 __EXPORT inline AppLog &emerg(AppLog& sl)
00543 {return sl.operator()(Slog::levelEmergency);}
00544
00550 __EXPORT inline AppLog &alert(AppLog& sl)
00551 {return sl.operator()(Slog::levelAlert);}
00552
00558 __EXPORT inline AppLog &critical(AppLog& sl)
00559 {return sl.operator()(Slog::levelCritical);}
00560
00566 __EXPORT inline AppLog ¬ice(AppLog& sl)
00567 {return sl.operator()(Slog::levelNotice);}
00568
00574 __EXPORT inline AppLog &info(AppLog& sl)
00575 {return sl.operator()(Slog::levelInfo);}
00576
00580 __EXPORT extern AppLog alog;
00581
00582 END_NAMESPACE
00583
00584 #endif //___APPLOG_H___