ucommon
applog.h
Go to the documentation of this file.
1 // Copyright (C) 2005-2010 Angelo Naselli, Penta Engineering s.r.l.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License for more details.
11 //
12 // You should have received a copy of the GNU General Public License
13 // along with this program; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 //
16 // As a special exception, you may use this file as part of a free software
17 // library without restriction. Specifically, if other files instantiate
18 // templates or use macros or inline functions from this file, or you compile
19 // this file and link it with other files to produce an executable, this
20 // file does not by itself cause the resulting executable to be covered by
21 // the GNU General Public License. This exception does not however
22 // invalidate any other reasons why the executable file might be covered by
23 // the GNU General Public License.
24 //
25 // This exception applies only to the code released under the name GNU
26 // Common C++. If you copy code from other releases into a copy of GNU
27 // Common C++, as the General Public License permits, the exception does
28 // not apply to the code that you add in this way. To avoid misleading
29 // anyone as to the status of such modified files, you must delete
30 // this exception notice from them.
31 //
32 // If you write modifications of your own for GNU Common C++, it is your choice
33 // whether to permit this exception to apply to your modifications.
34 // If you do not wish that, delete this exception notice.
35 //
36 
43 #ifndef COMMONCPP_APPLOG_H_
44 #define COMMONCPP_APPLOG_H_
45 
46 #ifndef COMMONCPP_SLOG_H_
47 #include <commoncpp/slog.h>
48 #endif
49 
50 #ifndef COMMONCPP_EXCEPTION_H_
51 #include <commoncpp/exception.h>
52 #endif
53 
54 #include <string>
55 #include <sstream>
56 #include <iostream>
57 #include <map>
58 
59 NAMESPACE_COMMONCPP
60 using namespace std;
69 class __EXPORT HEXdump
70 {
71  protected:
75  std::string _str;
76 
77  public:
78  // max_len: max number of bytes to be printed. 0 prints all.
87  HEXdump(const unsigned char *buffer, int buff_len, int max_len = 200);
88 
92  virtual ~HEXdump() { _str = string();}
93 
98  const char * c_str() const
99  {
100  return _str.c_str();
101  }
102 
106  std::string str()
107  {
108  return _str;
109  }
110 
116  friend std::ostream& operator<< (std::ostream& out, const HEXdump &hd)
117  {
118  out << hd.c_str();
119  return out;
120  }
121 
122 };
123 
124 #ifdef CCXX_EXCEPTIONS
125 
129 class __EXPORT AppLogException : public ost::Exception
130 {
131  public:
136  AppLogException(String &what_arg) : ost::Exception(what_arg) {};
137 
138 };
139 #endif
140 
141 class AppLogPrivate;
142 
171 class __EXPORT AppLog : protected streambuf, public ostream
172 {
173  protected:
174  // d pointer
175  AppLogPrivate *d;
176  void writeLog(bool endOfLine = true);
177  static std::map<string, Slog::Level> *assoc;
178 
179  public:
183  class __EXPORT Ident
184  {
185  private:
186  std::string _ident;
187  public:
188 
192  Ident() {};
193 
197  ~Ident() {};
198 
202  Ident(Ident& id) {_ident = id._ident;}
203 
207  Ident(const char *str) : _ident(str) {};
208 
212  std::string& str() {return _ident;}
213 
217  Ident& operator= (std::string &st) {_ident = st; return *this;}
218 
222  Ident& operator= (const char str[]) {_ident = str; return *this;}
223 
227  const char* c_str() {return _ident.c_str();}
228  };
229 
230 #ifndef _MSWINDOWS_
231 
238  AppLog(const char* logFileName = NULL, bool logDirectly = false , bool usePipe = false);
239 #else
240 
246  AppLog(const char* logFileName = NULL, bool logDirectly = false);
247 #endif
248 
251  virtual ~AppLog();
252 
257  void subscribe();
258 
262  void unsubscribe();
263 
264 #ifndef _MSWINDOWS_
265 
272  void logFileName(const char* FileName, bool logDirectly = false, bool usePipe = false);
273 #else
274 
280  void logFileName(const char* FileName, bool logDirectly = false);
281 #endif
282 
285  void close(void);
286 
291  void level(Slog::Level enable);
292 
297  void clogEnable(bool en = true);
298 
303  void slogEnable(bool en = true);
304 
310  void identLevel(const char *ident, Slog::Level level);
311 
316  void open(const char *ident);
317 
323  virtual int overflow(int c);
324 
328  virtual int sync();
329 
334  void emerg(const char *format, ...);
335 
340  void alert(const char *format, ...);
341 
346  void critical(const char *format, ...);
347 
352  void error(const char *format, ...);
353 
358  void warn(const char *format, ...);
359 
364  void notice(const char *format, ...);
365 
370  void info(const char *format, ...);
371 
376  void debug(const char *format, ...);
377 
384  AppLog &operator()(const char *ident, Slog::Level level = Slog::levelError);
385 
391  inline AppLog& operator()(Ident &ident)
392  {
393  open(ident.c_str());
394  return *this;
395  }
396 
402  AppLog &operator()(Slog::Level level);
403 
409  AppLog& operator<< (AppLog& (*pfManipulator)(AppLog&));
410 
416  AppLog& operator<< (ostream& (*pfManipulator)(ostream&));
417 
418  friend ostream& operator << (ostream &os, AppLog & al)
419  {
420  return al;
421  }
422 
428  inline AppLog& operator<< (Ident &ident)
429  {
430  open(ident.c_str());
431  return *this;
432  }
433 
434 
439  inline AppLog &warn(void)
440  {return operator()(Slog::levelWarning);}
441 
446  AppLog &error(void)
447  { return operator()(Slog::levelError);}
448 
453  inline AppLog &debug(void)
454  {return operator()(Slog::levelDebug);}
455 
460  inline AppLog &emerg(void)
461  {return operator()(Slog::levelEmergency);}
462 
467  inline AppLog &alert(void)
468  {return operator()(Slog::levelAlert);}
469 
474  inline AppLog &critical(void)
475  {return operator()(Slog::levelCritical);}
476 
481  inline AppLog &notice(void)
482  {return operator()(Slog::levelNotice);}
483 
488  inline AppLog &info(void)
489  {return operator()(Slog::levelInfo);}
490 
506  static Slog::Level levelTranslate(string name)
507  {
508  std::map<string, Slog::Level>::iterator it = assoc->find(name);
509  return (it != assoc->end()) ? it->second : Slog::levelEmergency;
510  }
511 
512 };
513 
519 __EXPORT inline AppLog &debug(AppLog& sl)
520 {return sl.operator()(Slog::levelDebug);}
521 
527 __EXPORT inline AppLog &warn(AppLog& sl)
528 {return sl.operator()(Slog::levelWarning);}
529 
535 __EXPORT inline AppLog &error(AppLog& sl)
536 { return sl.operator()(Slog::levelError);}
537 
543 __EXPORT inline AppLog &emerg(AppLog& sl)
544 {return sl.operator()(Slog::levelEmergency);}
545 
551 __EXPORT inline AppLog &alert(AppLog& sl)
552 {return sl.operator()(Slog::levelAlert);}
553 
559 __EXPORT inline AppLog &critical(AppLog& sl)
560 {return sl.operator()(Slog::levelCritical);}
561 
567 __EXPORT inline AppLog &notice(AppLog& sl)
568 {return sl.operator()(Slog::levelNotice);}
569 
575 __EXPORT inline AppLog &info(AppLog& sl)
576 {return sl.operator()(Slog::levelInfo);}
577 
581 __EXPORT extern AppLog alog;
582 
583 END_NAMESPACE
584 
585 #endif //___APPLOG_H___
Applog exception, used for memory problems at the moment.
Definition: applog.h:129
Produces a dump of a buffer in a hexdump way with its code Ascii translation and relative buffer addr...
Definition: applog.h:69
AppLog & info(void)
info level
Definition: applog.h:488
AppLog & alert(void)
alert level
Definition: applog.h:467
std::string & str()
std::string cast.
Definition: applog.h:212
~Ident()
Desctructor.
Definition: applog.h:197
virtual ~HEXdump()
HEXdump destructor.
Definition: applog.h:92
Ident class that represents module name.
Definition: applog.h:183
Ident(Ident &id)
Copy constructor.
Definition: applog.h:202
AppLog & critical(void)
critical level
Definition: applog.h:474
std::string _str
output string
Definition: applog.h:75
AppLog & notice(void)
notice level
Definition: applog.h:481
AppLog & error(void)
error level
Definition: applog.h:446
AppLog & debug(void)
debug level
Definition: applog.h:453
static Slog::Level levelTranslate(string name)
Translates level from string to Slog::Level, useful for configuration files for instance.
Definition: applog.h:506
Application logger is a class that implements a logger that can be used by applications to save log f...
Definition: applog.h:171
const char * c_str() const
const char* cast provided for conveneince.
Definition: applog.h:98
std::string str()
string cast provided for conveneince.
Definition: applog.h:106
Ident(const char *str)
const char* constructor, provided for convenience.
Definition: applog.h:207
AppLog & warn(void)
warn level
Definition: applog.h:439
GNU Common C++ exception model base classes.
const char * c_str()
const char* cast provided for conveneince.
Definition: applog.h:227
AppLog & operator()(Ident &ident)
operator to change ident
Definition: applog.h:391
System logging facilities abstraction.
Ident()
Constructor.
Definition: applog.h:192
Mainline exception handler, this is the root for all Common C++ exceptions and assures the ansi C++ e...
Definition: exception.h:74
A copy-on-write string class that operates by reference count.
Definition: string.h:82
AppLogException(String &what_arg)
Constructor.
Definition: applog.h:136
AppLog & emerg(void)
emerg level
Definition: applog.h:460