OR-Tools  9.6
vlog_is_on.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
15 
16 #include <errno.h>
17 #include <stdlib.h>
18 
19 #include <cstdio>
20 #include <cstring>
21 #include <string>
22 
23 #include "absl/flags/flag.h"
24 #include "absl/synchronization/mutex.h"
25 
26 ABSL_FLAG(int, v, 0,
27  "Show all VLOG(m) messages for m <= this."
28  " Overridable by --vmodule.");
29 
30 ABSL_FLAG(std::string, vmodule, "",
31  "per-module verbose level."
32  " Argument is a comma-separated list of <module name>=<log level>."
33  " <module name> is a glob pattern, matched against the filename base"
34  " (that is, name ignoring .cc/.h./-inl.h)."
35  " <log level> overrides any value given by --v.");
36 
37 namespace google {
38 
39 namespace logging_internal {
40 
41 // Used by logging_unittests.cc so can't make it static here.
42 bool SafeFNMatch_(const char* pattern, size_t patt_len, const char* str,
43  size_t str_len);
44 
45 // Implementation of fnmatch that does not need 0-termination
46 // of arguments and does not allocate any memory,
47 // but we only support "*" and "?" wildcards, not the "[...]" patterns.
48 // It's not a static function for the unittest.
49 bool SafeFNMatch_(const char* pattern, size_t patt_len, const char* str,
50  size_t str_len) {
51  size_t p = 0;
52  size_t s = 0;
53  while (1) {
54  if (p == patt_len && s == str_len) return true;
55  if (p == patt_len) return false;
56  if (s == str_len) return p + 1 == patt_len && pattern[p] == '*';
57  if (pattern[p] == str[s] || pattern[p] == '?') {
58  p += 1;
59  s += 1;
60  continue;
61  }
62  if (pattern[p] == '*') {
63  if (p + 1 == patt_len) return true;
64  do {
65  if (SafeFNMatch_(pattern + (p + 1), patt_len - (p + 1), str + s,
66  str_len - s)) {
67  return true;
68  }
69  s += 1;
70  } while (s != str_len);
71  return false;
72  }
73  return false;
74  }
75 }
76 
77 } // namespace logging_internal
78 
80 
81 int32_t kLogSiteUninitialized = 1000;
82 
83 // List of per-module log levels from absl::GetFlag(FLAGS_vmodule).
84 // Once created each element is never deleted/modified
85 // except for the vlog_level: other threads will read VModuleInfo blobs
86 // w/o locks and we'll store pointers to vlog_level at VLOG locations
87 // that will never go away.
88 // We can't use an STL struct here as we wouldn't know
89 // when it's safe to delete/update it: other threads need to use it w/o locks.
90 struct VModuleInfo {
91  std::string module_pattern;
92  mutable int32_t vlog_level; // Conceptually this is an AtomicWord, but it's
93  // too much work to use AtomicWord type here
94  // w/o much actual benefit.
95  const VModuleInfo* next;
96 };
97 
98 // This protects the following global variables.
99 static absl::Mutex vmodule_lock;
100 // Pointer to head of the VModuleInfo list.
101 // It's a map from module pattern to logging level for those module(s).
103 // Boolean initialization flag.
104 static bool inited_vmodule = false;
105 
106 // L >= vmodule_lock.
107 static void VLOG2Initializer() {
108  vmodule_lock.AssertHeld();
109  // Can now parse --vmodule flag and initialize mapping of module-specific
110  // logging levels.
111  inited_vmodule = false;
112  const std::string vmodule_flag = absl::GetFlag(FLAGS_vmodule);
113  const char* vmodule = vmodule_flag.c_str();
114  const char* sep;
115  VModuleInfo* head = nullptr;
116  VModuleInfo* tail = nullptr;
117  while ((sep = strchr(vmodule, '=')) != nullptr) {
118  std::string pattern(vmodule, sep - vmodule);
119  int module_level;
120  if (sscanf(sep, "=%d", &module_level) == 1) {
121  VModuleInfo* info = new VModuleInfo;
122  info->module_pattern = pattern;
123  info->vlog_level = module_level;
124  if (head)
125  tail->next = info;
126  else
127  head = info;
128  tail = info;
129  }
130  // Skip past this entry
131  vmodule = strchr(sep, ',');
132  if (vmodule == nullptr) break;
133  vmodule++; // Skip past ","
134  }
135  if (head) { // Put them into the list at the head:
136  tail->next = vmodule_list;
137  vmodule_list = head;
138  }
139  inited_vmodule = true;
140 }
141 
142 // This can be called very early, so we use SpinLock and RAW_VLOG here.
143 int SetVLOGLevel(const char* module_pattern, int log_level) {
144  int result = absl::GetFlag(FLAGS_v);
145  int const pattern_len = strlen(module_pattern);
146  bool found = false;
147  {
148  absl::MutexLock l(&vmodule_lock); // protect whole read-modify-write
149  for (const VModuleInfo* info = vmodule_list; info != nullptr;
150  info = info->next) {
151  if (info->module_pattern == module_pattern) {
152  if (!found) {
153  result = info->vlog_level;
154  found = true;
155  }
156  info->vlog_level = log_level;
157  } else if (!found && SafeFNMatch_(info->module_pattern.c_str(),
158  info->module_pattern.size(),
159  module_pattern, pattern_len)) {
160  result = info->vlog_level;
161  found = true;
162  }
163  }
164  if (!found) {
165  VModuleInfo* info = new VModuleInfo;
166  info->module_pattern = module_pattern;
167  info->vlog_level = log_level;
168  info->next = vmodule_list;
169  vmodule_list = info;
170  }
171  }
172  // RAW_VLOG(1, "Set VLOG level for \"%s\" to %d", module_pattern, log_level);
173  return result;
174 }
175 
176 // NOTE: Individual VLOG statements cache the integer log level pointers.
177 // NOTE: This function must not allocate memory or require any locks.
178 bool InitVLOG3__(int32_t** vmodule_info, bool* initialized, const char* fname,
179  int32_t verbose_level) {
180  absl::MutexLock l(&vmodule_lock);
181  bool read_vmodule_flag = inited_vmodule;
182  if (!read_vmodule_flag) {
184  }
185 
186  // protect the errno global in case someone writes:
187  // VLOG(..) << "The last error was " << strerror(errno)
188  int old_errno = errno;
189 
190  // Set the initialized flag.
191  *initialized = true;
192 
193  // Get basename for file
194  const char* base = strrchr(fname, '/');
195  base = base ? (base + 1) : fname;
196  const char* base_end = strchr(base, '.');
197  size_t base_length = base_end ? size_t(base_end - base) : strlen(base);
198 
199  // Trim out trailing "-inl" if any
200  if (base_length >= 4 && (memcmp(base + base_length - 4, "-inl", 4) == 0)) {
201  base_length -= 4;
202  }
203 
204  // TODO: Trim out _unittest suffix? Perhaps it is better to have
205  // the extra control and just leave it there.
206 
207  // find target in vector of modules, replace site_flag_value with
208  // a module-specific verbose level, if any.
209  for (const VModuleInfo* info = vmodule_list; info != nullptr;
210  info = info->next) {
211  if (SafeFNMatch_(info->module_pattern.c_str(), info->module_pattern.size(),
212  base, base_length)) {
213  *vmodule_info = &info->vlog_level;
214  // value at info->vlog_level is now what controls
215  // the VLOG at the caller site forever
216  break;
217  }
218  }
219 
220  // restore the errno in case something recoverable went wrong during
221  // the initialization of the VLOG mechanism (see above note "protect the..")
222  errno = old_errno;
223  return *vmodule_info == nullptr ? absl::GetFlag(FLAGS_v) >= verbose_level
224  : **vmodule_info >= verbose_level;
225 }
226 
227 } // namespace google
bool SafeFNMatch_(const char *pattern, size_t patt_len, const char *str, size_t str_len)
Definition: vlog_is_on.cc:49
int SetVLOGLevel(const char *module_pattern, int log_level)
Definition: vlog_is_on.cc:143
int32_t kLogSiteUninitialized
Definition: vlog_is_on.cc:81
static absl::Mutex vmodule_lock
Definition: vlog_is_on.cc:99
static bool inited_vmodule
Definition: vlog_is_on.cc:104
static VModuleInfo * vmodule_list
Definition: vlog_is_on.cc:102
bool InitVLOG3__(int32_t **vmodule_info, bool *initialized, const char *fname, int32_t verbose_level)
Definition: vlog_is_on.cc:178
static void VLOG2Initializer()
Definition: vlog_is_on.cc:107
int64_t tail
int64_t head
std::string module_pattern
Definition: vlog_is_on.cc:91
const VModuleInfo * next
Definition: vlog_is_on.cc:95
ABSL_FLAG(int, v, 0, "Show all VLOG(m) messages for m <= this." " Overridable by --vmodule.")