nanohttp-logging.c

Go to the documentation of this file.
00001 /******************************************************************
00002 *  $Id: nanohttp-logging.c,v 1.2 2006/11/25 17:03:20 m0gg Exp $
00003 *
00004 * CSOAP Project:  A http client/server library in C
00005 * Copyright (C) 2003  Ferhat Ayaz
00006 *
00007 * This library is free software; you can redistribute it and/or
00008 * modify it under the terms of the GNU Library General Public
00009 * License as published by the Free Software Foundation; either
00010 * version 2 of the License, or (at your option) any later version.
00011 *
00012 * This library is distributed in the hope that it will be useful,
00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 * Library General Public License for more details.
00016 *
00017 * You should have received a copy of the GNU Library General Public
00018 * License along with this library; if not, write to the
00019 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020 * Boston, MA  02111-1307, USA.
00021 *
00022 * Email: ayaz@jprogrammer.net
00023 ******************************************************************/
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #ifdef HAVE_STDIO_H
00029 #include <stdio.h>
00030 #endif
00031 
00032 #ifdef HAVE_STDLIB_H
00033 #include <stdlib.h>
00034 #endif
00035 
00036 #ifdef HAVE_STDARG_H
00037 #include <stdarg.h>
00038 #endif
00039 
00040 #ifdef HAVE_STRING_H
00041 #include <string.h>
00042 #endif
00043 
00044 #ifdef HAVE_CTYPE_H
00045 #include <ctype.h>
00046 #endif
00047 
00048 #ifdef HAVE_PTHREAD_H
00049 #include <pthread.h>
00050 #endif
00051 
00052 #include "nanohttp-logging.h"
00053 
00054 #ifdef WIN32
00055 #ifndef __MINGW32__
00056 
00057 /* not thread safe!*/
00058 char *
00059 VisualC_funcname(const char *file, int line)
00060 {
00061   static char buffer[256];
00062   int i = strlen(file) - 1;
00063   while (i > 0 && file[i] != '\\')
00064     i--;
00065   sprintf(buffer, "%s:%d", (file[i] != '\\') ? file : (file + i + 1), line);
00066   return buffer;
00067 }
00068 
00069 #endif
00070 #endif
00071 
00072 static log_level_t loglevel = HLOG_DEBUG;
00073 static char logfile[75] = { '\0' };
00074 static int log_background = 0;
00075 
00076 log_level_t
00077 hlog_set_level(log_level_t level)
00078 {
00079   log_level_t old = loglevel;
00080   loglevel = level;
00081   return old;
00082 }
00083 
00084 log_level_t
00085 hlog_get_level(void)
00086 {
00087   return loglevel;
00088 }
00089 
00090 void
00091 hlog_set_file(const char *filename)
00092 {
00093   if (filename)
00094     strncpy(logfile, filename, 75);
00095   else
00096     logfile[0] = '\0';
00097 
00098   return;
00099 }
00100 
00101 void
00102 hlog_set_background(int state)
00103 {
00104   log_background = state;
00105 
00106   return;
00107 }
00108 
00109 char *
00110 hlog_get_file(void)
00111 {
00112   if (logfile[0] == '\0')
00113     return NULL;
00114   return logfile;
00115 }
00116 
00117 static void
00118 _log_write(log_level_t level, const char *prefix,
00119           const char *func, const char *format, va_list ap)
00120 {
00121   char buffer[1054];
00122   char buffer2[1054];
00123   FILE *f;
00124 
00125   if (level < loglevel)
00126     return;
00127 
00128   if (!log_background || hlog_get_file())
00129   {
00130 #ifdef WIN32
00131     sprintf(buffer, "*%s*: [%s] %s\n", prefix, func, format);
00132 #else
00133     sprintf(buffer, "*%s*:(%ld) [%s] %s\n",
00134             prefix, pthread_self(), func, format);
00135 #endif
00136     vsprintf(buffer2, buffer, ap);
00137     if (!log_background)
00138     {
00139       printf(buffer2);
00140       fflush(stdout);
00141     }
00142 
00143     if (hlog_get_file())
00144     {
00145       f = fopen(hlog_get_file(), "a");
00146       if (!f)
00147         f = fopen(hlog_get_file(), "w");
00148       if (f)
00149       {
00150         fprintf(f, buffer2);
00151         fflush(f);
00152         fclose(f);
00153       }
00154     }
00155   }
00156 
00157   return;
00158 }
00159 
00160 void
00161 hlog_verbose(const char *FUNC, const char *format, ...)
00162 {
00163   va_list ap;
00164 
00165   va_start(ap, format);
00166   _log_write(HLOG_VERBOSE, "VERBOSE", FUNC, format, ap);
00167   va_end(ap);
00168 }
00169 
00170 void
00171 hlog_debug(const char *FUNC, const char *format, ...)
00172 {
00173   va_list ap;
00174 
00175   va_start(ap, format);
00176   _log_write(HLOG_DEBUG, "DEBUG", FUNC, format, ap);
00177   va_end(ap);
00178 }
00179 
00180 void
00181 hlog_info(const char *FUNC, const char *format, ...)
00182 {
00183   va_list ap;
00184 
00185   va_start(ap, format);
00186   _log_write(HLOG_INFO, "INFO", FUNC, format, ap);
00187   va_end(ap);
00188 }
00189 
00190 void
00191 hlog_warn(const char *FUNC, const char *format, ...)
00192 {
00193   va_list ap;
00194 
00195   va_start(ap, format);
00196   _log_write(HLOG_WARN, "WARN", FUNC, format, ap);
00197   va_end(ap);
00198 }
00199 
00200 void
00201 hlog_error(const char *FUNC, const char *format, ...)
00202 {
00203   va_list ap;
00204 
00205   va_start(ap, format);
00206   _log_write(HLOG_ERROR, "ERROR", FUNC, format, ap);
00207   va_end(ap);
00208 }
00209 

Generated on Thu Jan 25 23:36:02 2007 for csoap by  doxygen 1.4.6