log_default.c

Go to the documentation of this file.
00001 /* $Id: log_default.c,v 1.4 2007/09/18 06:05:10 fabguy Exp $
00002  * Copyright (c) 2001, Bit Farm, Inc. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  * 1. Redistributions of source code must retain the above copyright
00008  *    notice, this list of conditions and the following disclaimer.
00009  * 2. Redistributions in binary form must reproduce the above copyright
00010  *    notice, this list of conditions and the following disclaimer in the
00011  *    documentation and/or other materials provided with the distribution.
00012  * 3. The name of the author may not be used to endorse or promote products
00013  *    derived from this software without specific prior written permission.
00014  * 
00015  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00016  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00017  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00018  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00019  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00020  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00021  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00022  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00023  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00024  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025  */
00026 
00027 #include "log4c.h"
00028 #include <stdio.h>
00029 
00030 /**
00031  * The root category's default logging function.
00032  */
00033 
00034 static char *priorityNames[] = {
00035     "Zero Priority",
00036     "TRACE",
00037     "DEBUG",
00038     "INFO",
00039     "NOTICE",
00040     "WARNING",
00041     "ERROR",
00042     "CRITICAL ERROR",
00043     "ALERT",
00044     "EMERGENCY",
00045 };
00046 
00047 static void doAppend(struct LogAppender* this, struct LogEvent* ev);
00048 
00049 static struct DefaultLogAppender {
00050     struct LogAppender appender;
00051     FILE *file;
00052     int printLoc;
00053 } defaultLogAppender = { { doAppend }, NULL, 1 } ;
00054 
00055 struct LogAppender* log_defaultLogAppender  = &defaultLogAppender.appender;
00056 
00057 static void doAppend(struct LogAppender* this0, struct LogEvent* ev) {
00058 
00059     // TODO: define a format field in struct for timestamp, etc.
00060     char *pn = NULL;
00061     char buf[20];
00062     struct DefaultLogAppender* this = (struct DefaultLogAppender*)this0;
00063     
00064     if (this->file == NULL) this->file = stderr;
00065     
00066     if (ev->priority < 0) {
00067         pn = "???";
00068     }
00069     else if ((size_t)ev->priority < sizeof(priorityNames)) {
00070         pn = priorityNames[ev->priority];
00071     } else {
00072         sprintf(buf, "%s+%d",
00073                 priorityNames[sizeof(priorityNames)-1],
00074                 ev->priority - (int) sizeof(priorityNames) + 1);
00075     }
00076     fprintf(this->file, "%-7s ", pn);
00077     if (this->printLoc)
00078         fprintf(this->file, "%s:%d:%s\t",
00079                 ev->fileName, ev->lineNum, ev->functionName);
00080     else
00081         fprintf(this->file, "%s: ",
00082                 ev->cat->name);
00083     vfprintf(this->file, ev->fmt, ev->ap);
00084     fprintf(this->file, "\n");
00085 }

Generated on Fri Sep 28 00:35:45 2007 for raceintospace by  doxygen 1.5.3