1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
43 /* Logging importance levels. */
45 VLOG_LEVEL(EMER, LOG_ALERT) \
46 VLOG_LEVEL(ERR, LOG_ERR) \
47 VLOG_LEVEL(WARN, LOG_WARNING) \
48 VLOG_LEVEL(DBG, LOG_DEBUG)
50 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL) VLL_##NAME,
56 const char *vlog_get_level_name(enum vlog_level);
57 enum vlog_level vlog_get_level_val(const char *name);
59 /* Facilities that we can log to. */
60 #define VLOG_FACILITIES \
61 VLOG_FACILITY(SYSLOG, "%05N|%c|%p|%m") \
62 VLOG_FACILITY(CONSOLE, "%d{%b %d %H:%M:%S}|%05N|%c|%p|%m") \
63 VLOG_FACILITY(FILE, "%d{%b %d %H:%M:%S}|%05N|%c|%p|%m")
65 #define VLOG_FACILITY(NAME, PATTERN) VLF_##NAME,
72 const char *vlog_get_facility_name(enum vlog_facility);
73 enum vlog_facility vlog_get_facility_val(const char *name);
75 /* VLM_ constant for each vlog module. */
77 #define VLOG_MODULE(NAME) VLM_##NAME,
78 #include "vlog-modules.def"
83 const char *vlog_get_module_name(enum vlog_module);
84 enum vlog_module vlog_get_module_val(const char *name);
86 /* Rate-limiter for log messages. */
87 struct vlog_rate_limit {
88 /* Configuration settings. */
89 unsigned int rate; /* Tokens per second. */
90 unsigned int burst; /* Max cumulative tokens credit. */
93 unsigned int tokens; /* Current number of tokens. */
94 time_t last_fill; /* Last time tokens added. */
95 time_t first_dropped; /* Time first message was dropped. */
96 unsigned int n_dropped; /* Number of messages dropped. */
99 /* Number of tokens to emit a message. We add 'rate' token per second, which
100 * is 60 times the unit used for 'rate', thus 60 tokens are required to emit
102 #define VLOG_MSG_TOKENS 60
104 /* Initializer for a struct vlog_rate_limit, to set up a maximum rate of RATE
105 * messages per minute and a maximum burst size of BURST messages. */
106 #define VLOG_RATE_LIMIT_INIT(RATE, BURST) \
109 (MIN(BURST, UINT_MAX / VLOG_MSG_TOKENS) \
110 * VLOG_MSG_TOKENS), /* burst */ \
116 /* Configuring how each module logs messages. */
117 enum vlog_level vlog_get_level(enum vlog_module, enum vlog_facility);
118 void vlog_set_levels(enum vlog_module, enum vlog_facility, enum vlog_level);
119 char *vlog_set_levels_from_string(const char *);
120 char *vlog_get_levels(void);
121 bool vlog_is_enabled(enum vlog_module, enum vlog_level);
122 void vlog_set_verbosity(const char *arg);
124 /* Configuring log facilities. */
125 void vlog_set_pattern(enum vlog_facility, const char *pattern);
126 const char *vlog_get_log_file(void);
127 int vlog_set_log_file(const char *file_name);
128 int vlog_reopen_log_file(void);
130 /* Function for actual logging. */
131 void vlog_init(void);
132 void vlog_exit(void);
133 void vlog(enum vlog_module, enum vlog_level, const char *format, ...)
134 __attribute__((format(printf, 3, 4)));
135 void vlog_valist(enum vlog_module, enum vlog_level, const char *, va_list)
136 __attribute__((format(printf, 3, 0)));
137 void vlog_rate_limit(enum vlog_module, enum vlog_level,
138 struct vlog_rate_limit *, const char *, ...)
139 __attribute__((format(printf, 4, 5)));
141 /* Convenience macros. To use these, define THIS_MODULE as a macro that
142 * expands to the module used by the current source file, e.g.
144 * #define THIS_MODULE VLM_netlink
145 * Guaranteed to preserve errno.
147 #define VLOG_EMER(...) VLOG(VLL_EMER, __VA_ARGS__)
148 #define VLOG_ERR(...) VLOG(VLL_ERR, __VA_ARGS__)
149 #define VLOG_WARN(...) VLOG(VLL_WARN, __VA_ARGS__)
150 #define VLOG_DBG(...) VLOG(VLL_DBG, __VA_ARGS__)
152 /* More convenience macros, for testing whether a given level is enabled in
153 * THIS_MODULE. When constructing a log message is expensive, this enables it
155 #define VLOG_IS_EMER_ENABLED() true
156 #define VLOG_IS_ERR_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_EMER)
157 #define VLOG_IS_WARN_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_WARN)
158 #define VLOG_IS_DBG_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_DBG)
160 /* Convenience macros. To use these, define THIS_MODULE as a macro that
161 * expands to the module used by the current source file, e.g.
163 * #define THIS_MODULE VLM_netlink
164 * Guaranteed to preserve errno.
166 #define VLOG_ERR_RL(RL, ...) \
167 vlog_rate_limit(THIS_MODULE, VLL_ERR, RL, __VA_ARGS__)
168 #define VLOG_WARN_RL(RL, ...) \
169 vlog_rate_limit(THIS_MODULE, VLL_WARN, RL, __VA_ARGS__)
170 #define VLOG_DBG_RL(RL, ...) \
171 vlog_rate_limit(THIS_MODULE, VLL_DBG, RL, __VA_ARGS__)
173 /* Command line processing. */
174 #define VLOG_OPTION_ENUMS OPT_LOG_FILE
175 #define VLOG_LONG_OPTIONS \
176 {"verbose", optional_argument, 0, 'v'}, \
177 {"log-file", optional_argument, 0, OPT_LOG_FILE}
178 #define VLOG_OPTION_HANDLERS \
180 vlog_set_verbosity(optarg); \
183 vlog_set_log_file(optarg); \
185 void vlog_usage(void);
187 /* Implementation details. */
188 #define VLOG(LEVEL, ...) \
190 if (min_vlog_levels[THIS_MODULE] >= LEVEL) { \
191 vlog(THIS_MODULE, LEVEL, __VA_ARGS__); \
194 extern enum vlog_level min_vlog_levels[VLM_N_MODULES];