UCommon
applog.h
Go to the documentation of this file.
1// Copyright (C) 2005-2015 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_CONFIG_H_
46#include <commoncpp/config.h>
47#endif
48
49#ifndef COMMONCPP_SLOG_H_
50#include <commoncpp/slog.h>
51#endif
52
53#ifndef COMMONCPP_EXCEPTION_H_
54#include <commoncpp/exception.h>
55#endif
56
57#include <string>
58#include <sstream>
59#include <iostream>
60#include <map>
61
62namespace ost {
63using namespace std;
64
73class __EXPORT HEXdump
74{
75 protected:
79 std::string _str;
80
81 public:
82 // max_len: max number of bytes to be printed. 0 prints all.
91 HEXdump(const uint8_t *buffer, int buff_len, int max_len = 200);
92
96 virtual ~HEXdump() { _str = string();}
97
102 const char * c_str() const {
103 return _str.c_str();
104 }
105
109 std::string str() {
110 return _str;
111 }
112
118 friend std::ostream& operator<< (std::ostream& out, const HEXdump &hd)
119 {
120 out << hd.c_str();
121 return out;
122 }
123
124};
125
126#ifdef CCXX_EXCEPTIONS
131class __EXPORT AppLogException : public ost::Exception
132{
133 public:
138 AppLogException(const char *what_arg) : ost::Exception(what_arg) {}
139
140};
141#endif
142
143class __LOCAL AppLogPrivate;
144
173class __EXPORT AppLog : protected streambuf, public ostream
174{
175 protected:
176 // d pointer
177 AppLogPrivate *d;
178 void writeLog(bool endOfLine = true);
179 static std::map<string, Slog::Level> *assoc;
180
181 public:
185 class __EXPORT Ident
186 {
187 private:
188 std::string _ident;
189 public:
190
194 Ident() {}
195
200
204 Ident(Ident& id) {_ident = id._ident;}
205
209 Ident(const char *str) : _ident(str) {}
210
214 std::string& str() {return _ident;}
215
219 Ident& operator= (std::string &st) {_ident = st; return *this;}
220
224 Ident& operator= (const char str[]) {_ident = str; return *this;}
225
229 const char* c_str() const {return _ident.c_str();}
230 };
231
232#ifndef _MSWINDOWS_
240 AppLog(const char* logFileName = NULL, bool logDirectly = false , bool usePipe = false);
241#else
248 AppLog(const char* logFileName = NULL, bool logDirectly = false);
249#endif
253 virtual ~AppLog();
254
259 void subscribe();
260
265
266#ifndef _MSWINDOWS_
274 void logFileName(const char* FileName, bool logDirectly = false, bool usePipe = false);
275#else
282 void logFileName(const char* FileName, bool logDirectly = false);
283#endif
287 void close(void);
288
293 void level(Slog::Level enable);
294
299 void clogEnable(bool en = true);
300
305 void slogEnable(bool en = true);
306
312 void identLevel(const char *ident, Slog::Level level);
313
318 void open(const char *ident);
319
325 virtual int overflow(int c);
326
330 virtual int sync();
331
336 void emerg(const char *format, ...);
337
342 void alert(const char *format, ...);
343
348 void critical(const char *format, ...);
349
354 void error(const char *format, ...);
355
360 void warn(const char *format, ...);
361
366 void notice(const char *format, ...);
367
372 void info(const char *format, ...);
373
378 void debug(const char *format, ...);
379
386 AppLog &operator()(const char *ident, Slog::Level level = Slog::levelError);
387
393 inline AppLog& operator()(const Ident &ident) {
394 open(ident.c_str());
395 return *this;
396 }
397
403 AppLog &operator()(Slog::Level level);
404
410 AppLog& operator<< (AppLog& (*pfManipulator)(AppLog&));
411
417 AppLog& operator<< (ostream& (*pfManipulator)(ostream&));
418
419 friend ostream& operator << (ostream &os, AppLog & al)
420 {
421 return al;
422 }
423
429 inline AppLog& operator<< (Ident &ident)
430 {
431 open(ident.c_str());
432 return *this;
433 }
434
435
440 inline AppLog &warn(void)
441 {return operator()(Slog::levelWarning);}
442
448 { return operator()(Slog::levelError);}
449
454 inline AppLog &debug(void)
455 {return operator()(Slog::levelDebug);}
456
461 inline AppLog &emerg(void)
462 {return operator()(Slog::levelEmergency);}
463
468 inline AppLog &alert(void)
469 {return operator()(Slog::levelAlert);}
470
475 inline AppLog &critical(void)
476 {return operator()(Slog::levelCritical);}
477
482 inline AppLog &notice(void)
483 {return operator()(Slog::levelNotice);}
484
489 inline AppLog &info(void)
490 {return operator()(Slog::levelInfo);}
491
507 static Slog::Level levelTranslate(string name)
508 {
509 std::map<string, Slog::Level>::iterator it = assoc->find(name);
510 return (it != assoc->end()) ? it->second : Slog::levelEmergency;
511 }
512
513};
514
520__EXPORT inline AppLog &debug(AppLog& sl)
521{return sl.operator()(Slog::levelDebug);}
522
528__EXPORT inline AppLog &warn(AppLog& sl)
529{return sl.operator()(Slog::levelWarning);}
530
536__EXPORT inline AppLog &error(AppLog& sl)
537{ return sl.operator()(Slog::levelError);}
538
544__EXPORT inline AppLog &emerg(AppLog& sl)
545{return sl.operator()(Slog::levelEmergency);}
546
552__EXPORT inline AppLog &alert(AppLog& sl)
553{return sl.operator()(Slog::levelAlert);}
554
560__EXPORT inline AppLog &critical(AppLog& sl)
561{return sl.operator()(Slog::levelCritical);}
562
568__EXPORT inline AppLog &notice(AppLog& sl)
569{return sl.operator()(Slog::levelNotice);}
570
576__EXPORT inline AppLog &info(AppLog& sl)
577{return sl.operator()(Slog::levelInfo);}
578
582__EXPORT extern AppLog alog;
583
584} // namespace ost
585
586#endif //___APPLOG_H___
GNU Common C++ exception model base classes.
System logging facilities abstraction.
AppLog & notice(AppLog &sl)
Manipulator for notice level.
Definition applog.h:568
AppLog & alert(AppLog &sl)
Manipulator for alert level.
Definition applog.h:552
AppLog & debug(AppLog &sl)
Manipulator for debug level.
Definition applog.h:520
AppLog & error(AppLog &sl)
Manipulator for error level.
Definition applog.h:536
AppLog & critical(AppLog &sl)
Manipulator for critical level.
Definition applog.h:560
AppLog & emerg(AppLog &sl)
Manipulator for emerg level.
Definition applog.h:544
AppLog & warn(AppLog &sl)
Manipulator for warn level.
Definition applog.h:528
AppLog & info(AppLog &sl)
Manipulator for info level.
Definition applog.h:576
Produces a dump of a buffer in a hexdump way with its code Ascii translation and relative buffer addr...
Definition applog.h:74
std::string str()
string cast provided for conveneince.
Definition applog.h:109
HEXdump(const uint8_t *buffer, int buff_len, int max_len=200)
HEXdump constructor.
std::string _str
output string
Definition applog.h:79
virtual ~HEXdump()
HEXdump destructor.
Definition applog.h:96
const char * c_str() const
const char* cast provided for conveneince.
Definition applog.h:102
Applog exception, used for memory problems at the moment.
Definition applog.h:132
AppLogException(const char *what_arg)
Constructor.
Definition applog.h:138
Application logger is a class that implements a logger that can be used by applications to save log f...
Definition applog.h:174
AppLog & emerg(void)
emerg level
Definition applog.h:461
void info(const char *format,...)
info level printf style method, provided for convenience.
AppLog & critical(void)
critical level
Definition applog.h:475
void slogEnable(bool en=true)
Enables slog output for error level messages.
AppLog & notice(void)
notice level
Definition applog.h:482
void debug(const char *format,...)
debug level printf style method, provided for convenience.
void notice(const char *format,...)
notice level printf style method, provided for convenience.
AppLog & error(void)
error level
Definition applog.h:447
static Slog::Level levelTranslate(string name)
Translates level from string to Slog::Level, useful for configuration files for instance.
Definition applog.h:507
AppLog & operator()(const char *ident, Slog::Level level=Slog::levelError)
operator to change ident and log level
void subscribe()
Subscribes the current thread to logger, it reserves thread safe buffer for it.
void alert(const char *format,...)
alert level printf style method, provided for convenience.
void critical(const char *format,...)
critical level printf style method, provided for convenience.
AppLog & operator()(const Ident &ident)
operator to change ident
Definition applog.h:393
AppLog & alert(void)
alert level
Definition applog.h:468
virtual int sync()
stream sync() overload
AppLog(const char *logFileName=NULL, bool logDirectly=false, bool usePipe=false)
Constructor for a customized logger.
virtual ~AppLog()
Destructor.
void identLevel(const char *ident, Slog::Level level)
Sets the level for that ident.
AppLog & operator()(Slog::Level level)
operator to change logging level
AppLog & info(void)
info level
Definition applog.h:489
virtual int overflow(int c)
stream overflow() overload.
void clogEnable(bool en=true)
Enables clog output.
void error(const char *format,...)
error level printf style method, provided for convenience.
void open(const char *ident)
Opens the file if not already and sets ident.
AppLog & debug(void)
debug level
Definition applog.h:454
void emerg(const char *format,...)
emerg level printf style method, provided for convenience.
void level(Slog::Level enable)
Sets the log level.
void logFileName(const char *FileName, bool logDirectly=false, bool usePipe=false)
Allows to set up ost::alog parameters.
void unsubscribe()
Unsubscribes the current thread from logger.
void close(void)
if logDirectly is set it closes the file.
AppLog & warn(void)
warn level
Definition applog.h:440
void warn(const char *format,...)
warn level printf style method, provided for convenience.
Ident class that represents module name.
Definition applog.h:186
const char * c_str() const
const char* cast provided for conveneince.
Definition applog.h:229
Ident()
Constructor.
Definition applog.h:194
std::string & str()
std::string cast.
Definition applog.h:214
~Ident()
Desctructor.
Definition applog.h:199
Ident(Ident &id)
Copy constructor.
Definition applog.h:204
Ident(const char *str)
const char* constructor, provided for convenience.
Definition applog.h:209
Mainline exception handler, this is the root for all Common C++ exceptions and assures the ansi C++ e...
Definition exception.h:75