Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2011 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 BASE_LOGGING_WIN_H_
      6 #define BASE_LOGGING_WIN_H_
      7 #pragma once
      8 
      9 #include <string>
     10 
     11 #include "base/base_api.h"
     12 #include "base/basictypes.h"
     13 #include "base/win/event_trace_provider.h"
     14 #include "base/logging.h"
     15 
     16 template <typename Type>
     17 struct StaticMemorySingletonTraits;
     18 
     19 namespace logging {
     20 
     21 // Event ID for the log messages we generate.
     22 extern const GUID kLogEventId;
     23 
     24 // Feature enable mask for LogEventProvider.
     25 enum LogEnableMask {
     26   // If this bit is set in our provider enable mask, we will include
     27   // a stack trace with every log message.
     28   ENABLE_STACK_TRACE_CAPTURE = 0x0001,
     29   // If this bit is set in our provider enable mask, the provider will log
     30   // a LOG message with only the textual content of the message, and no
     31   // stack trace.
     32   ENABLE_LOG_MESSAGE_ONLY = 0x0002,
     33 };
     34 
     35 // The message types our log event provider generates.
     36 // ETW likes user message types to start at 10.
     37 enum LogMessageTypes {
     38   // A textual only log message, contains a zero-terminated string.
     39   LOG_MESSAGE = 10,
     40   // A message with a stack trace, followed by the zero-terminated
     41   // message text.
     42   LOG_MESSAGE_WITH_STACKTRACE = 11,
     43   // A message with:
     44   //  a stack trace,
     45   //  the line number as a four byte integer,
     46   //  the file as a zero terminated UTF8 string,
     47   //  the zero-terminated UTF8 message text.
     48   LOG_MESSAGE_FULL = 12,
     49 };
     50 
     51 // Trace provider class to drive log control and transport
     52 // with Event Tracing for Windows.
     53 class BASE_API LogEventProvider : public base::win::EtwTraceProvider {
     54  public:
     55   static LogEventProvider* GetInstance();
     56 
     57   static bool LogMessage(logging::LogSeverity severity, const char* file,
     58       int line, size_t message_start, const std::string& str);
     59 
     60   static void Initialize(const GUID& provider_name);
     61   static void Uninitialize();
     62 
     63  protected:
     64   // Overridden to manipulate the log level on ETW control callbacks.
     65   virtual void OnEventsEnabled();
     66   virtual void OnEventsDisabled();
     67 
     68  private:
     69   LogEventProvider();
     70 
     71   // The log severity prior to OnEventsEnabled,
     72   // restored in OnEventsDisabled.
     73   logging::LogSeverity old_log_level_;
     74 
     75   friend struct StaticMemorySingletonTraits<LogEventProvider>;
     76   DISALLOW_COPY_AND_ASSIGN(LogEventProvider);
     77 };
     78 
     79 }  // namespace logging
     80 
     81 #endif  // BASE_LOGGING_WIN_H_
     82