Add -lpthread in addition to -pthread on Linux.
[platform/upstream/glog.git] / src / vlog_is_on.cc
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
5
6 #include "utilities.h"
7
8 #include <string.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <fnmatch.h>  // fnmatch() is used in obsolete _InitVLOG
12 #include <cstdio>
13 #include <string>
14 #include "base/commandlineflags.h"
15 #include "glog/logging.h"
16 #include "glog/raw_logging.h"
17 #include "base/googleinit.h"
18
19 // glog doesn't have annotation
20 #define ANNOTATE_BENIGN_RACE(address, description)
21
22 using std::string;
23
24 DEFINE_int32(v, 0, "Show all VLOG(m) messages for m <= this."
25 " Overridable by --vmodule.");
26
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.");
32
33 _START_GOOGLE_NAMESPACE_
34
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) {
41   int p = 0;
42   int s = 0;
43   while (1) {
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] == '?') {
48       p += 1;
49       s += 1;
50       continue;
51     }
52     if (pattern[p] == '*') {
53       if (p+1 == patt_len) return true;
54       do {
55         if (SafeFNMatch_(pattern+(p+1), patt_len-(p+1), str+s, str_len-s)) {
56           return true;
57         }
58         s += 1;
59       } while (s != str_len);
60       return false;
61     }
62     return false;
63   }
64 }
65
66 int32 kLogSiteUninitialized = 1000;
67
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.
75 struct VModuleInfo {
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;
81 };
82
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;
90
91 // L >= vmodule_lock.
92 static void VLOG2Initializer() {
93   vmodule_lock.AssertHeld();
94   // Can now parse --vmodule flag and initialize mapping of module-specific
95   // logging levels.
96   inited_vmodule = false;
97   const char* vmodule = FLAGS_vmodule.c_str();
98   const char* sep;
99   VModuleInfo* head = NULL;
100   VModuleInfo* tail = NULL;
101   while ((sep = strchr(vmodule, '=')) != NULL) {
102     string pattern(vmodule, sep - vmodule);
103     int module_level;
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;
109       else  head = info;
110       tail = info;
111     }
112     // Skip past this entry
113     vmodule = strchr(sep, ',');
114     if (vmodule == NULL) break;
115     vmodule++;  // Skip past ","
116   }
117   if (head) {  // Put them into the list at the head:
118     tail->next = vmodule_list;
119     vmodule_list = head;
120   }
121   inited_vmodule = true;
122 }
123
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);
128   bool found = false;
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) {
133       if (!found) {
134         result = info->vlog_level;
135         found = true;
136       }
137       info->vlog_level = log_level;
138     } else if (!found  &&
139                SafeFNMatch_(info->module_pattern.c_str(),
140                             info->module_pattern.size(),
141                             module_pattern, pattern_len)) {
142       result = info->vlog_level;
143       found = true;
144     }
145   }
146   if (!found) {
147     VModuleInfo* info = new VModuleInfo;
148     info->module_pattern = module_pattern;
149     info->vlog_level = log_level;
150     info->next = vmodule_list;
151     vmodule_list = info;
152   }
153   RAW_VLOG(1, "Set VLOG level for \"%s\" to %d", module_pattern, log_level);
154   return result;
155 }
156
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) {
164     VLOG2Initializer();
165   }
166
167   // protect the errno global in case someone writes:
168   // VLOG(..) << "The last error was " << strerror(errno)
169   int old_errno = errno;
170
171   // site_default normally points to FLAGS_v
172   int32* site_flag_value = site_default;
173
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);
179
180   // Trim out trailing "-inl" if any
181   if (base_length >= 4 && (memcmp(base+base_length-4, "-inl", 4) == 0)) {
182     base_length -= 4;
183   }
184
185   // TODO: Trim out _unittest suffix?  Perhaps it is better to have
186   // the extra control and just leave it there.
187
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
197       break;
198     }
199   }
200
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;
206
207   // restore the errno in case something recoverable went wrong during
208   // the initialization of the VLOG mechanism (see above note "protect the..")
209   errno = old_errno;
210   return *site_flag_value >= verbose_level;
211 }
212
213 _END_GOOGLE_NAMESPACE_