| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 <chromeos/syslog_logging.h> | |
| 6 | |
| 7 #include <string> | |
| 8 #include <syslog.h> | |
| 9 | |
| 10 // syslog.h and base/logging.h both try to #define LOG_INFO and LOG_WARNING. | |
| 11 // We need to #undef at least these two before including base/logging.h. The | |
| 12 // others are included to be consistent. | |
| 13 namespace { | |
| 14 const int kSyslogInfo = LOG_INFO; | |
| 15 const int kSyslogWarning = LOG_WARNING; | |
| 16 const int kSyslogError = LOG_ERR; | |
| 17 const int kSyslogCritical = LOG_CRIT; | |
| 18 | |
| 19 #undef LOG_INFO | |
| 20 #undef LOG_WARNING | |
| 21 #undef LOG_ERR | |
| 22 #undef LOG_INFO | |
| 23 } // namespace | |
| 24 | |
| 25 #include <base/logging.h> | |
| 26 | |
| 27 static std::string s_ident; | |
| 28 static std::string s_accumulated; | |
| 29 static bool s_accumulate; | |
| 30 static bool s_log_to_syslog; | |
| 31 static bool s_log_to_stderr; | |
| 32 | |
| 33 static bool HandleMessage(int severity, const std::string &message) { | |
| 34 switch (severity) { | |
| 35 case logging::LOG_INFO: | |
| 36 severity = kSyslogInfo; | |
| 37 break; | |
| 38 | |
| 39 case logging::LOG_WARNING: | |
| 40 severity = kSyslogWarning; | |
| 41 break; | |
| 42 | |
| 43 case logging::LOG_ERROR: | |
| 44 case logging::LOG_ERROR_REPORT: | |
| 45 severity = kSyslogError; | |
| 46 break; | |
| 47 | |
| 48 case logging::LOG_FATAL: | |
| 49 severity = kSyslogCritical; | |
| 50 break; | |
| 51 } | |
| 52 | |
| 53 // The first "] " should be the end of the header added by the logging | |
| 54 // code. The meat of the message is two characters after that. | |
| 55 size_t pos = message.find("] "); | |
| 56 if (pos != std::string::npos && message.length() > pos + 2) { | |
| 57 pos += 2; | |
| 58 } else { | |
| 59 pos = 0; | |
| 60 } | |
| 61 | |
| 62 const char* str = message.c_str() + pos; | |
| 63 | |
| 64 if (s_log_to_syslog) | |
| 65 syslog(severity, "%s", str); | |
| 66 if (s_accumulate) | |
| 67 s_accumulated.append(str); | |
| 68 return !s_log_to_stderr; | |
| 69 } | |
| 70 | |
| 71 namespace chromeos { | |
| 72 void InitLog(int init_flags) { | |
| 73 logging::InitLogging("/dev/null", | |
| 74 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, | |
| 75 logging::DONT_LOCK_LOG_FILE, | |
| 76 logging::APPEND_TO_OLD_LOG_FILE); | |
| 77 logging::SetLogMessageHandler(HandleMessage); | |
| 78 s_log_to_syslog = (init_flags & kLogToSyslog) != 0; | |
| 79 s_log_to_stderr = (init_flags & kLogToStderr) != 0; | |
| 80 } | |
| 81 void OpenLog(const char* ident, bool log_pid) { | |
| 82 s_ident = ident; | |
| 83 openlog(s_ident.c_str(), log_pid ? LOG_PID : 0, LOG_USER); | |
| 84 } | |
| 85 void LogToString(bool enabled) { | |
| 86 s_accumulate = enabled; | |
| 87 } | |
| 88 std::string GetLog() { | |
| 89 return s_accumulated; | |
| 90 } | |
| 91 void ClearLog() { | |
| 92 s_accumulated.clear(); | |
| 93 } | |
| 94 bool FindLog(const char* string) { | |
| 95 return s_accumulated.find(string) != std::string::npos; | |
| 96 } | |
| 97 } // namespace chromeos | |
| OLD | NEW |