Merge branch 'master' into sr/pf
[collectd.git] / src / utils_complain.h
1 /**
2  * collectd - src/utils_complain.h
3  * Copyright (C) 2006-2007  Florian octo Forster
4  * Copyright (C) 2008  Sebastian tokkee Harl
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the License is applicable.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Florian octo Forster <octo at verplant.org>
21  *   Sebastian tokkee Harl <sh at tokkee.org>
22  **/
23
24 #ifndef UTILS_COMPLAIN_H
25 #define UTILS_COMPLAIN_H 1
26
27 #include <time.h>
28
29 typedef struct
30 {
31         /* time of the last report */
32         time_t last;
33
34         /* how long to wait until reporting again
35          *   0 indicates that the complaint is no longer valid
36          * < 0 indicates that the complaint has been reported once
37          *     => c_complain_once will not report again
38          *     => c_complain uses the absolute value to reset the old value */
39         int interval;
40 } c_complain_t;
41
42 #define C_COMPLAIN_INIT_STATIC { 0, 0 }
43 #define C_COMPLAIN_INIT(c) do { (c)->last = 0; (c)->interval = 0; } while (0)
44
45 /*
46  * NAME
47  *   c_complain
48  *
49  * DESCRIPTION
50  *   Complain about something. This function will report a message (usually
51  *   indicating some error condition) using the collectd logging mechanism.
52  *   When this function is called again, reporting the message again will be
53  *   deferred by an increasing interval (up to one day) to prevent flooding
54  *   the logs. A call to `c_release' resets the counter.
55  *
56  * PARAMETERS
57  *   `level'  The log level passed to `plugin_log'.
58  *   `c'      Identifier for the complaint.
59  *   `format' Message format - see the documentation of printf(3).
60  */
61 void c_complain (int level, c_complain_t *c, const char *format, ...);
62
63 /*
64  * NAME
65  *   c_complain_once
66  *
67  * DESCRIPTION
68  *   Complain about something once. This function will not report anything
69  *   again, unless `c_release' has been called in between. If used after some
70  *   calls to `c_complain', it will report again on the next interval and stop
71  *   after that.
72  *
73  *   See `c_complain' for further details and a description of the parameters.
74  */
75 void c_complain_once (int level, c_complain_t *c, const char *format, ...);
76
77 /*
78  * NAME
79  *   c_would_release
80  *
81  * DESCRIPTION
82  *   Returns true if the specified complaint would be released, false else.
83  */
84 #define c_would_release(c) ((c)->interval != 0)
85
86 /*
87  * NAME
88  *   c_release
89  *
90  * DESCRIPTION
91  *   Release a complaint. This will report a message once, marking the
92  *   complaint as released.
93  *
94  *   See `c_complain' for a description of the parameters.
95  */
96 void c_do_release (int level, c_complain_t *c, const char *format, ...);
97 #define c_release(level, c, ...) \
98         do { \
99                 if (c_would_release (c)) \
100                         c_do_release(level, c, __VA_ARGS__); \
101         } while (0)
102
103 #endif /* UTILS_COMPLAIN_H */
104
105 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
106