vlog: Make VLOG_ONCE thread-safe.
[sliver-openvswitch.git] / lib / vlog.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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  *     http://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
17 #ifndef VLOG_H
18 #define VLOG_H 1
19
20 #include <limits.h>
21 #include <stdarg.h>
22 #include <stdbool.h>
23 #include <time.h>
24 #include "compiler.h"
25 #include "ovs-thread.h"
26 #include "sat-math.h"
27 #include "token-bucket.h"
28 #include "util.h"
29
30 #ifdef  __cplusplus
31 extern "C" {
32 #endif
33
34 /* Logging severity levels.
35  *
36  * ovs-appctl(8) defines each of the log levels. */
37 #define VLOG_LEVELS                             \
38     VLOG_LEVEL(OFF, LOG_ALERT)                  \
39     VLOG_LEVEL(EMER, LOG_ALERT)                 \
40     VLOG_LEVEL(ERR, LOG_ERR)                    \
41     VLOG_LEVEL(WARN, LOG_WARNING)               \
42     VLOG_LEVEL(INFO, LOG_NOTICE)                \
43     VLOG_LEVEL(DBG, LOG_DEBUG)
44 enum vlog_level {
45 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL) VLL_##NAME,
46     VLOG_LEVELS
47 #undef VLOG_LEVEL
48     VLL_N_LEVELS
49 };
50
51 const char *vlog_get_level_name(enum vlog_level);
52 enum vlog_level vlog_get_level_val(const char *name);
53
54 /* Facilities that we can log to. */
55 #define VLOG_FACILITIES                                                 \
56     VLOG_FACILITY(SYSLOG, "ovs|%05N|%c%T|%p|%m")                            \
57     VLOG_FACILITY(CONSOLE, "%D{%Y-%m-%dT%H:%M:%SZ}|%05N|%c%T|%p|%m")    \
58     VLOG_FACILITY(FILE, "%D{%Y-%m-%dT%H:%M:%SZ}|%05N|%c%T|%p|%m")
59 enum vlog_facility {
60 #define VLOG_FACILITY(NAME, PATTERN) VLF_##NAME,
61     VLOG_FACILITIES
62 #undef VLOG_FACILITY
63     VLF_N_FACILITIES,
64     VLF_ANY_FACILITY = -1
65 };
66
67 const char *vlog_get_facility_name(enum vlog_facility);
68 enum vlog_facility vlog_get_facility_val(const char *name);
69
70 /* A log module. */
71 struct vlog_module {
72     const char *name;             /* User-visible name. */
73     int levels[VLF_N_FACILITIES]; /* Minimum log level for each facility. */
74     int min_level;                /* Minimum log level for any facility. */
75     bool honor_rate_limits;       /* Set false to ignore rate limits. */
76 };
77
78 /* Creates and initializes a global instance of a module named MODULE. */
79 #if USE_LINKER_SECTIONS
80 #define VLOG_DEFINE_MODULE(MODULE)                                      \
81         VLOG_DEFINE_MODULE__(MODULE)                                    \
82         extern struct vlog_module *vlog_module_ptr_##MODULE;            \
83         struct vlog_module *vlog_module_ptr_##MODULE                    \
84             __attribute__((section("vlog_modules"))) = &VLM_##MODULE
85 #else
86 #define VLOG_DEFINE_MODULE(MODULE) extern struct vlog_module VLM_##MODULE
87 #endif
88
89 const char *vlog_get_module_name(const struct vlog_module *);
90 struct vlog_module *vlog_module_from_name(const char *name);
91
92 /* Rate-limiter for log messages. */
93 struct vlog_rate_limit {
94     struct token_bucket token_bucket;
95     time_t first_dropped;       /* Time first message was dropped. */
96     time_t last_dropped;        /* Time of most recent message drop. */
97     unsigned int n_dropped;     /* Number of messages dropped. */
98     pthread_mutex_t mutex;      /* Mutual exclusion for rate limit. */
99 };
100
101 /* Number of tokens to emit a message.  We add 'rate' tokens per millisecond,
102  * thus 60,000 tokens are required to emit one message per minute. */
103 #define VLOG_MSG_TOKENS (60 * 1000)
104
105 /* Initializer for a struct vlog_rate_limit, to set up a maximum rate of RATE
106  * messages per minute and a maximum burst size of BURST messages. */
107 #define VLOG_RATE_LIMIT_INIT(RATE, BURST)                               \
108         {                                                               \
109             TOKEN_BUCKET_INIT(RATE, SAT_MUL(BURST, VLOG_MSG_TOKENS)),   \
110             0,                              /* first_dropped */         \
111             0,                              /* last_dropped */          \
112             0,                              /* n_dropped */             \
113             PTHREAD_ADAPTIVE_MUTEX_INITIALIZER /* mutex */              \
114         }
115
116 /* Configuring how each module logs messages. */
117 enum vlog_level vlog_get_level(const struct vlog_module *, enum vlog_facility);
118 void vlog_set_levels(struct vlog_module *,
119                      enum vlog_facility, enum vlog_level);
120 char *vlog_set_levels_from_string(const char *) WARN_UNUSED_RESULT;
121 void vlog_set_levels_from_string_assert(const char *);
122 char *vlog_get_levels(void);
123 bool vlog_is_enabled(const struct vlog_module *, enum vlog_level);
124 bool vlog_should_drop(const struct vlog_module *, enum vlog_level,
125                       struct vlog_rate_limit *);
126 void vlog_set_verbosity(const char *arg);
127
128 /* Configuring log facilities. */
129 void vlog_set_pattern(enum vlog_facility, const char *pattern);
130 int vlog_set_log_file(const char *file_name);
131 int vlog_reopen_log_file(void);
132
133 /* Initialization. */
134 void vlog_init(void);
135
136 /* Functions for actual logging. */
137 void vlog(const struct vlog_module *, enum vlog_level, const char *format, ...)
138     PRINTF_FORMAT (3, 4);
139 void vlog_valist(const struct vlog_module *, enum vlog_level,
140                  const char *, va_list)
141     PRINTF_FORMAT (3, 0);
142
143 void vlog_fatal(const struct vlog_module *, const char *format, ...)
144     PRINTF_FORMAT (2, 3) NO_RETURN;
145 void vlog_fatal_valist(const struct vlog_module *, const char *format, va_list)
146     PRINTF_FORMAT (2, 0) NO_RETURN;
147
148 void vlog_abort(const struct vlog_module *, const char *format, ...)
149     PRINTF_FORMAT (2, 3) NO_RETURN;
150 void vlog_abort_valist(const struct vlog_module *, const char *format, va_list)
151     PRINTF_FORMAT (2, 0) NO_RETURN;
152
153 void vlog_rate_limit(const struct vlog_module *, enum vlog_level,
154                      struct vlog_rate_limit *, const char *, ...)
155     PRINTF_FORMAT (4, 5);
156
157 /* Creates and initializes a global instance of a module named MODULE, and
158  * defines a static variable named THIS_MODULE that points to it, for use with
159  * the convenience macros below. */
160 #define VLOG_DEFINE_THIS_MODULE(MODULE)                                 \
161         VLOG_DEFINE_MODULE(MODULE);                                     \
162         static struct vlog_module *const THIS_MODULE = &VLM_##MODULE
163
164 /* Convenience macros.  These assume that THIS_MODULE points to a "struct
165  * vlog_module" for the current module, as set up by e.g. the
166  * VLOG_DEFINE_MODULE macro above.
167  *
168  * Guaranteed to preserve errno.
169  */
170 #define VLOG_FATAL(...) vlog_fatal(THIS_MODULE, __VA_ARGS__)
171 #define VLOG_ABORT(...) vlog_abort(THIS_MODULE, __VA_ARGS__)
172 #define VLOG_EMER(...) VLOG(VLL_EMER, __VA_ARGS__)
173 #define VLOG_ERR(...) VLOG(VLL_ERR, __VA_ARGS__)
174 #define VLOG_WARN(...) VLOG(VLL_WARN, __VA_ARGS__)
175 #define VLOG_INFO(...) VLOG(VLL_INFO, __VA_ARGS__)
176 #define VLOG_DBG(...) VLOG(VLL_DBG, __VA_ARGS__)
177
178 /* More convenience macros, for testing whether a given level is enabled in
179  * THIS_MODULE.  When constructing a log message is expensive, this enables it
180  * to be skipped. */
181 #define VLOG_IS_ERR_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_ERR)
182 #define VLOG_IS_WARN_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_WARN)
183 #define VLOG_IS_INFO_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_INFO)
184 #define VLOG_IS_DBG_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_DBG)
185
186 /* Convenience macros for rate-limiting.
187  * Guaranteed to preserve errno.
188  */
189 #define VLOG_ERR_RL(RL, ...) VLOG_RL(RL, VLL_ERR, __VA_ARGS__)
190 #define VLOG_WARN_RL(RL, ...) VLOG_RL(RL, VLL_WARN, __VA_ARGS__)
191 #define VLOG_INFO_RL(RL, ...) VLOG_RL(RL, VLL_INFO, __VA_ARGS__)
192 #define VLOG_DBG_RL(RL, ...) VLOG_RL(RL, VLL_DBG, __VA_ARGS__)
193
194 #define VLOG_DROP_ERR(RL) vlog_should_drop(THIS_MODULE, VLL_ERR, RL)
195 #define VLOG_DROP_WARN(RL) vlog_should_drop(THIS_MODULE, VLL_WARN, RL)
196 #define VLOG_DROP_INFO(RL) vlog_should_drop(THIS_MODULE, VLL_INFO, RL)
197 #define VLOG_DROP_DBG(RL) vlog_should_drop(THIS_MODULE, VLL_DBG, RL)
198
199 /* Macros for logging at most once per execution. */
200 #define VLOG_ERR_ONCE(...) VLOG_ONCE(VLL_ERR, __VA_ARGS__)
201 #define VLOG_WARN_ONCE(...) VLOG_ONCE(VLL_WARN, __VA_ARGS__)
202 #define VLOG_INFO_ONCE(...) VLOG_ONCE(VLL_INFO, __VA_ARGS__)
203 #define VLOG_DBG_ONCE(...) VLOG_ONCE(VLL_DBG, __VA_ARGS__)
204
205 /* Command line processing. */
206 #define VLOG_OPTION_ENUMS OPT_LOG_FILE
207 #define VLOG_LONG_OPTIONS                                   \
208         {"verbose",     optional_argument, NULL, 'v'},         \
209         {"log-file",    optional_argument, NULL, OPT_LOG_FILE}
210 #define VLOG_OPTION_HANDLERS                    \
211         case 'v':                               \
212             vlog_set_verbosity(optarg);         \
213             break;                              \
214         case OPT_LOG_FILE:                      \
215             vlog_set_log_file(optarg);          \
216             break;
217 void vlog_usage(void);
218
219 /* Implementation details. */
220 #define VLOG(LEVEL, ...)                                \
221     do {                                                \
222         enum vlog_level level__ = LEVEL;                \
223         if (THIS_MODULE->min_level >= level__) {        \
224             vlog(THIS_MODULE, level__, __VA_ARGS__);    \
225         }                                               \
226     } while (0)
227 #define VLOG_RL(RL, LEVEL, ...)                                     \
228     do {                                                            \
229         enum vlog_level level__ = LEVEL;                            \
230         if (THIS_MODULE->min_level >= level__) {                    \
231             vlog_rate_limit(THIS_MODULE, level__, RL, __VA_ARGS__); \
232         }                                                           \
233     } while (0)
234 #define VLOG_ONCE(LEVEL, ...)                                           \
235     do {                                                                \
236         static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; \
237         if (ovsthread_once_start(&once)) {                              \
238             vlog(THIS_MODULE, LEVEL, __VA_ARGS__);                      \
239             ovsthread_once_done(&once);                                 \
240         }                                                               \
241     } while (0)
242
243 #define VLOG_DEFINE_MODULE__(MODULE)                                    \
244         extern struct vlog_module VLM_##MODULE;                         \
245         struct vlog_module VLM_##MODULE =                               \
246         {                                                               \
247             #MODULE,                                        /* name */  \
248             { [ 0 ... VLF_N_FACILITIES - 1] = VLL_INFO }, /* levels */  \
249             VLL_INFO,                                  /* min_level */  \
250             true                               /* honor_rate_limits */  \
251         };
252
253 #ifdef  __cplusplus
254 }
255 #endif
256
257
258 #endif /* vlog.h */