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