]> git.sommitrealweird.co.uk Git - onak.git/commitdiff
cscvs to tla changeset 58
authorJonathan McDowell <noodles@earth.li>
Mon, 31 May 2004 23:47:28 +0000 (23:47 +0000)
committerJonathan McDowell <noodles@earth.li>
Mon, 31 May 2004 23:47:28 +0000 (23:47 +0000)
Author: noodles
Date: 2003/02/13 19:34:14
Add a logging structure so we can easily log to a file instead of stderr.

17 files changed:
Makefile
add.c
gpgwww.c
keydb_db2.c
keydb_db3.c
keydb_pg.c
keyid.c
keyindex.c
log.c [new file with mode: 0644]
log.h [new file with mode: 0644]
lookup.c
merge.c
merge.h
onak-conf.c
onak-conf.h
onak.c
parsekey.c

index 018eb04972052bfb1d400d7c9ffa9c74f6374356..fb2eab480a11fd91059162b9d9f5fa48199174da 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -12,11 +12,11 @@ LIBS = -L/usr/local/lib -ldb3
 
 PROGS = add lookup gpgwww onak
 CORE_OBJS = armor.o charfuncs.o decodekey.o getcgi.o hash.o keydb_$(DBTYPE).o \
 
 PROGS = add lookup gpgwww onak
 CORE_OBJS = armor.o charfuncs.o decodekey.o getcgi.o hash.o keydb_$(DBTYPE).o \
-       keyid.o keyindex.o ll.o mem.o onak-conf.o parsekey.o sha.o md5.o
+       keyid.o keyindex.o ll.o mem.o onak-conf.o parsekey.o sha.o md5.o log.o
 OBJS = merge.o stats.o sendsync.o $(CORE_OBJS)
 SRCS = armor.c parsekey.c merge.c keyid.c md5.c sha.c main.c getcgi.c stats.c \
        keyindex.c mem.c lookup.c add.c keydb_$(DBTYPE).c ll.c hash.c \
 OBJS = merge.o stats.o sendsync.o $(CORE_OBJS)
 SRCS = armor.c parsekey.c merge.c keyid.c md5.c sha.c main.c getcgi.c stats.c \
        keyindex.c mem.c lookup.c add.c keydb_$(DBTYPE).c ll.c hash.c \
-       gpgwww.c onak-conf.c charfuncs.c sendsync.c
+       gpgwww.c onak-conf.c charfuncs.c sendsync.c log.c
 
 all: $(PROGS) testparse maxpath sixdegrees
 
 
 all: $(PROGS) testparse maxpath sixdegrees
 
diff --git a/add.c b/add.c
index b0a7fd227c90f1853612aa08cb1b0d4a3c8d0fb5..2a99036adbe2fe017359f00962a027b8a9127fce 100644 (file)
--- a/add.c
+++ b/add.c
@@ -16,6 +16,7 @@
 #include "getcgi.h"
 #include "keydb.h"
 #include "keystructs.h"
 #include "getcgi.h"
 #include "keydb.h"
 #include "keystructs.h"
+#include "log.h"
 #include "mem.h"
 #include "merge.h"
 #include "onak-conf.h"
 #include "mem.h"
 #include "merge.h"
 #include "onak-conf.h"
@@ -53,25 +54,27 @@ int main(int argc, char *argv[])
        if (ctx.buffer == NULL) {
                puts("Error: No keytext to add supplied.");
        } else {
        if (ctx.buffer == NULL) {
                puts("Error: No keytext to add supplied.");
        } else {
+               readconfig();
+               initlogthing("add", config.logfile);
                dearmor_openpgp_stream(buffer_fetchchar,
                                        &ctx,
                                        &packets);
                if (packets != NULL) {
                        parse_keys(packets, &keys);
                dearmor_openpgp_stream(buffer_fetchchar,
                                        &ctx,
                                        &packets);
                if (packets != NULL) {
                        parse_keys(packets, &keys);
-                       readconfig();
                        initdb();
                        printf("Got %d new keys.\n",
                        initdb();
                        printf("Got %d new keys.\n",
-                                       update_keys(&keys, false));
+                                       update_keys(&keys));
                        if (keys != NULL) {
                                sendkeysync(keys);
                                free_publickey(keys);
                                keys = NULL;
                        }
                        cleanupdb();
                        if (keys != NULL) {
                                sendkeysync(keys);
                                free_publickey(keys);
                                keys = NULL;
                        }
                        cleanupdb();
-                       cleanupconfig();
                } else {
                        puts("No OpenPGP packets found in input.");
                }
                } else {
                        puts("No OpenPGP packets found in input.");
                }
+               cleanuplogthing();
+               cleanupconfig();
        }
        end_html();
        return (EXIT_SUCCESS);
        }
        end_html();
        return (EXIT_SUCCESS);
index c225b827c90e095176e4d473b0a1fec9d82d0cd7..f5b16448de0f4591318631bda69dd9c093b19976 100644 (file)
--- a/gpgwww.c
+++ b/gpgwww.c
@@ -14,6 +14,7 @@
 #include "getcgi.h"
 #include "hash.h"
 #include "keydb.h"
 #include "getcgi.h"
 #include "hash.h"
 #include "keydb.h"
+#include "log.h"
 #include "onak-conf.h"
 #include "stats.h"
 
 #include "onak-conf.h"
 #include "stats.h"
 
