ucommon
applog.h
Go to the documentation of this file.
1 // Copyright (C) 2005-2014 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 Lesser General Public License
13 // along with this program. If not, see <http://www.gnu.org/licenses/>.
14 //
15 // As a special exception, you may use this file as part of a free software
16 // library without restriction. Specifically, if other files instantiate
17 // templates or use macros or inline functions from this file, or you compile
18 // this file and link it with other files to produce an executable, this
19 // file does not by itself cause the resulting executable to be covered by
20 // the GNU General Public License. This exception does not however
21 // invalidate any other reasons why the executable file might be covered by
22 // the GNU General Public License.
23 //
24 // This exception applies only to the code released under the name GNU
25 // Common C++. If you copy code from other releases into a copy of GNU
26 // Common C++, as the General Public License permits, the exception does
27 // not apply to the code that you add in this way. To avoid misleading
28 // anyone as to the status of such modified files, you must delete
29 // this exception notice from them.
30 //
31 // If you write modifications of your own for GNU Common C++, it is your choice
32 // whether to permit this exception to apply to your modifications.
33 // If you do not wish that, delete this exception notice.
34 //
35 
42 #ifndef COMMONCPP_APPLOG_H_
43 #define COMMONCPP_APPLOG_H_
44 
45 #ifndef COMMONCPP_SLOG_H_
46 #include <commoncpp/slog.h>
47 #endif
48 
49 #ifndef COMMONCPP_EXCEPTION_H_
50 #include <commoncpp/exception.h>
51 #endif
52 
53 #include <string>
54 #include <sstream>
55 #include <iostream>
56 #include <map>
57 
58 NAMESPACE_COMMONCPP
59 using namespace std;
68 class __EXPORT HEXdump
69 {
70  protected:
74  std::string _str;
75 
76  public:
77  // max_len: max number of bytes to be printed. 0 prints all.
86  HEXdump(const unsigned char *buffer, int buff_len, int max_len = 200);
87 
91  virtual ~HEXdump() { _str = string();}
92 
97  const char * c_str() const
98  {
99  return _str.c_str();
100  }
101 
105  std::string str()
106  {
107  return _str;
108  }
109 
115  friend std::ostream& operator<< (std::ostream& out, const HEXdump &hd)
116  {
117  out << hd.c_str();
118  return out;
119  }
120 
121 };
122 
123 #ifdef CCXX_EXCEPTIONS
124 
128 class __EXPORT AppLogException : public ost::Exception
129 {
130  public:
135  AppLogException(const char *what_arg) : ost::Exception(what_arg) {};
136 
137 };
138 #endif
139 
140 class AppLogPrivate;
141 
170 class __EXPORT AppLog : protected streambuf, public ostream
171 {
172  protected:
173  // d pointer
174  AppLogPrivate *d;
175  void writeLog(bool endOfLine = true);
176  static std::map<string, Slog::Level> *assoc;
177 
178  public:
182  class __EXPORT Ident
183  {
184  private:
185  std::string _ident;
186  public:
187 
191  Ident() {};
192 
196  ~Ident() {};
197 
201  Ident(Ident& id) {_ident = id._ident;}
202 
206  Ident(const char *str) : _ident(str) {};
207 
211  std::string& str() {return _ident;}
212 
216  Ident& operator= (std::string &st) {_ident = st; return *this;}
217 
221  Ident& operator= (const char str[]) {_ident = str; return *this;}
222 
226  const char* c_str() {return _ident.c_str();}
227  };
228 
229 #ifndef _MSWINDOWS_
230 
237  AppLog(const char* logFileName = NULL, bool logDirectly = false , bool usePipe = false);
238 #else
239 
245  AppLog(const char* logFileName = NULL, bool logDirectly = false);
246 #endif
247 
250  virtual ~AppLog();
251 
256  void subscribe();
257 
261  void unsubscribe();
262 
263 #ifndef _MSWINDOWS_
264 
271  void logFileName(const char* FileName, bool logDirectly = false, bool usePipe = false);
272 #else
273 
279  void logFileName(const char* FileName, bool logDirectly = false);
280 #endif
281 
284  void close(void);
285 
290  void level(Slog::Level enable);
291 
296  void clogEnable(bool en = true);
297 
302  void slogEnable(bool en = true);
303 
309  void identLevel(const char *ident, Slog::Level level);
310 
315  void open(const char *ident);
316 
322  virtual int overflow(int c);
323 
327  virtual int sync();
328 
333  void emerg(const char *format, ...);
334 
339  void alert(const char *format, ...);
340 
345  void critical(const char *format, ...);
346 
351  void error(const char *format, ...);
352 
357  void warn(const char *format, ...);
358 
363  void notice(const char *format, ...);
364 
369  void info(const char *format, ...);
370 
375  void debug(const char *format, ...);
376 
383  AppLog &operator()(const char *ident, Slog::Level level = Slog::levelError);
384 
390  inline AppLog& operator()(Ident &ident)
391  {
392  open(ident.c_str());
393  return *this;
394  }
395 
401  AppLog &operator()(Slog::Level level);
402 
408  AppLog& operator<< (AppLog& (*pfManipulator)(AppLog&));
409 
415  AppLog& operator<< (ostream& (*pfManipulator)(ostream&));
416 
417  friend ostream& operator << (ostream &os, AppLog & al)
418  {
419  return al;
420  }
421 
427  inline AppLog& operator<< (Ident &ident)
428  {
429  open(ident.c_str());
430  return *this;
431  }
432 
433 
438  inline AppLog &warn(void)
439  {return operator()(Slog::levelWarning);}
440 
445  AppLog &error(void)
446  { return operator()(Slog::levelError);}
447 
452  inline AppLog &debug(void)
453  {return operator()(Slog::levelDebug);}
454 
459  inline AppLog &emerg(void)
460  {return operator()(Slog::levelEmergency);}
461 
466  inline AppLog &alert(void)
467  {return operator()(Slog::levelAlert);}
468 
473  inline AppLog &critical(void)
474  {return operator()(Slog::levelCritical);}
475 
480  inline AppLog &notice(void)
481  {return operator()(Slog::levelNotice);}
482 
487  inline AppLog &info(void)
488  {return operator()(Slog::levelInfo);}
489 
505  static Slog::Level levelTranslate(string name)
506  {
507  std::map<string, Slog::Level>::iterator it = assoc->find(name);
508  return (it != assoc->end()) ? it->second : Slog::levelEmergency;
509  }
510 
511 };
512 
518 __EXPORT inline AppLog &debug(AppLog& sl)
519 {return sl.operator()(Slog::levelDebug);}
520 
526 __EXPORT inline AppLog &warn(AppLog& sl)
527 {return sl.operator()(Slog::levelWarning);}
528 
534 __EXPORT inline AppLog &error(AppLog& sl)
535 { return sl.operator()(Slog::levelError);}
536 
542 __EXPORT inline AppLog &emerg(AppLog& sl)
543 {return sl.operator()(Slog::levelEmergency);}
544 
550 __EXPORT inline AppLog &alert(AppLog& sl)
551 {return sl.operator()(Slog::levelAlert);}
552 
558 __EXPORT inline AppLog &critical(AppLog& sl)
559 {return sl.operator()(Slog::levelCritical);}
560 
566 __EXPORT inline AppLog &notice(AppLog& sl)
567 {return sl.operator()(Slog::levelNotice);}
568 
574 __EXPORT inline AppLog &info(AppLog& sl)
575 {return sl.operator()(Slog::levelInfo);}
576 
580 __EXPORT extern AppLog alog;
581 
582 END_NAMESPACE
583 
584 #endif //___APPLOG_H___
Application logger is a class that implements a logger that can be used by applications to save log f...
Definition: applog.h:170
AppLog & critical(void)
critical level
Definition: applog.h:473
const char * c_str()
const char* cast provided for conveneince.
Definition: applog.h:226
std::string & str()
std::string cast.
Definition: applog.h:211
Ident()
Constructor.
Definition: applog.h:191
AppLog & debug(void)
debug level
Definition: applog.h:452
AppLog & info(void)
info level
Definition: applog.h:487
System logging facilities abstraction.
Produces a dump of a buffer in a hexdump way with its code Ascii translation and relative buffer addr...
Definition: applog.h:68
AppLogException(const char *what_arg)
Constructor.
Definition: applog.h:135
AppLog & error(void)
error level
Definition: applog.h:445
GNU Common C++ exception model base classes.
virtual ~HEXdump()
HEXdump destructor.
Definition: applog.h:91
Applog exception, used for memory problems at the moment.
Definition: applog.h:128
Ident(const char *str)
const char* constructor, provided for convenience.
Definition: applog.h:206
AppLog & alert(void)
alert level
Definition: applog.h:466
std::string _str
output string
Definition: applog.h:74
AppLog & emerg(void)
emerg level
Definition: applog.h:459
AppLog & notice(void)
notice level
Definition: applog.h:480
Definition: address.h:58
Ident class that represents module name.
Definition: applog.h:182
const char * c_str() const
const char* cast provided for conveneince.
Definition: applog.h:97
std::string str()
string cast provided for conveneince.
Definition: applog.h:105
Ident(Ident &id)
Copy constructor.
Definition: applog.h:201
Mainline exception handler, this is the root for all Common C++ exceptions and assures the ansi C++ e...
Definition: exception.h:73
~Ident()
Desctructor.
Definition: applog.h:196
static Slog::Level levelTranslate(string name)
Translates level from string to Slog::Level, useful for configuration files for instance.
Definition: applog.h:505
AppLog & warn(void)
warn level
Definition: applog.h:438
AppLog & operator()(Ident &ident)
operator to change ident
Definition: applog.h:390