syslog-ng source
messages.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2012 Balabit
3  * Copyright (c) 1998-2012 Balázs Scheidler
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * As an additional exemption you are allowed to compile & link against the
20  * OpenSSL libraries as published by the OpenSSL project. See the file
21  * COPYING for details.
22  *
23  */
24 
25 #ifndef MESSAGES_H_INCLUDED
26 #define MESSAGES_H_INCLUDED
27 
28 #include "syslog-ng.h"
29 #include <errno.h>
30 #include <evtlog.h>
31 
32 extern int startup_debug_flag;
33 extern int debug_flag;
34 extern int verbose_flag;
35 extern int trace_flag;
36 extern int log_stderr;
37 
38 typedef void (*MsgPostFunc)(LogMessage *msg);
39 
40 void msg_set_context(LogMessage *msg);
41 EVTREC *msg_event_create(gint prio, const char *desc, EVTTAG *tag1, ...) __attribute__((nonnull(2)));
42 EVTREC *msg_event_create_from_desc(gint prio, const char *desc);
43 void msg_event_free(EVTREC *e);
44 void msg_event_send(EVTREC *e);
46 void msg_event_print_event_to_stderr(EVTREC *e);
47 
48 
50 gint msg_map_string_to_log_level(const gchar *log_level);
51 void msg_set_log_level(gint new_log_level);
52 gint msg_get_log_level(void);
53 void msg_apply_cmdline_log_level(gint new_log_level);
54 void msg_apply_config_log_level(gint new_log_level);
55 
56 void msg_init(gboolean interactive);
57 void msg_deinit(void);
58 
59 void msg_add_option_group(GOptionContext *ctx);
60 
61 #define evt_tag_error(tag) evt_tag_errno(tag, __local_copy_of_errno)
62 
63 #define CAPTURE_ERRNO(lambda) do {\
64  int __local_copy_of_errno G_GNUC_UNUSED = errno; \
65  lambda; \
66 } while(0)
67 
68 /* fatal->warning goes out to the console during startup, notice and below
69  * comes goes to the log even during startup */
70 #define msg_fatal(desc, tags...) CAPTURE_ERRNO(\
71  msg_event_suppress_recursions_and_send(msg_event_create(EVT_PRI_CRIT, desc, ##tags, NULL )))
72 #define msg_error(desc, tags...) CAPTURE_ERRNO(\
73  msg_event_suppress_recursions_and_send(msg_event_create(EVT_PRI_ERR, desc, ##tags, NULL )))
74 #define msg_warning(desc, tags...) CAPTURE_ERRNO(\
75  msg_event_suppress_recursions_and_send(msg_event_create(EVT_PRI_WARNING, desc, ##tags, NULL )))
76 #define msg_notice(desc, tags...) CAPTURE_ERRNO(\
77  msg_event_suppress_recursions_and_send(msg_event_create(EVT_PRI_NOTICE, desc, ##tags, NULL )))
78 #define msg_info(desc, tags...) CAPTURE_ERRNO(\
79  msg_event_suppress_recursions_and_send(msg_event_create(EVT_PRI_INFO, desc, ##tags, NULL )))
80 
81 /* just like msg_info, but prepends the message with a timestamp -- useful in interactive
82  * tools with long running time to provide some feedback */
83 #define msg_progress(desc, tags...) \
84  do { \
85  time_t t; \
86  char *timestamp, *newdesc; \
87  \
88  t = time(0); \
89  timestamp = ctime(&t); \
90  timestamp[strlen(timestamp) - 1] = 0; \
91  newdesc = g_strdup_printf("[%s] %s", timestamp, desc); \
92  msg_event_send(msg_event_create(EVT_PRI_INFO, newdesc, ##tags, NULL )); \
93  g_free(newdesc); \
94  } while (0)
95 
96 #define msg_verbose(desc, tags...) \
97  do { \
98  if (G_UNLIKELY(verbose_flag)) \
99  msg_info(desc, ##tags ); \
100  } while (0)
101 
102 #define msg_debug(desc, tags...) \
103  do { \
104  if (G_UNLIKELY(debug_flag)) \
105  CAPTURE_ERRNO(msg_event_suppress_recursions_and_send( \
106  msg_event_create(EVT_PRI_DEBUG, desc, ##tags, NULL ))); \
107  } while (0)
108 
109 #define msg_trace(desc, tags...) \
110  do { \
111  if (G_UNLIKELY(trace_flag)) \
112  CAPTURE_ERRNO(msg_event_suppress_recursions_and_send( \
113  msg_event_create(EVT_PRI_DEBUG, desc, ##tags, NULL ))); \
114  } while (0)
115 
116 #define msg_trace_printf(fmt, values...) \
117  do { \
118  if (G_UNLIKELY(trace_flag)) \
119  msg_send_message_printf(EVT_PRI_DEBUG, fmt, ##values); \
120  } while (0)
121 
122 #define msg_diagnostics(desc, tags...) \
123  do { \
124  if (G_UNLIKELY(trace_flag)) \
125  CAPTURE_ERRNO(msg_event_print_event_to_stderr( \
126  msg_event_create(EVT_PRI_DEBUG, desc, ##tags, NULL ))); \
127  } while (0)
128 
129 #define __once() \
130  ({ \
131  static gboolean __guard = TRUE; \
132  gboolean __current_guard = __guard; \
133  __guard = FALSE; \
134  __current_guard; \
135  })
136 
137 #define msg_warning_once(desc, tags...) \
138  do { \
139  if (__once()) \
140  msg_warning(desc, ##tags ); \
141  } while (0)
142 
143 void msg_post_message(LogMessage *msg);
144 void msg_send_formatted_message(int prio, const char *msg);
145 void msg_send_message_printf(int prio, const gchar *fmt, ...) G_GNUC_PRINTF(2, 3);
146 
147 #endif
void msg_event_send(EVTREC *e)
Definition: messages.c:210
void msg_set_context(LogMessage *msg)
Definition: messages.c:88
int debug_flag
Definition: messages.c:63
EVTREC * msg_event_create(gint prio, const char *desc, EVTTAG *tag1,...) __attribute__((nonnull(2)))
void msg_set_post_func(MsgPostFunc func)
Definition: messages.c:299
void msg_deinit(void)
Definition: messages.c:392
void(* MsgPostFunc)(LogMessage *msg)
Definition: messages.h:38
void msg_send_message_printf(int prio, const gchar *fmt,...) G_GNUC_PRINTF(2
int verbose_flag
Definition: messages.c:64
int trace_flag
Definition: messages.c:65
void msg_event_free(EVTREC *e)
Definition: messages.c:275
void msg_post_message(LogMessage *msg)
Definition: messages.c:305
gint msg_map_string_to_log_level(const gchar *log_level)
Definition: messages.c:314
void msg_event_print_event_to_stderr(EVTREC *e)
Definition: messages.c:223
void msg_add_option_group(GOptionContext *ctx)
Definition: messages.c:453
void msg_set_log_level(gint new_log_level)
Definition: messages.c:328
void msg_apply_config_log_level(gint new_log_level)
Definition: messages.c:362
void msg_apply_cmdline_log_level(gint new_log_level)
Definition: messages.c:355
int startup_debug_flag
Definition: messages.c:62
EVTREC * msg_event_create_from_desc(gint prio, const char *desc)
Definition: messages.c:269
void msg_init(gboolean interactive)
Definition: messages.c:372
void msg_event_suppress_recursions_and_send(EVTREC *e)
Definition: messages.c:216
void msg_send_formatted_message(int prio, const char *msg)
Definition: messages.c:161
gint msg_get_log_level(void)
Definition: messages.c:347
int log_stderr
Definition: messages.c:66
__attribute__((__visibility__("hidden")))
Definition: native-parser.c:32
LogMessage * msg
Definition: test_rename.c:35