@@ -55,11 +56,13 @@ int main(int argc, char *argv[])
 
        printf("<P>Looking for path from 0x%llX to 0x%llX</P>\n", from, to);
        readconfig();
 
        printf("<P>Looking for path from 0x%llX to 0x%llX</P>\n", from, to);
        readconfig();
+       initlogthing("gpgwww", config.logfile);
        initdb();
        inithash();
        dofindpath(from, to, true);
        destroyhash();
        cleanupdb();
        initdb();
        inithash();
        dofindpath(from, to, true);
        destroyhash();
        cleanupdb();
+       cleanuplogthing();
        cleanupconfig();
 
        puts("<HR>");
        cleanupconfig();
 
        puts("<HR>");
index 7490ff30b27ccad71edf7e9e9c002be7ffce528f..0c597c4b1b1e00584db0c6ebdf7ee50f370ac178 100644 (file)
@@ -21,6 +21,7 @@
 #include "keyid.h"
 #include "keyindex.h"
 #include "keystructs.h"
 #include "keyid.h"
 #include "keyindex.h"
 #include "keystructs.h"
+#include "log.h"
 #include "mem.h"
 #include "onak-conf.h"
 #include "parsekey.h"
 #include "mem.h"
 #include "onak-conf.h"
 #include "parsekey.h"
@@ -91,13 +92,15 @@ void initdb(void)
                                        &db2_env, &keydbinfo,
                                        &db2_keydbfiles[i]);
                        if (ret) {
                                        &db2_env, &keydbinfo,
                                        &db2_keydbfiles[i]);
                        if (ret) {
-                               fprintf(stderr, "Error opening db file %d (errno %d)\n",
+                               logthing(LOGTHING_CRITICAL,
+                                       "Error opening db file %d (errno %d)",
                                        i, ret);
                                exit(1);
                        }
                }
        } else {
                                        i, ret);
                                exit(1);
                        }
                }
        } else {
-               fprintf(stderr, "Error initializing db (%d).\n", ret);
+               logthing(LOGTHING_CRITICAL, "Error initializing db (%d).",
+                               ret);
                exit(1);
        }
 }
                exit(1);
        }
 }
index de24f750d2656ad04c6c53fc7367fdbbbb601eb2..2cb61aacaccacc4b353df1a25ea314ca3100c34d 100644 (file)
@@ -25,6 +25,7 @@
 #include "decodekey.h"
 #include "keystructs.h"
 #include "mem.h"
 #include "decodekey.h"
 #include "keystructs.h"
 #include "mem.h"
+#include "log.h"
 #include "onak-conf.h"
 #include "parsekey.h"
 
 #include "onak-conf.h"
 #include "parsekey.h"
 
@@ -111,7 +112,8 @@ void initdb(void)
 
        ret = db_env_create(&dbenv, 0);
        if (ret != 0) {
 
        ret = db_env_create(&dbenv, 0);
        if (ret != 0) {
-               fprintf(stderr, "db_env_create: %s\n", db_strerror(ret));
+               logthing(LOGTHING_CRITICAL,
+                       "db_env_create: %s", db_strerror(ret));
                exit(1);
        }
 
                exit(1);
        }
 
@@ -138,7 +140,8 @@ void initdb(void)
 
        ret = db_create(&dbconn, dbenv, 0);
        if (ret != 0) {
 
        ret = db_create(&dbconn, dbenv, 0);
        if (ret != 0) {
-               fprintf(stderr, "db_create: %s\n", db_strerror(ret));
+               logthing(LOGTHING_CRITICAL,
+                               "db_create: %s", db_strerror(ret));
                exit(1);
        }
 
                exit(1);
        }
 
@@ -154,7 +157,7 @@ void initdb(void)
 
        ret = db_create(&worddb, dbenv, 0);
        if (ret != 0) {
 
        ret = db_create(&worddb, dbenv, 0);
        if (ret != 0) {
-               fprintf(stderr, "db_create: %s\n", db_strerror(ret));
+               logthing(LOGTHING_CRITICAL, "db_create: %s", db_strerror(ret));
                exit(1);
        }
        ret = worddb->set_flags(worddb, DB_DUP);
                exit(1);
        }
        ret = worddb->set_flags(worddb, DB_DUP);
index adfbc69619a05bea2ed5e81d9c36989d3a628ceb..3b127910467ff9e4e1f7dcf417f67983dd6bd2e9 100644 (file)
@@ -25,6 +25,7 @@
 #include "keyid.h"
 #include "decodekey.h"
 #include "keystructs.h"
 #include "keyid.h"
 #include "decodekey.h"
 #include "keystructs.h"
+#include "log.h"
 #include "mem.h"
 #include "onak-conf.h"
 #include "parsekey.h"
 #include "mem.h"
 #include "onak-conf.h"
 #include "parsekey.h"
@@ -68,8 +69,8 @@ void initdb(void)
                        config.pg_dbpass); // password
 
        if (PQstatus(dbconn) == CONNECTION_BAD) {
                        config.pg_dbpass); // password
 
        if (PQstatus(dbconn) == CONNECTION_BAD) {
-               fprintf(stderr, "Connection to database failed.\n");
-               fprintf(stderr, "%s\n", PQerrorMessage(dbconn));
+               logthing(LOGTHING_CRITICAL, "Connection to database failed.");
+               logthing(LOGTHING_CRITICAL, "%s", PQerrorMessage(dbconn));
                PQfinish(dbconn);
                dbconn = NULL;
                exit(1);
                PQfinish(dbconn);
                dbconn = NULL;
                exit(1);
@@ -169,7 +170,8 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
 
                        fd = lo_open(dbconn, key_oid, INV_READ);
                        if (fd < 0) {
 
                        fd = lo_open(dbconn, key_oid, INV_READ);
                        if (fd < 0) {
-                               fprintf(stderr, "Can't open large object.\n");
+                               logthing(LOGTHING_ERROR,
+                                               "Can't open large object.");
                        } else {
                                read_openpgp_stream(keydb_fetchchar, &fd,
                                                &packets);
                        } else {
                                read_openpgp_stream(keydb_fetchchar, &fd,
                                                &packets);
@@ -180,7 +182,7 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
                        }
                }
        } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
                        }
                }
        } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
