logging_win.h 2.5 KB

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