OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync_file_system/logger.h" | 5 #include "chrome/browser/sync_file_system/logger.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
9 #include "chrome/browser/google_apis/event_logger.h" | 9 #include "chrome/browser/google_apis/event_logger.h" |
10 | 10 |
11 namespace sync_file_system { | 11 namespace sync_file_system { |
12 namespace util { | 12 namespace util { |
13 namespace { | 13 namespace { |
14 | 14 |
15 static base::LazyInstance<google_apis::EventLogger> g_logger = | 15 static base::LazyInstance<google_apis::EventLogger> g_logger = |
16 LAZY_INSTANCE_INITIALIZER; | 16 LAZY_INSTANCE_INITIALIZER; |
17 | 17 |
18 } // namespace | 18 } // namespace |
19 | 19 |
20 void Log(const char* format, ...) { | 20 std::string LogSeverityToString(logging::LogSeverity level) { |
| 21 switch (level) { |
| 22 case logging::LOG_ERROR: |
| 23 return "Error"; |
| 24 case logging::LOG_WARNING: |
| 25 return "Warning"; |
| 26 case logging::LOG_INFO: |
| 27 return "Info"; |
| 28 } |
| 29 |
| 30 NOTREACHED(); |
| 31 return "Unknown Log Severity"; |
| 32 } |
| 33 |
| 34 // Need helper function since the LOG macro can't handle argument values. |
| 35 void LogToConsole(logging::LogSeverity level, std::string log_event) { |
| 36 switch (level) { |
| 37 case logging::LOG_ERROR: |
| 38 LOG(ERROR) << log_event; |
| 39 return; |
| 40 case logging::LOG_WARNING: |
| 41 LOG(WARNING) << log_event; |
| 42 return; |
| 43 case logging::LOG_INFO: |
| 44 LOG(INFO) << log_event; |
| 45 return; |
| 46 } |
| 47 |
| 48 LOG(FATAL) << "LogToConsole() missing case for LogSeverity=" << level; |
| 49 NOTREACHED(); |
| 50 } |
| 51 |
| 52 void Log(logging::LogSeverity level, std::string& format, ...) { |
| 53 va_list args; |
| 54 Log(level, format.c_str(), args); |
| 55 } |
| 56 |
| 57 void Log(logging::LogSeverity level, const char* format, ...) { |
| 58 // Ignore log if level is not high enough. |
| 59 if (level < logging::GetMinLogLevel()) |
| 60 return; |
| 61 |
21 std::string what; | 62 std::string what; |
22 | 63 |
23 va_list args; | 64 va_list args; |
24 va_start(args, format); | 65 va_start(args, format); |
25 base::StringAppendV(&what, format, args); | 66 base::StringAppendV(&what, format, args); |
26 va_end(args); | 67 va_end(args); |
27 | 68 |
28 // On thread-safety: LazyInstance guarantees thread-safety for the object | 69 // On thread-safety: LazyInstance guarantees thread-safety for the object |
29 // creation. EventLogger::Log() internally maintains the lock. | 70 // creation. EventLogger::Log() internally maintains the lock. |
30 google_apis::EventLogger* ptr = g_logger.Pointer(); | 71 google_apis::EventLogger* ptr = g_logger.Pointer(); |
31 ptr->Log("%s", what.c_str()); | 72 std::string level_string = "[" + LogSeverityToString(level) + "] "; |
| 73 ptr->Log("%s", (level_string + what).c_str()); |
| 74 |
| 75 // Mirror log to console just in case syncfs-internals page crashes. |
| 76 LogToConsole(level, what); |
32 } | 77 } |
33 | 78 |
34 std::vector<google_apis::EventLogger::Event> GetLogHistory() { | 79 std::vector<google_apis::EventLogger::Event> GetLogHistory() { |
35 google_apis::EventLogger* ptr = g_logger.Pointer(); | 80 google_apis::EventLogger* ptr = g_logger.Pointer(); |
36 return ptr->GetHistory(); | 81 return ptr->GetHistory(); |
37 } | 82 } |
38 | 83 |
39 } // namespace util | 84 } // namespace util |
40 } // namespace sync_file_system | 85 } // namespace sync_file_system |
OLD | NEW |