028dda6fc236b10d01913624df76cc3d75820dbe
[collectd.git] / src / utils_complain.h
1 /**
2  * collectd - src/utils_complain.h
3  * Copyright (C) 2006-2013  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 "utils_time.h"
28
29 typedef struct
30 {
31         /* time of the last report */
32         cdtime_t last;
33
34         /* How long to wait until reporting again.
35          * 0 indicates that the complaint is no longer valid. */
36         cdtime_t interval;
37
38         _Bool complained_once;
39 } c_complain_t;
40
41 #define C_COMPLAIN_INIT_STATIC { 0, 0, 0 }
42 #define C_COMPLAIN_INIT(c) do { \
43         (c)->last = 0; \
44         (c)->interval = 0; \
45         (c)->complained_once = 0; \
46 } while (0)
47
48 /*
49  * NAME
50  *   c_complain
51  *
52  * DESCRIPTION
53  *   Complain about something. This function will report a message (usually
54  *   indicating some error condition) using the collectd logging mechanism.
55  *   When this function is called again, reporting the message again will be
56  *   deferred by an increasing interval (up to one day) to prevent flooding
57  *   the logs. A call to `c_release' resets the counter.
58  *
59  * PARAMETERS
60  *   `level'  The log level passed to `plugin_log'.
61  *   `c'      Identifier for the complaint.
62  *   `format' Message format - see the documentation of printf(3).
63  */
64 void c_complain (int level, c_complain_t *c, const char *format, ...);
65
66 /*
67  * NAME
68  *   c_complain_once
69  *
70  * DESCRIPTION
71  *   Complain about something once. This function will not report anything
72  *   again, unless `c_release' has been called in between. If used after some
73  *   calls to `c_complain', it will report again on the next interval and stop
74  *   after that.
75  *
76  *   See `c_complain' for further details and a description of the parameters.
77  */
78 void c_complain_once (int level, c_complain_t *c, const char *format, ...);
79
80 /*
81  * NAME
82  *   c_would_release
83  *
84  * DESCRIPTION
85  *   Returns true if the specified complaint would be released, false else.
86  */
87 #define c_would_release(c) ((c)->interval != 0)
88
89 /*
90  * NAME
91  *   c_release
92  *
93  * DESCRIPTION
94  *   Release a complaint. This will report a message once, marking the
95  *   complaint as released.
96  *
97  *   See `c_complain' for a description of the parameters.
98  */
99 void c_do_release (int level, c_complain_t *c, const char *format, ...);
100 #define c_release(level, c, ...) \
101         do { \
102                 if (c_would_release (c)) \
103                         c_do_release(level, c, __VA_ARGS__); \
104         } while (0)
105
106 #endif /* UTILS_COMPLAIN_H */
107
108 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
109