-               fprintf(stderr, "Problem retrieving key from DB.\n");
+               logthing(LOGTHING_ERROR, "Problem retrieving key from DB.");
        }
 
        PQclear(result);
        }
 
        PQclear(result);
@@ -235,7 +237,8 @@ int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
 
                        fd = lo_open(dbconn, key_oid, INV_READ);
                        if (fd < 0) {
 
                        fd = lo_open(dbconn, key_oid, INV_READ);
                        if (fd < 0) {
-                               fprintf(stderr, "Can't open large object.\n");
+                               logthing(LOGTHING_ERROR,
+                                               "Can't open large object.");
                        } else {
                                read_openpgp_stream(keydb_fetchchar, &fd,
                                                &packets);
                        } else {
                                read_openpgp_stream(keydb_fetchchar, &fd,
                                                &packets);
@@ -246,7 +249,7 @@ int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
                        }
                }
        } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
                        }
                }
        } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
-               fprintf(stderr, "Problem retrieving key from DB.\n");
+               logthing(LOGTHING_ERROR, "Problem retrieving key from DB.");
        }
 
        PQclear(result);
        }
 
        PQclear(result);
@@ -307,7 +310,7 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
                
        key_oid = lo_creat(dbconn, INV_READ | INV_WRITE);
        if (key_oid == 0) {
                
        key_oid = lo_creat(dbconn, INV_READ | INV_WRITE);
        if (key_oid == 0) {
-               fprintf(stderr, "Can't create key OID\n");
+               logthing(LOGTHING_ERROR, "Can't create key OID");
        } else {
                fd = lo_open(dbconn, key_oid, INV_WRITE);
                write_openpgp_stream(keydb_putchar, &fd, packets);
        } else {
                fd = lo_open(dbconn, key_oid, INV_WRITE);
                write_openpgp_stream(keydb_putchar, &fd, packets);
@@ -324,8 +327,8 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
        result = PQexec(dbconn, statement);
 
        if (PQresultStatus(result) != PGRES_COMMAND_OK) {
        result = PQexec(dbconn, statement);
 
        if (PQresultStatus(result) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "Problem storing key in DB.\n");
-               fprintf(stderr, "%s\n", PQresultErrorMessage(result));
+               logthing(LOGTHING_ERROR, "Problem storing key in DB.");
+               logthing(LOGTHING_ERROR, "%s", PQresultErrorMessage(result));
        }
        PQclear(result);
 
        }
        PQclear(result);
 
@@ -356,8 +359,9 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
                        }
 
                        if (PQresultStatus(result) != PGRES_COMMAND_OK) {
                        }
 
                        if (PQresultStatus(result) != PGRES_COMMAND_OK) {
-                               fprintf(stderr, "Problem storing key in DB.\n");
-                               fprintf(stderr, "%s\n",
+                               logthing(LOGTHING_ERROR,
+                                               "Problem storing key in DB.");
+                               logthing(LOGTHING_ERROR, "%s",
                                                PQresultErrorMessage(result));
                        }
                        /*
                                                PQresultErrorMessage(result));
                        }
                        /*
@@ -445,7 +449,8 @@ int delete_key(uint64_t keyid, bool intrans)
                        keyid);
                result = PQexec(dbconn, statement);
        } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
                        keyid);
                result = PQexec(dbconn, statement);
        } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
-               fprintf(stderr, "Problem retrieving key (%llX) from DB.\n",
+               logthing(LOGTHING_ERROR,
+                               "Problem retrieving key (%llX) from DB.",
                                keyid);
        }
 
                                keyid);
        }
 
@@ -484,7 +489,8 @@ char *keyid2uid(uint64_t keyid)
                        PQntuples(result) >= 1) {
                uid = strdup(PQgetvalue(result, 0, 0));
        } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
                        PQntuples(result) >= 1) {
                uid = strdup(PQgetvalue(result, 0, 0));
        } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
-               fprintf(stderr, "Problem retrieving key (%llX) from DB.\n",
+               logthing(LOGTHING_ERROR,
+                               "Problem retrieving key (%llX) from DB.",
                                keyid);
        }
 
                                keyid);
        }
 
@@ -539,7 +545,7 @@ struct ll *getkeysigs(uint64_t keyid)
                        sigs = lladd(sigs, createandaddtohash(signer));
                }
        } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
                        sigs = lladd(sigs, createandaddtohash(signer));
                }
        } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
-               fprintf(stderr, "Problem retrieving key from DB.\n");
+               logthing(LOGTHING_ERROR, "Problem retrieving key from DB.");
        }
 
        PQclear(result);
        }
 
        PQclear(result);
diff --git a/keyid.c b/keyid.c
index ced0c458997a79818518133067b645b8c851faec..0a61c1f91c926b8f838b42aba0e510b5b2928321 100644 (file)
--- a/keyid.c
+++ b/keyid.c
@@ -10,6 +10,7 @@
 
 #include "keyid.h"
 #include "keystructs.h"
 
 #include "keyid.h"
 #include "keystructs.h"
+#include "log.h"
 #include "md5.h"
 #include "sha.h"
 
 #include "md5.h"
 #include "sha.h"
 
@@ -93,7 +94,7 @@ unsigned char *get_fingerprint(struct openpgp_packet *packet,
                memcpy(fingerprint, buff, *len);
                break;
        default:
                memcpy(fingerprint, buff, *len);
                break;
        default:
-               fprintf(stderr, "Unknown key type: %d\n",
+               logthing(LOGTHING_ERROR, "Unknown key type: %d",
                                packet->data[0]);
        }
 
                                packet->data[0]);
        }
 
@@ -135,7 +136,8 @@ uint64_t get_packetid(struct openpgp_packet *packet)
                                keyid += packet->data[offset++];
                        }
                } else {
                                keyid += packet->data[offset++];
                        }
                } else {
-                       fputs("Type 2 or 3 key, but not RSA.\n", stderr);
+                       logthing(LOGTHING_ERROR,
+                                       "Type 2 or 3 key, but not RSA.");
                }
                break;
        case 4:
                }
                break;
        case 4:
@@ -148,7 +150,7 @@ uint64_t get_packetid(struct openpgp_packet *packet)
 
                break;
        default:
 
                break;
        default:
-               fprintf(stderr, "Unknown key type: %d\n",
+               logthing(LOGTHING_ERROR, "Unknown key type: %d",
                                packet->data[0]);
        }
 
                                packet->data[0]);
        }
 
index 2e14e778f2c83a80b1d1f1d9fe90b2ab1377fd9c..8277866d5ab31d4d2b038005303d00de5ef92689 100644 (file)
@@ -21,6 +21,7 @@
 #include "keyid.h"
 #include "keyindex.h"
 #include "keystructs.h"
 #include "keyid.h"
 #include "keyindex.h"
 #include "keystructs.h"
+#include "log.h"
 
 int list_sigs(struct openpgp_packet_list *sigs, bool html)
 {
 
 int list_sigs(struct openpgp_packet_list *sigs, bool html)
 {
@@ -114,7 +115,8 @@ int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose,
                                        subkeys->packet->data[7];
                                break;
                        default:
                                        subkeys->packet->data[7];
                                break;
                        default:
-                               fprintf(stderr, "Unknown key type: %d\n",
+                               logthing(LOGTHING_ERROR,
+                                       "Unknown key type: %d",
                                        subkeys->packet->data[0]);
                        }
                
                                        subkeys->packet->data[0]);
                        }
                
@@ -205,7 +207,7 @@ int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint,
                                        keys->publickey->data[7];
                        break;
                default:
                                        keys->publickey->data[7];
                        break;
                default:
-                       fprintf(stderr, "Unknown key type: %d\n",
+                       logthing(LOGTHING_ERROR, "Unknown key type: %d",
                                keys->publickey->data[0]);
                }
                
                                keys->publickey->data[0]);
                }
                
diff --git a/log.c b/log.c
new file mode 100644 (file)
index 0000000..5d067bc
--- /dev/null
+++ b/log.c
@@ -0,0 +1,155 @@
+/*
+ * log.c - Simple logging framework.
+ *
+ * Jonathan McDowell <noodles@earth.li>
+ *
+ * Copyright 2003 Project Purple
+ */
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "log.h"
+
+/*
+ *     logthres - holds the minimum log level we'll output
+ *
+ *     This variable keeps track of the threshold we've set for outputting
+ *     logs - if we're asked to log something below this level we won't output
+ *     it.
+ */
+static loglevels logthres = LOGTHING_DEBUG;
+
+/*
+ *     logappname - the name of the application using us.
+ *
+ *     This holds information about the name of the application we're being
+ *     called by. It's set when we're initialized.
+ */
+static char *logappname = NULL;
+
+/*
+ *     logfilename - the file to log to.
+ *
+ *     The full name and path of the file we should log to.
+ */
+static char *logfilename = NULL;
+
+/*
+ *     initlogthing - initialize the logging module
+ *     @appname: The application name to use in the log.
+ *     @filename: The filename to log to. NULL means stderr.
+ *
+ *      This function sets up the logging module ready to log. The appname is
+ *      written as part of every log entry and the filename is the file we
+ *      should log to. If the appname is NULL then none is written. If the
+ *      filename is NULL all output is sent to stderr.
+ */
+int initlogthing(const char *appname, const char *filename)
+{
+       if (appname != NULL) {
+               logappname = strdup(appname);
+       }
+
+       if (filename != NULL) {
+               logfilename = strdup(filename);
+       }
+
+       return 0;
+}
+
+/*
+ *     cleanuplogthing - clean up the logging module
+ *
+ *      This function cleans up the logging module after use.
+ */
+void cleanuplogthing(void)
+{
+       if (logappname != NULL) {
+               free(logappname);
+               logappname = NULL;
+       }
+
+       if (logfilename != NULL) {
+               free(logfilename);
+               logfilename = NULL;
+       }
+
+       return;
+}
+
+/*
+ *     setlogthreshold - set the threshold for log output
+ *     @loglevel: The minimum log level we should output
+ *
+ *     Sets the threshold for log output; anything logged with a log level
+ *     lower than this will be silently dropped. Returns the old log threshold
+ *     value.
+ */
+loglevels setlogthreshold(loglevels loglevel)
+{
+       loglevels oldlevel;
+
+       oldlevel = logthres;
+       logthres = loglevel;
+
+       return oldlevel;
+}
+
+/*
+ *     logthing - output a log entry
+ *      @loglevel: The level of the log.
+ *      @format: A format string, followed by any parameters required.
+ *
+ *     This function outputs a log entry. A leading time/date stamp and a
+ *     trailing newline are automatically added. The loglevel is compared to
+ *     the current log threshold and if equal or above the log entry is
+ *     output. The format parameter is of the same nature as that used in
+ *     printf.
+ */
+int logthing(loglevels loglevel, const char *format, ...)
+{
+       FILE      *logfile = NULL;
+       struct tm *timestamp = NULL;
+       time_t     timer = 0;
+       va_list    ap;
+
+       if (loglevel >= logthres) {
+               timer = time(NULL);
+               timestamp = localtime(&timer);
+
+               if (logfilename != NULL) {
+                       logfile = fopen(logfilename, "a");
+                       flockfile(logfile);
+               } else {
+                       logfile = stderr;
+               }
+       
+               fprintf(logfile, "[%02d/%02d/%4d %02d:%02d:%02d] %s[%d]: ",
+                               timestamp->tm_mday,
+                               timestamp->tm_mon,
+                               timestamp->tm_year + 1900,
+                               timestamp->tm_hour,
+                               timestamp->tm_min,
+                               timestamp->tm_sec,
+                               (logappname == NULL) ? "" : logappname,
+                               getpid());
+               va_start(ap, format);
+               vfprintf(logfile, format, ap);
+               va_end(ap);
+               fprintf(logfile, "\n");
+
+
+               if (logfilename != NULL) {
+                       funlockfile(logfile);
+                       fclose(logfile);
+                       logfile = NULL;
+               }
+       }
+
+       return 0;
+}
diff --git a/log.h b/log.h
new file mode 100644 (file)
index 0000000..ed2818f
--- /dev/null
+++ b/log.h
@@ -0,0 +1,81 @@
+/*
+ * log.h - Simple logging framework.
+ *
+ * Jonathan McDowell <noodles@earth.li>
+ *
+ * Copyright 2003 Project Purple
+ */
+
+#ifndef __LOG_H__
+#define __LOG_H__
+
+/*
+ *     loglevels - levels of severity for a log entry
+ *
+ *     These provide various different levels of severity for a log entry. In
+ *     acesending order they are:
+ *
+ *     LOGTHING_TRACE
+ *     LOGTHING_DEBUG
+ *     LOGTHING_INFO
+ *     LOGTHING_NOTICE
+ *     LOGTHING_ERROR
+ *     LOGTHING_SERIOUS
+ *     LOGTHING_CRITICAL
+ *
+ *     By default the log threshold is set to LOGTHING_NOTICE, meaning
+ *     anything with a lower priority won't be output.
+ */
+typedef enum {
+       LOGTHING_TRACE = 0,
+       LOGTHING_DEBUG = 1,
+       LOGTHING_INFO = 2,
+       LOGTHING_NOTICE = 3,
+       LOGTHING_ERROR = 4,
+       LOGTHING_SERIOUS = 5,
+       LOGTHING_CRITICAL = 6
+} loglevels;
+
+/*
+ *     initlogthing - initialize the logging module
+ *     @appname: The application name to use in the log.
+ *     @filename: The filename to log to. NULL means stderr.
+ *
+ *      This function sets up the logging module ready to log. The appname is
+ *      written as part of every log entry and the filename is the file we
+ *      should log to. If the appname is NULL then none is written. If the
+ *      filename is NULL all output is sent to stderr.
+ */
+int initlogthing(const char *appname, const char *filename);
+
+/*
+ *     cleanuplogthing - clean up the logging module
+ *
+ *      This function cleans up the logging module after use.
+ */
+void cleanuplogthing(void);
+
+/*
+ *     setlogthreshold - set the threshold for log output
+ *     @loglevel: The minimum log level we should output
+ *
+ *     Sets the threshold for log output; anything logged with a log level
+ *     lower than this will be silently dropped. Returns the old log threshold
+ *     value.
+ */
+loglevels setlogthreshold(loglevels loglevel);
+
+/*
+ *     logthing - output a log entry
+ *      @loglevel: The level of the log.
+ *      @format: A format string, followed by any parameters required.
+ *
+ *     This function outputs a log entry. A leading time/date stamp and a
+ *     trailing newline are automatically added. The loglevel is compared to
+ *     the current log threshold and if equal or above the log entry is
+ *     output. The format parameter is of the same nature as that used in
+ *     printf.
+ */
+int logthing(loglevels loglevel, const char *format, ...);
+
+#endif /* __LOG_H__ */
index 00bc4165c7a1e59b88af27840aa8d370e85bb503..d6017d23267d5bb34038f9de901e2bc2977c7aaa 100644 (file)
--- a/lookup.c
+++ b/lookup.c
@@ -17,6 +17,7 @@
 #include "getcgi.h"
 #include "keydb.h"
 #include "keyindex.h"
 #include "getcgi.h"
 #include "keydb.h"
 #include "keyindex.h"
+#include "log.h"
 #include "mem.h"
 #include "onak-conf.h"
 #include "parsekey.h"
 #include "mem.h"
 #include "onak-conf.h"
 #include "parsekey.h"
@@ -120,6 +121,7 @@ int main(int argc, char *argv[])
                puts("Error: No key to search for supplied.");
        } else {
                readconfig();
                puts("Error: No key to search for supplied.");
        } else {
                readconfig();
+               initlogthing("lookup", config.logfile);
                initdb();
                switch (op) {
                case OP_GET:
                initdb();
                switch (op) {
                case OP_GET:
@@ -148,6 +150,7 @@ int main(int argc, char *argv[])
                        puts("Unknown operation!");
                }
                cleanupdb();
                        puts("Unknown operation!");
                }
                cleanupdb();
+               cleanuplogthing();
                cleanupconfig();
        }
        puts("<hr>");
                cleanupconfig();
        }
        puts("<hr>");
