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.
30 /* Logging importance levels. */
32 VLOG_LEVEL(EMER, LOG_ALERT) \
33 VLOG_LEVEL(ERR, LOG_ERR) \
34 VLOG_LEVEL(WARN, LOG_WARNING) \
35 VLOG_LEVEL(INFO, LOG_NOTICE) \
36 VLOG_LEVEL(DBG, LOG_DEBUG)
38 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL) VLL_##NAME,
44 const char *vlog_get_level_name(enum vlog_level);
45 enum vlog_level vlog_get_level_val(const char *name);
47 /* Facilities that we can log to. */
48 #define VLOG_FACILITIES \
49 VLOG_FACILITY(SYSLOG, "%05N|%c|%p|%m") \
50 VLOG_FACILITY(CONSOLE, "%d{%b %d %H:%M:%S}|%05N|%c|%p|%m") \
51 VLOG_FACILITY(FILE, "%d{%b %d %H:%M:%S}|%05N|%c|%p|%m")
53 #define VLOG_FACILITY(NAME, PATTERN) VLF_##NAME,
60 const char *vlog_get_facility_name(enum vlog_facility);
61 enum vlog_facility vlog_get_facility_val(const char *name);
63 /* VLM_ constant for each vlog module. */
65 #define VLOG_MODULE(NAME) VLM_##NAME,
66 #include "vlog-modules.def"
71 const char *vlog_get_module_name(enum vlog_module);
72 enum vlog_module vlog_get_module_val(const char *name);
74 /* Rate-limiter for log messages. */
75 struct vlog_rate_limit {
76 /* Configuration settings. */
77 unsigned int rate; /* Tokens per second. */
78 unsigned int burst; /* Max cumulative tokens credit. */
81 unsigned int tokens; /* Current number of tokens. */
82 time_t last_fill; /* Last time tokens added. */
83 time_t first_dropped; /* Time first message was dropped. */
84 unsigned int n_dropped; /* Number of messages dropped. */
87 /* Number of tokens to emit a message. We add 'rate' tokens per second, which
88 * is 60 times the unit used for 'rate', thus 60 tokens are required to emit
90 #define VLOG_MSG_TOKENS 60
92 /* Initializer for a struct vlog_rate_limit, to set up a maximum rate of RATE
93 * messages per minute and a maximum burst size of BURST messages. */
94 #define VLOG_RATE_LIMIT_INIT(RATE, BURST) \
97 (MIN(BURST, UINT_MAX / VLOG_MSG_TOKENS) \
98 * VLOG_MSG_TOKENS), /* burst */ \
101 0, /* first_dropped */ \
105 /* Configuring how each module logs messages. */
106 enum vlog_level vlog_get_level(enum vlog_module, enum vlog_facility);
107 void vlog_set_levels(enum vlog_module, enum vlog_facility, enum vlog_level);
108 char *vlog_set_levels_from_string(const char *);
109 char *vlog_get_levels(void);
110 bool vlog_is_enabled(enum vlog_module, enum vlog_level);
111 bool vlog_should_drop(enum vlog_module, enum vlog_level,
112 struct vlog_rate_limit *);
113 void vlog_set_verbosity(const char *arg);
115 /* Configuring log facilities. */
116 void vlog_set_pattern(enum vlog_facility, const char *pattern);
117 const char *vlog_get_log_file(void);
118 int vlog_set_log_file(const char *file_name);
119 int vlog_reopen_log_file(void);
121 /* Function for actual logging. */
122 void vlog_init(void);
123 void vlog_exit(void);
124 void vlog(enum vlog_module, enum vlog_level, const char *format, ...)
125 __attribute__((format(printf, 3, 4)));
126 void vlog_valist(enum vlog_module, enum vlog_level, const char *, va_list)
127 __attribute__((format(printf, 3, 0)));
128 void vlog_rate_limit(enum vlog_module, enum vlog_level,
129 struct vlog_rate_limit *, const char *, ...)
130 __attribute__((format(printf, 4, 5)));
132 /* Convenience macros. To use these, define THIS_MODULE as a macro that
133 * expands to the module used by the current source file, e.g.
135 * #define THIS_MODULE VLM_netlink
136 * Guaranteed to preserve errno.
138 #define VLOG_EMER(...) VLOG(VLL_EMER, __VA_ARGS__)
139 #define VLOG_ERR(...) VLOG(VLL_ERR, __VA_ARGS__)
140 #define VLOG_WARN(...) VLOG(VLL_WARN, __VA_ARGS__)
141 #define VLOG_INFO(...) VLOG(VLL_INFO, __VA_ARGS__)
142 #define VLOG_DBG(...) VLOG(VLL_DBG, __VA_ARGS__)
144 /* More convenience macros, for testing whether a given level is enabled in
145 * THIS_MODULE. When constructing a log message is expensive, this enables it
147 #define VLOG_IS_EMER_ENABLED() true
148 #define VLOG_IS_ERR_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_EMER)
149 #define VLOG_IS_WARN_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_WARN)
150 #define VLOG_IS_INFO_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_INFO)
151 #define VLOG_IS_DBG_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_DBG)
153 /* Convenience macros for rate-limiting.
154 * Guaranteed to preserve errno.
156 #define VLOG_ERR_RL(RL, ...) VLOG_RL(RL, VLL_ERR, __VA_ARGS__)
157 #define VLOG_WARN_RL(RL, ...) VLOG_RL(RL, VLL_WARN, __VA_ARGS__)
158 #define VLOG_INFO_RL(RL, ...) VLOG_RL(RL, VLL_INFO, __VA_ARGS__)
159 #define VLOG_DBG_RL(RL, ...) VLOG_RL(RL, VLL_DBG, __VA_ARGS__)
161 #define VLOG_DROP_ERR(RL) vlog_should_drop(THIS_MODULE, VLL_ERR, RL)
162 #define VLOG_DROP_WARN(RL) vlog_should_drop(THIS_MODULE, VLL_WARN, RL)
163 #define VLOG_DROP_INFO(RL) vlog_should_drop(THIS_MODULE, VLL_INFO, RL)
164 #define VLOG_DROP_DBG(RL) vlog_should_drop(THIS_MODULE, VLL_DBG, RL)
166 /* Command line processing. */
167 #define VLOG_OPTION_ENUMS OPT_LOG_FILE
168 #define VLOG_LONG_OPTIONS \
169 {"verbose", optional_argument, 0, 'v'}, \
170 {"log-file", optional_argument, 0, OPT_LOG_FILE}
171 #define VLOG_OPTION_HANDLERS \
173 vlog_set_verbosity(optarg); \
176 vlog_set_log_file(optarg); \
178 void vlog_usage(void);
180 /* Implementation details. */
181 #define VLOG(LEVEL, ...) \
183 if (min_vlog_levels[THIS_MODULE] >= LEVEL) { \
184 vlog(THIS_MODULE, LEVEL, __VA_ARGS__); \
187 #define VLOG_RL(RL, LEVEL, ...) \
189 if (min_vlog_levels[THIS_MODULE] >= LEVEL) { \
190 vlog_rate_limit(THIS_MODULE, LEVEL, RL, __VA_ARGS__); \
193 extern enum vlog_level min_vlog_levels[VLM_N_MODULES];