globals.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 Y_ABSL_LOG_GLOBALS_H_
  21. #define Y_ABSL_LOG_GLOBALS_H_
  22. #include "y_absl/base/attributes.h"
  23. #include "y_absl/base/config.h"
  24. #include "y_absl/base/log_severity.h"
  25. #include "y_absl/log/internal/vlog_config.h"
  26. #include "y_absl/strings/string_view.h"
  27. namespace y_absl {
  28. Y_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 y_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. Y_ABSL_MUST_USE_RESULT y_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(y_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(y_absl::LogSeverityAtLeast severity);
  54. ScopedMinLogLevel(const ScopedMinLogLevel&) = delete;
  55. ScopedMinLogLevel& operator=(const ScopedMinLogLevel&) = delete;
  56. ~ScopedMinLogLevel();
  57. private:
  58. y_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 y_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. Y_ABSL_MUST_USE_RESULT y_absl::LogSeverityAtLeast StderrThreshold();
  74. // SetStderrThreshold()
  75. //
  76. // Updates the Stderr Threshold parameter.
  77. // This function is async-signal-safe.
  78. void SetStderrThreshold(y_absl::LogSeverityAtLeast severity);
  79. inline void SetStderrThreshold(y_absl::LogSeverity severity) {
  80. y_absl::SetStderrThreshold(static_cast<y_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(y_absl::LogSeverityAtLeast severity);
  88. ScopedStderrThreshold(const ScopedStderrThreshold&) = delete;
  89. ScopedStderrThreshold& operator=(const ScopedStderrThreshold&) = delete;
  90. ~ScopedStderrThreshold();
  91. private:
  92. y_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. Y_ABSL_MUST_USE_RESULT bool ShouldLogBacktraceAt(y_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(y_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. //
  124. // ShouldPrependLogPrefix()
  125. //
  126. // Returns the value of the Prepend Log Prefix option.
  127. // This function is async-signal-safe.
  128. Y_ABSL_MUST_USE_RESULT bool ShouldPrependLogPrefix();
  129. // EnableLogPrefix()
  130. //
  131. // Updates the value of the Prepend Log Prefix option.
  132. // This function is async-signal-safe.
  133. void EnableLogPrefix(bool on_off);
  134. //------------------------------------------------------------------------------
  135. // `VLOG` Configuration
  136. //------------------------------------------------------------------------------
  137. //
  138. // These methods set the `(Y_ABSL_)VLOG(_IS_ON)` threshold. They allow
  139. // programmatic control of the thresholds set by the --v and --vmodule flags.
  140. //
  141. // Only `VLOG`s with a severity level LESS THAN OR EQUAL TO the threshold will
  142. // be evaluated.
  143. //
  144. // For example, if the threshold is 2, then:
  145. //
  146. // VLOG(2) << "This message will be logged.";
  147. // VLOG(3) << "This message will NOT be logged.";
  148. //
  149. // The default threshold is 0. Since `VLOG` levels must not be negative, a
  150. // negative threshold value will turn off all VLOGs.
  151. // SetGlobalVLogLevel()
  152. //
  153. // Sets the global `VLOG` level to threshold. Returns the previous global
  154. // threshold.
  155. inline int SetGlobalVLogLevel(int threshold) {
  156. return y_absl::log_internal::UpdateGlobalVLogLevel(threshold);
  157. }
  158. // SetVLogLevel()
  159. //
  160. // Sets the `VLOG` threshold for all files that match `module_pattern`,
  161. // overwriting any prior value. Files that don't match aren't affected.
  162. // Returns the threshold that previously applied to `module_pattern`.
  163. inline int SetVLogLevel(y_absl::string_view module_pattern, int threshold) {
  164. return y_absl::log_internal::PrependVModule(module_pattern, threshold);
  165. }
  166. //------------------------------------------------------------------------------
  167. // Configure Android Native Log Tag
  168. //------------------------------------------------------------------------------
  169. //
  170. // The logging library forwards to the Android system log API when built for
  171. // Android. That API takes a string "tag" value in addition to a message and
  172. // severity level. The tag is used to identify the source of messages and to
  173. // filter them. This library uses the tag "native" by default.
  174. // SetAndroidNativeTag()
  175. //
  176. // Stores a copy of the string pointed to by `tag` and uses it as the Android
  177. // logging tag thereafter. `tag` must not be null.
  178. // This function must not be called more than once!
  179. void SetAndroidNativeTag(const char* tag);
  180. namespace log_internal {
  181. // GetAndroidNativeTag()
  182. //
  183. // Returns the configured Android logging tag.
  184. const char* GetAndroidNativeTag();
  185. } // namespace log_internal
  186. namespace log_internal {
  187. using LoggingGlobalsListener = void (*)();
  188. void SetLoggingGlobalsListener(LoggingGlobalsListener l);
  189. // Internal implementation for the setter routines. These are used
  190. // to break circular dependencies between flags and globals. Each "Raw"
  191. // routine corresponds to the non-"Raw" counterpart and used to set the
  192. // configuration parameter directly without calling back to the listener.
  193. void RawSetMinLogLevel(y_absl::LogSeverityAtLeast severity);
  194. void RawSetStderrThreshold(y_absl::LogSeverityAtLeast severity);
  195. void RawEnableLogPrefix(bool on_off);
  196. } // namespace log_internal
  197. Y_ABSL_NAMESPACE_END
  198. } // namespace y_absl
  199. #endif // Y_ABSL_LOG_GLOBALS_H_