diff --git a/merge.c b/merge.c
index 7f1773eca706e8d0cfa4b7def3c83e54e78a24c3..3e8e8fcf7eb772ff8ca0b7cd8c1289c4d7ddb112 100644 (file)
--- a/merge.c
+++ b/merge.c
@@ -15,6 +15,7 @@
 #include "keyid.h"
 #include "keystructs.h"
 #include "ll.h"
 #include "keyid.h"
 #include "keystructs.h"
 #include "ll.h"
+#include "log.h"
 #include "mem.h"
 #include "merge.h"
 
 #include "mem.h"
 #include "merge.h"
 
@@ -353,7 +354,6 @@ int merge_keys(struct openpgp_publickey *a, struct openpgp_publickey *b)
 /**
  *     update_keys - Takes a list of public keys and updates them in the DB.
  *     @keys: The keys to update in the DB.
 /**
  *     update_keys - Takes a list of public keys and updates them in the DB.
  *     @keys: The keys to update in the DB.
- *     @verbose: Should we output more information as we add keys?
  *
  *     Takes a list of keys and adds them to the database, merging them with
  *     the key in the database if it's already present there. The key list is
  *
  *     Takes a list of keys and adds them to the database, merging them with
  *     the key in the database if it's already present there. The key list is
@@ -361,7 +361,7 @@ int merge_keys(struct openpgp_publickey *a, struct openpgp_publickey *b)
  *     we had before to what we have now (ie the set of data that was added to
  *     the DB). Returns the number of entirely new keys added.
  */
  *     we had before to what we have now (ie the set of data that was added to
  *     the DB). Returns the number of entirely new keys added.
  */
