blob: 2b35280f88007de36823cbc035875df8fc649942 [file] [log] [blame]
mukesh agrawal7202e082012-02-09 15:30:14 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Ken Mixterf15efc62011-03-02 18:01:37 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -07005#include "brillo/syslog_logging.h"
Ben Chanc7b97b82014-04-24 00:04:42 -07006
7#include <syslog.h>
Mike Frysingera1a7e932017-08-15 23:49:40 -04008#include <unistd.h>
Ken Mixterf15efc62011-03-02 18:01:37 -08009
10#include <string>
Ken Mixterf15efc62011-03-02 18:01:37 -080011
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.
15namespace {
Chris Masonee7960e02011-05-24 16:34:43 -070016const int kSyslogDebug = LOG_DEBUG;
Ken Mixterf15efc62011-03-02 18:01:37 -080017const int kSyslogInfo = LOG_INFO;
18const int kSyslogWarning = LOG_WARNING;
19const int kSyslogError = LOG_ERR;
20const int kSyslogCritical = LOG_CRIT;
21
22#undef LOG_INFO
23#undef LOG_WARNING
24#undef LOG_ERR
Chris Masonee7960e02011-05-24 16:34:43 -070025#undef LOG_CRIT
Ken Mixterf15efc62011-03-02 18:01:37 -080026} // namespace
27
28#include <base/logging.h>
29
30static std::string s_ident;
31static std::string s_accumulated;
32static bool s_accumulate;
33static bool s_log_to_syslog;
34static bool s_log_to_stderr;
mukesh agrawal7202e082012-02-09 15:30:14 -080035static bool s_log_header;
Ken Mixterf15efc62011-03-02 18:01:37 -080036
Chris Masonecac4d8e2011-05-12 10:40:11 -070037static bool HandleMessage(int severity,
Christopher Wiley2fd46ba2016-01-04 08:59:09 -080038 const char* /* file */,
39 int /* line */,
Chris Masonecac4d8e2011-05-12 10:40:11 -070040 size_t message_start,
Alex Vakulenko05d29042015-01-13 09:39:25 -080041 const std::string& message) {
Ken Mixterf15efc62011-03-02 18:01:37 -080042 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 Mixterf15efc62011-03-02 18:01:37 -080052 severity = kSyslogError;
53 break;
54
55 case logging::LOG_FATAL:
56 severity = kSyslogCritical;
57 break;
Chris Masonee7960e02011-05-24 16:34:43 -070058
59 default:
60 severity = kSyslogDebug;
61 break;
Ken Mixterf15efc62011-03-02 18:01:37 -080062 }
63
Alex Vakulenko05d29042015-01-13 09:39:25 -080064 const char* str;
mukesh agrawal7202e082012-02-09 15:30:14 -080065 if (s_log_header) {
66 str = message.c_str();
67 } else {
68 str = message.c_str() + message_start;
69 }
Ken Mixterf15efc62011-03-02 18:01:37 -080070
71 if (s_log_to_syslog)
72 syslog(severity, "%s", str);
73 if (s_accumulate)
74 s_accumulated.append(str);
Christopher Wileya6b3fcd2012-08-10 10:44:03 -070075 return !s_log_to_stderr && severity != kSyslogCritical;
Ken Mixterf15efc62011-03-02 18:01:37 -080076}
77
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070078namespace brillo {
Christopher Wileya6b3fcd2012-08-10 10:44:03 -070079void SetLogFlags(int log_flags) {
80 s_log_to_syslog = (log_flags & kLogToSyslog) != 0;
81 s_log_to_stderr = (log_flags & kLogToStderr) != 0;
Mike Frysingera1a7e932017-08-15 23:49:40 -040082 if ((log_flags & kLogToStderrIfTty) && isatty(0))
83 s_log_to_stderr = true;
Christopher Wileya6b3fcd2012-08-10 10:44:03 -070084 s_log_header = (log_flags & kLogHeader) != 0;
85}
86int GetLogFlags() {
87 int flags = 0;
Alex Vakulenko05d29042015-01-13 09:39:25 -080088 flags |= (s_log_to_syslog) ? kLogToSyslog : 0;
89 flags |= (s_log_to_stderr) ? kLogToStderr : 0;
90 flags |= (s_log_header) ? kLogHeader : 0;
Christopher Wileya6b3fcd2012-08-10 10:44:03 -070091 return flags;
92}
Ken Mixterf15efc62011-03-02 18:01:37 -080093void InitLog(int init_flags) {
Daniel Erat601d1672014-01-08 16:51:21 -080094 logging::LoggingSettings settings;
95 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
Daniel Erat601d1672014-01-08 16:51:21 -080096 logging::InitLogging(settings);
mukesh agrawalba284f72014-06-20 13:58:59 -070097
98 const bool kOptionPID = false;
99 const bool kOptionTID = false;
100 const bool kOptionTimestamp = false;
101 const bool kOptionTickcount = false;
Alex Vakulenko05d29042015-01-13 09:39:25 -0800102 logging::SetLogItems(
103 kOptionPID, kOptionTID, kOptionTimestamp, kOptionTickcount);
Ken Mixterf15efc62011-03-02 18:01:37 -0800104 logging::SetLogMessageHandler(HandleMessage);
Christopher Wileya6b3fcd2012-08-10 10:44:03 -0700105 SetLogFlags(init_flags);
Ken Mixterf15efc62011-03-02 18:01:37 -0800106}
107void 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}
111void LogToString(bool enabled) {
112 s_accumulate = enabled;
113}
114std::string GetLog() {
115 return s_accumulated;
116}
117void ClearLog() {
118 s_accumulated.clear();
119}
120bool FindLog(const char* string) {
121 return s_accumulated.find(string) != std::string::npos;
122}
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -0700123} // namespace brillo