Permalink
Browse files

Syslog support for APE (based on "Garo"'s patch on mailing list)

This cleans up the original patch somewhat and adds the ability to
specify the syslog facility to use in the config file.

The original patch can be seen at
http://www.mail-archive.com/ape-project@googlegroups.com/msg01163.html
  • Loading branch information...
1 parent 3a6e0be commit 2d6e83c6d6fe0ad948d4e58b05ec089360e22263 @Vagabond committed Oct 14, 2010
Showing with 82 additions and 19 deletions.
  1. +1 −0 bin/ape.conf
  2. +81 −19 src/log.c
View
@@ -17,6 +17,7 @@ Server {
Log {
debug = 1
use_syslog = 0
+ syslog_facility = local2
logfile = ./ape.log
}
View
@@ -42,6 +42,48 @@ void ape_log_init(acetables *g_ape)
if ((g_ape->logs.fd = open(CONFIG_VAL(Log, logfile, g_ape->srv), O_APPEND | O_WRONLY | O_CREAT, 0644)) == -1) {
g_ape->logs.fd = STDERR_FILENO;
}
+ } else {
+ char *facility = CONFIG_VAL(Log, syslog_facility, g_ape->srv);
+ int facility_num = LOG_LOCAL2;
+
+ /* decode the facility requested in the config file */
+ if (!strncasecmp(facility, "local", 5)) {
+ int local = 0;
+ facility += 5;
@paraboul
paraboul Nov 9, 2010

facility = &facility[5]; is more readable

+ local = atoi(facility);
+
+ if (!local && !strncmp("0", facility, 1)) {
+ facility_num = LOG_LOCAL0;
+ } else if (local < 8 && local > 0) {
+ facility_num = ((local + 16)<<3);
@paraboul
paraboul Nov 9, 2010

Not sure if these flags match with other platform than linux. (make it constant in the array below)

@Vagabond
Vagabond Nov 9, 2010 Owner

I checked this with OSX, OpenBSD and Linux, I believe. The syslog facilities are standard.

+ }
+ } else if (!strncasecmp("kern", facility, 4)) {
@paraboul
paraboul Nov 9, 2010

Using an array instead of this "unreadable" bunch of else if.
Maybe something like :

struct _syslog_facilities {
    int facility;
    char str[12];
} syslog_facilities[] = {
    {LOG_KERN, "kern"},
    {LOG_KERN, "authpriv"},
    /* ... */
}; /* Num member = sizeof(syslog_facilities) / sizeof(struct _syslog_facilities) */

At the top of the log.c file and iterate over it.

+ facility_num = LOG_KERN;
+ } else if (!strncasecmp("user", facility, 4)) {
+ facility_num = LOG_USER;
+ } else if (!strncasecmp("mail", facility, 4)) {
+ facility_num = LOG_MAIL;
+ } else if (!strncasecmp("daemon", facility, 6)) {
+ facility_num = LOG_DAEMON;
+ } else if (!strncasecmp("auth", facility, 4)) {
+ facility_num = LOG_AUTH;
@paraboul
paraboul Nov 9, 2010

LOG_AUTH security/authorization messages (DEPRECATED Use LOG_AUTHPRIV instead

+ } else if (!strncasecmp("syslog", facility, 6)) {
+ facility_num = LOG_SYSLOG;
+ } else if (!strncasecmp("lpr", facility, 3)) {
+ facility_num = LOG_LPR;
+ } else if (!strncasecmp("news", facility, 4)) {
+ facility_num = LOG_NEWS;
+ } else if (!strncasecmp("uucp", facility, 4)) {
+ facility_num = LOG_UUCP;
+ } else if (!strncasecmp("cron", facility, 4)) {
+ facility_num = LOG_CRON;
+ } else if (!strncasecmp("authpriv", facility, 8)) {
+ facility_num = LOG_AUTHPRIV;
+ } else if (!strncasecmp("ftp", facility, 3)) {
+ facility_num = LOG_FTP;
+ }
+
+ openlog("APE", LOG_CONS | LOG_PID, facility_num);
}
}
@@ -50,31 +92,51 @@ void ape_log(ape_log_lvl_t lvl, const char *file, unsigned long int line, acetab
if (lvl == APE_DEBUG && !g_ape->logs.lvl&APE_DEBUG) {
return;
} else {
- time_t log_ts;
char *buff;
- char date[32];
-
- int len, datelen;
+ int len;
va_list val;
- log_ts = time(NULL);
va_start(val, buf);
len = vasprintf(&buff, buf, val);
va_end(val);
-
- datelen = strftime(date, 32, "%Y-%m-%d %H:%M:%S - ", localtime(&log_ts));
-
- write(g_ape->logs.fd, date, datelen);
- if (g_ape->logs.lvl&APE_DEBUG) {
- char *debug_file;
- int dlen;
- dlen = asprintf(&debug_file, "%s:%li - ", file, line);
- write(g_ape->logs.fd, debug_file, dlen);
- free(debug_file);
+
+ if (g_ape->logs.use_syslog) {
+ int level = LOG_ERR;
+ switch (lvl) {
@paraboul
paraboul Nov 9, 2010

May be we should directly using LOG_* instead of custom APE_* constants

+ case APE_DEBUG:
+ level = LOG_DEBUG;
+ break;
+ case APE_WARN:
+ level = LOG_WARNING;
+ break;
+ case APE_ERR:
+ level = LOG_ERR;
+ break;
+ case APE_INFO:
+ level = LOG_INFO;
+ break;
+ }
+ syslog(level, "%s:%li - %s", file, line, buff);
+ } else {
+ int datelen;
+ char date[32];
+ time_t log_ts;
+ log_ts = time(NULL);
+
+ datelen = strftime(date, 32, "%Y-%m-%d %H:%M:%S - ", localtime(&log_ts));
+
+ write(g_ape->logs.fd, date, datelen);
+ if (g_ape->logs.lvl&APE_DEBUG) {
+ char *debug_file;
+ int dlen;
+ dlen = asprintf(&debug_file, "%s:%li - ", file, line);
+ write(g_ape->logs.fd, debug_file, dlen);
+ free(debug_file);
+ }
+ write(g_ape->logs.fd, buff, len);
+ write(g_ape->logs.fd, "\n", 1);
+
+ free(buff);
}
- write(g_ape->logs.fd, buff, len);
- write(g_ape->logs.fd, "\n", 1);
-
- free(buff);
}
}

0 comments on commit 2d6e83c

Please sign in to comment.