-int update_keys(struct openpgp_publickey **keys, bool verbose)
+int update_keys(struct openpgp_publickey **keys)
 {
        struct openpgp_publickey *curkey = NULL;
        struct openpgp_publickey *oldkey = NULL;
 {
        struct openpgp_publickey *curkey = NULL;
        struct openpgp_publickey *oldkey = NULL;
@@ -371,13 +371,10 @@ int update_keys(struct openpgp_publickey **keys, bool verbose)
 
        for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
                intrans = starttrans();
 
        for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
                intrans = starttrans();
-               if (verbose) {
-                       fprintf(stderr, "Fetching key 0x%llX, result: %d\n",
-                               get_keyid(curkey),
-                               fetch_key(get_keyid(curkey), &oldkey, intrans));
-               } else {
-                       fetch_key(get_keyid(curkey), &oldkey, intrans);
-               }
+               logthing(LOGTHING_INFO,
+                       "Fetching key 0x%llX, result: %d",
+                       get_keyid(curkey),
+                       fetch_key(get_keyid(curkey), &oldkey, intrans));
 
                /*
                 * If we already have the key stored in the DB then merge it
 
                /*
                 * If we already have the key stored in the DB then merge it
@@ -400,19 +397,15 @@ int update_keys(struct openpgp_publickey **keys, bool verbose)
                                }
                        } else {
                                prev = curkey;
                                }
                        } else {
                                prev = curkey;
-                               if (verbose) {
-                                       fprintf(stderr,
-                                       "Merged key; storing updated key.\n");
-                               }
+                               logthing(LOGTHING_INFO,
+                                       "Merged key; storing updated key.");
                                store_key(oldkey, intrans, true);
                        }
                        free_publickey(oldkey);
                        oldkey = NULL;
                } else {
                                store_key(oldkey, intrans, true);
                        }
                        free_publickey(oldkey);
                        oldkey = NULL;
                } else {
-                       if (verbose) {
-                               fprintf(stderr,
-                                       "Storing completely new key.\n");
-                       }
+                       logthing(LOGTHING_INFO,
+                               "Storing completely new key.");
                        store_key(curkey, intrans, false);
                        newkeys++;
                }
                        store_key(curkey, intrans, false);
                        newkeys++;
                }
diff --git a/merge.h b/merge.h
index f2981720dc1ee8a1d6f08b1984de048b063f1b86..208c39bd0477cd8ae312c755c4f7571191bf0a4c 100644 (file)
--- a/merge.h
+++ b/merge.h
@@ -28,7 +28,6 @@ int merge_keys(struct openpgp_publickey *a, struct openpgp_publickey *b);
 /**
  *     update_keys - Takes a list of public keys and updates them in the DB.
  *     @keys: The keys to update in the DB.
 /**
  *     update_keys - Takes a list of public keys and updates them in the DB.
  *     @keys: The keys to update in the DB.
- *     @verbose: Should we output more information as we add keys?
  *
  *     Takes a list of keys and adds them to the database, merging them with
  *     the key in the database if it's already present there. The key list is
  *
  *     Takes a list of keys and adds them to the database, merging them with
  *     the key in the database if it's already present there. The key list is
@@ -36,6 +35,6 @@ int merge_keys(struct openpgp_publickey *a, struct openpgp_publickey *b);
  *     we had before to what we have now (ie the set of data that was added to
  *     the DB). Returns the number of entirely new keys added.
  */
  *     we had before to what we have now (ie the set of data that was added to
  *     the DB). Returns the number of entirely new keys added.
  */
-int update_keys(struct openpgp_publickey **keys, bool verbose);
+int update_keys(struct openpgp_publickey **keys);
 
 #endif
 
 #endif
index 93a9320b4a3e165db6dbdd3e8ca1691b255d01db..f215b319670e8fabc0a5e76491495e46a1bfef07 100644 (file)
@@ -12,6 +12,7 @@
 #include <string.h>
 
 #include "ll.h"
 #include <string.h>
 
 #include "ll.h"
+#include "log.h"
 #include "onak-conf.h"
 
 /*
 #include "onak-conf.h"
 
 /*
@@ -26,6 +27,7 @@ struct onak_config config = {
        NULL,                   /* adminemail */
        NULL,                   /* mta */
        NULL,                   /* syncsites */
        NULL,                   /* adminemail */
        NULL,                   /* mta */
        NULL,                   /* syncsites */
+       NULL,                   /* logfile */
 
        /*
         * Options for directory backends.
 
        /*
         * Options for directory backends.
@@ -104,6 +106,8 @@ void readconfig(void) {
                } else if (!strncmp("syncsite ", curline, 9)) {
                        config.syncsites =
                                lladd(config.syncsites, strdup(&curline[9]));
                } else if (!strncmp("syncsite ", curline, 9)) {
                        config.syncsites =
                                lladd(config.syncsites, strdup(&curline[9]));
+               } else if (!strncmp("logfile ", curline, 8)) {
+                       config.logfile = strdup(&curline[8]);
                } else if (!strncmp("this_site ", curline, 10)) {
                        config.thissite = strdup(&curline[10]);
                } else if (!strncmp("socket_name ", curline, 12) ||
                } else if (!strncmp("this_site ", curline, 10)) {
                        config.thissite = strdup(&curline[10]);
                } else if (!strncmp("socket_name ", curline, 12) ||
@@ -114,14 +118,16 @@ void readconfig(void) {
                         * Not applicable; ignored for compatibility with pksd.
                         */
                } else {
                         * Not applicable; ignored for compatibility with pksd.
                         */
                } else {
-                       fprintf(stderr, "Unknown config line: %s\n", curline);
+                       logthing(LOGTHING_ERROR,
+                               "Unknown config line: %s", curline);
                }
 
                        fgets(curline, 1023, conffile);
                }
                fclose(conffile);
        } else {
                }
 
                        fgets(curline, 1023, conffile);
                }
                fclose(conffile);
        } else {
-               fprintf(stderr, "Couldn't open config file; using defaults.\n");
+               logthing(LOGTHING_NOTICE,
+                               "Couldn't open config file; using defaults.");
        }
 }
 
        }
 }
 
