2
* Copyright (c) 2002-2007 BalaBit IT Ltd, Budapest, Hungary
2
* Copyright (c) 2002-2009 BalaBit IT Ltd, Budapest, Hungary
4
4
* This program is free software; you can redistribute it and/or modify it
5
5
* under the terms of the GNU General Public License version 2 as published
20
20
* along with this program; if not, write to the Free Software
21
21
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
24
#include "messages.h"
25
25
#include "logmsg.h"
34
34
#include <evtlog.h>
36
typedef struct _MsgContext
38
guint16 recurse_count;
39
gboolean recurse_warning:1;
37
42
gboolean debug_flag = 0;
38
43
gboolean verbose_flag = 0;
39
static gboolean log_stderr = FALSE, syslog_started = FALSE;
44
gboolean trace_flag = 0;
45
static gboolean log_stderr = FALSE;
46
static gboolean log_syslog = FALSE;
47
static gboolean syslog_started = FALSE;
40
48
static EVTCONTEXT *evt_context;
41
GQueue *internal_msg_queue = NULL;
49
MsgQueue *internal_msg_queue = NULL;
50
static GStaticPrivate msg_context_private = G_STATIC_PRIVATE_INIT;
51
static GStaticMutex evtlog_lock = G_STATIC_MUTEX_INIT;
59
context = g_static_private_get(&msg_context_private);
62
context = g_new0(MsgContext, 1);
63
g_static_private_set(&msg_context_private, context, g_free);
69
msg_set_context(LogMessage *msg)
71
MsgContext *context = msg_get_context();
73
if (msg && (msg->flags & LF_INTERNAL))
75
context->recurse_count = msg->recurse_count + 1;
79
context->recurse_count = 0;
83
#define MAX_RECURSIONS 1
86
msg_limit_internal_message(void)
93
context = msg_get_context();
95
if (context->recurse_count > MAX_RECURSIONS)
97
if (!context->recurse_warning)
100
msg_event_create(EVT_PRI_WARNING, "syslog-ng internal() messages are looping back, preventing loop by suppressing further messages",
101
evt_tag_int("recurse_count", context->recurse_count),
103
context->recurse_warning = TRUE;
44
112
msg_send_internal_message(int prio, const char *msg)
48
if (log_stderr || (!syslog_started && (prio & 0x7) <= EVT_PRI_WARNING))
114
if (G_UNLIKELY(log_stderr || (!syslog_started && (prio & 0x7) <= EVT_PRI_WARNING)))
50
116
fprintf(stderr, "%s\n", msg);
56
122
if (G_LIKELY(internal_msg_queue))
58
buf = g_strdup_printf("<%d> syslog-ng[%d]: %s\n", prio, getpid(), msg);
59
m = log_msg_new(buf, strlen(buf), NULL, LP_INTERNAL | LP_LOCAL, NULL);
60
g_queue_push_tail(internal_msg_queue, m);
124
MsgContext *context = msg_get_context();
126
if (context->recurse_count == 0)
127
context->recurse_warning = FALSE;
128
m = log_msg_new_internal(prio, msg, LP_INTERNAL | LP_LOCAL);
129
m->recurse_count = context->recurse_count;
130
msg_queue_push(internal_msg_queue, m);
84
155
msg_event_send(EVTREC *e)
87
/* this prevents infinite loops, debug messages causing
88
* internal messages causing debug messages again */
89
if (evt_rec_get_syslog_pri(e) != EVT_PRI_DEBUG || log_stderr)
162
syslog(evt_rec_get_syslog_pri(e), "%s", msg);
93
166
msg_send_internal_message(evt_rec_get_syslog_pri(e) | EVT_FAC_SYSLOG, msg);
169
g_static_mutex_lock(&evtlog_lock);
171
g_static_mutex_unlock(&evtlog_lock);
118
193
syslog_started = TRUE;
122
msg_init(int use_stderr)
197
msg_redirect_to_syslog(const gchar *program_name)
124
internal_msg_queue = g_queue_new();
200
openlog(program_name, LOG_NDELAY | LOG_PID, LOG_SYSLOG);
126
log_stderr = use_stderr;
204
msg_init(gboolean interactive)
208
internal_msg_queue = msg_queue_new();
209
g_log_set_handler(G_LOG_DOMAIN, 0xff, msg_log_func, NULL);
210
g_log_set_handler("GLib", 0xff, msg_log_func, NULL);
127
216
evt_context = evt_ctx_init("syslog-ng", EVT_FAC_SYSLOG);
129
g_log_set_handler(G_LOG_DOMAIN, 0xff, msg_log_func, NULL);
130
g_log_set_handler("GLib", 0xff, msg_log_func, NULL);
138
223
evt_ctx_free(evt_context);
139
g_queue_free(internal_msg_queue);
140
internal_msg_queue = NULL;
224
if (internal_msg_queue)
226
msg_queue_free(internal_msg_queue);
227
internal_msg_queue = NULL;
231
static GOptionEntry msg_option_entries[] =
233
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose_flag, "Be a bit more verbose", NULL },
234
{ "debug", 'd', 0, G_OPTION_ARG_NONE, &debug_flag, "Enable debug messages", NULL},
235
{ "trace", 't', 0, G_OPTION_ARG_NONE, &trace_flag, "Enable trace messages", NULL },
236
{ "stderr", 'e', 0, G_OPTION_ARG_NONE, &log_stderr, "Log messages to stderr", NULL},
241
msg_add_option_group(GOptionContext *ctx)
245
group = g_option_group_new("log", "Log options", "Log options", NULL, NULL);
246
g_option_group_add_entries(group, msg_option_entries);
247
g_option_context_add_group(ctx, group);