home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / BBS / DOORL093.ZIP / doorlib-0.9.3.tar.bz2 / doorlib-0.9.3.tar / doorlib-0.9.3 / src / logger.c < prev    next >
C/C++ Source or Header  |  2003-12-21  |  3KB  |  119 lines

  1. /*****************************************************************************
  2.  *
  3.  * $Id: logger.c,v 1.7 2003/12/21 13:04:15 mbroek Exp $
  4.  * Purpose ...............: Logging functions
  5.  *
  6.  *****************************************************************************
  7.  * Copyright (C) 2003 
  8.  *   
  9.  * Michiel Broek        FIDO:        2:280/2802
  10.  * Beekmansbos 10
  11.  * 1971 BV IJmuiden
  12.  * the Netherlands
  13.  *
  14.  * This file is part of doorlib for Unix.
  15.  *
  16.  * This BBS is free software; you can redistribute it and/or modify it
  17.  * under the terms of the GNU General Public License as published by the
  18.  * Free Software Foundation; either version 2, or (at your option) any
  19.  * later version.
  20.  *
  21.  * MBSE BBS is distributed in the hope that it will be useful, but
  22.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  24.  * General Public License for more details.
  25.  * 
  26.  * You should have received a copy of the GNU General Public License
  27.  * along with MBSE BBS; see the file COPYING.  If not, write to the Free
  28.  * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  29.  *****************************************************************************/
  30.  
  31. #include "../config.h"
  32. #include "door.h"
  33.  
  34.  
  35. char        *_door_logfile = NULL;
  36. char        *_door_doorname = NULL;
  37. int        _door_logdebug = FALSE;
  38.  
  39.  
  40. static char *_mon[] = {
  41.     (char *)"Jan",(char *)"Feb",(char *)"Mar", (char *)"Apr",(char *)"May",(char *)"Jun",
  42.     (char *)"Jul",(char *)"Aug",(char *)"Sep", (char *)"Oct",(char *)"Nov",(char *)"Dec"
  43. };
  44.  
  45.  
  46.  
  47. char *date(void);
  48. char *date(void)
  49. {
  50.     struct tm   ptm;
  51.     time_t      now;
  52.     static char buf[20];
  53.  
  54.     now = time(NULL);
  55.     ptm = *localtime(&now);
  56.     sprintf(buf,"%02d-%s-%04d %02d:%02d:%02d", ptm.tm_mday, _mon[ptm.tm_mon], ptm.tm_year+1900,
  57.         ptm.tm_hour, ptm.tm_min, ptm.tm_sec);
  58.     return(buf);
  59. }
  60.  
  61.  
  62.  
  63. int door_loginit(char *fname, char *dname, int debug)
  64. {
  65.     FILE    *fp;
  66.  
  67.     _door_logfile  = door_xstrcpy(fname);
  68.     _door_doorname = door_xstrcpy(dname);
  69.     _door_logdebug = debug;
  70.  
  71.     if ((fp = fopen(_door_logfile, "a")) == NULL)
  72.     return FALSE;
  73.  
  74.     fclose(fp);
  75.     return TRUE;
  76. }
  77.  
  78.  
  79.  
  80. void door_log(int grade, const char *format, ...)
  81. {
  82.     va_list va_ptr;
  83.     char    outstr[1024];
  84.     FILE    *fp;
  85.  
  86.     if (_door_logfile == NULL) {
  87.     /*
  88.      * Logfile not set or not available
  89.      */
  90.     return;
  91.     }
  92.     
  93.     if (grade == '+' || grade == '-' || grade == '!' || grade == '?' || grade == ' ' || _door_logdebug) {
  94.     va_start(va_ptr, format);
  95.     vsprintf(outstr, format, va_ptr);
  96.     va_end(va_ptr);
  97.  
  98.     if ((fp = fopen(_door_logfile, "a")) == NULL)
  99.         return;
  100.  
  101.     fprintf(fp, "%c %s ", grade, date());
  102.     if (_door_doorname)
  103.         fprintf(fp, "%s", _door_doorname);
  104.     else
  105.         fprintf(fp, "doorlib");
  106.     fprintf(fp, "[%d] ", getpid());
  107.     fprintf(fp, *outstr == '$' ? outstr+1 : outstr);
  108.     if (*outstr == '$')
  109.         fprintf(fp, ": %s\n", strerror(errno));
  110.     else
  111.         fprintf(fp, "\n");
  112.  
  113.     fflush(fp);
  114.     fclose(fp);
  115.     chmod(_door_logfile, 0666);
  116.     }
  117. }
  118.  
  119.