OpenSDN source code
logging.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include "base/logging.h"
6 
7 #include <sys/types.h>
8 #include <unistd.h>
9 #include <log4cplus/helpers/pointer.h>
10 #include <log4cplus/configurator.h>
11 #include <log4cplus/fileappender.h>
12 #include <log4cplus/syslogappender.h>
13 
14 #include <boost/format.hpp>
15 #include <boost/algorithm/string/predicate.hpp>
16 
17 using namespace log4cplus;
18 
19 static bool disabled_;
20 static bool use_syslog_;
21 static const char *loggingPattern = "%D{%Y-%m-%d %a %H:%M:%S:%Q %Z} "
22  " %h [Thread %t, Pid %i]: %m%n";
23 
25  : initializer_{} {
26 }
27 
29 }
30 
31 void Logging::Init() {
32  LoggingInit();
33 }
34 
35 void Logging::Init(const std::string &filename,
36  long maxFileSize,
37  int maxBackupIndex,
38  bool useSyslog,
39  const std::string &syslogFacility,
40  const std::string &ident,
41  log4cplus::LogLevel logLevel) {
42  LoggingInit(filename,
43  maxFileSize,
44  maxBackupIndex,
45  useSyslog,
46  syslogFacility,
47  ident,
48  logLevel);
49 }
50 
51 void Logging::Init(const std::string &filename) {
52  LoggingInit(filename);
53 }
54 
56  return disabled_;
57 }
58 
59 void SetLoggingDisabled(bool flag) {
60  disabled_ = flag;
61 }
62 
64  return use_syslog_;
65 }
66 
67 void SetUseSysLog(bool use_syslog) {
68  use_syslog_ = use_syslog;
69 }
70 
72  if (getenv("LOG_DISABLE") != NULL) {
73  SetLoggingDisabled(true);
74  }
75 }
76 
77 void SetLoggingLevel(LogLevel logLevel) {
78  Logger logger = Logger::getRoot();
79  logger.setLogLevel(logLevel);
80 }
81 
82 void LoggingInit() {
83  BasicConfigurator config;
84  config.configure();
85  Logger logger = Logger::getRoot();
86  std::unique_ptr<Layout> layout_ptr(new PatternLayout(loggingPattern));
87  logger.getAllAppenders().at(0)->setLayout(std::move(layout_ptr));
89 }
90 
91 void LoggingInit(const std::string &filename, long maxFileSize, int maxBackupIndex,
92  bool useSyslog, const std::string &syslogFacility,
93  const std::string &ident, LogLevel logLevel) {
94  Logger logger = Logger::getRoot();
95  logger.setLogLevel(logLevel);
96 
97  if (useSyslog) {
98  helpers::Properties props;
99  std::string syslogident = boost::str(
100  boost::format("%1%[%2%]") % ident % getpid());
101  props.setProperty(LOG4CPLUS_TEXT("facility"),
102  boost::starts_with(syslogFacility, "LOG_")
103  ? syslogFacility.substr(4)
104  : syslogFacility);
105  props.setProperty(LOG4CPLUS_TEXT("ident"), syslogident);
106  SharedAppenderPtr syslogappender(new SysLogAppender(props));
107  std::unique_ptr<Layout> syslog_layout_ptr(new PatternLayout(
108  loggingPattern));
109  syslogappender->setLayout(std::move(syslog_layout_ptr));
110  logger.addAppender(syslogappender);
111  use_syslog_ = useSyslog;
112  } else {
113  if (filename == "<stdout>" || filename.length() == 0) {
114  BasicConfigurator config;
115  config.configure();
116  } else {
117  SharedAppenderPtr fileappender(new RollingFileAppender(filename,
118  maxFileSize, maxBackupIndex));
119  logger.addAppender(fileappender);
120  }
121 
122  std::unique_ptr<Layout> layout_ptr(new PatternLayout(loggingPattern));
123  logger.getAllAppenders().at(0)->setLayout(std::move(layout_ptr));
124  }
126 }
127 
128 
129 void LoggingInit(const std::string &propertyFile) {
130  PropertyConfigurator::doConfigure(propertyFile);
132 }
133 
void Init()
Performs basic initialization of the logging system ( log4cplus).
Definition: logging.cc:31
Logging()
Prepares log4cplus library for execution. Uses RAII to free resources after the completion of the pro...
Definition: logging.cc:24
~Logging()
Destroys the object and shutdowns the logging system.
Definition: logging.cc:28
bool LoggingDisabled()
Definition: logging.cc:55
static const char * loggingPattern
Definition: logging.cc:21
void CheckEnvironmentAndUpdate()
Definition: logging.cc:71
void SetLoggingLevel(LogLevel logLevel)
Definition: logging.cc:77
static bool disabled_
Definition: logging.cc:19
void LoggingInit()
Definition: logging.cc:82
void SetUseSysLog(bool use_syslog)
Definition: logging.cc:67
static bool use_syslog_
Definition: logging.cc:20
bool LoggingUseSyslog()
Definition: logging.cc:63
void SetLoggingDisabled(bool flag)
Definition: logging.cc:59