OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  return disabled_;
26 }
27 
28 void SetLoggingDisabled(bool flag) {
29  disabled_ = flag;
30 }
31 
33  return use_syslog_;
34 }
35 
36 void SetUseSysLog(bool use_syslog) {
37  use_syslog_ = use_syslog;
38 }
39 
41  if (getenv("LOG_DISABLE") != NULL) {
42  SetLoggingDisabled(true);
43  }
44 }
45 
46 void SetLoggingLevel(LogLevel logLevel) {
47  Logger logger = Logger::getRoot();
48  logger.setLogLevel(logLevel);
49 }
50 
51 void LoggingInit() {
52  BasicConfigurator config;
53  config.configure();
54  Logger logger = Logger::getRoot();
55  std::unique_ptr<Layout> layout_ptr(new PatternLayout(loggingPattern));
56  logger.getAllAppenders().at(0)->setLayout(std::move(layout_ptr));
58 }
59 
60 void LoggingInit(const std::string &filename, long maxFileSize, int maxBackupIndex,
61  bool useSyslog, const std::string &syslogFacility,
62  const std::string &ident, LogLevel logLevel) {
63  Logger logger = Logger::getRoot();
64  logger.setLogLevel(logLevel);
65 
66  if (useSyslog) {
67  helpers::Properties props;
68  std::string syslogident = boost::str(
69  boost::format("%1%[%2%]") % ident % getpid());
70  props.setProperty(LOG4CPLUS_TEXT("facility"),
71  boost::starts_with(syslogFacility, "LOG_")
72  ? syslogFacility.substr(4)
73  : syslogFacility);
74  props.setProperty(LOG4CPLUS_TEXT("ident"), syslogident);
75  SharedAppenderPtr syslogappender(new SysLogAppender(props));
76  std::unique_ptr<Layout> syslog_layout_ptr(new PatternLayout(
78  syslogappender->setLayout(std::move(syslog_layout_ptr));
79  logger.addAppender(syslogappender);
80  use_syslog_ = useSyslog;
81  } else {
82  if (filename == "<stdout>" || filename.length() == 0) {
83  BasicConfigurator config;
84  config.configure();
85  } else {
86  SharedAppenderPtr fileappender(new RollingFileAppender(filename,
87  maxFileSize, maxBackupIndex));
88  logger.addAppender(fileappender);
89  }
90 
91  std::unique_ptr<Layout> layout_ptr(new PatternLayout(loggingPattern));
92  logger.getAllAppenders().at(0)->setLayout(std::move(layout_ptr));
93  }
95 }
96 
97 
98 void LoggingInit(const std::string &propertyFile) {
99  PropertyConfigurator::doConfigure(propertyFile);
101 }
102 
static const char * loggingPattern
Definition: logging.cc:21
static bool use_syslog_
Definition: logging.cc:20
void LoggingInit()
Definition: logging.cc:51
static bool disabled_
Definition: logging.cc:19
bool LoggingUseSyslog()
Definition: logging.cc:32
void CheckEnvironmentAndUpdate()
Definition: logging.cc:40
void SetLoggingDisabled(bool flag)
Definition: logging.cc:28
bool LoggingDisabled()
Definition: logging.cc:24
void SetLoggingLevel(LogLevel logLevel)
Definition: logging.cc:46
void SetUseSysLog(bool use_syslog)
Definition: logging.cc:36