logger.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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
3// found in the LICENSE file.
4
5#include "chrome/browser/sync_file_system/logger.h"
6
7#include "base/file_util.h"
8#include "base/lazy_instance.h"
9#include "base/location.h"
10#include "base/strings/stringprintf.h"
11#include "chrome/browser/google_apis/event_logger.h"
12
13namespace sync_file_system {
14namespace util {
15namespace {
16
17static base::LazyInstance<google_apis::EventLogger> g_logger =
18    LAZY_INSTANCE_INITIALIZER;
19
20std::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}  // namespace
35
36void ClearLog() {
37  g_logger.Pointer()->SetHistorySize(google_apis::kDefaultHistorySize);
38}
39
40void Log(logging::LogSeverity severity,
41         const tracked_objects::Location& location,
42         const char* format,
43         ...) {
44  std::string what;
45
46  va_list args;
47  va_start(args, format);
48  base::StringAppendV(&what, format, args);
49  va_end(args);
50
51  // Use same output format as normal console logger.
52  base::FilePath path = base::FilePath::FromUTF8Unsafe(location.file_name());
53  std::string log_output = base::StringPrintf(
54      "[%s: %s(%d)] %s",
55      LogSeverityToString(severity).c_str(),
56      path.BaseName().AsUTF8Unsafe().c_str(),
57      location.line_number(),
58      what.c_str());
59
60  // Log to WebUI regardless of LogSeverity (e.g. ignores command line flags).
61  // On thread-safety: LazyInstance guarantees thread-safety for the object
62  // creation. EventLogger::Log() internally maintains the lock.
63  google_apis::EventLogger* ptr = g_logger.Pointer();
64  ptr->Log("%s", log_output.c_str());
65
66  // Log to console if the severity is at or above the min level. Need to do
67  // check manually here as using LogMessage directly instead of the LOG macro
68  // doesn't invoke the log severity check.
69  if (severity < logging::GetMinLogLevel())
70    return;
71  logging::LogMessage(location.file_name(), location.line_number(), severity)
72      .stream() << what;
73}
74
75std::vector<google_apis::EventLogger::Event> GetLogHistory() {
76  google_apis::EventLogger* ptr = g_logger.Pointer();
77  return ptr->GetHistory();
78}
79
80}  // namespace util
81}  // namespace sync_file_system
82