ucommon
commoncpp/applog.h
Go to the documentation of this file.
00001 // Copyright (C) 2005-2010 Angelo Naselli, Penta Engineering s.r.l.
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010 // GNU General Public License for more details.
00011 //
00012 // You should have received a copy of the GNU General Public License
00013 // along with this program; if not, write to the Free Software
00014 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00015 //
00016 // As a special exception, you may use this file as part of a free software
00017 // library without restriction.  Specifically, if other files instantiate
00018 // templates or use macros or inline functions from this file, or you compile
00019 // this file and link it with other files to produce an executable, this
00020 // file does not by itself cause the resulting executable to be covered by
00021 // the GNU General Public License.  This exception does not however
00022 // invalidate any other reasons why the executable file might be covered by
00023 // the GNU General Public License.
00024 //
00025 // This exception applies only to the code released under the name GNU
00026 // Common C++.  If you copy code from other releases into a copy of GNU
00027 // Common C++, as the General Public License permits, the exception does
00028 // not apply to the code that you add in this way.  To avoid misleading
00029 // anyone as to the status of such modified files, you must delete
00030 // this exception notice from them.
00031 //
00032 // If you write modifications of your own for GNU Common C++, it is your choice
00033 // whether to permit this exception to apply to your modifications.
00034 // If you do not wish that, delete this exception notice.
00035 //
00036 
00043 #ifndef COMMONCPP_APPLOG_H_
00044 #define COMMONCPP_APPLOG_H_
00045 
00046 #ifndef COMMONCPP_SLOG_H_
00047 #include <commoncpp/slog.h>
00048 #endif
00049 
00050 #ifndef COMMONCPP_EXCEPTION_H_
00051 #include <commoncpp/exception.h>
00052 #endif
00053 
00054 #include <string>
00055 #include <sstream>
00056 #include <iostream>
00057 #include <map>
00058 
00059 NAMESPACE_COMMONCPP
00060 using namespace std;
00069 class __EXPORT HEXdump
00070 {
00071   protected:
00075     std::string _str;
00076 
00077   public:
00078     // max_len: max number of bytes to be printed. 0 prints all.
00087     HEXdump(const unsigned char *buffer, int buff_len, int max_len = 200);
00088 
00092     virtual ~HEXdump() { _str = string();}
00093 
00098     const char * c_str() const
00099     {
00100       return _str.c_str();
00101     }
00102 
00106     std::string str()
00107     {
00108       return _str;
00109     }
00110 
00116     friend std::ostream& operator<< (std::ostream& out, const HEXdump &hd)
00117     {
00118       out << hd.c_str();
00119       return out;
00120     }
00121 
00122 };
00123 
00124 #ifdef  CCXX_EXCEPTIONS
00125 
00129 class __EXPORT AppLogException : public ost::Exception
00130 {
00131   public:
00136     AppLogException(String &what_arg) : ost::Exception(what_arg) {};
00137 
00138 };
00139 #endif
00140 
00141 class AppLogPrivate;
00142 
00171 class __EXPORT AppLog : protected streambuf, public ostream
00172 {
00173   protected:
00174     // d pointer
00175     AppLogPrivate *d;
00176     void writeLog(bool endOfLine = true);
00177     static std::map<string, Slog::Level> *assoc;
00178 
00179   public:
00183     class __EXPORT Ident
00184     {
00185       private:
00186         std::string _ident;
00187       public:
00188 
00192         Ident() {};
00193 
00197         ~Ident() {};
00198 
00202         Ident(Ident& id) {_ident = id._ident;}
00203 
00207         Ident(const char *str) : _ident(str) {};
00208 
00212         std::string& str() {return _ident;}
00213 
00217         Ident& operator= (std::string &st) {_ident = st; return *this;}
00218 
00222         Ident& operator= (const char str[]) {_ident = str; return *this;}
00223 
00227         const char* c_str() {return _ident.c_str();}
00228     };
00229 
00230 #ifndef _MSWINDOWS_
00231 
00238     AppLog(const char* logFileName = NULL, bool logDirectly = false , bool usePipe = false);
00239 #else
00240 
00246     AppLog(const char* logFileName = NULL, bool logDirectly = false);
00247 #endif
00248 
00251     virtual ~AppLog();
00252 
00257     void subscribe();
00258 
00262     void unsubscribe();
00263 
00264 #ifndef _MSWINDOWS_
00265 
00272     void logFileName(const char* FileName, bool logDirectly = false, bool usePipe = false);
00273 #else
00274 
00280     void logFileName(const char* FileName, bool logDirectly = false);
00281 #endif
00282 
00285     void close(void);
00286 
00291     void level(Slog::Level enable);
00292 
00297     void clogEnable(bool en = true);
00298 
00303     void slogEnable(bool en = true);
00304 
00310     void identLevel(const char *ident, Slog::Level level);
00311 
00316     void open(const char *ident);
00317 
00323     virtual int overflow(int c);
00324 
00328     virtual int sync();
00329 
00334     void emerg(const char *format, ...);
00335 
00340     void alert(const char *format, ...);
00341 
00346     void critical(const char *format, ...);
00347 
00352     void error(const char *format, ...);
00353 
00358     void warn(const char *format, ...);
00359 
00364     void notice(const char *format, ...);
00365 
00370     void info(const char *format, ...);
00371 
00376     void debug(const char *format, ...);
00377 
00384     AppLog &operator()(const char *ident, Slog::Level level = Slog::levelError);
00385 
00391     inline AppLog& operator()(Ident &ident)
00392     {
00393       open(ident.c_str());
00394       return *this;
00395     }
00396 
00402     AppLog &operator()(Slog::Level level);
00403 
00409     AppLog& operator<< (AppLog& (*pfManipulator)(AppLog&));
00410 
00416     AppLog& operator<< (ostream& (*pfManipulator)(ostream&));
00417 
00418     friend  ostream& operator << (ostream &out, AppLog & al)
00419     {
00420       return al;
00421     }
00422 
00428     inline AppLog& operator<< (Ident &ident)
00429     {
00430       open(ident.c_str());
00431       return *this;
00432     }
00433 
00434 
00439     inline AppLog &warn(void)
00440     {return operator()(Slog::levelWarning);}
00441 
00446     AppLog &error(void)
00447     { return operator()(Slog::levelError);}
00448 
00453     inline AppLog &debug(void)
00454     {return operator()(Slog::levelDebug);}
00455 
00460     inline AppLog &emerg(void)
00461     {return operator()(Slog::levelEmergency);}
00462 
00467     inline AppLog &alert(void)
00468     {return operator()(Slog::levelAlert);}
00469 
00474     inline AppLog &critical(void)
00475     {return operator()(Slog::levelCritical);}
00476 
00481     inline AppLog &notice(void)
00482     {return operator()(Slog::levelNotice);}
00483 
00488     inline AppLog &info(void)
00489     {return operator()(Slog::levelInfo);}
00490 
00506     static Slog::Level levelTranslate(string name)
00507     {
00508     std::map<string, Slog::Level>::iterator  it = assoc->find(name);
00509     return (it != assoc->end()) ? it->second : Slog::levelEmergency;
00510     }
00511 
00512 };
00513 
00519 __EXPORT inline AppLog &debug(AppLog& sl)
00520 {return sl.operator()(Slog::levelDebug);}
00521 
00527 __EXPORT inline AppLog &warn(AppLog& sl)
00528 {return sl.operator()(Slog::levelWarning);}
00529 
00535 __EXPORT inline AppLog &error(AppLog& sl)
00536 { return sl.operator()(Slog::levelError);}
00537 
00543 __EXPORT inline AppLog &emerg(AppLog& sl)
00544 {return sl.operator()(Slog::levelEmergency);}
00545 
00551 __EXPORT inline AppLog &alert(AppLog& sl)
00552 {return sl.operator()(Slog::levelAlert);}
00553 
00559 __EXPORT inline AppLog &critical(AppLog& sl)
00560 {return sl.operator()(Slog::levelCritical);}
00561 
00567 __EXPORT inline AppLog &notice(AppLog& sl)
00568 {return sl.operator()(Slog::levelNotice);}
00569 
00575 __EXPORT inline AppLog &info(AppLog& sl)
00576 {return sl.operator()(Slog::levelInfo);}
00577 
00581 __EXPORT extern AppLog alog;
00582 
00583 END_NAMESPACE
00584 
00585 #endif //___APPLOG_H___