@@ -162,4 +168,8 @@ void cleanupconfig(void) {
                llfree(config.syncsites, free);
                config.syncsites = NULL;
        }
                llfree(config.syncsites, free);
                config.syncsites = NULL;
        }
+       if (config.logfile != NULL) {
+               free(config.logfile);
+               config.logfile = NULL;
+       }
 }
 }
index 04ee2291396a506157c2960e444f0f48e48baef6..a8751c77a775dffb34ebac2bc855a5a6c0a1afcc 100644 (file)
@@ -39,6 +39,7 @@ struct onak_config {
        char *adminemail;
        char *mta;
        struct ll *syncsites;
        char *adminemail;
        char *mta;
        struct ll *syncsites;
+       char *logfile;
 
        /*
         * Options for any database backend that needs a directory, be it the
 
        /*
         * Options for any database backend that needs a directory, be it the
diff --git a/onak.c b/onak.c
index f673bd38b5038dcb26042f6dcb6a92038c7bec39..311b44f9b16d9e67a741d04d7666280886e851e1 100644 (file)
--- a/onak.c
+++ b/onak.c
@@ -18,6 +18,7 @@
 #include "keyid.h"
 #include "keyindex.h"
 #include "keystructs.h"
 #include "keyid.h"
 #include "keyindex.h"
 #include "keystructs.h"
+#include "log.h"
 #include "mem.h"
 #include "merge.h"
 #include "onak-conf.h"
 #include "mem.h"
 #include "merge.h"
 #include "onak-conf.h"
@@ -112,11 +113,13 @@ int main(int argc, char *argv[])
                        break;
                case 'v': 
                        verbose = true;
                        break;
                case 'v': 
                        verbose = true;
+                       setlogthreshold(LOGTHING_INFO);
                        break;
                }
        }
 
        readconfig();
                        break;
                }
        }
 
        readconfig();
+       initlogthing("onak", config.logfile);
 
        if ((argc - optind) < 1) {
                usage();
 
        if ((argc - optind) < 1) {
                usage();
@@ -128,10 +131,8 @@ int main(int argc, char *argv[])
                if (binary) {
                        result = read_openpgp_stream(stdin_getchar, NULL,
                                 &packets);
                if (binary) {
                        result = read_openpgp_stream(stdin_getchar, NULL,
                                 &packets);
-                       if (verbose) {
-                               fprintf(stderr,
-                                       "read_openpgp_stream: %d\n", result);
-                       }
+                       logthing(LOGTHING_INFO,
+                                       "read_openpgp_stream: %d", result);
                } else {
                        dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
                }
                } else {
                        dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
                }
@@ -139,14 +140,12 @@ int main(int argc, char *argv[])
                        result = parse_keys(packets, &keys);
                        free_packet_list(packets);
                        packets = NULL;
                        result = parse_keys(packets, &keys);
                        free_packet_list(packets);
                        packets = NULL;
-                       if (verbose) {
-                               fprintf(stderr, "Finished reading %d keys.\n",
+                       logthing(LOGTHING_INFO, "Finished reading %d keys.",
                                        result);
                                        result);
-                       }
 
                        initdb();
 
                        initdb();
-                       fprintf(stderr, "Got %d new keys.\n",
-                                       update_keys(&keys, verbose));
+                       logthing(LOGTHING_NOTICE, "Got %d new keys.",
+                                       update_keys(&keys));
                        if (keys != NULL && update) {
                                flatten_publickey(keys,
                                        &packets,
                        if (keys != NULL && update) {
                                flatten_publickey(keys,
                                        &packets,
@@ -160,7 +159,7 @@ int main(int argc, char *argv[])
                        cleanupdb();
                } else {
                        rc = 1;
                        cleanupdb();
                } else {
                        rc = 1;
-                       fprintf(stderr, "No keys read.\n");
+                       logthing(LOGTHING_NOTICE, "No keys read.");
                }
 
                if (keys != NULL) {
                }
 
                if (keys != NULL) {
@@ -168,7 +167,7 @@ int main(int argc, char *argv[])
                        keys = NULL;
                } else {
                        rc = 1;
                        keys = NULL;
                } else {
                        rc = 1;
-                       fprintf(stderr, "No changes.\n");
+                       logthing(LOGTHING_NOTICE, "No changes.");
                }
        } else if ((argc - optind) == 2) {
                search = argv[optind+1];
                }
        } else if ((argc - optind) == 2) {
                search = argv[optind+1];
@@ -191,9 +190,7 @@ int main(int argc, char *argv[])
                        delete_key(getfullkeyid(keyid), false);
                } else if (!strcmp("get", argv[optind])) {
                        if (fetch_key(keyid, &keys, false)) {
                        delete_key(getfullkeyid(keyid), false);
                } else if (!strcmp("get", argv[optind])) {
                        if (fetch_key(keyid, &keys, false)) {
-                               if (verbose) {
-                                       fprintf(stderr, "Got key.\n");
-                               }
+                               logthing(LOGTHING_INFO, "Got key.");
                                flatten_publickey(keys,
                                                &packets,
                                                &list_end);
                                flatten_publickey(keys,
                                                &packets,
                                                &list_end);
@@ -211,6 +208,7 @@ int main(int argc, char *argv[])
                usage();
        }
 
                usage();
        }
 
+       cleanuplogthing();
        cleanupconfig();
 
        return rc;
        cleanupconfig();
 
        return rc;
index 4ec7ef3338a43d2d7a02502bbe02ab6f5d7d39cd..4109bb8961aab003c40ab8687e63ae2bd9ffe1b9 100644 (file)
@@ -15,6 +15,7 @@
 #include "keyid.h"
 #include "keystructs.h"
 #include "ll.h"
 #include "keyid.h"
 #include "keystructs.h"
 #include "ll.h"
+#include "log.h"
 #include "mem.h"
 #include "parsekey.h"
 
 #include "mem.h"
 #include "parsekey.h"
 
@@ -126,7 +127,8 @@ int parse_keys(struct openpgp_packet_list *packets,
                         */
                        break;
                default:
                         */
                        break;
                default:
-                       fprintf(stderr, "Unsupported packet type: %d\n",
+                       logthing(LOGTHING_ERROR,
+                                       "Unsupported packet type: %d",
                                        packets->packet->tag);
                }
                packets = packets->next;
                                        packets->packet->tag);
                }
                packets = packets->next;
