summaryrefslogtreecommitdiff
path: root/src/gsmd/log.c
diff options
context:
space:
mode:
authorlaforge <laforge@99fdad57-331a-0410-800a-d7fa5415bdb3>2006-10-22 15:16:22 +0000
committerlaforge <laforge@99fdad57-331a-0410-800a-d7fa5415bdb3>2006-10-22 15:16:22 +0000
commit1d0806498974e90467535dee84926d295fe256d2 (patch)
treed761f7cd60f4fd64e373ff6ceb8aea17809eb8cd /src/gsmd/log.c
parent4939b432c1dd37ef5cb6d21d7a8d7f570e6cad72 (diff)
add logging infrastructure to gsm daemon
git-svn-id: http://svn.openmoko.org/trunk/src/target/gsm@98 99fdad57-331a-0410-800a-d7fa5415bdb3
Diffstat (limited to 'src/gsmd/log.c')
-rw-r--r--src/gsmd/log.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/gsmd/log.c b/src/gsmd/log.c
new file mode 100644
index 0000000..671916e
--- /dev/null
+++ b/src/gsmd/log.c
@@ -0,0 +1,83 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <syslog.h>
+#include <time.h>
+
+#include <sys/types.h>
+
+#include "gsmd.h"
+
+static FILE *logfile;
+static FILE syslog_dummy;
+static int loglevel;
+
+static int gsmd2syslog[] = {
+ [GSMD_DEBUG] = LOG_DEBUG,
+ [GSMD_INFO] = LOG_INFO,
+ [GSMD_NOTICE] = LOG_NOTICE,
+ [GSMD_ERROR] = LOG_ERR,
+ [GSMD_FATAL] = LOG_CRIT,
+};
+
+static inline int gsmd2syslog_level(int level)
+{
+ if (level >= ARRAY_SIZE(gsmd2syslog))
+ return LOG_ERR;
+
+ return gsmd2syslog[level];
+}
+
+void __gsmd_log(int level, const char *file, int line, const char *format, ...)
+{
+ char *timestr;
+ va_list ap;
+ time_t tm;
+ FILE *outfd;
+
+ if (level < loglevel)
+ return;
+
+ if (logfile == &syslog_dummy) {
+ va_start(ap, format);
+ vsyslog(gsmd2syslog_level(level), format, ap);
+ va_end(ap);
+ } else {
+ if (logfile)
+ outfd = logfile;
+ else
+ outfd = stderr;
+
+ tm = time(NULL);
+ timestr = ctime(&tm);
+ timestr[strlen(timestr)-1] = '\0';
+ fprintf(outfd, "%s <%1.1d> %s:%d ", timestr, level, file, line);
+
+ va_start(ap, format);
+ vfprintf(outfd, format, ap);
+ va_end(ap);
+
+ fflush(outfd);
+ }
+}
+
+int gsmdlog_init(const char *path)
+{
+
+ if (!strcmp(path, "syslog")) {
+ logfile = &syslog_dummy;
+ openlog("gsmd", 0, LOG_DAEMON);
+ } else {
+ logfile = fopen(path, "a+");
+ }
+
+ if (logfile == NULL)
+ return -1;
+
+ gsmd_log(LOG_INFO, "logfile successfully opened\n");
+
+ return 0;
+}
personal git repositories of Harald Welte. Your mileage may vary