-
Notifications
You must be signed in to change notification settings - Fork 390
/
logging.cc
90 lines (73 loc) · 2.77 KB
/
logging.cc
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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "base/logging.h"
#include <sys/types.h>
#include <unistd.h>
#include <log4cplus/helpers/pointer.h>
#include <log4cplus/configurator.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/syslogappender.h>
#include <boost/format.hpp>
#include <boost/algorithm/string/predicate.hpp>
using namespace log4cplus;
static bool disabled_;
static const char *loggingPattern = "%D{%Y-%m-%d %a %H:%M:%S:%Q %Z} "
" %h [Thread %t, Pid %i]: %m%n";
bool LoggingDisabled() {
return disabled_;
}
void SetLoggingDisabled(bool flag) {
disabled_ = flag;
}
void CheckEnvironmentAndUpdate() {
if (getenv("LOG_DISABLE") != NULL) {
SetLoggingDisabled(true);
}
}
void LoggingInit() {
BasicConfigurator config;
config.configure();
Logger logger = Logger::getRoot();
std::auto_ptr<Layout> layout_ptr(new PatternLayout(loggingPattern));
logger.getAllAppenders().at(0)->setLayout(layout_ptr);
CheckEnvironmentAndUpdate();
}
void LoggingInit(const std::string &filename, long maxFileSize, int maxBackupIndex,
bool useSyslog, const std::string &syslogFacility,
const std::string &ident) {
helpers::Properties props;
props.setProperty(LOG4CPLUS_TEXT("rootLogger"),
LOG4CPLUS_TEXT("DEBUG"));
PropertyConfigurator config(props);
Logger logger = Logger::getRoot();
if (filename == "<stdout>" || filename.length() == 0) {
BasicConfigurator config;
config.configure();
} else {
SharedAppenderPtr fileappender(new RollingFileAppender(filename,
maxFileSize, maxBackupIndex));
logger.addAppender(fileappender);
}
std::auto_ptr<Layout> layout_ptr(new PatternLayout(loggingPattern));
logger.getAllAppenders().at(0)->setLayout(layout_ptr);
if (useSyslog) {
std::string syslogident = boost::str(
boost::format("%1%[%2%]") % ident % getpid());
props.setProperty(LOG4CPLUS_TEXT("facility"),
boost::starts_with(syslogFacility, "LOG_")
? syslogFacility.substr(4)
: syslogFacility);
props.setProperty(LOG4CPLUS_TEXT("ident"), syslogident);
SharedAppenderPtr syslogappender(new SysLogAppender(props));
std::auto_ptr<Layout> syslog_layout_ptr(new PatternLayout(
loggingPattern));
syslogappender->setLayout(syslog_layout_ptr);
logger.addAppender(syslogappender);
}
CheckEnvironmentAndUpdate();
}
void LoggingInit(const std::string &propertyFile) {
PropertyConfigurator::doConfigure(propertyFile);
CheckEnvironmentAndUpdate();
}