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/file_util.h" |
7 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/location.h" |
8 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
9 #include "chrome/browser/google_apis/event_logger.h" | 11 #include "chrome/browser/google_apis/event_logger.h" |
10 | 12 |
11 namespace sync_file_system { | 13 namespace sync_file_system { |
12 namespace util { | 14 namespace util { |
13 namespace { | 15 namespace { |
14 | 16 |
15 static base::LazyInstance<google_apis::EventLogger> g_logger = | 17 static base::LazyInstance<google_apis::EventLogger> g_logger = |
16 LAZY_INSTANCE_INITIALIZER; | 18 LAZY_INSTANCE_INITIALIZER; |
17 | 19 |
| 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 |
18 } // namespace | 34 } // namespace |
19 | 35 |
20 void Log(const char* format, ...) { | 36 void ClearLog() { |
| 37 g_logger.Pointer()->SetHistorySize(google_apis::kDefaultHistorySize); |
| 38 } |
| 39 |
| 40 void Log(logging::LogSeverity severity, |
| 41 const tracked_objects::Location& location, |
| 42 const char* format, |
| 43 ...) { |
| 44 // Ignore log if level severity is not high enough. |
| 45 if (severity < logging::GetMinLogLevel()) |
| 46 return; |
| 47 |
21 std::string what; | 48 std::string what; |
22 | 49 |
23 va_list args; | 50 va_list args; |
24 va_start(args, format); | 51 va_start(args, format); |
25 base::StringAppendV(&what, format, args); | 52 base::StringAppendV(&what, format, args); |
26 va_end(args); | 53 va_end(args); |
27 | 54 |
| 55 // Use same output format as normal console logger. |
| 56 base::FilePath path = base::FilePath::FromUTF8Unsafe(location.file_name()); |
| 57 std::string log_output = base::StringPrintf( |
| 58 "[%s: %s(%d)] %s", |
| 59 LogSeverityToString(severity).c_str(), |
| 60 path.BaseName().AsUTF8Unsafe().c_str(), |
| 61 location.line_number(), |
| 62 what.c_str()); |
| 63 |
| 64 // Log to WebUI. |
28 // On thread-safety: LazyInstance guarantees thread-safety for the object | 65 // On thread-safety: LazyInstance guarantees thread-safety for the object |
29 // creation. EventLogger::Log() internally maintains the lock. | 66 // creation. EventLogger::Log() internally maintains the lock. |
30 google_apis::EventLogger* ptr = g_logger.Pointer(); | 67 google_apis::EventLogger* ptr = g_logger.Pointer(); |
31 ptr->Log("%s", what.c_str()); | 68 ptr->Log("%s", log_output.c_str()); |
| 69 |
| 70 // Log to console. |
| 71 logging::RawLog(severity, log_output.c_str()); |
32 } | 72 } |
33 | 73 |
34 std::vector<google_apis::EventLogger::Event> GetLogHistory() { | 74 std::vector<google_apis::EventLogger::Event> GetLogHistory() { |
35 google_apis::EventLogger* ptr = g_logger.Pointer(); | 75 google_apis::EventLogger* ptr = g_logger.Pointer(); |
36 return ptr->GetHistory(); | 76 return ptr->GetHistory(); |
37 } | 77 } |
38 | 78 |
39 } // namespace util | 79 } // namespace util |
40 } // namespace sync_file_system | 80 } // namespace sync_file_system |
OLD | NEW |