* Trace file using lprintf() similarly to citserver
authorMichael Hampton <io_error@uncensored.citadel.org>
Thu, 24 Jan 2002 23:50:37 +0000 (23:50 +0000)
committerMichael Hampton <io_error@uncensored.citadel.org>
Thu, 24 Jan 2002 23:50:37 +0000 (23:50 +0000)
webcit/ChangeLog
webcit/context_loop.c
webcit/mime_parser.c
webcit/snprintf.c
webcit/tcp_sockets.c
webcit/tools.c
webcit/webcit.c
webcit/webserver.c
webcit/webserver.h

index a2e3e089177ec296f8fa9f429d50595f6f189cac..905450eb481e81f1cacfc5c58c44ea89b837754b 100644 (file)
@@ -1,4 +1,7 @@
 $Log$
+Revision 323.1  2002/01/24 23:50:36  error
+* Trace file using lprintf() similarly to citserver
+
 Revision 323.0  2002/01/13 09:07:06  ajc
 * THIS IS 3.23
 
@@ -713,3 +716,4 @@ Sun Dec  6 19:50:55 EST 1998 Art Cancro <ajc@uncnsrd.mt-kisco.ny.us>
 
 1998-12-03 Nathan Bryant <bryant@cs.usm.maine.edu>
        * webserver.c: warning fix
+
index 03ec66e1a00a563360aed972325801304d42c927..99de823e712317c72b7a2f8f254e22078a6f0f20 100644 (file)
@@ -253,7 +253,7 @@ void context_loop(int sock)
        } while (strlen(buf) > 0);
 
        strcpy(buf, req->line);
-       fprintf(stderr, "%s\n", buf);
+       lprintf(5, "HTTP: %s\n", buf);
 
        /* Check for bogus requests */
        if (is_bogus(buf)) goto bail;
