logger.cc revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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/drive/event_logger.h"
12
13namespace sync_file_system {
14namespace util {
15namespace {
16
17static base::LazyInstance<drive::EventLogger> g_logger =
18    LAZY_INSTANCE_INITIALIZER;
19
20const char* 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    case logging::LOG_VERBOSE:
29      return "VERBOSE";
30  }
31
32  NOTREACHED();
33  return "Unknown Log Severity";
34}
35
36}  // namespace
37
38void ClearLog() {
39  g_logger.Pointer()->SetHistorySize(drive::kDefaultHistorySize);
40}
41
42void Log(logging::LogSeverity severity,
43         const tracked_objects::Location& location,
44         const char* format,
45         ...) {
46  std::string what;
47
48  va_list args;
49  va_start(args, format);
50  base::StringAppendV(&what, format, args);
51  va_end(args);
52
53  // Log to WebUI regardless of LogSeverity (e.g. ignores command line flags).
54  // On thread-safety: LazyInstance guarantees thread-safety for the object
55  // creation. EventLogger::Log() internally maintains the lock.
56  drive::EventLogger* ptr = g_logger.Pointer();
57  ptr->Log(severity, base::StringPrintf("[%s] %s",
58                                        LogSeverityToString(severity),
59                                        what.c_str()));
60
61  // Log to console if the severity is at or above the min level.
62  // LOG_VERBOSE logs are also output if the verbosity of this module
63  // (sync_file_system/logger) is >= 1.
64  // TODO(kinuko,calvinlo): Reconsider this logging hack, it's not recommended
65  // to directly use LogMessage.
66  if (severity < logging::GetMinLogLevel() && !VLOG_IS_ON(1))
67    return;
68  logging::LogMessage(location.file_name(), location.line_number(), severity)
69      .stream() << what;
70}
71
72std::vector<drive::EventLogger::Event> GetLogHistory() {
73  drive::EventLogger* ptr = g_logger.Pointer();
74  return ptr->GetHistory();
75}
76
77}  // namespace util
78}  // namespace sync_file_system
79