1/* ---------------------------------------------------------------------------
2** This software is in the public domain, furnished "as is", without technical
3** support, and with no warranty, express or implied, as to its usefulness for
4** any purpose.
5**
6** logger.h
7**
8** -------------------------------------------------------------------------*/
9
10#ifndef LOGGER_H
11#define LOGGER_H
12
13#include <unistd.h>
14
15#ifdef HAVE_LOG4CPP
16#include "log4cpp/Category.hh"
17#include "log4cpp/FileAppender.hh"
18#include "log4cpp/PatternLayout.hh"
19
20#define LOG(__level) log4cpp::Category::getRoot() << log4cpp::Priority::__level << __FILE__ << ":" << __LINE__ << "\n\t"
21
22inline void initLogger(int verbose)
23{
24 // initialize log4cpp
25 log4cpp::Category &log = log4cpp::Category::getRoot();
26 log4cpp::Appender *app = new log4cpp::FileAppender("root", fileno(stdout));
27 if (app)
28 {
29 log4cpp::PatternLayout *plt = new log4cpp::PatternLayout();
30 if (plt)
31 {
32 plt->setConversionPattern("%d [%-6p] - %m%n");
33 app->setLayout(plt);
34 }
35 log.addAppender(app);
36 }
37 switch (verbose)
38 {
39 case 2: log.setPriority(log4cpp::Priority::DEBUG); break;
40 case 1: log.setPriority(log4cpp::Priority::INFO); break;
41 default: log.setPriority(log4cpp::Priority::NOTICE); break;
42
43 }
44 LOG(INFO) << "level:" << log4cpp::Priority::getPriorityName(log.getPriority());
45}
46#else
47
48typedef enum {EMERG = 0,
49 FATAL = 0,
50 ALERT = 100,
51 CRIT = 200,
52 ERROR = 300,
53 WARN = 400,
54 NOTICE = 500,
55 INFO = 600,
56 DEBUG = 700,
57 NOTSET = 800
58} PriorityLevel;
59
60#include <iostream>
61extern int LogLevel;
62#define LOG(__level) if (__level<=LogLevel) std::cout << "\n[" << #__level << "] " << __FILE__ << ":" << __LINE__ << "\n\t"
63
64inline void initLogger(int verbose)
65{
66 switch (verbose)
67 {
68 case 2: LogLevel=DEBUG; break;
69 case 1: LogLevel=INFO; break;
70 default: LogLevel=NOTICE; break;
71
72 }
73 std::cout << "log level:" << LogLevel << std::endl;
74}
75
76#endif
77
78#endif
79
80