summaryrefslogtreecommitdiffstats
path: root/server/parser
diff options
context:
space:
mode:
authorDavid Sommerseth <davids@redhat.com>2009-10-22 11:21:34 +0200
committerDavid Sommerseth <davids@redhat.com>2009-10-22 11:21:34 +0200
commit51243cbc51fd027978a7747fdb2bbca561ed74c9 (patch)
treef63097912e62801e89298a308dc3e8f6d25b7022 /server/parser
parenta23c86d89c1001f78c7f8eb88645b91750fb157f (diff)
downloadrteval-51243cbc51fd027978a7747fdb2bbca561ed74c9.tar.gz
rteval-51243cbc51fd027978a7747fdb2bbca561ed74c9.tar.xz
rteval-51243cbc51fd027978a7747fdb2bbca561ed74c9.zip
Added missing log.[ch] files
Diffstat (limited to 'server/parser')
-rw-r--r--server/parser/log.c143
-rw-r--r--server/parser/log.h42
2 files changed, 185 insertions, 0 deletions
diff --git a/server/parser/log.c b/server/parser/log.c
new file mode 100644
index 0000000..992f408
--- /dev/null
+++ b/server/parser/log.c
@@ -0,0 +1,143 @@
+/*
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * This application is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; version 2.
+ *
+ * This application is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+/**
+ * @file log.c
+ * @author David Sommerseth <davids@redhat.com>
+ * @date Wed Oct 21 11:38:51 2009
+ *
+ * @brief Generic log functions
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <assert.h>
+#include <stdarg.h>
+#include <pthread.h>
+#include <syslog.h>
+
+#include <eurephia_nullsafe.h>
+#include <log.h>
+
+LogContext *init_log(const char *fname, unsigned int verblvl) {
+ LogContext *logctx = NULL;
+
+ logctx = (LogContext *) calloc(1, sizeof(LogContext)+2);
+ assert( logctx != NULL);
+
+ logctx->logfp = NULL;
+ logctx->verbosity = verblvl;
+
+ if( fname == NULL ) {
+ logctx->logtype = ltSYSLOG;
+ openlog("rteval_parserd", LOG_PID, LOG_DAEMON);
+ } else {
+ if( strncmp(fname, "syslog:", 7) == 0 ) {
+ const char *fac = fname+7;
+ int facid = LOG_DAEMON;
+
+ if( strcasecmp(fac, "local0") == 0 ) {
+ facid = LOG_LOCAL0;
+ } else if( strcasecmp(fac, "local1") == 0 ) {
+ facid = LOG_LOCAL1;
+ } else if( strcasecmp(fac, "local2") == 0 ) {
+ facid = LOG_LOCAL2;
+ } else if( strcasecmp(fac, "local3") == 0 ) {
+ facid = LOG_LOCAL3;
+ } else if( strcasecmp(fac, "local4") == 0 ) {
+ facid = LOG_LOCAL4;
+ } else if( strcasecmp(fac, "local5") == 0 ) {
+ facid = LOG_LOCAL5;
+ } else if( strcasecmp(fac, "local6") == 0 ) {
+ facid = LOG_LOCAL6;
+ } else if( strcasecmp(fac, "local7") == 0 ) {
+ facid = LOG_LOCAL7;
+ } else if( strcasecmp(fac, "user") == 0 ) {
+ facid = LOG_USER;
+ }
+ logctx->logtype = ltSYSLOG;
+ openlog("rteval_parserd", LOG_PID, facid);
+ } else if( strcmp(fname, "stderr:") == 0 ) {
+ logctx->logtype = ltCONSOLE;
+ logctx->logfp = stderr;
+ } else if( strcmp(fname, "stdout:") == 0 ) {
+ logctx->logtype = ltCONSOLE;
+ logctx->logfp = stdout;
+ } else {
+ logctx->logtype = ltFILE;
+ logctx->logfp = fopen(fname, "a");
+ if( logctx->logfp == NULL ) {
+ fprintf(stderr, "** ERROR ** Failed to open log file %s: %s\n",
+ fname, strerror(errno));
+ free_nullsafe(logctx);
+ return NULL;
+ }
+ }
+ }
+
+ if( logctx->logtype != ltSYSLOG ) {
+ static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
+ logctx->mtx_log = &mtx;
+ }
+ return logctx;
+}
+
+
+void close_log(LogContext *lctx) {
+ if( !lctx ) {
+ return;
+ }
+
+ switch( lctx->logtype ) {
+ case ltFILE:
+ fclose(lctx->logfp);
+ break;
+
+ case ltSYSLOG:
+ closelog();
+ break;
+
+ case ltCONSOLE:
+ break;
+ }
+ free_nullsafe(lctx);
+}
+
+
+void writelog(LogContext *lctx, unsigned int loglvl, const char *fmt, ... ) {
+ if( !lctx || !fmt ) {
+ return;
+ }
+
+ if( lctx->verbosity >= loglvl ) {
+ va_list ap;
+
+ va_start(ap, fmt);
+ switch( lctx->logtype ) {
+ case ltSYSLOG:
+ vsyslog(loglvl, fmt, ap);
+ break;
+
+ case ltCONSOLE:
+ case ltFILE:
+ pthread_mutex_lock(lctx->mtx_log);
+ vfprintf(lctx->logfp, fmt, ap);
+ pthread_mutex_unlock(lctx->mtx_log);
+ break;
+ }
+ va_end(ap);
+ }
+}
diff --git a/server/parser/log.h b/server/parser/log.h
new file mode 100644
index 0000000..29128bf
--- /dev/null
+++ b/server/parser/log.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * This application is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; version 2.
+ *
+ * This application is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+/**
+ * @file log.h
+ * @author David Sommerseth <davids@redhat.com>
+ * @date Wed Oct 21 11:38:51 2009
+ *
+ * @brief Generic log functions
+ *
+ */
+
+#ifndef _RTEVAL_LOG_H
+#define _RTEVAL_LOG_H
+
+#include <syslog.h>
+
+typedef enum { ltSYSLOG, ltFILE, ltCONSOLE } LogType;
+
+typedef struct {
+ LogType logtype;
+ FILE *logfp;
+ unsigned int verbosity;
+ pthread_mutex_t *mtx_log;
+} LogContext;
+
+
+LogContext *init_log(const char *fname, unsigned int verblvl);
+void close_log(LogContext *lctx);
+void writelog(LogContext *lctx, unsigned int loglvl, const char *fmt, ... );
+
+#endif