LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/base - logging.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 42 72 58.3 %
Date: 2026-10-05 02:12:29 Functions: 9 14 64.3 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      24           1 : Logging::Logging()
      25           1 :     : initializer_{} {
      26           1 : }
      27             : 
      28           1 : Logging::~Logging() {
      29           1 : }
      30             : 
      31           1 : void Logging::Init() {
      32           1 :     LoggingInit();
      33           1 : }
      34             : 
      35           0 : 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           0 :     LoggingInit(filename,
      43             :                 maxFileSize,
      44             :                 maxBackupIndex,
      45             :                 useSyslog,
      46             :                 syslogFacility,
      47             :                 ident,
      48             :                 logLevel);
      49           0 : }
      50             : 
      51           0 : void Logging::Init(const std::string &filename) {
      52           0 :     LoggingInit(filename);
      53           0 : }
      54             : 
      55     3397326 : bool LoggingDisabled() {
      56     3397326 :     return disabled_;
      57             : }
      58             : 
      59          10 : void SetLoggingDisabled(bool flag) {
      60          10 :     disabled_ = flag;
      61          10 : }
      62             : 
      63           0 : bool LoggingUseSyslog() {
      64           0 :     return use_syslog_;
      65             : }
      66             : 
      67           0 : void SetUseSysLog(bool use_syslog) {
      68           0 :     use_syslog_ = use_syslog;
      69           0 : }
      70             : 
      71         109 : void CheckEnvironmentAndUpdate() {
      72         109 :     if (getenv("LOG_DISABLE") != NULL) {
      73           2 :         SetLoggingDisabled(true);
      74             :     }
      75         109 : }
      76             : 
      77          59 : void SetLoggingLevel(LogLevel logLevel) {
      78          59 :     Logger logger = Logger::getRoot();
      79          59 :     logger.setLogLevel(logLevel);
      80          59 : }
      81             : 
      82          65 : void LoggingInit() {
      83          65 :     BasicConfigurator config;
      84          65 :     config.configure();
      85          65 :     Logger logger = Logger::getRoot();
      86         130 :     std::unique_ptr<Layout> layout_ptr(new PatternLayout(loggingPattern));
      87          65 :     logger.getAllAppenders().at(0)->setLayout(std::move(layout_ptr));
      88          65 :     CheckEnvironmentAndUpdate();
      89          65 : }
      90             : 
      91          44 : 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          44 :     Logger logger = Logger::getRoot();
      95          44 :     logger.setLogLevel(logLevel);
      96             : 
      97          44 :     if (useSyslog) {
      98           0 :         helpers::Properties props;
      99             :         std::string syslogident = boost::str(
     100           0 :             boost::format("%1%[%2%]") % ident % getpid());
     101           0 :         props.setProperty(LOG4CPLUS_TEXT("facility"),
     102           0 :                           boost::starts_with(syslogFacility, "LOG_")
     103           0 :                         ? syslogFacility.substr(4)
     104             :                         : syslogFacility);
     105           0 :         props.setProperty(LOG4CPLUS_TEXT("ident"), syslogident);
     106           0 :         SharedAppenderPtr syslogappender(new SysLogAppender(props));
     107             :         std::unique_ptr<Layout> syslog_layout_ptr(new PatternLayout(
     108           0 :                                                     loggingPattern));
     109           0 :         syslogappender->setLayout(std::move(syslog_layout_ptr));
     110           0 :         logger.addAppender(syslogappender);
     111           0 :         use_syslog_ = useSyslog;
     112           0 :     } else {
     113          44 :         if (filename == "<stdout>" || filename.length() == 0) {
     114           0 :             BasicConfigurator config;
     115           0 :             config.configure();
     116           0 :         } else {
     117             :             SharedAppenderPtr fileappender(new RollingFileAppender(filename,
     118          44 :                                            maxFileSize, maxBackupIndex));
     119          44 :             logger.addAppender(fileappender);
     120          44 :         }
     121             : 
     122          88 :         std::unique_ptr<Layout> layout_ptr(new PatternLayout(loggingPattern));
     123          44 :         logger.getAllAppenders().at(0)->setLayout(std::move(layout_ptr));
     124          44 :     }
     125          44 :     CheckEnvironmentAndUpdate();
     126          44 : }
     127             : 
     128             : 
     129           0 : void LoggingInit(const std::string &propertyFile) {
     130           0 :     PropertyConfigurator::doConfigure(propertyFile);
     131           0 :     CheckEnvironmentAndUpdate();
     132           0 : }
     133             : 

Generated by: LCOV version 1.14