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