-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathLogger.cpp
124 lines (108 loc) · 3.73 KB
/
Logger.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#if HAVE_LOG4CXX
#include <log4cxx/logger.h>
#include <log4cxx/basicconfigurator.h>
#include <log4cxx/xml/domconfigurator.h>
#include <log4cxx/level.h>
#include <log4cxx/helpers/objectptr.h>
#include <log4cxx/patternlayout.h>
#include <log4cxx/consoleappender.h>
#include "Logger.h"
#include <stdio.h>
#include <sstream>
namespace KrisLibrary {
//static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("KrisLibrary"));
bool rootLoggerDefined = false;
log4cxx::LoggerPtr rootLogger = 0;
log4cxx::LoggerPtr myLogger = 0;
log4cxx::LoggerPtr logger()
{
if (rootLoggerDefined){
return rootLogger;
}
rootLogger = log4cxx::Logger::getRootLogger();
//non-additive logging by default
rootLogger->setAdditivity(false);
try{
log4cxx::xml::DOMConfigurator::configure("./log4cxx.xml");
if (rootLogger->getAllAppenders().empty()){
log4cxx::LayoutPtr defaultLayout = new log4cxx::PatternLayout("%m %n");
log4cxx::AppenderPtr defaultAppender = new log4cxx::ConsoleAppender(defaultLayout);
rootLogger->addAppender(defaultAppender);
rootLogger->setLevel(log4cxx::Level::getInfo());
printf("KrisLibrary::logger(): configured as default\n");
//log4cxx::BasicConfigurator::configure();
}
}
catch(...){
log4cxx::LayoutPtr defaultLayout = new log4cxx::PatternLayout("%m %n");
log4cxx::AppenderPtr defaultAppender = new log4cxx::ConsoleAppender(defaultLayout);
rootLogger->addAppender(defaultAppender);
rootLogger->setLevel(log4cxx::Level::getInfo());
printf("KrisLibrary::logger(): configured as default\n");
//log4cxx::BasicConfigurator::configure();
}
rootLoggerDefined = true;
return rootLogger;
}
log4cxx::LoggerPtr logger(const char* s)
{
if(rootLoggerDefined){
//we have read the file or at least set a root logger previously
try{
myLogger = log4cxx::Logger::getLogger(s);
if(myLogger->getAllAppenders().empty()){
printf("KrisLibrary::logger(): Logger %s has no appenders, using root logger.\n", s);
return rootLogger;
}
return myLogger;
}
catch(...){
//logger i
printf("KrisLibrary::logger(): Logger %s is not defined, using root logger.\n", s);
return rootLogger;
}
}else{
// otherwise, logger needs to be configured
try{
log4cxx::xml::DOMConfigurator::configure("./log4cxx.xml");
myLogger = log4cxx::Logger::getLogger(s);
if (myLogger->getAllAppenders().empty()){
//sets up basic setup if logger doesn't have appender set up.
std::stringstream ss;
ss<<s<<": %m %n";
log4cxx::LayoutPtr defaultLayout = new log4cxx::PatternLayout(ss.str().c_str());
log4cxx::AppenderPtr defaultAppender = new log4cxx::ConsoleAppender(defaultLayout);
myLogger->addAppender(defaultAppender);
myLogger->setLevel(log4cxx::Level::getInfo());
printf("KrisLibrary::logger(): %s configured as default\n",s);
//log4cxx::BasicConfigurator::configure();
}
}
catch(...){
//catches null logger errors
rootLogger = log4cxx::Logger::getRootLogger();
printf("KrisLibrary::logger(): Hit error. Setting up default logger\n");
log4cxx::LayoutPtr defaultLayout = new log4cxx::PatternLayout("%m %n");
log4cxx::AppenderPtr defaultAppender = new log4cxx::ConsoleAppender(defaultLayout);
rootLogger->addAppender(defaultAppender);
rootLogger->setLevel(log4cxx::Level::getInfo()); // Log level set to INFO
//log4cxx::BasicConfigurator::configure();
rootLoggerDefined = true;
return logger(s);
}
return myLogger;
}
}
void loggerWait()
{
loggerWait(KrisLibrary::logger());
}
void loggerWait(log4cxx::LoggerPtr logger)
{
if(logger->isEnabledFor(log4cxx::Level::getDebug())){
LOG4CXX_ERROR(logger," Press enter to continue...");
getchar();
}
}
} //namespace KrisLibrary
#endif //HAVE_LOG4CXX