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: 45 72 62.5 %
Date: 2026-08-03 02:19:58 Functions: 10 14 71.4 %
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           4 : Logging::Logging()
      25           4 :     : initializer_{} {
      26           4 : }
      27             : 
      28           1 : Logging::~Logging() {
      29           1 : }
      30             : 
      31           1 : void Logging::Init() {
      32           1 :     LoggingInit();
      33           1 : }
      34             : 
      35           3 : 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           3 :     LoggingInit(filename,
      43             :                 maxFileSize,
      44             :                 maxBackupIndex,
      45             :                 useSyslog,
      46             :                 syslogFacility,
      47             :                 ident,
      48             :                 logLevel);
      49           3 : }
      50             : 
      51           0 : void Logging::Init(const std::string &filename) {
      52           0 :     LoggingInit(filename);
      53           0 : }
      54             : 
      55    38158057 : bool LoggingDisabled() {
      56    38158057 :     return disabled_;
      57             : }
      58             : 
      59         338 : void SetLoggingDisabled(bool flag) {
      60         338 :     disabled_ = flag;
      61         338 : }
      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         372 : void CheckEnvironmentAndUpdate() {
      72         372 :     if (getenv("LOG_DISABLE") != NULL) {
      73          72 :         SetLoggingDisabled(true);
      74             :     }
      75         372 : }
      76             : 
      77         241 : void SetLoggingLevel(LogLevel logLevel) {
      78         241 :     Logger logger = Logger::getRoot();
      79         241 :     logger.setLogLevel(logLevel);
      80         241 : }
      81             : 
      82         325 : void LoggingInit() {
      83         325 :     BasicConfigurator config;
      84         325 :     config.configure();
      85         325 :     Logger logger = Logger::getRoot();
      86         650 :     std::unique_ptr<Layout> layout_ptr(new PatternLayout(loggingPattern));
      87         325 :     logger.getAllAppenders().at(0)->setLayout(std::move(layout_ptr));
      88         325 :     CheckEnvironmentAndUpdate();
      89         325 : }
      90             : 
      91          47 : 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          47 :     Logger logger = Logger::getRoot();
      95          47 :     logger.setLogLevel(logLevel);
      96             : 
      97          47 :     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          47 :         if (filename == "<stdout>" || filename.length() == 0) {
     114           0 :             BasicConfigurator config;
     115           0 :             config.configure();
     116           0 :         } else {
     117             :             SharedAppenderPtr fileappender(new RollingFileAppender(filename,
     118          47 :                                            maxFileSize, maxBackupIndex));
     119          47 :             logger.addAppender(fileappender);
     120          47 :         }
     121             : 
     122          94 :         std::unique_ptr<Layout> layout_ptr(new PatternLayout(loggingPattern));
     123          47 :         logger.getAllAppenders().at(0)->setLayout(std::move(layout_ptr));
     124          47 :     }
     125          47 :     CheckEnvironmentAndUpdate();
     126          47 : }
     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