Permalink
Browse files

Merge branch 'syslog' of https://github.com/Vagabond/APE_Server into …

…Vagabond-syslog
  • Loading branch information...
2 parents 6d9f56e + 70aa680 commit 43d8a4a9bb5af255b86912a80cd0b35a0f23b623 @paraboul paraboul committed Nov 17, 2010
Showing with 91 additions and 19 deletions.
  1. +1 −0 bin/ape.conf
  2. +90 −19 src/log.c
View
@@ -17,6 +17,7 @@ Server {
Log {
debug = 1
use_syslog = 0
+ syslog_facility = local2
logfile = ./ape.log
}
View
109 src/log.c
@@ -42,6 +42,57 @@ 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 = -1;
+ struct _syslog_facilities {
+ int facility;
+ char str[12];
+ } syslog_facilities[] = {
+ {LOG_KERN, "kern"},
+ {LOG_USER, "user"},
+ {LOG_MAIL, "mail"},
+ {LOG_DAEMON, "daemon"},
+ {LOG_AUTH, "auth"},
+ {LOG_SYSLOG, "syslog"},
+ {LOG_LPR, "lpr"},
+ {LOG_NEWS, "news"},
+ {LOG_UUCP, "uucp"},
+ {LOG_CRON, "cron"},
+ /*{LOG_AUTHPRIV, "authpriv"}, private facility */
+ {LOG_FTP, "ftp"}
+ };
+ size_t syslog_max_facilities = sizeof(syslog_facilities) / sizeof(struct _syslog_facilities);
+
+ /* decode the facility requested in the config file */
+ if (!strncasecmp(facility, "local", 5) && strlen(facility) == 6) {
+ int local = 0;
+ char *localnum;
+ // increment the facility pointer by 5 to move past "local"
+ localnum = &facility[5];
+ local = atoi(localnum);
+
+ if (!local && !strncmp("0", localnum, 2)) {
+ facility_num = LOG_LOCAL0;
+ } else if (local < 8 && local > 0) {
+ facility_num = ((local + 16)<<3);
+ }
+ } else {
+ int i;
+ for(i = 0; i < syslog_max_facilities; i++) {
+ if (!strncasecmp(syslog_facilities[i].str, facility, strlen(syslog_facilities[i].str) + 1)) {
+ facility_num = syslog_facilities[i].facility;
+ break;
+ }
+ }
+ }
+
+ if (facility_num == -1) {
+ printf("[WARN] Invalid facility '%s' requested, defaulting to LOCAL2\n", facility);
+ facility_num = LOG_LOCAL2;
+ }
+
+ openlog("APE", LOG_CONS | LOG_PID, facility_num);
}
}
@@ -50,31 +101,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) {
+ 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 43d8a4a

Please sign in to comment.