absl_vlog_is_on.h 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/absl_vlog_is_on.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header defines the `ABSL_VLOG_IS_ON()` macro that controls the
  20. // variable-verbosity conditional logging.
  21. //
  22. // It's used by `VLOG` in log.h, or it can also be used directly like this:
  23. //
  24. // if (ABSL_VLOG_IS_ON(2)) {
  25. // foo_server.RecomputeStatisticsExpensive();
  26. // LOG(INFO) << foo_server.LastStatisticsAsString();
  27. // }
  28. //
  29. // Each source file has an effective verbosity level that's a non-negative
  30. // integer computed from the `--vmodule` and `--v` flags.
  31. // `ABSL_VLOG_IS_ON(n)` is true, and `VLOG(n)` logs, if that effective verbosity
  32. // level is greater than or equal to `n`.
  33. //
  34. // `--vmodule` takes a comma-delimited list of key=value pairs. Each key is a
  35. // pattern matched against filenames, and the values give the effective severity
  36. // level applied to matching files. '?' and '*' characters in patterns are
  37. // interpreted as single-character and zero-or-more-character wildcards.
  38. // Patterns including a slash character are matched against full pathnames,
  39. // while those without are matched against basenames only. One suffix (i.e. the
  40. // last . and everything after it) is stripped from each filename prior to
  41. // matching, as is the special suffix "-inl".
  42. //
  43. // Example: --vmodule=module_a=1,module_b=2
  44. //
  45. // Files are matched against globs in `--vmodule` in order, and the first match
  46. // determines the verbosity level.
  47. //
  48. // Files which do not match any pattern in `--vmodule` use the value of `--v` as
  49. // their effective verbosity level. The default is 0.
  50. //
  51. // SetVLogLevel helper function is provided to do limited dynamic control over
  52. // V-logging by appending to `--vmodule`. Because these go at the beginning of
  53. // the list, they take priority over any globs previously added.
  54. //
  55. // Resetting --vmodule will override all previous modifications to `--vmodule`,
  56. // including via SetVLogLevel.
  57. #ifndef ABSL_LOG_ABSL_VLOG_IS_ON_H_
  58. #define ABSL_LOG_ABSL_VLOG_IS_ON_H_
  59. #include "absl/base/attributes.h"
  60. #include "absl/base/config.h"
  61. #include "absl/log/internal/vlog_config.h" // IWYU pragma: export
  62. #include "absl/strings/string_view.h"
  63. // IWYU pragma: private, include "absl/log/log.h"
  64. // This is expanded at the callsite to allow the compiler to optimize
  65. // always-false cases out of the build.
  66. // An ABSL_MAX_VLOG_VERBOSITY of 2 means that VLOG(3) and above should never
  67. // log.
  68. #ifdef ABSL_MAX_VLOG_VERBOSITY
  69. #define ABSL_LOG_INTERNAL_MAX_LOG_VERBOSITY_CHECK(x) \
  70. ((x) <= ABSL_MAX_VLOG_VERBOSITY)&&
  71. #else
  72. #define ABSL_LOG_INTERNAL_MAX_LOG_VERBOSITY_CHECK(x)
  73. #endif
  74. // Each ABSL_VLOG_IS_ON call site gets its own VLogSite that registers with the
  75. // global linked list of sites to asynchronously update its verbosity level on
  76. // changes to --v or --vmodule. The verbosity can also be set by manually
  77. // calling SetVLogLevel.
  78. //
  79. // ABSL_VLOG_IS_ON is not async signal safe, but it is guaranteed not to
  80. // allocate new memory.
  81. #define ABSL_VLOG_IS_ON(verbose_level) \
  82. (ABSL_LOG_INTERNAL_MAX_LOG_VERBOSITY_CHECK(verbose_level)[]() \
  83. ->::absl::log_internal::VLogSite * \
  84. { \
  85. ABSL_CONST_INIT static ::absl::log_internal::VLogSite site(__FILE__); \
  86. return &site; \
  87. }() \
  88. ->IsEnabled(verbose_level))
  89. #endif // ABSL_LOG_ABSL_VLOG_IS_ON_H_