Home | History | Annotate | Download | only in brillo
      1 // Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "brillo/syslog_logging.h"
      6 
      7 #include <syslog.h>
      8 
      9 #include <string>
     10 
     11 // syslog.h and base/logging.h both try to #define LOG_INFO and LOG_WARNING.
     12 // We need to #undef at least these two before including base/logging.h.  The
     13 // others are included to be consistent.
     14 namespace {
     15 const int kSyslogDebug    = LOG_DEBUG;
     16 const int kSyslogInfo     = LOG_INFO;
     17 const int kSyslogWarning  = LOG_WARNING;
     18 const int kSyslogError    = LOG_ERR;
     19 const int kSyslogCritical = LOG_CRIT;
     20 
     21 #undef LOG_INFO
     22 #undef LOG_WARNING
     23 #undef LOG_ERR
     24 #undef LOG_CRIT
     25 }  // namespace
     26 
     27 #include <base/logging.h>
     28 
     29 static std::string s_ident;
     30 static std::string s_accumulated;
     31 static bool s_accumulate;
     32 static bool s_log_to_syslog;
     33 static bool s_log_to_stderr;
     34 static bool s_log_header;
     35 
     36 static bool HandleMessage(int severity,
     37                           const char* /* file */,
     38                           int /* line */,
     39                           size_t message_start,
     40                           const std::string& message) {
     41   switch (severity) {
     42     case logging::LOG_INFO:
     43       severity = kSyslogInfo;
     44       break;
     45 
     46     case logging::LOG_WARNING:
     47       severity = kSyslogWarning;
     48       break;
     49 
     50     case logging::LOG_ERROR:
     51       severity = kSyslogError;
     52       break;
     53 
     54     case logging::LOG_FATAL:
     55       severity = kSyslogCritical;
     56       break;
     57 
     58     default:
     59       severity = kSyslogDebug;
     60       break;
     61   }
     62 
     63   const char* str;
     64   if (s_log_header) {
     65     str = message.c_str();
     66   } else {
     67     str = message.c_str() + message_start;
     68   }
     69 
     70   if (s_log_to_syslog)
     71     syslog(severity, "%s", str);
     72   if (s_accumulate)
     73     s_accumulated.append(str);
     74   return !s_log_to_stderr && severity != kSyslogCritical;
     75 }
     76 
     77 namespace brillo {
     78 void SetLogFlags(int log_flags) {
     79   s_log_to_syslog = (log_flags & kLogToSyslog) != 0;
     80   s_log_to_stderr = (log_flags & kLogToStderr) != 0;
     81   s_log_header = (log_flags & kLogHeader) != 0;
     82 }
     83 int GetLogFlags() {
     84   int flags = 0;
     85   flags |= (s_log_to_syslog) ? kLogToSyslog : 0;
     86   flags |= (s_log_to_stderr) ? kLogToStderr : 0;
     87   flags |= (s_log_header) ? kLogHeader : 0;
     88   return flags;
     89 }
     90 void InitLog(int init_flags) {
     91   logging::LoggingSettings settings;
     92   settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
     93   logging::InitLogging(settings);
     94 
     95   const bool kOptionPID = false;
     96   const bool kOptionTID = false;
     97   const bool kOptionTimestamp = false;
     98   const bool kOptionTickcount = false;
     99   logging::SetLogItems(
    100       kOptionPID, kOptionTID, kOptionTimestamp, kOptionTickcount);
    101   logging::SetLogMessageHandler(HandleMessage);
    102   SetLogFlags(init_flags);
    103 }
    104 void OpenLog(const char* ident, bool log_pid) {
    105   s_ident = ident;
    106   openlog(s_ident.c_str(), log_pid ? LOG_PID : 0, LOG_USER);
    107 }
    108 void LogToString(bool enabled) {
    109   s_accumulate = enabled;
    110 }
    111 std::string GetLog() {
    112   return s_accumulated;
    113 }
    114 void ClearLog() {
    115   s_accumulated.clear();
    116 }
    117 bool FindLog(const char* string) {
    118   return s_accumulated.find(string) != std::string::npos;
    119 }
    120 }  // namespace brillo
    121