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

Generated by: LCOV version 1.14