Re: [PATCH] Logging to syslog
Stipe Tolj <[email protected]>
| Newsgroups | gmane.comp.mobile.kannel.devel |
|---|---|
| Organization | tolj.org system architecture |
| Message-ID | <[email protected]> |
Am 02.12.2010 18:40, schrieb Alejandro Guerrieri:
> Ok. I'll write the userguide documentation and commit afterwards.
thanks Alejandro.
I have made some source code intend cleanup, and removed some unnecessary
Octstr* that have been used. So it's a bit more "compact" now. In addition added
the "without [<pid>]" logging for the syslog target, so we don't duplicates of
PID values in the syslog line.
Can you please use the attached patch as "base" for the commit? ;)
Stipe
--
-------------------------------------------------------------------
Kölner Landstrasse 419
40589 Düsseldorf, NRW, Germany
tolj.org system architecture Kannel Software Foundation (KSF)
http://www.tolj.org/ http://www.kannel.org/
mailto:st_{at}_tolj.org mailto:stolj_{at}_kannel.org
-------------------------------------------------------------------
syslog.diff
(text/plain, 10.8 KB)
Index: gwlib/cfg.def
===================================================================
--- gwlib/cfg.def (revision 4871)
+++ gwlib/cfg.def (working copy)
@@ -94,6 +94,8 @@
OCTSTR(wdp-interface-name)
OCTSTR(log-file)
OCTSTR(log-level)
+ OCTSTR(syslog-level)
+ OCTSTR(syslog-facility)
OCTSTR(access-log)
OCTSTR(access-log-time)
OCTSTR(access-log-format)
@@ -152,6 +154,7 @@
OCTSTR(log-file)
OCTSTR(log-level)
OCTSTR(syslog-level)
+ OCTSTR(syslog-facility)
OCTSTR(smart-errors)
OCTSTR(access-log)
OCTSTR(access-log-time)
@@ -263,6 +266,8 @@
OCTSTR(global-sender)
OCTSTR(log-file)
OCTSTR(log-level)
+ OCTSTR(syslog-level)
+ OCTSTR(syslog-facility)
OCTSTR(access-log)
OCTSTR(access-log-time)
OCTSTR(access-log-clean)
Index: gwlib/log.c
===================================================================
--- gwlib/log.c (revision 4871)
+++ gwlib/log.c (working copy)
@@ -66,12 +66,14 @@
#include <time.h>
#include <stdarg.h>
#include <string.h>
+#include <ctype.h>
#ifdef HAVE_EXECINFO_H
#include <execinfo.h>
#endif
#if HAVE_SYSLOG_H
+#define SYSLOG_NAMES
#include <syslog.h>
#else
@@ -144,9 +146,27 @@
* Syslog support.
*/
static int sysloglevel;
+static int syslogfacility = LOG_DAEMON;
static int dosyslog = 0;
+/*
+ * Decode the syslog name to its int value
+ */
+static int decode(char *name, CODE *facilities)
+{
+ register CODE *c;
+ if (isdigit(*name)) {
+ return (atoi(name));
+ }
+ for (c = facilities; c->c_name; c++) {
+ if (!strcasecmp(name, c->c_name)) {
+ return (c->c_val);
+ }
+ }
+ return LOG_DAEMON;
+}
+
/*
* Make sure stderr is included in the list.
*/
@@ -192,10 +212,10 @@
int i;
for (i = 0; i < num_logfiles; ++i) {
- if (logfiles[i].file == stderr) {
- logfiles[i].minimum_output_level = level;
- break;
- }
+ if (logfiles[i].file == stderr) {
+ logfiles[i].minimum_output_level = level;
+ break;
+ }
}
}
@@ -212,16 +232,21 @@
}
}
+void log_set_syslog_facility(char *facility)
+{
+ if (facility != NULL)
+ syslogfacility = decode(facility, facilitynames);
+}
void log_set_syslog(const char *ident, int syslog_level)
{
if (ident == NULL)
- dosyslog = 0;
+ dosyslog = 0;
else {
- dosyslog = 1;
- sysloglevel = syslog_level;
- openlog(ident, LOG_PID, LOG_DAEMON);
- debug("gwlib.log", 0, "Syslog logging enabled.");
+ dosyslog = 1;
+ sysloglevel = syslog_level;
+ openlog(ident, LOG_PID, syslogfacility);
+ debug("gwlib.log", 0, "Syslog logging enabled.");
}
}
@@ -360,15 +385,15 @@
#define FORMAT_SIZE (1024)
static void format(char *buf, int level, const char *place, int e,
- const char *fmt, int with_timestamp)
+ const char *fmt, int with_timestamp_and_pid)
{
static char *tab[] = {
- "DEBUG: ",
- "INFO: ",
- "WARNING: ",
- "ERROR: ",
- "PANIC: ",
- "LOG: "
+ "DEBUG: ",
+ "INFO: ",
+ "WARNING: ",
+ "ERROR: ",
+ "PANIC: ",
+ "LOG: "
};
static int tab_size = sizeof(tab) / sizeof(tab[0]);
time_t t;
@@ -378,7 +403,7 @@
p = prefix;
- if (with_timestamp) {
+ if (with_timestamp_and_pid) {
time(&t);
#if LOG_TIMESTAMP_LOCALTIME
tm = gw_localtime(t);
@@ -388,33 +413,38 @@
sprintf(p, "%04d-%02d-%02d %02d:%02d:%02d ",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
-
+
p = strchr(p, '\0');
+
+ /* print PID and thread ID */
+ gwthread_self_ids(&tid, &pid);
+ sprintf(p, "[%ld] [%ld] ", pid, tid);
+ } else {
+ /* thread ID only */
+ tid = gwthread_self();
+ sprintf(p, "[%ld] ", tid);
}
- gwthread_self_ids(&tid, &pid);
- sprintf(p, "[%ld] [%ld] ", pid, tid);
-
p = strchr(p, '\0');
if (level < 0 || level >= tab_size)
- sprintf(p, "UNKNOWN: ");
+ sprintf(p, "UNKNOWN: ");
else
- sprintf(p, "%s", tab[level]);
+ sprintf(p, "%s", tab[level]);
p = strchr(p, '\0');
if (place != NULL && *place != '\0')
- sprintf(p, "%s: ", place);
+ sprintf(p, "%s: ", place);
if (strlen(prefix) + strlen(fmt) > FORMAT_SIZE / 2) {
- sprintf(buf, "%s <OUTPUT message too long>\n", prefix);
- return;
+ sprintf(buf, "%s <OUTPUT message too long>\n", prefix);
+ return;
}
if (e == 0)
- sprintf(buf, "%s%s\n", prefix, fmt);
+ sprintf(buf, "%s%s\n", prefix, fmt);
else
- sprintf(buf, "%s%s\n%sSystem error %d: %s\n",
- prefix, fmt, prefix, e, strerror(e));
+ sprintf(buf, "%s%s\n%sSystem error %d: %s\n",
+ prefix, fmt, prefix, e, strerror(e));
}
@@ -431,35 +461,35 @@
int translog;
if (level >= sysloglevel && dosyslog) {
- if (args == NULL) {
- strncpy(buf, format, sizeof(buf));
- buf[sizeof(buf) - 1] = '\0';
- } else {
- vsnprintf(buf, sizeof(buf), format, args);
- /* XXX vsnprint not 100% portable */
- }
+ if (args == NULL) {
+ strncpy(buf, format, sizeof(buf));
+ buf[sizeof(buf) - 1] = '\0';
+ } else {
+ vsnprintf(buf, sizeof(buf), format, args);
+ /* XXX vsnprint not 100% portable */
+ }
- switch(level) {
- case GW_DEBUG:
- translog = LOG_DEBUG;
- break;
- case GW_INFO:
- translog = LOG_INFO;
- break;
- case GW_WARNING:
- translog = LOG_WARNING;
- break;
- case GW_ERROR:
- translog = LOG_ERR;
- break;
- case GW_PANIC:
- translog = LOG_ALERT;
- break;
- default:
- translog = LOG_INFO;
- break;
- }
- syslog(translog, "%s", buf);
+ switch(level) {
+ case GW_DEBUG:
+ translog = LOG_DEBUG;
+ break;
+ case GW_INFO:
+ translog = LOG_INFO;
+ break;
+ case GW_WARNING:
+ translog = LOG_WARNING;
+ break;
+ case GW_ERROR:
+ translog = LOG_ERR;
+ break;
+ case GW_PANIC:
+ translog = LOG_ALERT;
+ break;
+ default:
+ translog = LOG_INFO;
+ break;
+ }
+ syslog(translog, "%s", buf);
}
}
@@ -608,7 +638,8 @@
len = strlen(pat);
if (pat[len-1] == '*')
- return (strncasecmp(place, pat, len - 1) == 0);
+ return (strncasecmp(place, pat, len - 1) == 0);
+
return (strcasecmp(place, pat) == 0);
}
@@ -620,9 +651,9 @@
if (num_places == 0)
return 1;
for (i = 0; i < num_places; ++i) {
- if (*loggable_places[i] != '-' &&
- place_matches(place, loggable_places[i]))
- return 1;
+ if (*loggable_places[i] != '-' &&
+ place_matches(place, loggable_places[i]))
+ return 1;
}
return 0;
}
@@ -635,9 +666,9 @@
if (num_places == 0)
return 0;
for (i = 0; i < num_places; ++i) {
- if (*loggable_places[i] == '-' &&
- place_matches(place, loggable_places[i]+1))
- return 1;
+ if (*loggable_places[i] == '-' &&
+ place_matches(place, loggable_places[i]+1))
+ return 1;
}
return 0;
}
@@ -670,8 +701,8 @@
p = strtok(gw_strdup(places), " ,");
num_places = 0;
while (p != NULL && num_places < MAX_LOGGABLE_PLACES) {
- loggable_places[num_places++] = p;
- p = strtok(NULL, " ,");
+ loggable_places[num_places++] = p;
+ p = strtok(NULL, " ,");
}
}
@@ -685,4 +716,3 @@
thread_id, logfiles[idx].filename, logfiles[idx].minimum_output_level);
thread_to[thread_id] = idx;
}
-
Index: gwlib/log.h
===================================================================
--- gwlib/log.h (revision 4871)
+++ gwlib/log.h (working copy)
@@ -138,6 +138,11 @@
void log_set_log_level(enum output_level level);
/*
+ * Set the syslog facility to use.
+ */
+void log_set_syslog_facility(char *facility);
+
+/*
* Set syslog usage. If `ident' is NULL, syslog is not used.
*/
void log_set_syslog(const char *ident, int syslog_level);
Index: gw/bearerbox.c
===================================================================
--- gw/bearerbox.c (revision 4871)
+++ gw/bearerbox.c (working copy)
@@ -390,6 +390,22 @@
log_open(octstr_get_cstr(log), loglevel, GW_NON_EXCL);
octstr_destroy(log);
}
+ if ((val = cfg_get(grp, octstr_imm("syslog-level"))) != NULL) {
+ long level;
+ Octstr *facility;
+ if ((facility = cfg_get(grp, octstr_imm("syslog-facility"))) != NULL) {
+ log_set_syslog_facility(octstr_get_cstr(facility));
+ octstr_destroy(facility);
+ }
+ if (octstr_compare(val, octstr_imm("none")) == 0) {
+ log_set_syslog(NULL, 0);
+ } else if (octstr_parse_long(&level, val, 0, 10) > 0) {
+ log_set_syslog("bearerbox", level);
+ }
+ octstr_destroy(val);
+ } else {
+ log_set_syslog(NULL, 0);
+ }
if (check_config(cfg) == -1)
panic(0, "Cannot start with corrupted configuration");
Index: gw/smsbox.c
===================================================================
--- gw/smsbox.c (revision 4871)
+++ gw/smsbox.c (working copy)
@@ -3403,6 +3403,22 @@
log_open(octstr_get_cstr(logfile), lvl, GW_NON_EXCL);
octstr_destroy(logfile);
}
+ if ((p = cfg_get(grp, octstr_imm("syslog-level"))) != NULL) {
+ long level;
+ Octstr *facility;
+ if ((facility = cfg_get(grp, octstr_imm("syslog-facility"))) != NULL) {
+ log_set_syslog_facility(octstr_get_cstr(facility));
+ octstr_destroy(facility);
+ }
+ if (octstr_compare(p, octstr_imm("none")) == 0) {
+ log_set_syslog(NULL, 0);
+ } else if (octstr_parse_long(&level, p, 0, 10) > 0) {
+ log_set_syslog("smsbox", level);
+ }
+ octstr_destroy(p);
+ } else {
+ log_set_syslog(NULL, 0);
+ }
if (global_sender != NULL) {
info(0, "Service global sender set as '%s'",
octstr_get_cstr(global_sender));
Index: gw/wapbox.c
===================================================================
--- gw/wapbox.c (revision 4871)
+++ gw/wapbox.c (working copy)
@@ -172,11 +172,15 @@
if ((s = cfg_get(grp, octstr_imm("syslog-level"))) != NULL) {
long level;
-
+ Octstr *facility;
+ if ((facility = cfg_get(grp, octstr_imm("syslog-facility"))) != NULL) {
+ log_set_syslog_facility(octstr_get_cstr(facility));
+ octstr_destroy(facility);
+ }
if (octstr_compare(s, octstr_imm("none")) == 0) {
log_set_syslog(NULL, 0);
debug("wap", 0, "syslog parameter is none");
- } else if (octstr_parse_long(&level, s, 0, 0) == -1) {
+ } else if (octstr_parse_long(&level, s, 0, 10) > 0) {
log_set_syslog("wapbox", level);
debug("wap", 0, "syslog parameter is %ld", level);
}