tLogging functions moved into a separate module. - vaccinewars - be a doctor and try to vaccinate the world
 (HTM) git clone git://src.adamsgaard.dk/vaccinewars
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 509602a28911593055ddfce22972b3736b8271b1
 (DIR) parent 010239fbe0cf8f453f0f09a0af10bb090e977997
 (HTM) Author: Ben Webb <ben@salilab.org>
       Date:   Fri, 24 May 2002 11:35:23 +0000
       
       Logging functions moved into a separate module.
       
       
       Diffstat:
         M src/Makefile.am                     |       6 +++---
         M src/dopewars.c                      |      98 +------------------------------
         M src/dopewars.h                      |      10 ----------
         A src/log.c                           |     130 +++++++++++++++++++++++++++++++
         A src/log.h                           |      43 ++++++++++++++++++++++++++++++
       
       5 files changed, 177 insertions(+), 110 deletions(-)
       ---
 (DIR) diff --git a/src/Makefile.am b/src/Makefile.am
       t@@ -25,9 +25,9 @@ dopewars_DEPENDENCIES = @GUILIB@ @CURSESLIB@ @GTKPORTLIB@ @CURSESPORTLIB@ @INTLL
        
        bin_PROGRAMS = dopewars
        dopewars_SOURCES = admin.c admin.h AIPlayer.c AIPlayer.h util.c util.h \
       -                   dopewars.c dopewars.h error.c error.h message.c message.h \
       -                   network.c network.h nls.h serverside.c serverside.h \
       -                   sound.c sound.h \
       +                   dopewars.c dopewars.h error.c error.h log.c log.h \
       +                   message.c message.h network.c network.h nls.h \
       +                   serverside.c serverside.h sound.c sound.h \
                           tstring.c tstring.h winmain.c winmain.h
        INCLUDES   = -I../intl -I${srcdir} -I.. @GTK_CFLAGS@
        DEFS       = @DEFS@ -DLOCALEDIR=\"${localedir}\" @PLUGINDEF@
 (DIR) diff --git a/src/dopewars.c b/src/dopewars.c
       t@@ -42,10 +42,8 @@
        #include <errno.h>
        #include <glib.h>
        #include <stdarg.h>
       -#ifdef HAVE_SYSLOG_H
       -#include <syslog.h>
       -#endif
        #include "admin.h"
       +#include "log.h"
        #include "message.h"
        #include "nls.h"
        #include "serverside.h"
       t@@ -1454,42 +1452,6 @@ void RemoveAllEntries(DopeList *List, Player *Play)
          } while (i >= 0);
        }
        
       -/* 
       - * General logging function. All messages should be given a loglevel,
       - * from 0 to 5 (0=vital, 2=normal, 5=maximum debugging output). This
       - * is essentially just a wrapper around the GLib g_log function.
       - */
       -void dopelog(const int loglevel, const LogFlags flags,
       -             const gchar *format, ...)
       -{
       -  va_list args;
       -
       -  /* Don't print server log messages when running standalone */
       -  if (flags & LF_SERVER && !Network)
       -    return;
       -
       -  va_start(args, format);
       -  g_logv(G_LOG_DOMAIN, 1 << (loglevel + G_LOG_LEVEL_USER_SHIFT), format,
       -         args);
       -  va_end(args);
       -
       -#ifdef HAVE_SYSLOG_H
       -  if (loglevel <= Log.Level) {
       -    va_start(args, format);
       -    vsyslog(LOG_INFO, format, args);
       -    va_end(args);
       -  }
       -#endif
       -}
       -
       -/* 
       - * Returns the bitmask necessary to catch all custom log messages.
       - */
       -GLogLevelFlags LogMask()
       -{
       -  return ((1 << (MAXLOG)) - 1) << G_LOG_LEVEL_USER_SHIFT;
       -}
       -
        void ResizeLocations(int NewNum)
        {
          int i;
       t@@ -2764,64 +2726,6 @@ void StripTerminators(gchar *str)
          }
        }
        
       -/* 
       - * Returns the text to be displayed in a log message, if any.
       - */
       -GString *GetLogString(GLogLevelFlags log_level, const gchar *message)
       -{
       -  GString *text;
       -  gchar TimeBuf[80];
       -  gint i;
       -  time_t tim;
       -  struct tm *timep;
       -
       -  text = g_string_new("");
       -  if (Log.Timestamp) {
       -    tim = time(NULL);
       -    timep = localtime(&tim);
       -    strftime(TimeBuf, 80, Log.Timestamp, timep);
       -    TimeBuf[79] = '\0';
       -    g_string_append(text, TimeBuf);
       -  }
       -
       -  for (i = 0; i < MAXLOG; i++)
       -    if (log_level & (1 << (G_LOG_LEVEL_USER_SHIFT + i))) {
       -      if (i > Log.Level) {
       -        g_string_free(text, TRUE);
       -        return NULL;
       -      }
       -      g_string_sprintfa(text, "%d: ", i);
       -    }
       -  g_string_append(text, message);
       -  return text;
       -}
       -
       -void OpenLog(void)
       -{
       -  CloseLog();
       -#ifdef HAVE_SYSLOG_H
       -  openlog(PACKAGE, LOG_PID, LOG_USER);
       -#endif
       -  if (Log.File[0] == '\0')
       -    return;
       -  Log.fp = fopen(Log.File, "a");
       -  if (Log.fp) {
       -#ifdef SETVBUF_REVERSED         /* 2nd and 3rd arguments are reversed on
       -                                 * some systems */
       -    setvbuf(Log.fp, _IOLBF, (char *)NULL, 0);
       -#else
       -    setvbuf(Log.fp, (char *)NULL, _IOLBF, 0);
       -#endif
       -  }
       -}
       -
       -void CloseLog(void)
       -{
       -  if (Log.fp)
       -    fclose(Log.fp);
       -  Log.fp = NULL;
       -}
       -
        #ifndef CYGWIN
        
        #if NETWORKING && !GUI_SERVER
 (DIR) diff --git a/src/dopewars.h b/src/dopewars.h
       t@@ -154,10 +154,6 @@ typedef enum {
          PEYOTE, SHROOMS, SPEED, WEED
        } DrugIndex;
        
       -typedef enum {
       -  LF_SERVER = (1 << 0)
       -} LogFlags;
       -
        struct LOG {
          gchar *File;
          gint Level;
       t@@ -428,14 +424,8 @@ void PrintConfigValue(int GlobalIndex, int StructIndex,
        gboolean SetConfigValue(int GlobalIndex, int StructIndex,
                                gboolean IndexGiven, GScanner *scanner);
        gboolean IsCop(Player *Play);
       -void dopelog(const int loglevel, const LogFlags flags,
       -             const gchar *format, ...);
       -GLogLevelFlags LogMask(void);
       -GString *GetLogString(GLogLevelFlags log_level, const gchar *message);
        void RestoreConfig(void);
        void ScannerErrorHandler(GScanner *scanner, gchar *msg, gint error);
       -void OpenLog(void);
       -void CloseLog(void);
        gboolean IsConnectedPlayer(Player *play);
        void BackupConfig(void);
        void WriteConfigFile(FILE *fp);
 (DIR) diff --git a/src/log.c b/src/log.c
       t@@ -0,0 +1,130 @@
       +/************************************************************************
       + * log.c          dopewars - logging functions                          *
       + * Copyright (C)  1998-2002  Ben Webb                                   *
       + *                Email: ben@bellatrix.pcl.ox.ac.uk                     *
       + *                WWW: http://dopewars.sourceforge.net/                 *
       + *                                                                      *
       + * This program is free software; you can redistribute it and/or        *
       + * modify it under the terms of the GNU General Public License          *
       + * as published by the Free Software Foundation; either version 2       *
       + * of the License, or (at your option) any later version.               *
       + *                                                                      *
       + * This program is distributed in the hope that it will be useful,      *
       + * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
       + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
       + * GNU General Public License for more details.                         *
       + *                                                                      *
       + * You should have received a copy of the GNU General Public License    *
       + * along with this program; if not, write to the Free Software          *
       + * Foundation, Inc., 59 Temple Place - Suite 330, Boston,               *
       + *                   MA  02111-1307, USA.                               *
       + ************************************************************************/
       +
       +#ifdef HAVE_CONFIG_H
       +#include <config.h>
       +#endif
       +
       +#include <stdarg.h>
       +#include <stdio.h>
       +#include <time.h>
       +#include <glib.h>
       +#ifdef HAVE_SYSLOG_H
       +#include <syslog.h>
       +#endif
       +
       +#include "dopewars.h"
       +#include "log.h"
       +
       +/* 
       + * General logging function. All messages should be given a loglevel,
       + * from 0 to 5 (0=vital, 2=normal, 5=maximum debugging output). This
       + * is essentially just a wrapper around the GLib g_log function.
       + */
       +void dopelog(const int loglevel, const LogFlags flags,
       +             const gchar *format, ...)
       +{
       +  va_list args;
       +
       +  /* Don't print server log messages when running standalone */
       +  if (flags & LF_SERVER && !Network)
       +    return;
       +
       +  va_start(args, format);
       +  g_logv(G_LOG_DOMAIN, 1 << (loglevel + G_LOG_LEVEL_USER_SHIFT), format,
       +         args);
       +  va_end(args);
       +
       +#ifdef HAVE_SYSLOG_H
       +  if (loglevel <= Log.Level) {
       +    va_start(args, format);
       +    vsyslog(LOG_INFO, format, args);
       +    va_end(args);
       +  }
       +#endif
       +}
       +
       +/* 
       + * Returns the bitmask necessary to catch all custom log messages.
       + */
       +GLogLevelFlags LogMask()
       +{
       +  return ((1 << (MAXLOG)) - 1) << G_LOG_LEVEL_USER_SHIFT;
       +}
       +
       +/* 
       + * Returns the text to be displayed in a log message, if any.
       + */
       +GString *GetLogString(GLogLevelFlags log_level, const gchar *message)
       +{
       +  GString *text;
       +  gchar TimeBuf[80];
       +  gint i;
       +  time_t tim;
       +  struct tm *timep;
       +
       +  text = g_string_new("");
       +  if (Log.Timestamp) {
       +    tim = time(NULL);
       +    timep = localtime(&tim);
       +    strftime(TimeBuf, 80, Log.Timestamp, timep);
       +    TimeBuf[79] = '\0';
       +    g_string_append(text, TimeBuf);
       +  }
       +
       +  for (i = 0; i < MAXLOG; i++)
       +    if (log_level & (1 << (G_LOG_LEVEL_USER_SHIFT + i))) {
       +      if (i > Log.Level) {
       +        g_string_free(text, TRUE);
       +        return NULL;
       +      }
       +      g_string_sprintfa(text, "%d: ", i);
       +    }
       +  g_string_append(text, message);
       +  return text;
       +}
       +
       +void OpenLog(void)
       +{
       +  CloseLog();
       +#ifdef HAVE_SYSLOG_H
       +  openlog(PACKAGE, LOG_PID, LOG_USER);
       +#endif
       +  if (Log.File[0] == '\0')
       +    return;
       +  Log.fp = fopen(Log.File, "a");
       +  if (Log.fp) {
       +#ifdef SETVBUF_REVERSED         /* 2nd and 3rd arguments are reversed on
       +                                 * some systems */
       +    setvbuf(Log.fp, _IOLBF, (char *)NULL, 0);
       +#else
       +    setvbuf(Log.fp, (char *)NULL, _IOLBF, 0);
       +#endif
       +  }
       +}
       +
       +void CloseLog(void)
       +{
       +  if (Log.fp)
       +    fclose(Log.fp);
       +  Log.fp = NULL;
       +}
 (DIR) diff --git a/src/log.h b/src/log.h
       t@@ -0,0 +1,43 @@
       +/************************************************************************
       + * log.h          Logging functions for dopewars                        *
       + * Copyright (C)  1998-2002  Ben Webb                                   *
       + *                Email: ben@bellatrix.pcl.ox.ac.uk                     *
       + *                WWW: http://dopewars.sourceforge.net/                 *
       + *                                                                      *
       + * This program is free software; you can redistribute it and/or        *
       + * modify it under the terms of the GNU General Public License          *
       + * as published by the Free Software Foundation; either version 2       *
       + * of the License, or (at your option) any later version.               *
       + *                                                                      *
       + * This program is distributed in the hope that it will be useful,      *
       + * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
       + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
       + * GNU General Public License for more details.                         *
       + *                                                                      *
       + * You should have received a copy of the GNU General Public License    *
       + * along with this program; if not, write to the Free Software          *
       + * Foundation, Inc., 59 Temple Place - Suite 330, Boston,               *
       + *                   MA  02111-1307, USA.                               *
       + ************************************************************************/
       +
       +#ifndef __DP_LOG_H__
       +#define __DP_LOG_H__
       +
       +#ifdef HAVE_CONFIG_H
       +#include <config.h>
       +#endif
       +
       +#include <glib.h>
       +
       +typedef enum {
       +    LF_SERVER = (1 << 0)
       +} LogFlags;
       +
       +void dopelog(const int loglevel, const LogFlags flags,
       +                 const gchar *format, ...);
       +GLogLevelFlags LogMask(void);
       +GString *GetLogString(GLogLevelFlags log_level, const gchar *message);
       +void OpenLog(void);
       +void CloseLog(void);
       +
       +#endif /* __DP_LOG_H__ */