Set DHCP maximum retransmission timeout to 3 seconds in secchan.
[sliver-openvswitch.git] / include / vlog.h
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
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:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
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
27  * SOFTWARE.
28  * 
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.
32  */
33
34 #ifndef VLOG_H
35 #define VLOG_H 1
36
37 #include <limits.h>
38 #include <stdarg.h>
39 #include <stdbool.h>
40 #include <time.h>
41 #include "util.h"
42
43 /* Logging importance levels. */
44 #define VLOG_LEVELS                             \
45     VLOG_LEVEL(EMER)                            \
46     VLOG_LEVEL(ERR)                             \
47     VLOG_LEVEL(WARN)                            \
48     VLOG_LEVEL(DBG)
49 enum vlog_level {
50 #define VLOG_LEVEL(NAME) VLL_##NAME,
51     VLOG_LEVELS
52 #undef VLOG_LEVEL
53     VLL_N_LEVELS
54 };
55
56 const char *vlog_get_level_name(enum vlog_level);
57 enum vlog_level vlog_get_level_val(const char *name);
58
59 /* Facilities that we can log to. */
60 #define VLOG_FACILITIES                         \
61     VLOG_FACILITY(SYSLOG)                       \
62     VLOG_FACILITY(CONSOLE)
63 enum vlog_facility {
64 #define VLOG_FACILITY(NAME) VLF_##NAME,
65     VLOG_FACILITIES
66 #undef VLOG_FACILITY
67     VLF_N_FACILITIES,
68     VLF_ANY_FACILITY = -1
69 };
70
71 const char *vlog_get_facility_name(enum vlog_facility);
72 enum vlog_facility vlog_get_facility_val(const char *name);
73
74 /* VLM_ constant for each vlog module. */
75 enum vlog_module {
76 #define VLOG_MODULE(NAME) VLM_##NAME,
77 #include "vlog-modules.def"
78     VLM_N_MODULES,
79     VLM_ANY_MODULE = -1
80 };
81
82 const char *vlog_get_module_name(enum vlog_module);
83 enum vlog_module vlog_get_module_val(const char *name);
84
85 /* Rate-limiter for log messages. */
86 struct vlog_rate_limit {
87     /* Configuration settings. */
88     unsigned int rate;          /* Tokens per second. */
89     unsigned int burst;         /* Max cumulative tokens credit. */
90
91     /* Current status. */
92     unsigned int tokens;        /* Current number of tokens. */
93     time_t last_fill;           /* Last time tokens added. */
94     time_t first_dropped;       /* Time first message was dropped. */
95     unsigned int n_dropped;     /* Number of messages dropped. */
96 };
97
98 /* Number of tokens to emit a message.  We add 'rate' token per second, which
99  * is 60 times the unit used for 'rate', thus 60 tokens are required to emit
100  * one message. */
101 #define VLOG_MSG_TOKENS 60
102
103 /* Initializer for a struct vlog_rate_limit, to set up a maximum rate of RATE
104  * messages per minute and a maximum burst size of BURST messages. */
105 #define VLOG_RATE_LIMIT_INIT(RATE, BURST)                   \
106         {                                                   \
107             RATE,                           /* rate */      \
108             (MIN(BURST, UINT_MAX / VLOG_MSG_TOKENS)         \
109              * VLOG_MSG_TOKENS),            /* burst */     \
110             0,                              /* tokens */    \
111             0,                              /* last_fill */ \
112             0,                              /* n_dropped */ \
113         }
114
115 /* Configuring how each module logs messages. */
116 enum vlog_level vlog_get_level(enum vlog_module, enum vlog_facility);
117 void vlog_set_levels(enum vlog_module, enum vlog_facility, enum vlog_level);
118 char *vlog_set_levels_from_string(const char *);
119 char *vlog_get_levels(void);
120 bool vlog_is_enabled(enum vlog_module, enum vlog_level);
121 void vlog_set_verbosity(const char *arg);
122
123 /* Function for actual logging. */
124 void vlog_init(void);
125 void vlog_exit(void);
126 void vlog(enum vlog_module, enum vlog_level, const char *format, ...)
127     __attribute__((format(printf, 3, 4)));
128 void vlog_valist(enum vlog_module, enum vlog_level, const char *, va_list)
129     __attribute__((format(printf, 3, 0)));
130 void vlog_rate_limit(enum vlog_module, enum vlog_level,
131                      struct vlog_rate_limit *, const char *, ...)
132     __attribute__((format(printf, 4, 5)));
133
134 /* Convenience macros.  To use these, define THIS_MODULE as a macro that
135  * expands to the module used by the current source file, e.g.
136  *      #include "vlog.h"
137  *      #define THIS_MODULE VLM_netlink
138  * Guaranteed to preserve errno.
139  */
140 #define VLOG_EMER(...) vlog(THIS_MODULE, VLL_EMER, __VA_ARGS__)
141 #define VLOG_ERR(...) vlog(THIS_MODULE, VLL_ERR, __VA_ARGS__)
142 #define VLOG_WARN(...) vlog(THIS_MODULE, VLL_WARN, __VA_ARGS__)
143 #define VLOG_DBG(...) vlog(THIS_MODULE, VLL_DBG, __VA_ARGS__)
144
145 /* More convenience macros, for testing whether a given level is enabled in
146  * THIS_MODULE.  When constructing a log message is expensive, this enables it
147  * to be skipped. */
148 #define VLOG_IS_EMER_ENABLED() true
149 #define VLOG_IS_ERR_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_EMER)
150 #define VLOG_IS_WARN_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_WARN)
151 #define VLOG_IS_DBG_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_DBG)
152
153 /* Convenience macros.  To use these, define THIS_MODULE as a macro that
154  * expands to the module used by the current source file, e.g.
155  *      #include "vlog.h"
156  *      #define THIS_MODULE VLM_netlink
157  * Guaranteed to preserve errno.
158  */
159 #define VLOG_ERR_RL(RL, ...) \
160         vlog_rate_limit(THIS_MODULE, VLL_ERR, RL, __VA_ARGS__)
161 #define VLOG_WARN_RL(RL, ...) \
162         vlog_rate_limit(THIS_MODULE, VLL_WARN, RL, __VA_ARGS__)
163 #define VLOG_DBG_RL(RL, ...) \
164         vlog_rate_limit(THIS_MODULE, VLL_DBG, RL, __VA_ARGS__)
165
166 #endif /* vlog.h */