@@ -259,7 +261,8 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
                                        curpacket->packet->length += curchar;
                                        break;
                                case 3:
                                        curpacket->packet->length += curchar;
                                        break;
                                case 3:
-                                       fprintf(stderr, "Unsupported length type 3.\n");
+                                       logthing(LOGTHING_ERROR,
+                                               "Unsupported length type 3.");
                                        curpacket->packet->length = 0;
                                        curpacket->packet->data = NULL;
                                        rc = -1;
                                        curpacket->packet->length = 0;
                                        curpacket->packet->data = NULL;
                                        rc = -1;
@@ -277,7 +280,7 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
                        }
                        inpacket = false;
                } else {
                        }
                        inpacket = false;
                } else {
-                       fprintf(stderr, "Unexpected character: 0x%X\n",
+                       logthing(LOGTHING_ERROR, "Unexpected character: 0x%X",
                                curchar);
                }
        }
                                curchar);
                }
        }
@@ -321,7 +324,8 @@ int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
                                         0xFF;
                                putchar_func(ctx, 1, &curchar);
                        } else {
                                         0xFF;
                                putchar_func(ctx, 1, &curchar);
                        } else {
-                               fputs("Unsupported new format length.\n", stderr);
+                               logthing(LOGTHING_ERROR,
+                                       "Unsupported new format length.");
                        }
                } else {
                        curchar |= (packets->packet->tag << 2);
                        }
                } else {
                        curchar |= (packets->packet->tag << 2);