1 // Copyright 1999, 2007 Google Inc. All Rights Reserved.
2 // Author: Ray Sidney and many others
3 // Broken out from logging.cc by Soren Lassen
4 // logging_unittest.cc covers the functionality herein
11 #include <fnmatch.h> // fnmatch() is used in obsolete _InitVLOG
14 #include "base/commandlineflags.h"
15 #include "glog/logging.h"
16 #include "glog/raw_logging.h"
17 #include "base/googleinit.h"
19 // glog doesn't have annotation
20 #define ANNOTATE_BENIGN_RACE(address, description)
24 DEFINE_int32(v, 0, "Show all VLOG(m) messages for m <= this."
25 " Overridable by --vmodule.");
27 DEFINE_string(vmodule, "", "per-module verbose level."
28 " Argument is a comma-separated list of <module name>=<log level>."
29 " <module name> is a glob pattern, matched against the filename base"
30 " (that is, name ignoring .cc/.h./-inl.h)."
31 " <log level> overrides any value given by --v.");
33 _START_GOOGLE_NAMESPACE_
35 // Implementation of fnmatch that does not need 0-termination
36 // of arguments and does not allocate any memory,
37 // but we only support "*" and "?" wildcards, not the "[...]" patterns.
38 // It's not a static function for the unittest.
39 bool SafeFNMatch_(const char* pattern, size_t patt_len,
40 const char* str, size_t str_len) {
44 if (p == patt_len && s == str_len) return true;
45 if (p == patt_len) return false;
46 if (s == str_len) return p+1 == patt_len && pattern[p] == '*';
47 if (pattern[p] == str[s] || pattern[p] == '?') {
52 if (pattern[p] == '*') {
53 if (p+1 == patt_len) return true;
55 if (SafeFNMatch_(pattern+(p+1), patt_len-(p+1), str+s, str_len-s)) {
59 } while (s != str_len);
66 int32 kLogSiteUninitialized = 1000;
68 // List of per-module log levels from FLAGS_vmodule.
69 // Once created each element is never deleted/modified
70 // except for the vlog_level: other threads will read VModuleInfo blobs
71 // w/o locks and we'll store pointers to vlog_level at VLOG locations
72 // that will never go away.
73 // We can't use an STL struct here as we wouldn't know
74 // when it's safe to delete/update it: other threads need to use it w/o locks.
76 string module_pattern;
77 mutable int32 vlog_level; // Conceptually this is an AtomicWord, but it's
78 // too much work to use AtomicWord type here
79 // w/o much actual benefit.
80 const VModuleInfo* next;
83 // This protects the following global variables.
84 static Mutex vmodule_lock;
85 // Pointer to head of the VModuleInfo list.
86 // It's a map from module pattern to logging level for those module(s).
87 static VModuleInfo* vmodule_list = 0;
88 // Boolean initialization flag.
89 static bool inited_vmodule = false;
92 static void VLOG2Initializer() {
93 vmodule_lock.AssertHeld();
94 // Can now parse --vmodule flag and initialize mapping of module-specific
96 inited_vmodule = false;
97 const char* vmodule = FLAGS_vmodule.c_str();
99 VModuleInfo* head = NULL;
100 VModuleInfo* tail = NULL;
101 while ((sep = strchr(vmodule, '=')) != NULL) {
102 string pattern(vmodule, sep - vmodule);
104 if (sscanf(sep, "=%d", &module_level) == 1) {
105 VModuleInfo* info = new VModuleInfo;
106 info->module_pattern = pattern;
107 info->vlog_level = module_level;
108 if (head) tail->next = info;
112 // Skip past this entry
113 vmodule = strchr(sep, ',');
114 if (vmodule == NULL) break;
115 vmodule++; // Skip past ","
117 if (head) { // Put them into the list at the head:
118 tail->next = vmodule_list;
121 inited_vmodule = true;
124 // This can be called very early, so we use SpinLock and RAW_VLOG here.
125 int SetVLOGLevel(const char* module_pattern, int log_level) {
126 int result = FLAGS_v;
127 int const pattern_len = strlen(module_pattern);
129 MutexLock l(&vmodule_lock); // protect whole read-modify-write
130 for (const VModuleInfo* info = vmodule_list;
131 info != NULL; info = info->next) {
132 if (info->module_pattern == module_pattern) {
134 result = info->vlog_level;
137 info->vlog_level = log_level;
139 SafeFNMatch_(info->module_pattern.c_str(),
140 info->module_pattern.size(),
141 module_pattern, pattern_len)) {
142 result = info->vlog_level;
147 VModuleInfo* info = new VModuleInfo;
148 info->module_pattern = module_pattern;
149 info->vlog_level = log_level;
150 info->next = vmodule_list;
153 RAW_VLOG(1, "Set VLOG level for \"%s\" to %d", module_pattern, log_level);
157 // NOTE: Individual VLOG statements cache the integer log level pointers.
158 // NOTE: This function must not allocate memory or require any locks.
159 bool InitVLOG3__(int32** site_flag, int32* site_default,
160 const char* fname, int32 verbose_level) {
161 MutexLock l(&vmodule_lock);
162 bool read_vmodule_flag = inited_vmodule;
163 if (!read_vmodule_flag) {
167 // protect the errno global in case someone writes:
168 // VLOG(..) << "The last error was " << strerror(errno)
169 int old_errno = errno;
171 // site_default normally points to FLAGS_v
172 int32* site_flag_value = site_default;
174 // Get basename for file
175 const char* base = strrchr(fname, '/');
176 base = base ? (base+1) : fname;
177 const char* base_end = strchr(base, '.');
178 size_t base_length = base_end ? (base_end - base) : strlen(base);
180 // Trim out trailing "-inl" if any
181 if (base_length >= 4 && (memcmp(base+base_length-4, "-inl", 4) == 0)) {
185 // TODO: Trim out _unittest suffix? Perhaps it is better to have
186 // the extra control and just leave it there.
188 // find target in vector of modules, replace site_flag_value with
189 // a module-specific verbose level, if any.
190 for (const VModuleInfo* info = vmodule_list;
191 info != NULL; info = info->next) {
192 if (SafeFNMatch_(info->module_pattern.c_str(), info->module_pattern.size(),
193 base, base_length)) {
194 site_flag_value = &info->vlog_level;
195 // value at info->vlog_level is now what controls
196 // the VLOG at the caller site forever
201 // Cache the vlog value pointer if --vmodule flag has been parsed.
202 ANNOTATE_BENIGN_RACE(site_flag,
203 "*site_flag may be written by several threads,"
204 " but the value will be the same");
205 if (read_vmodule_flag) *site_flag = site_flag_value;
207 // restore the errno in case something recoverable went wrong during
208 // the initialization of the VLOG mechanism (see above note "protect the..")
210 return *site_flag_value >= verbose_level;
213 _END_GOOGLE_NAMESPACE_