globals.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/log/internal/vlog_config.h"
  26. #include "absl/strings/string_view.h"
  27. namespace absl {
  28. ABSL_NAMESPACE_BEGIN
  29. //------------------------------------------------------------------------------
  30. // Minimum Log Level
  31. //------------------------------------------------------------------------------
  32. //
  33. // Messages logged at or above this severity are directed to all registered log
  34. // sinks or skipped otherwise. This parameter can also be modified using
  35. // command line flag --minloglevel.
  36. // See absl/base/log_severity.h for descriptions of severity levels.
  37. // MinLogLevel()
  38. //
  39. // Returns the value of the Minimum Log Level parameter.
  40. // This function is async-signal-safe.
  41. ABSL_MUST_USE_RESULT absl::LogSeverityAtLeast MinLogLevel();
  42. // SetMinLogLevel()
  43. //
  44. // Updates the value of Minimum Log Level parameter.
  45. // This function is async-signal-safe.
  46. void SetMinLogLevel(absl::LogSeverityAtLeast severity);
  47. namespace log_internal {
  48. // ScopedMinLogLevel
  49. //
  50. // RAII type used to temporarily update the Min Log Level parameter.
  51. class ScopedMinLogLevel final {
  52. public:
  53. explicit ScopedMinLogLevel(absl::LogSeverityAtLeast severity);
  54. ScopedMinLogLevel(const ScopedMinLogLevel&) = delete;
  55. ScopedMinLogLevel& operator=(const ScopedMinLogLevel&) = delete;
  56. ~ScopedMinLogLevel();
  57. private:
  58. absl::LogSeverityAtLeast saved_severity_;
  59. };
  60. } // namespace log_internal
  61. //------------------------------------------------------------------------------
  62. // Stderr Threshold
  63. //------------------------------------------------------------------------------
  64. //
  65. // Messages logged at or above this level are directed to stderr in
  66. // addition to other registered log sinks. This parameter can also be modified
  67. // using command line flag --stderrthreshold.
  68. // See absl/base/log_severity.h for descriptions of severity levels.
  69. // StderrThreshold()
  70. //
  71. // Returns the value of the Stderr Threshold parameter.
  72. // This function is async-signal-safe.
  73. ABSL_MUST_USE_RESULT absl::LogSeverityAtLeast StderrThreshold();
  74. // SetStderrThreshold()
  75. //
  76. // Updates the Stderr Threshold parameter.
  77. // This function is async-signal-safe.
  78. void SetStderrThreshold(absl::LogSeverityAtLeast severity);
  79. inline void SetStderrThreshold(absl::LogSeverity severity) {
  80. absl::SetStderrThreshold(static_cast<absl::LogSeverityAtLeast>(severity));
  81. }
  82. // ScopedStderrThreshold
  83. //
  84. // RAII type used to temporarily update the Stderr Threshold parameter.
  85. class ScopedStderrThreshold final {
  86. public:
  87. explicit ScopedStderrThreshold(absl::LogSeverityAtLeast severity);
  88. ScopedStderrThreshold(const ScopedStderrThreshold&) = delete;
  89. ScopedStderrThreshold& operator=(const ScopedStderrThreshold&) = delete;
  90. ~ScopedStderrThreshold();
  91. private:
  92. absl::LogSeverityAtLeast saved_severity_;
  93. };
  94. //------------------------------------------------------------------------------
  95. // Log Backtrace At
  96. //------------------------------------------------------------------------------
  97. //
  98. // Users can request an existing `LOG` statement, specified by file and line
  99. // number, to also include a backtrace when logged.
  100. // ShouldLogBacktraceAt()
  101. //
  102. // Returns true if we should log a backtrace at the specified location.
  103. namespace log_internal {
  104. ABSL_MUST_USE_RESULT bool ShouldLogBacktraceAt(absl::string_view file,
  105. int line);
  106. } // namespace log_internal
  107. // SetLogBacktraceLocation()
  108. //
  109. // Sets the location the backtrace should be logged at. If the specified
  110. // location isn't a `LOG` statement, the effect will be the same as
  111. // `ClearLogBacktraceLocation` (but less efficient).
  112. void SetLogBacktraceLocation(absl::string_view file, int line);
  113. // ClearLogBacktraceLocation()
  114. //
  115. // Clears the set location so that backtraces will no longer be logged at it.
  116. void ClearLogBacktraceLocation();
  117. //------------------------------------------------------------------------------
  118. // Prepend Log Prefix
  119. //------------------------------------------------------------------------------
  120. //
  121. // This option tells the logging library that every logged message
  122. // should include the prefix (severity, date, time, PID, etc.)
  123. // ShouldPrependLogPrefix()
  124. //
  125. // Returns the value of the Prepend Log Prefix option.
  126. // This function is async-signal-safe.
  127. ABSL_MUST_USE_RESULT bool ShouldPrependLogPrefix();
  128. // EnableLogPrefix()
  129. //
  130. // Updates the value of the Prepend Log Prefix option.
  131. // This function is async-signal-safe.
  132. void EnableLogPrefix(bool on_off);
  133. //------------------------------------------------------------------------------
  134. // Set Global VLOG Level
  135. //------------------------------------------------------------------------------
  136. //
  137. // Sets the global `(ABSL_)VLOG(_IS_ON)` level to `log_level`. This level is
  138. // applied to any sites whose filename doesn't match any `module_pattern`.
  139. // Returns the prior value.
  140. inline int SetGlobalVLogLevel(int log_level) {
  141. return absl::log_internal::UpdateGlobalVLogLevel(log_level);
  142. }
  143. //------------------------------------------------------------------------------
  144. // Set VLOG Level
  145. //------------------------------------------------------------------------------
  146. //
  147. // Sets `(ABSL_)VLOG(_IS_ON)` level for `module_pattern` to `log_level`. This
  148. // allows programmatic control of what is normally set by the --vmodule flag.
  149. // Returns the level that previously applied to `module_pattern`.
  150. inline int SetVLogLevel(absl::string_view module_pattern, int log_level) {
  151. return absl::log_internal::PrependVModule(module_pattern, log_level);
  152. }
  153. //------------------------------------------------------------------------------
  154. // Configure Android Native Log Tag
  155. //------------------------------------------------------------------------------
  156. //
  157. // The logging library forwards to the Android system log API when built for
  158. // Android. That API takes a string "tag" value in addition to a message and
  159. // severity level. The tag is used to identify the source of messages and to
  160. // filter them. This library uses the tag "native" by default.
  161. // SetAndroidNativeTag()
  162. //
  163. // Stores a copy of the string pointed to by `tag` and uses it as the Android
  164. // logging tag thereafter. `tag` must not be null.
  165. // This function must not be called more than once!
  166. void SetAndroidNativeTag(const char* tag);
  167. namespace log_internal {
  168. // GetAndroidNativeTag()
  169. //
  170. // Returns the configured Android logging tag.
  171. const char* GetAndroidNativeTag();
  172. } // namespace log_internal
  173. namespace log_internal {
  174. using LoggingGlobalsListener = void (*)();
  175. void SetLoggingGlobalsListener(LoggingGlobalsListener l);
  176. // Internal implementation for the setter routines. These are used
  177. // to break circular dependencies between flags and globals. Each "Raw"
  178. // routine corresponds to the non-"Raw" counterpart and used to set the
  179. // configuration parameter directly without calling back to the listener.
  180. void RawSetMinLogLevel(absl::LogSeverityAtLeast severity);
  181. void RawSetStderrThreshold(absl::LogSeverityAtLeast severity);
  182. void RawEnableLogPrefix(bool on_off);
  183. } // namespace log_internal
  184. ABSL_NAMESPACE_END
  185. } // namespace absl
  186. #endif // ABSL_LOG_GLOBALS_H_