2 * Copyright (c) 2008, 2009 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 /* Logging importance levels. */
28 VLOG_LEVEL(EMER, LOG_ALERT) \
29 VLOG_LEVEL(ERR, LOG_ERR) \
30 VLOG_LEVEL(WARN, LOG_WARNING) \
31 VLOG_LEVEL(INFO, LOG_NOTICE) \
32 VLOG_LEVEL(DBG, LOG_DEBUG)
34 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL) VLL_##NAME,
40 const char *vlog_get_level_name(enum vlog_level);
41 enum vlog_level vlog_get_level_val(const char *name);
43 /* Facilities that we can log to. */
44 #define VLOG_FACILITIES \
45 VLOG_FACILITY(SYSLOG, "%05N|%c|%p|%m") \
46 VLOG_FACILITY(CONSOLE, "%d{%b %d %H:%M:%S}|%05N|%c|%p|%m") \
47 VLOG_FACILITY(FILE, "%d{%b %d %H:%M:%S}|%05N|%c|%p|%m")
49 #define VLOG_FACILITY(NAME, PATTERN) VLF_##NAME,
56 const char *vlog_get_facility_name(enum vlog_facility);
57 enum vlog_facility vlog_get_facility_val(const char *name);
59 /* VLM_ constant for each vlog module. */
61 #define VLOG_MODULE(NAME) VLM_##NAME,
62 #include "vlog-modules.def"
67 const char *vlog_get_module_name(enum vlog_module);
68 enum vlog_module vlog_get_module_val(const char *name);
70 /* Rate-limiter for log messages. */
71 struct vlog_rate_limit {
72 /* Configuration settings. */
73 unsigned int rate; /* Tokens per second. */
74 unsigned int burst; /* Max cumulative tokens credit. */
77 unsigned int tokens; /* Current number of tokens. */
78 time_t last_fill; /* Last time tokens added. */
79 time_t first_dropped; /* Time first message was dropped. */
80 unsigned int n_dropped; /* Number of messages dropped. */
83 /* Number of tokens to emit a message. We add 'rate' tokens per second, which
84 * is 60 times the unit used for 'rate', thus 60 tokens are required to emit
86 #define VLOG_MSG_TOKENS 60
88 /* Initializer for a struct vlog_rate_limit, to set up a maximum rate of RATE
89 * messages per minute and a maximum burst size of BURST messages. */
90 #define VLOG_RATE_LIMIT_INIT(RATE, BURST) \
93 (MIN(BURST, UINT_MAX / VLOG_MSG_TOKENS) \
94 * VLOG_MSG_TOKENS), /* burst */ \
97 0, /* first_dropped */ \
101 /* Configuring how each module logs messages. */
102 enum vlog_level vlog_get_level(enum vlog_module, enum vlog_facility);
103 void vlog_set_levels(enum vlog_module, enum vlog_facility, enum vlog_level);
104 char *vlog_set_levels_from_string(const char *);
105 char *vlog_get_levels(void);
106 bool vlog_is_enabled(enum vlog_module, enum vlog_level);
107 bool vlog_should_drop(enum vlog_module, enum vlog_level,
108 struct vlog_rate_limit *);
109 void vlog_set_verbosity(const char *arg);
111 /* Configuring log facilities. */
112 void vlog_set_pattern(enum vlog_facility, const char *pattern);
113 const char *vlog_get_log_file(void);
114 int vlog_set_log_file(const char *file_name);
115 int vlog_reopen_log_file(void);
117 /* Function for actual logging. */
118 void vlog_init(void);
119 void vlog_exit(void);
120 void vlog(enum vlog_module, enum vlog_level, const char *format, ...)
121 __attribute__((format(printf, 3, 4)));
122 void vlog_valist(enum vlog_module, enum vlog_level, const char *, va_list)
123 __attribute__((format(printf, 3, 0)));
124 void vlog_rate_limit(enum vlog_module, enum vlog_level,
125 struct vlog_rate_limit *, const char *, ...)
126 __attribute__((format(printf, 4, 5)));
128 /* Convenience macros. To use these, define THIS_MODULE as a macro that
129 * expands to the module used by the current source file, e.g.
131 * #define THIS_MODULE VLM_netlink
132 * Guaranteed to preserve errno.
134 #define VLOG_EMER(...) VLOG(VLL_EMER, __VA_ARGS__)
135 #define VLOG_ERR(...) VLOG(VLL_ERR, __VA_ARGS__)
136 #define VLOG_WARN(...) VLOG(VLL_WARN, __VA_ARGS__)
137 #define VLOG_INFO(...) VLOG(VLL_INFO, __VA_ARGS__)
138 #define VLOG_DBG(...) VLOG(VLL_DBG, __VA_ARGS__)
140 /* More convenience macros, for testing whether a given level is enabled in
141 * THIS_MODULE. When constructing a log message is expensive, this enables it
143 #define VLOG_IS_EMER_ENABLED() true
144 #define VLOG_IS_ERR_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_EMER)
145 #define VLOG_IS_WARN_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_WARN)
146 #define VLOG_IS_INFO_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_INFO)
147 #define VLOG_IS_DBG_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_DBG)
149 /* Convenience macros for rate-limiting.
150 * Guaranteed to preserve errno.
152 #define VLOG_ERR_RL(RL, ...) VLOG_RL(RL, VLL_ERR, __VA_ARGS__)
153 #define VLOG_WARN_RL(RL, ...) VLOG_RL(RL, VLL_WARN, __VA_ARGS__)
154 #define VLOG_INFO_RL(RL, ...) VLOG_RL(RL, VLL_INFO, __VA_ARGS__)
155 #define VLOG_DBG_RL(RL, ...) VLOG_RL(RL, VLL_DBG, __VA_ARGS__)
157 #define VLOG_DROP_ERR(RL) vlog_should_drop(THIS_MODULE, VLL_ERR, RL)
158 #define VLOG_DROP_WARN(RL) vlog_should_drop(THIS_MODULE, VLL_WARN, RL)
159 #define VLOG_DROP_INFO(RL) vlog_should_drop(THIS_MODULE, VLL_INFO, RL)
160 #define VLOG_DROP_DBG(RL) vlog_should_drop(THIS_MODULE, VLL_DBG, RL)
162 /* Command line processing. */
163 #define VLOG_OPTION_ENUMS OPT_LOG_FILE
164 #define VLOG_LONG_OPTIONS \
165 {"verbose", optional_argument, 0, 'v'}, \
166 {"log-file", optional_argument, 0, OPT_LOG_FILE}
167 #define VLOG_OPTION_HANDLERS \
169 vlog_set_verbosity(optarg); \
172 vlog_set_log_file(optarg); \
174 void vlog_usage(void);
176 /* Implementation details. */
177 #define VLOG(LEVEL, ...) \
179 if (min_vlog_levels[THIS_MODULE] >= LEVEL) { \
180 vlog(THIS_MODULE, LEVEL, __VA_ARGS__); \
183 #define VLOG_RL(RL, LEVEL, ...) \
185 if (min_vlog_levels[THIS_MODULE] >= LEVEL) { \
186 vlog_rate_limit(THIS_MODULE, LEVEL, RL, __VA_ARGS__); \
189 extern enum vlog_level min_vlog_levels[VLM_N_MODULES];