tlog.c - vaccinewars - be a doctor and try to vaccinate the world
git clone git://src.adamsgaard.dk/vaccinewars
Log
Files
Refs
README
LICENSE
---
tlog.c (3870B)
---
     1 /************************************************************************
     2  * log.c          dopewars - logging functions                          *
     3  * Copyright (C)  1998-2021  Ben Webb                                   *
     4  *                Email: benwebb@users.sf.net                           *
     5  *                WWW: https://dopewars.sourceforge.io/                 *
     6  *                                                                      *
     7  * This program is free software; you can redistribute it and/or        *
     8  * modify it under the terms of the GNU General Public License          *
     9  * as published by the Free Software Foundation; either version 2       *
    10  * of the License, or (at your option) any later version.               *
    11  *                                                                      *
    12  * This program is distributed in the hope that it will be useful,      *
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
    15  * GNU General Public License for more details.                         *
    16  *                                                                      *
    17  * You should have received a copy of the GNU General Public License    *
    18  * along with this program; if not, write to the Free Software          *
    19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,               *
    20  *                   MA  02111-1307, USA.                               *
    21  ************************************************************************/
    22 
    23 #ifdef HAVE_CONFIG_H
    24 #include 
    25 #endif
    26 
    27 #include 
    28 #include 
    29 #include 
    30 #include 
    31 #ifdef HAVE_SYSLOG_H
    32 #include 
    33 #endif
    34 
    35 #include "dopewars.h"
    36 #include "log.h"
    37 
    38 /* 
    39  * General logging function. All messages should be given a loglevel,
    40  * from 0 to 5 (0=vital, 2=normal, 5=maximum debugging output). This
    41  * is essentially just a wrapper around the GLib g_log function.
    42  */
    43 void dopelog(const int loglevel, const LogFlags flags,
    44              const gchar *format, ...)
    45 {
    46   va_list args;
    47 
    48   /* Don't print server log messages when running standalone */
    49   if (flags & LF_SERVER && !Network)
    50     return;
    51 
    52   va_start(args, format);
    53   g_logv(G_LOG_DOMAIN, 1 << (loglevel + G_LOG_LEVEL_USER_SHIFT), format, args);
    54   va_end(args);
    55 
    56 #ifdef HAVE_SYSLOG_H
    57   if (loglevel <= Log.Level) {
    58     va_start(args, format);
    59     vsyslog(LOG_INFO, format, args);
    60     va_end(args);
    61   }
    62 #endif
    63 }
    64 
    65 /* 
    66  * Returns the bitmask necessary to catch all custom log messages.
    67  */
    68 GLogLevelFlags LogMask()
    69 {
    70   return ((1 << (MAXLOG)) - 1) << G_LOG_LEVEL_USER_SHIFT;
    71 }
    72 
    73 /* 
    74  * Returns the text to be displayed in a log message, if any.
    75  */
    76 GString *GetLogString(GLogLevelFlags log_level, const gchar *message)
    77 {
    78   GString *text;
    79   gchar TimeBuf[80];
    80   gint i;
    81   time_t tim;
    82   struct tm *timep;
    83 
    84   text = g_string_new("");
    85   if (Log.Timestamp) {
    86     tim = time(NULL);
    87     timep = localtime(&tim);
    88     strftime(TimeBuf, 80, Log.Timestamp, timep);
    89     TimeBuf[79] = '\0';
    90     g_string_append(text, TimeBuf);
    91   }
    92 
    93   for (i = 0; i < MAXLOG; i++)
    94     if (log_level & (1 << (G_LOG_LEVEL_USER_SHIFT + i))) {
    95       if (i > Log.Level) {
    96         g_string_free(text, TRUE);
    97         return NULL;
    98       }
    99       g_string_append_printf(text, "%d: ", i);
   100     }
   101   g_string_append(text, message);
   102   return text;
   103 }
   104 
   105 void OpenLog(void)
   106 {
   107   CloseLog();
   108 #ifdef HAVE_SYSLOG_H
   109   openlog(PACKAGE, LOG_PID, LOG_USER);
   110 #endif
   111   if (Log.File[0] == '\0')
   112     return;
   113   Log.fp = fopen(Log.File, "a");
   114   if (Log.fp) {
   115 #ifdef SETVBUF_REVERSED         /* 2nd and 3rd arguments are reversed on
   116                                  * some systems */
   117     setvbuf(Log.fp, _IOLBF, (char *)NULL, 0);
   118 #else
   119     setvbuf(Log.fp, (char *)NULL, _IOLBF, 0);
   120 #endif
   121   }
   122 }
   123 
   124 void CloseLog(void)
   125 {
   126   if (Log.fp)
   127     fclose(Log.fp);
   128   Log.fp = NULL;
   129 }