file_logger.h revision 868fa2fe829687343ffae624259930155e16dbd8
1// Copyright (c) 2012 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#ifndef CHROME_TEST_LOGGING_WIN_FILE_LOGGER_H_
6#define CHROME_TEST_LOGGING_WIN_FILE_LOGGER_H_
7
8#include <guiddef.h>
9
10#include "base/basictypes.h"
11#include "base/strings/string16.h"
12#include "base/win/event_trace_controller.h"
13
14namespace base {
15class FilePath;
16}
17
18namespace logging_win {
19
20// A file logger instance captures LOG messages and trace events emitted via
21// Event Tracing for Windows (ETW) and sends them to a file.  Events can be
22// pulled from the file sometime later with PrintLogFile or ReadLogFile
23// (currently in log_file_printer_win.h and log_file_reader_win.h,
24// respectively).
25//
26// Important usage notes (read this):
27// - Due to the nature of event generation, only one instance of this class may
28//   be initialized at a time.
29// - This class is not thread safe.
30// - This class uses facilities that require the process to run with admin
31//   rights; StartLogging() will return false if this is not the case.
32class FileLogger {
33 public:
34  enum EventProviderBits {
35    // Log messages from chrome.exe.
36    CHROME_LOG_PROVIDER         = 1 << 0,
37    // Log messages from npchrome_frame.dll.
38    CHROME_FRAME_LOG_PROVIDER   = 1 << 1,
39    // Log messages from the current process.
40    CHROME_TESTS_LOG_PROVIDER   = 1 << 2,
41    // Trace events.
42    CHROME_TRACE_EVENT_PROVIDER = 1 << 3,
43  };
44
45  static const uint32 kAllEventProviders = (CHROME_LOG_PROVIDER |
46                                            CHROME_FRAME_LOG_PROVIDER |
47                                            CHROME_TESTS_LOG_PROVIDER |
48                                            CHROME_TRACE_EVENT_PROVIDER);
49
50  FileLogger();
51  ~FileLogger();
52
53  // Initializes the instance to collect logs from all supported providers.
54  void Initialize();
55
56  // Initializes the instance to collect logs from the providers present in
57  // the given mask; see EventProviderBits.
58  void Initialize(uint32 event_provider_mask);
59
60  // Starts capturing logs from all providers into |log_file|.  The common file
61  // extension for such files is .etl.  Returns false if the session could not
62  // be started (e.g., if not running as admin) or if no providers could be
63  // enabled.
64  bool StartLogging(const base::FilePath& log_file);
65
66  // Stops capturing logs.
67  void StopLogging();
68
69  // Returns true if logs are being captured.
70  bool is_logging() const {
71    return controller_.session_name() && *controller_.session_name();
72  }
73
74 private:
75  bool EnableProviders();
76  void DisableProviders();
77
78  static bool is_initialized_;
79
80  base::win::EtwTraceController controller_;
81  uint32 event_provider_mask_;
82
83  DISALLOW_COPY_AND_ASSIGN(FileLogger);
84};
85
86}  // namespace logging_win
87
88#endif  // CHROME_TEST_LOGGING_WIN_FILE_LOGGER_H_
89