globals.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2022 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: log/globals.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header declares global logging library configuration knobs.
  20. #ifndef ABSL_LOG_GLOBALS_H_
  21. #define ABSL_LOG_GLOBALS_H_
  22. #include "absl/base/attributes.h"
  23. #include "absl/base/config.h"
  24. #include "absl/base/log_severity.h"
  25. #include "absl/strings/string_view.h"
  26. namespace absl {
  27. ABSL_NAMESPACE_BEGIN
  28. //------------------------------------------------------------------------------
  29. // Minimum Log Level
  30. //------------------------------------------------------------------------------
  31. //
  32. // Messages logged at or above this severity are directed to all registered log
  33. // sinks or skipped otherwise. This parameter can also be modified using
  34. // command line flag --minloglevel.
  35. // See absl/base/log_severity.h for descriptions of severity levels.
  36. // MinLogLevel()
  37. //
  38. // Returns the value of the Minimum Log Level parameter.
  39. // This function is async-signal-safe.
  40. ABSL_MUST_USE_RESULT absl::LogSeverityAtLeast MinLogLevel();
  41. // SetMinLogLevel()
  42. //
  43. // Updates the value of Minimum Log Level parameter.
  44. // This function is async-signal-safe.
  45. void SetMinLogLevel(absl::LogSeverityAtLeast severity);
  46. namespace log_internal {
  47. // ScopedMinLogLevel
  48. //
  49. // RAII type used to temporarily update the Min Log Level parameter.
  50. class ScopedMinLogLevel final {
  51. public:
  52. explicit ScopedMinLogLevel(absl::LogSeverityAtLeast severity);
  53. ScopedMinLogLevel(const ScopedMinLogLevel&) = delete;
  54. ScopedMinLogLevel& operator=(const ScopedMinLogLevel&) = delete;
  55. ~ScopedMinLogLevel();
  56. private:
  57. absl::LogSeverityAtLeast saved_severity_;
  58. };
  59. } // namespace log_internal
  60. //------------------------------------------------------------------------------
  61. // Stderr Threshold
  62. //------------------------------------------------------------------------------
  63. //
  64. // Messages logged at or above this level are directed to stderr in
  65. // addition to other registered log sinks. This parameter can also be modified
  66. // using command line flag --stderrthreshold.
  67. // See absl/base/log_severity.h for descriptions of severity levels.
  68. // StderrThreshold()
  69. //
  70. // Returns the value of the Stderr Threshold parameter.
  71. // This function is async-signal-safe.
  72. ABSL_MUST_USE_RESULT absl::LogSeverityAtLeast StderrThreshold();
  73. // SetStderrThreshold()
  74. //
  75. // Updates the Stderr Threshold parameter.
  76. // This function is async-signal-safe.
  77. void SetStderrThreshold(absl::LogSeverityAtLeast severity);
  78. inline void SetStderrThreshold(absl::LogSeverity severity) {
  79. absl::SetStderrThreshold(static_cast<absl::LogSeverityAtLeast>(severity));
  80. }
  81. // ScopedStderrThreshold
  82. //
  83. // RAII type used to temporarily update the Stderr Threshold parameter.
  84. class ScopedStderrThreshold final {
  85. public:
  86. explicit ScopedStderrThreshold(absl::LogSeverityAtLeast severity);
  87. ScopedStderrThreshold(const ScopedStderrThreshold&) = delete;
  88. ScopedStderrThreshold& operator=(const ScopedStderrThreshold&) = delete;
  89. ~ScopedStderrThreshold();
  90. private:
  91. absl::LogSeverityAtLeast saved_severity_;
  92. };
  93. //------------------------------------------------------------------------------
  94. // Log Backtrace At
  95. //------------------------------------------------------------------------------
  96. //
  97. // Users can request backtrace to be logged at specific locations, specified
  98. // by file and line number.
  99. // ShouldLogBacktraceAt()
  100. //
  101. // Returns true if we should log a backtrace at the specified location.
  102. namespace log_internal {
  103. ABSL_MUST_USE_RESULT bool ShouldLogBacktraceAt(absl::string_view file,
  104. int line);
  105. } // namespace log_internal
  106. // SetLogBacktraceLocation()
  107. //
  108. // Sets the location the backtrace should be logged at.
  109. void SetLogBacktraceLocation(absl::string_view file, int line);
  110. //------------------------------------------------------------------------------
  111. // Prepend Log Prefix
  112. //------------------------------------------------------------------------------
  113. //
  114. // This option tells the logging library that every logged message
  115. // should include the prefix (severity, date, time, PID, etc.)
  116. // ShouldPrependLogPrefix()
  117. //
  118. // Returns the value of the Prepend Log Prefix option.
  119. // This function is async-signal-safe.
  120. ABSL_MUST_USE_RESULT bool ShouldPrependLogPrefix();
  121. // EnableLogPrefix()
  122. //
  123. // Updates the value of the Prepend Log Prefix option.
  124. // This function is async-signal-safe.
  125. void EnableLogPrefix(bool on_off);
  126. namespace log_internal {
  127. using LoggingGlobalsListener = void (*)();
  128. void SetLoggingGlobalsListener(LoggingGlobalsListener l);
  129. // Internal implementation for the setter routines. These are used
  130. // to break circular dependencies between flags and globals. Each "Raw"
  131. // routine corresponds to the non-"Raw" counterpart and used to set the
  132. // configuration parameter directly without calling back to the listener.
  133. void RawSetMinLogLevel(absl::LogSeverityAtLeast severity);
  134. void RawSetStderrThreshold(absl::LogSeverityAtLeast severity);
  135. void RawEnableLogPrefix(bool on_off);
  136. } // namespace log_internal
  137. ABSL_NAMESPACE_END
  138. } // namespace absl
  139. #endif // ABSL_LOG_GLOBALS_H_