houxiao
2016-12-30 cc445067d1f61e12dbea4e6458f2c85ba58f01bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* ---------------------------------------------------------------------------
** This software is in the public domain, furnished "as is", without technical
** support, and with no warranty, express or implied, as to its usefulness for
** any purpose.
**
** logger.h
** 
** -------------------------------------------------------------------------*/
 
#ifndef LOGGER_H
#define LOGGER_H
 
#include <unistd.h>
 
#include "log4cpp/Category.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/PatternLayout.hh"
 
 
#define LOG(__level)  log4cpp::Category::getRoot() << log4cpp::Priority::__level << __FILE__ << ":" << __LINE__ << "\t" 
#define LOGP(__level, __format, arg...) log4cpp::Category::getRoot().log(log4cpp::Priority::__level, "%s:%d\t" __format, __FILE__, __LINE__, ##arg);
 
#define LOG_DEBUG    LOG(DEBUG) // Debug message do not care in any production environment
#define LOG_INFO     LOG(INFO) // Not significant event but useful for deal with online problem
#define LOG_NOTICE   LOG(NOTICE) // Important event
#define LOG_WARN     LOG(WARN) // Important event or input which will lead to errors
#define LOG_ERROR    LOG(ERROR) // Error message means program running in an abnormal (not expected) way
 
inline void initLogger(int verbose)
{
    // initialize log4cpp
    log4cpp::Category &log = log4cpp::Category::getRoot();
    log4cpp::Appender *app = new log4cpp::FileAppender("root", fileno(stdout));
    if (app)
    {
        log4cpp::PatternLayout *plt = new log4cpp::PatternLayout();
        if (plt)
        {
            plt->setConversionPattern("%d [%-6p] - %m%n");
            app->setLayout(plt);
        }
        log.addAppender(app);
    }
    switch (verbose)
    {
        case 2: log.setPriority(log4cpp::Priority::DEBUG); break;
        case 1: log.setPriority(log4cpp::Priority::INFO); break;
        default: log.setPriority(log4cpp::Priority::NOTICE); break;
        
    }
    LOG_INFO << "level:" << log4cpp::Priority::getPriorityName(log.getPriority()); 
}
    
#endif