flags.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // Copyright 2022 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "absl/log/internal/flags.h"
  16. #include <stddef.h>
  17. #include <algorithm>
  18. #include <cstdlib>
  19. #include <string>
  20. #include "absl/base/attributes.h"
  21. #include "absl/base/config.h"
  22. #include "absl/base/log_severity.h"
  23. #include "absl/flags/flag.h"
  24. #include "absl/flags/marshalling.h"
  25. #include "absl/log/globals.h"
  26. #include "absl/log/internal/config.h"
  27. #include "absl/log/internal/vlog_config.h"
  28. #include "absl/strings/numbers.h"
  29. #include "absl/strings/string_view.h"
  30. namespace absl {
  31. ABSL_NAMESPACE_BEGIN
  32. namespace log_internal {
  33. namespace {
  34. void SyncLoggingFlags() {
  35. absl::SetFlag(&FLAGS_minloglevel, static_cast<int>(absl::MinLogLevel()));
  36. absl::SetFlag(&FLAGS_log_prefix, absl::ShouldPrependLogPrefix());
  37. }
  38. bool RegisterSyncLoggingFlags() {
  39. log_internal::SetLoggingGlobalsListener(&SyncLoggingFlags);
  40. return true;
  41. }
  42. ABSL_ATTRIBUTE_UNUSED const bool unused = RegisterSyncLoggingFlags();
  43. template <typename T>
  44. T GetFromEnv(const char* varname, T dflt) {
  45. const char* val = ::getenv(varname);
  46. if (val != nullptr) {
  47. std::string err;
  48. ABSL_INTERNAL_CHECK(absl::ParseFlag(val, &dflt, &err), err.c_str());
  49. }
  50. return dflt;
  51. }
  52. constexpr absl::LogSeverityAtLeast StderrThresholdDefault() {
  53. return absl::LogSeverityAtLeast::kError;
  54. }
  55. } // namespace
  56. } // namespace log_internal
  57. ABSL_NAMESPACE_END
  58. } // namespace absl
  59. ABSL_FLAG(int, stderrthreshold,
  60. static_cast<int>(absl::log_internal::StderrThresholdDefault()),
  61. "Log messages at or above this threshold level are copied to stderr.")
  62. .OnUpdate([] {
  63. absl::log_internal::RawSetStderrThreshold(
  64. static_cast<absl::LogSeverityAtLeast>(
  65. absl::GetFlag(FLAGS_stderrthreshold)));
  66. });
  67. ABSL_FLAG(int, minloglevel, static_cast<int>(absl::LogSeverityAtLeast::kInfo),
  68. "Messages logged at a lower level than this don't actually "
  69. "get logged anywhere")
  70. .OnUpdate([] {
  71. absl::log_internal::RawSetMinLogLevel(
  72. static_cast<absl::LogSeverityAtLeast>(
  73. absl::GetFlag(FLAGS_minloglevel)));
  74. });
  75. ABSL_FLAG(std::string, log_backtrace_at, "",
  76. "Emit a backtrace when logging at file:linenum.")
  77. .OnUpdate([] {
  78. const std::string log_backtrace_at =
  79. absl::GetFlag(FLAGS_log_backtrace_at);
  80. if (log_backtrace_at.empty()) {
  81. absl::ClearLogBacktraceLocation();
  82. return;
  83. }
  84. const size_t last_colon = log_backtrace_at.rfind(':');
  85. if (last_colon == log_backtrace_at.npos) {
  86. absl::ClearLogBacktraceLocation();
  87. return;
  88. }
  89. const absl::string_view file =
  90. absl::string_view(log_backtrace_at).substr(0, last_colon);
  91. int line;
  92. if (!absl::SimpleAtoi(
  93. absl::string_view(log_backtrace_at).substr(last_colon + 1),
  94. &line)) {
  95. absl::ClearLogBacktraceLocation();
  96. return;
  97. }
  98. absl::SetLogBacktraceLocation(file, line);
  99. });
  100. ABSL_FLAG(bool, log_prefix, true,
  101. "Prepend the log prefix to the start of each log line")
  102. .OnUpdate([] {
  103. absl::log_internal::RawEnableLogPrefix(absl::GetFlag(FLAGS_log_prefix));
  104. });
  105. ABSL_FLAG(int, v, 0,
  106. "Show all VLOG(m) messages for m <= this. Overridable by --vmodule.")
  107. .OnUpdate([] {
  108. absl::log_internal::UpdateGlobalVLogLevel(absl::GetFlag(FLAGS_v));
  109. });
  110. ABSL_FLAG(
  111. std::string, vmodule, "",
  112. "per-module log verbosity level."
  113. " Argument is a comma-separated list of <module name>=<log level>."
  114. " <module name> is a glob pattern, matched against the filename base"
  115. " (that is, name ignoring .cc/.h./-inl.h)."
  116. " A pattern without slashes matches just the file name portion, otherwise"
  117. " the whole file path below the workspace root"
  118. " (still without .cc/.h./-inl.h) is matched."
  119. " ? and * in the glob pattern match any single or sequence of characters"
  120. " respectively including slashes."
  121. " <log level> overrides any value given by --v.")
  122. .OnUpdate([] {
  123. absl::log_internal::UpdateVModule(absl::GetFlag(FLAGS_vmodule));
  124. });