@@ -302,7 +302,7 @@ void context_loop(int sock)
         * Create a new session if we have to
         */
        if (TheSession == NULL) {
-               fprintf(stderr, "Creating a new session\n");
+               lprintf(3, "Creating a new session\n");
                TheSession = (struct wcsession *)
                        malloc(sizeof(struct wcsession));
                memset(TheSession, 0, sizeof(struct wcsession));
index 2fa52d9a1e517304b0677d15c075ea35f5e698b8..6e60fe33e46fcf9b42810017928542eb40e1124d 100644 (file)
@@ -18,6 +18,7 @@
 #include <sys/stat.h>
 #include <errno.h>
 #include "webcit.h"
+#include "webserver.h"
 
 
 void extract_key(char *target, char *source, char *key)
@@ -164,7 +165,7 @@ void mime_decode(char *partnum,
        char *decoded;
        size_t bytes_decoded = 0;
 
-       fprintf(stderr, "mime_decode() called\n");
+       lprintf(9, "mime_decode() called\n");
 
        /* Some encodings aren't really encodings */
        if (!strcasecmp(encoding, "7bit"))
@@ -186,7 +187,7 @@ void mime_decode(char *partnum,
        
        if ((strcasecmp(encoding, "base64"))
            && (strcasecmp(encoding, "quoted-printable"))) {
-               fprintf(stderr, "ERROR: unknown MIME encoding '%s'\n", encoding);
+               lprintf(1, "ERROR: unknown MIME encoding '%s'\n", encoding);
                return;
        }
        /*
@@ -195,16 +196,16 @@ void mime_decode(char *partnum,
         * will never be larger than the encoded data.  This is a safe
         * assumption with base64, uuencode, and quoted-printable.
         */
-       fprintf(stderr, "About to allocate %d bytes for decoded part\n",
+       lprintf(9, "About to allocate %d bytes for decoded part\n",
                length+2048);
        decoded = malloc(length+2048);
        if (decoded == NULL) {
-               fprintf(stderr, "ERROR: cannot allocate memory.\n");
+               lprintf(1, "ERROR: cannot allocate memory.\n");
                return;
        }
-       fprintf(stderr, "Got it!\n");
+       lprintf(9, "Got it!\n");
 
-       fprintf(stderr, "Decoding %s\n", encoding);
+       lprintf(9, "Decoding %s\n", encoding);
        if (!strcasecmp(encoding, "base64")) {
                bytes_decoded = decode_base64(decoded, part_start);
        }
@@ -212,7 +213,7 @@ void mime_decode(char *partnum,
                bytes_decoded = decode_quoted_printable(decoded,
                                                        part_start, length);
        }
-       fprintf(stderr, "Bytes decoded: %d\n", bytes_decoded);
+       lprintf(9, "Bytes decoded: %d\n", bytes_decoded);
 
        if (bytes_decoded > 0) if (CallBack != NULL) {
                CallBack(name, filename, fixed_partnum(partnum),
@@ -285,7 +286,7 @@ void the_mime_parser(char *partnum,
        size_t length;
        char nested_partnum[SIZ];
 
-       fprintf(stderr, "the_mime_parser() called\n");
+       lprintf(9, "the_mime_parser() called\n");
        ptr = content_start;
        content_length = 0;
 
@@ -336,7 +337,7 @@ void the_mime_parser(char *partnum,
                        if (!strncasecmp(header, "Content-type: ", 14)) {
                                strcpy(content_type, &header[14]);
                                extract_key(name, content_type, "name");
-                               fprintf(stderr, "Extracted content-type <%s>\n",
+                               lprintf(9, "Extracted content-type <%s>\n",
                                        content_type);
                        }
                        if (!strncasecmp(header, "Content-Disposition: ", 21)) {
@@ -374,7 +375,7 @@ void the_mime_parser(char *partnum,
                is_multipart = 0;
        }
 
-       fprintf(stderr, "is_multipart=%d, boundary=<%s>\n",
+       lprintf(9, "is_multipart=%d, boundary=<%s>\n",
                is_multipart, boundary);
 
        /* If this is a multipart message, then recursively process it */
@@ -394,7 +395,7 @@ void the_mime_parser(char *partnum,
                do {
                        if ( (!strncasecmp(ptr, startary, strlen(startary)))
                           || (!strncasecmp(ptr, endary, strlen(endary))) ) {
-                               fprintf(stderr, "hit boundary!\n");
+                               lprintf(9, "hit boundary!\n");
                                if (part_start != NULL) {
                                        if (strlen(partnum) > 0) {
                                                sprintf(nested_partnum, "%s.%d",
@@ -429,7 +430,7 @@ void the_mime_parser(char *partnum,
 
        /* If it's not a multipart message, then do something with it */
        if (!is_multipart) {
-               fprintf(stderr, "doing non-multipart thing\n");
+               lprintf(9, "doing non-multipart thing\n");
                part_start = ptr;
                length = 0;
                while (ptr < content_end) {
@@ -441,7 +442,7 @@ void the_mime_parser(char *partnum,
                /* Truncate if the header told us to */
                if ( (content_length > 0) && (length > content_length) ) {
                        length = content_length;
-                       fprintf(stderr, "truncated to %ld\n", (long)content_length);
+                       lprintf(9, "truncated to %ld\n", (long)content_length);
                }
                
                mime_decode(partnum,
@@ -513,7 +514,7 @@ void mime_parser(char *content_start,
 )
 {
 
-       fprintf(stderr, "mime_parser() called\n");
+       lprintf(9, "mime_parser() called\n");
        the_mime_parser("", content_start, content_end,
                        CallBack,
                        PreMultiPartCallBack,
index 5cf567fd65b99be635573219278561c59378c3ca..7db792c20827fea32b19e8a5dbd3fa51f3549dd2 100644 (file)
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
+#include "webserver.h"
 
 static int needed(const char *fmt, va_list argp)
 {
@@ -50,7 +51,7 @@ int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp)
        int size;
 
        if ((p = malloc(needed(fmt, argp) + 1)) == NULL) {
-               fprintf(stderr, "vsnprintf: malloc failed, aborting\n");
+               lprintf(1, "vsnprintf: malloc failed, aborting\n");
                abort();
        }
        if ((size = vsprintf(p, fmt, argp)) >= max)
index d457e71c6a18cb693521f23a54bc4835c5a719d7..9c32b71588a744752ec4fea7101838e329ad309f 100644 (file)
@@ -29,6 +29,8 @@
 #include <pthread.h>
 #include <signal.h>
 #include "webcit.h"
+#include "webserver.h"
+
 
 #ifndef INADDR_NONE
 #define INADDR_NONE 0xffffffff
@@ -36,7 +38,7 @@
 
 RETSIGTYPE timeout(int signum)
 {
-       fprintf(stderr, "Connection timed out.\n");
+       lprintf(1, "Connection timed out.\n");
        exit(3);
 }
 
@@ -56,13 +58,13 @@ int uds_connectsock(char *sockpath)
 
        s = socket(AF_UNIX, SOCK_STREAM, 0);
        if (s < 0) {
-               fprintf(stderr, "Can't create socket: %s\n",
+               lprintf(1, "Can't create socket: %s\n",
                        strerror(errno));
                return(-1);
        }
 
        if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-               fprintf(stderr, "can't connect: %s\n",
+               lprintf(1, "Can't connect: %s\n",
                        strerror(errno));
                return(-1);
        }
@@ -89,33 +91,33 @@ int tcp_connectsock(char *host, char *service)
        if (pse) {
                sin.sin_port = pse->s_port;
        } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
-               fprintf(stderr, "Can't get %s service entry\n", service);
+               lprintf(1, "Can't get %s service entry\n", service);
                return (-1);
        }
        phe = gethostbyname(host);
        if (phe) {
                memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
        } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
-               fprintf(stderr, "Can't get %s host entry: %s\n",
+               lprintf(1, "Can't get %s host entry: %s\n",
                        host, strerror(errno));
                return (-1);
        }
        if ((ppe = getprotobyname("tcp")) == 0) {
-               fprintf(stderr, "Can't get TCP protocol entry: %s\n",
+               lprintf(1, "Can't get TCP protocol entry: %s\n",
                        strerror(errno));
                return (-1);
        }
 
        s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
        if (s < 0) {
-               fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
+               lprintf(1, "Can't create socket: %s\n", strerror(errno));
                return (-1);
        }
        signal(SIGALRM, timeout);
        alarm(30);
 
        if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
-               fprintf(stderr, "can't connect to %s.%s: %s\n",
+               lprintf(1, "Can't connect to %s.%s: %s\n",
                        host, service, strerror(errno));
                return (-1);
        }
@@ -139,7 +141,7 @@ void serv_read(char *buf, int bytes)
        while (len < bytes) {
                rlen = read(WC->serv_sock, &buf[len], bytes - len);
                if (rlen < 1) {
-                       fprintf(stderr, "Server connection broken: %s\n",
+                       lprintf(1, "Server connection broken: %s\n",
                                strerror(errno));
                        WC->connected = 0;
                        WC->logged_in = 0;
@@ -166,7 +168,7 @@ void serv_gets(char *strbuf)
                strbuf[len++] = ch;
        } while ((ch != 10) && (ch != 13) && (ch != 0) && (len < 255));
        strbuf[len - 1] = 0;
-       /* fprintf(stderr, ">%s\n", strbuf); */
+       /* lprintf(9, ">%s\n", strbuf); */
 }
 
 
@@ -182,7 +184,7 @@ void serv_write(char *buf, int nbytes)
                retval = write(WC->serv_sock, &buf[bytes_written],
                               nbytes - bytes_written);
                if (retval < 1) {
-                       fprintf(stderr, "Server connection broken: %s\n",
+                       lprintf(1, "Server connection broken: %s\n",
                                strerror(errno));
                        WC->connected = 0;
                        WC->logged_in = 0;
@@ -219,5 +221,5 @@ void serv_printf(const char *format,...)
 
        strcat(buf, "\n");
        serv_write(buf, strlen(buf));
-       /* fprintf(stderr, "<%s", buf); */
+       /* lprintf(9, "<%s", buf); */
 }
index cc1bbbedf116da237c3b1b42738df50246b05c48..8bcccaed682a696b5d1703abd6ebec47cc741bdf 100644 (file)
@@ -25,6 +25,7 @@
 #include <signal.h>
 #include <sys/time.h>
 #include "webcit.h"
+#include "webserver.h"
 
 
 char *ascmonths[] = {
@@ -40,7 +41,7 @@ char *ascdays[] = {
 char *safestrncpy(char *dest, const char *src, size_t n)
 {
        if (dest == NULL || src == NULL) {
-               fprintf(stderr, "safestrncpy: NULL argument\n");
+               lprintf(1, "safestrncpy: NULL argument\n");
                abort();
        }
        strncpy(dest, src, n);
index 530d60e6c2c64621fdc5bb2ea64327b49d07d293..4cae23969a032f24e02efad88913dda5115c778b 100644 (file)
@@ -29,6 +29,7 @@
 #include <pthread.h>
 #include <signal.h>
 #include "webcit.h"
+#include "webserver.h"
 #include "mime_parser.h"
 
 /*
@@ -435,7 +436,7 @@ void output_static(char *what)
 
                fstat(fileno(fp), &statbuf);
                bytes = statbuf.st_size;
-               fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
+               lprintf(5, "Static: %s, %ld bytes\n", what, bytes);
                wprintf("Content-length: %ld\n", (long) bytes);
                wprintf("\n");
                bigbuffer = malloc(bytes);
@@ -631,12 +632,12 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
                        char *encoding, void *userdata)
 {
 
-       fprintf(stderr, "UPLOAD HANDLER CALLED\n");
-       fprintf(stderr, "    name = %s\n", name);
-       fprintf(stderr, "filename = %s\n", filename);
-       fprintf(stderr, "encoding = %s\n", encoding);
-       fprintf(stderr, "    type = %s\n", cbtype);
-       fprintf(stderr, "  length = %ld\n", (long)length);
+       lprintf(9, "UPLOAD HANDLER CALLED\n");
+       lprintf(9, "    name = %s\n", name);
+       lprintf(9, "filename = %s\n", filename);
+       lprintf(9, "encoding = %s\n", encoding);
+       lprintf(9, "    type = %s\n", cbtype);
+       lprintf(9, "  length = %ld\n", (long)length);
 
        if (length > 0) {
                WC->upload = malloc(length);
@@ -645,7 +646,7 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
                        memcpy(WC->upload, content, length);
                }
                else {
-                       fprintf(stderr, "malloc() failed: %s\n",
+                       lprintf(1, "malloc() failed: %s\n",
                                strerror(errno));
                }
        }
@@ -717,7 +718,7 @@ void session_loop(struct httprequest *req)
        }
 
        if (ContentLength > 0) {
-               fprintf(stderr, "Content length: %d\n", ContentLength);
+               lprintf(5, "Content length: %d\n", ContentLength);
                content = malloc(ContentLength + 1);
                memset(content, 0, ContentLength+1);
                BytesRead = 0;
index 334187d7fb451ee06beecd3dc9ff5a43e4706c29..da0282b00d8f3891561e82d81ff7d946b37cf3ed 100644 (file)
 #include <pthread.h>
 #include <signal.h>
 #include "webcit.h"
+#include "webserver.h"
 
 #ifndef HAVE_SNPRINTF
 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
 #endif
 
+int verbosity = 9;             /* Logging level */
 int msock;                     /* master listening socket */
 extern void *context_loop(int);
 extern void *housekeeping_loop(void);
@@ -70,15 +72,14 @@ int ig_tcp_server(int port_number, int queue_len)
        sin.sin_addr.s_addr = INADDR_ANY;
 
        if (port_number == 0) {
-               fprintf(stderr,
-                       "webcit: Cannot start: no port number specified.\n");
+               lprintf(1, "Cannot start: no port number specified.\n");
                exit(1);
        }
        sin.sin_port = htons((u_short) port_number);
 
        s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
        if (s < 0) {
-               fprintf(stderr, "webcit: Can't create a socket: %s\n",
+               lprintf(1, "Can't create a socket: %s\n",
                       strerror(errno));
                exit(errno);
        }
@@ -87,11 +88,11 @@ int ig_tcp_server(int port_number, int queue_len)
        setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
 
        if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
-               fprintf(stderr, "webcit: Can't bind: %s\n", strerror(errno));
+               lprintf(1, "Can't bind: %s\n", strerror(errno));
                exit(errno);
        }
        if (listen(s, queue_len) < 0) {
-               fprintf(stderr, "webcit: Can't listen: %s\n", strerror(errno));
+               lprintf(1, "Can't listen: %s\n", strerror(errno));
                exit(errno);
        }
        return (s);
@@ -126,7 +127,7 @@ int client_read_to(int sock, char *buf, int bytes, int timeout)
                }
                rlen = read(sock, &buf[len], bytes - len);
                if (rlen < 1) {
-                       fprintf(stderr, "client_read() failed: %s\n",
+                       lprintf(2, "client_read() failed: %s\n",
                               strerror(errno));
                        return(-1);
                }
@@ -201,7 +202,7 @@ void spawn_another_worker_thread() {
        pthread_t SessThread;   /* Thread descriptor */
        pthread_attr_t attr;    /* Thread attributes */
 
-       fprintf(stderr, "Creating a new thread\n");
+       lprintf(3, "Creating a new thread\n");
 
        /* set attributes for the new thread */
        pthread_attr_init(&attr);
@@ -211,7 +212,7 @@ void spawn_another_worker_thread() {
        if (pthread_create(&SessThread, &attr,
                        (void *(*)(void *)) worker_entry, NULL)
                   != 0) {
-               fprintf(stderr, "webcit: can't create thread: %s\n",
+               lprintf(1, "Can't create thread: %s\n",
                        strerror(errno));
        }
 }
@@ -239,6 +240,9 @@ int main(int argc, char **argv)
                        freopen(tracefile, "w", stderr);
                        freopen(tracefile, "r", stdin);
                        break;
+               case 'x':
+                       verbosity = atoi(optarg);
+                       break;
                case 'c':
                        server_cookie = malloc(SIZ);
                        if (server_cookie != NULL) {
@@ -246,7 +250,7 @@ int main(int argc, char **argv)
                                if (gethostname(
                                   &server_cookie[strlen(server_cookie)],
                                   200) != 0) {
-                                       fprintf(stderr, "gethostname: %s\n",
+                                       lprintf(2, "gethostname: %s\n",
                                                strerror(errno));
                                        free(server_cookie);
                                }
@@ -265,7 +269,7 @@ int main(int argc, char **argv)
                        defaultport = argv[optind];
        }
        /* Tell 'em who's in da house */
-       fprintf(stderr, SERVER "\n"
+       lprintf(1, SERVER "\n"
 "Copyright (C) 1996-2001 by the Citadel/UX development team.\n"
 "This software is distributed under the terms of the GNU General Public\n"
 "License.  If you paid for this software, someone is ripping you off.\n\n");
@@ -279,7 +283,7 @@ int main(int argc, char **argv)
          * wcsession struct to which the thread is currently bound.
          */
         if (pthread_key_create(&MyConKey, NULL) != 0) {
-                fprintf(stderr, "Can't create TSD key: %s\n", strerror(errno));
+                lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
         }
 
        /*
@@ -287,9 +291,9 @@ int main(int argc, char **argv)
         * There is no need to check for errors, because ig_tcp_server()
         * exits if it doesn't succeed.
         */
-       fprintf(stderr, "Attempting to bind to port %d...\n", port);
+       lprintf(2, "Attempting to bind to port %d...\n", port);
        msock = ig_tcp_server(port, LISTEN_QUEUE_LENGTH);
-       fprintf(stderr, "Listening on socket %d\n", msock);
+       lprintf(2, "Listening on socket %d\n", msock);
        signal(SIGPIPE, SIG_IGN);
 
        pthread_mutex_init(&SessionListMutex, NULL);
@@ -340,8 +344,7 @@ void worker_entry(void) {
                }
 
                if (ssock < 0) {
-                       fprintf(stderr, "webcit: accept() failed: %s\n",
-                       strerror(errno));
+                       lprintf(2, "accept() failed: %s\n", strerror(errno));
                } else {
                        /* Set the SO_REUSEADDR socket option */
                        i = 1;
@@ -359,3 +362,39 @@ void worker_entry(void) {
 
        pthread_exit(NULL);
 }
+
+
+int lprintf(int loglevel, const char *format, ...)
+{
+       va_list ap;
+       char buf[4096];
+
+       va_start(ap, format);
+       vsprintf(buf, format, ap);
+       va_end(ap);
+
+       if (loglevel <= verbosity) {
+               struct timeval tv;
+               struct tm *tim;
+
+               gettimeofday(&tv, NULL);
+               tim = localtime(&(tv.tv_sec));
+
+               if (WC && WC->wc_session) {
+                       fprintf(stderr,
+                               "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%ld:%d] %s",
+                               tim->tm_year + 1900, tim->tm_mon + 1,
+                               tim->tm_mday, tim->tm_hour, tim->tm_min,
+                               tim->tm_sec, (long)tv.tv_usec / 1000,
+                               pthread_self(), WC->wc_session, buf);
+               } else {
+                       fprintf(stderr,
+                               "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%ld] %s",
+                               tim->tm_year + 1900, tim->tm_mon + 1,
+                               tim->tm_mday, tim->tm_hour, tim->tm_min,
+                               tim->tm_sec, (long)tv.tv_usec / 1000,
+                               pthread_self(), buf);
+               }
+       }
+       return 1;
+}
index 9add4b88800e63b6f3932d178a59946299e694db..9bfede9eca6e0b078b21818856fddca980a70d19 100644 (file)
@@ -1,3 +1,4 @@
 /* $Id$ */
 int client_gets(int sock, char *buf);
 int client_read(int sock, char *buf, int bytes);
+int lprintf(int loglevel, const char *format, ...);