ucommon
commoncpp/applog.h
Go to the documentation of this file.
00001 // Copyright (C) 2005-2014 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 Lesser General Public License
00013 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00014 //
00015 // As a special exception, you may use this file as part of a free software
00016 // library without restriction.  Specifically, if other files instantiate
00017 // templates or use macros or inline functions from this file, or you compile
00018 // this file and link it with other files to produce an executable, this
00019 // file does not by itself cause the resulting executable to be covered by
00020 // the GNU General Public License.  This exception does not however
00021 // invalidate any other reasons why the executable file might be covered by
00022 // the GNU General Public License.
00023 //
00024 // This exception applies only to the code released under the name GNU
00025 // Common C++.  If you copy code from other releases into a copy of GNU
00026 // Common C++, as the General Public License permits, the exception does
00027 // not apply to the code that you add in this way.  To avoid misleading
00028 // anyone as to the status of such modified files, you must delete
00029 // this exception notice from them.
00030 //
00031 // If you write modifications of your own for GNU Common C++, it is your choice
00032 // whether to permit this exception to apply to your modifications.
00033 // If you do not wish that, delete this exception notice.
00034 //
00035 
00042 #ifndef COMMONCPP_APPLOG_H_
00043 #define COMMONCPP_APPLOG_H_
00044 
00045 #ifndef COMMONCPP_CONFIG_H_
00046 #include <commoncpp/config.h>
00047 #endif
00048 
00049 #ifndef COMMONCPP_SLOG_H_
00050 #include <commoncpp/slog.h>
00051 #endif
00052 
00053 #ifndef COMMONCPP_EXCEPTION_H_
00054 #include <commoncpp/exception.h>
00055 #endif
00056 
00057 #include <string>
00058 #include <sstream>
00059 #include <iostream>
00060 #include <map>
00061 
00062 namespace ost {
00063 using namespace std;
00064 
00073 class __EXPORT HEXdump
00074 {
00075   protected:
00079     std::string _str;
00080 
00081   public:
00082     // max_len: max number of bytes to be printed. 0 prints all.
00091     HEXdump(const unsigned char *buffer, int buff_len, int max_len = 200);
00092 
00096     virtual ~HEXdump() { _str = string();}
00097 
00102     const char * c_str() const
00103     {
00104       return _str.c_str();
00105     }
00106 
00110     std::string str()
00111     {
00112       return _str;
00113     }
00114 
00120     friend std::ostream& operator<< (std::ostream& out, const HEXdump &hd)
00121     {
00122       out << hd.c_str();
00123       return out;
00124     }
00125 
00126 };
00127 
00128 #ifdef  CCXX_EXCEPTIONS
00129 
00133 class __EXPORT AppLogException : public ost::Exception
00134 {
00135   public:
00140     AppLogException(const char *what_arg) : ost::Exception(what_arg) {}
00141 
00142 };
00143 #endif
00144 
00145 class __LOCAL AppLogPrivate;
00146 
00175 class __EXPORT AppLog : protected streambuf, public ostream
00176 {
00177   protected:
00178     // d pointer
00179     AppLogPrivate *d;
00180     void writeLog(bool endOfLine = true);
00181     static std::map<string, Slog::Level> *assoc;
00182 
00183   public:
00187     class __EXPORT Ident
00188     {
00189       private:
00190         std::string _ident;
00191       public:
00192 
00196         Ident() {}
00197 
00201         ~Ident() {}
00202 
00206         Ident(Ident& id) {_ident = id._ident;}
00207 
00211         Ident(const char *str) : _ident(str) {}
00212 
00216         std::string& str() {return _ident;}
00217 
00221         Ident& operator= (std::string &st) {_ident = st; return *this;}
00222 
00226         Ident& operator= (const char str[]) {_ident = str; return *this;}
00227 
00231         const char* c_str() {return _ident.c_str();}
00232     };
00233 
00234 #ifndef _MSWINDOWS_
00235 
00242     AppLog(const char* logFileName = NULL, bool logDirectly = false , bool usePipe = false);
00243 #else
00244 
00250     AppLog(const char* logFileName = NULL, bool logDirectly = false);
00251 #endif
00252 
00255     virtual ~AppLog();
00256 
00261     void subscribe();
00262 
00266     void unsubscribe();
00267 
00268 #ifndef _MSWINDOWS_
00269 
00276     void logFileName(const char* FileName, bool logDirectly = false, bool usePipe = false);
00277 #else
00278 
00284     void logFileName(const char* FileName, bool logDirectly = false);
00285 #endif
00286 
00289     void close(void);
00290 
00295     void level(Slog::Level enable);
00296 
00301     void clogEnable(bool en = true);
00302 
00307     void slogEnable(bool en = true);
00308 
00314     void identLevel(const char *ident, Slog::Level level);
00315 
00320     void open(const char *ident);
00321 
00327     virtual int overflow(int c);
00328 
00332     virtual int sync();
00333 
00338     void emerg(const char *format, ...);
00339 
00344     void alert(const char *format, ...);
00345 
00350     void critical(const char *format, ...);
00351 
00356     void error(const char *format, ...);
00357 
00362     void warn(const char *format, ...);
00363 
00368     void notice(const char *format, ...);
00369 
00374     void info(const char *format, ...);
00375 
00380     void debug(const char *format, ...);
00381 
00388     AppLog &operator()(const char *ident, Slog::Level level = Slog::levelError);
00389 
00395     inline AppLog& operator()(Ident &ident)
00396     {
00397       open(ident.c_str());
00398       return *this;
00399     }
00400 
00406     AppLog &operator()(Slog::Level level);
00407 
00413     AppLog& operator<< (AppLog& (*pfManipulator)(AppLog&));
00414 
00420     AppLog& operator<< (ostream& (*pfManipulator)(ostream&));
00421 
00422     friend  ostream& operator << (ostream &os, AppLog & al)
00423     {
00424       return al;
00425     }
00426 
00432     inline AppLog& operator<< (Ident &ident)
00433     {
00434       open(ident.c_str());
00435       return *this;
00436     }
00437 
00438 
00443     inline AppLog &warn(void)
00444     {return operator()(Slog::levelWarning);}
00445 
00450     AppLog &error(void)
00451     { return operator()(Slog::levelError);}
00452 
00457     inline AppLog &debug(void)
00458     {return operator()(Slog::levelDebug);}
00459 
00464     inline AppLog &emerg(void)
00465     {return operator()(Slog::levelEmergency);}
00466 
00471     inline AppLog &alert(void)
00472     {return operator()(Slog::levelAlert);}
00473 
00478     inline AppLog &critical(void)
00479     {return operator()(Slog::levelCritical);}
00480 
00485     inline AppLog &notice(void)
00486     {return operator()(Slog::levelNotice);}
00487 
00492     inline AppLog &info(void)
00493     {return operator()(Slog::levelInfo);}
00494 
00510     static Slog::Level levelTranslate(string name)
00511     {
00512     std::map<string, Slog::Level>::iterator  it = assoc->find(name);
00513     return (it != assoc->end()) ? it->second : Slog::levelEmergency;
00514     }
00515 
00516 };
00517 
00523 __EXPORT inline AppLog &debug(AppLog& sl)
00524 {return sl.operator()(Slog::levelDebug);}
00525 
00531 __EXPORT inline AppLog &warn(AppLog& sl)
00532 {return sl.operator()(Slog::levelWarning);}
00533 
00539 __EXPORT inline AppLog &error(AppLog& sl)
00540 { return sl.operator()(Slog::levelError);}
00541 
00547 __EXPORT inline AppLog &emerg(AppLog& sl)
00548 {return sl.operator()(Slog::levelEmergency);}
00549 
00555 __EXPORT inline AppLog &alert(AppLog& sl)
00556 {return sl.operator()(Slog::levelAlert);}
00557 
00563 __EXPORT inline AppLog &critical(AppLog& sl)
00564 {return sl.operator()(Slog::levelCritical);}
00565 
00571 __EXPORT inline AppLog &notice(AppLog& sl)
00572 {return sl.operator()(Slog::levelNotice);}
00573 
00579 __EXPORT inline AppLog &info(AppLog& sl)
00580 {return sl.operator()(Slog::levelInfo);}
00581 
00585 __EXPORT extern AppLog alog;
00586 
00587 } // namespace ost
00588 
00589 #endif //___APPLOG_H___