| 1 |
/************************************************************************ |
| 2 |
* log.c dopewars - logging functions * |
| 3 |
* Copyright (C) 1998-2022 Ben Webb * |
| 4 |
* Email: [email protected] * |
| 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 <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <stdarg.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <time.h> |
| 30 |
#include <glib.h> |
| 31 |
#ifdef HAVE_SYSLOG_H |
| 32 |
#include <syslog.h> |
| 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 |
#ifdef HAVE_LOCALTIME_R |
| 84 |
struct tm tmbuf; |
| 85 |
#endif |
| 86 |
|
| 87 |
text = g_string_new(""); |
| 88 |
if (Log.Timestamp) { |
| 89 |
tim = time(NULL); |
| 90 |
#ifdef HAVE_LOCALTIME_R |
| 91 |
timep = localtime_r(&tim, &tmbuf); |
| 92 |
#else |
| 93 |
timep = localtime(&tim); |
| 94 |
#endif |
| 95 |
strftime(TimeBuf, 80, Log.Timestamp, timep); |
| 96 |
TimeBuf[79] = '\0'; |
| 97 |
g_string_append(text, TimeBuf); |
| 98 |
} |
| 99 |
|
| 100 |
for (i = 0; i < MAXLOG; i++) |
| 101 |
if (log_level & (1 << (G_LOG_LEVEL_USER_SHIFT + i))) { |
| 102 |
if (i > Log.Level) { |
| 103 |
g_string_free(text, TRUE); |
| 104 |
return NULL; |
| 105 |
} |
| 106 |
g_string_append_printf(text, "%d: ", i); |
| 107 |
} |
| 108 |
g_string_append(text, message); |
| 109 |
return text; |
| 110 |
} |
| 111 |
|
| 112 |
void OpenLog(void) |
| 113 |
{ |
| 114 |
CloseLog(); |
| 115 |
#ifdef HAVE_SYSLOG_H |
| 116 |
openlog(PACKAGE, LOG_PID, LOG_USER); |
| 117 |
#endif |
| 118 |
if (Log.File[0] == '\0') |
| 119 |
return; |
| 120 |
Log.fp = fopen(Log.File, "a"); |
| 121 |
if (Log.fp) { |
| 122 |
#ifdef SETVBUF_REVERSED /* 2nd and 3rd arguments are reversed on |
| 123 |
* some systems */ |
| 124 |
setvbuf(Log.fp, _IOLBF, (char *)NULL, 0); |
| 125 |
#else |
| 126 |
setvbuf(Log.fp, (char *)NULL, _IOLBF, 0); |
| 127 |
#endif |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
void CloseLog(void) |
| 132 |
{ |
| 133 |
if (Log.fp) |
| 134 |
fclose(Log.fp); |
| 135 |
Log.fp = NULL; |
| 136 |
} |