flags.cc (4670B)
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 16 #include "absl/log/internal/flags.h" 17 18 #include <stddef.h> 19 20 #include <algorithm> 21 #include <cstdlib> 22 #include <string> 23 24 #include "absl/base/attributes.h" 25 #include "absl/base/config.h" 26 #include "absl/base/log_severity.h" 27 #include "absl/flags/flag.h" 28 #include "absl/flags/marshalling.h" 29 #include "absl/log/globals.h" 30 #include "absl/log/internal/config.h" 31 #include "absl/log/internal/vlog_config.h" 32 #include "absl/strings/numbers.h" 33 #include "absl/strings/string_view.h" 34 35 namespace absl { 36 ABSL_NAMESPACE_BEGIN 37 namespace log_internal { 38 namespace { 39 40 void SyncLoggingFlags() { 41 absl::SetFlag(&FLAGS_minloglevel, static_cast<int>(absl::MinLogLevel())); 42 absl::SetFlag(&FLAGS_log_prefix, absl::ShouldPrependLogPrefix()); 43 } 44 45 bool RegisterSyncLoggingFlags() { 46 log_internal::SetLoggingGlobalsListener(&SyncLoggingFlags); 47 return true; 48 } 49 50 ABSL_ATTRIBUTE_UNUSED const bool unused = RegisterSyncLoggingFlags(); 51 52 template <typename T> 53 T GetFromEnv(const char* varname, T dflt) { 54 const char* val = ::getenv(varname); 55 if (val != nullptr) { 56 std::string err; 57 ABSL_INTERNAL_CHECK(absl::ParseFlag(val, &dflt, &err), err.c_str()); 58 } 59 return dflt; 60 } 61 62 constexpr absl::LogSeverityAtLeast StderrThresholdDefault() { 63 return absl::LogSeverityAtLeast::kError; 64 } 65 66 } // namespace 67 } // namespace log_internal 68 ABSL_NAMESPACE_END 69 } // namespace absl 70 71 ABSL_FLAG(int, stderrthreshold, 72 static_cast<int>(absl::log_internal::StderrThresholdDefault()), 73 "Log messages at or above this threshold level are copied to stderr.") 74 .OnUpdate([] { 75 absl::log_internal::RawSetStderrThreshold( 76 static_cast<absl::LogSeverityAtLeast>( 77 absl::GetFlag(FLAGS_stderrthreshold))); 78 }); 79 80 ABSL_FLAG(int, minloglevel, static_cast<int>(absl::LogSeverityAtLeast::kInfo), 81 "Messages logged at a lower level than this don't actually " 82 "get logged anywhere") 83 .OnUpdate([] { 84 absl::log_internal::RawSetMinLogLevel( 85 static_cast<absl::LogSeverityAtLeast>( 86 absl::GetFlag(FLAGS_minloglevel))); 87 }); 88 89 ABSL_FLAG(std::string, log_backtrace_at, "", 90 "Emit a backtrace when logging at file:linenum.") 91 .OnUpdate([] { 92 const std::string log_backtrace_at = 93 absl::GetFlag(FLAGS_log_backtrace_at); 94 if (log_backtrace_at.empty()) { 95 absl::ClearLogBacktraceLocation(); 96 return; 97 } 98 99 const size_t last_colon = log_backtrace_at.rfind(':'); 100 if (last_colon == log_backtrace_at.npos) { 101 absl::ClearLogBacktraceLocation(); 102 return; 103 } 104 105 const absl::string_view file = 106 absl::string_view(log_backtrace_at).substr(0, last_colon); 107 int line; 108 if (!absl::SimpleAtoi( 109 absl::string_view(log_backtrace_at).substr(last_colon + 1), 110 &line)) { 111 absl::ClearLogBacktraceLocation(); 112 return; 113 } 114 absl::SetLogBacktraceLocation(file, line); 115 }); 116 117 ABSL_FLAG(bool, log_prefix, true, 118 "Prepend the log prefix to the start of each log line") 119 .OnUpdate([] { 120 absl::log_internal::RawEnableLogPrefix(absl::GetFlag(FLAGS_log_prefix)); 121 }); 122 123 ABSL_FLAG(int, v, 0, 124 "Show all VLOG(m) messages for m <= this. Overridable by --vmodule.") 125 .OnUpdate([] { 126 absl::log_internal::UpdateGlobalVLogLevel(absl::GetFlag(FLAGS_v)); 127 }); 128 129 ABSL_FLAG( 130 std::string, vmodule, "", 131 "per-module log verbosity level." 132 " Argument is a comma-separated list of <module name>=<log level>." 133 " <module name> is a glob pattern, matched against the filename base" 134 " (that is, name ignoring .cc/.h./-inl.h)." 135 " A pattern without slashes matches just the file name portion, otherwise" 136 " the whole file path below the workspace root" 137 " (still without .cc/.h./-inl.h) is matched." 138 " ? and * in the glob pattern match any single or sequence of characters" 139 " respectively including slashes." 140 " <log level> overrides any value given by --v.") 141 .OnUpdate([] { 142 absl::log_internal::UpdateVModule(absl::GetFlag(FLAGS_vmodule)); 143 });