| 1 |
adx |
61 |
/* |
| 2 |
|
|
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
| 3 |
|
|
* s_log.c: Logger functions. |
| 4 |
|
|
* |
| 5 |
|
|
* Copyright (C) 2002 by the past and present ircd coders, and others. |
| 6 |
|
|
* |
| 7 |
|
|
* This program is free software; you can redistribute it and/or modify |
| 8 |
|
|
* it under the terms of the GNU General Public License as published by |
| 9 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
|
|
* (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, MA 02111-1307 |
| 20 |
|
|
* USA |
| 21 |
|
|
* |
| 22 |
|
|
* $Id: s_log.c 33 2005-10-02 20:50:00Z knight $ |
| 23 |
|
|
*/ |
| 24 |
|
|
|
| 25 |
|
|
#include "stdinc.h" |
| 26 |
|
|
|
| 27 |
|
|
#ifdef USE_SYSLOG |
| 28 |
|
|
# ifdef HAVE_SYS_SYSLOG_H |
| 29 |
|
|
# include <sys/syslog.h> |
| 30 |
|
|
# else |
| 31 |
|
|
# ifdef HAVE_SYSLOG_H |
| 32 |
|
|
# include <syslog.h> |
| 33 |
|
|
# endif |
| 34 |
|
|
# endif |
| 35 |
|
|
#endif |
| 36 |
|
|
|
| 37 |
|
|
#include "s_log.h" |
| 38 |
|
|
#include "fileio.h" |
| 39 |
|
|
#include "irc_string.h" |
| 40 |
|
|
#include "sprintf_irc.h" |
| 41 |
|
|
#include "s_misc.h" |
| 42 |
|
|
#include "event.h" /* Needed for EVH etc. */ |
| 43 |
|
|
#include "memory.h" |
| 44 |
|
|
|
| 45 |
|
|
/* some older syslogs would overflow at 2024 */ |
| 46 |
|
|
#define LOG_BUFSIZE 2000 |
| 47 |
|
|
|
| 48 |
adx |
65 |
static FBFILE *logFile = NULL; |
| 49 |
adx |
61 |
static int logLevel = INIT_LOG_LEVEL; |
| 50 |
|
|
|
| 51 |
|
|
#ifndef SYSLOG_USERS |
| 52 |
|
|
static EVH user_log_resync; |
| 53 |
adx |
65 |
FBFILE *user_log_fb = NULL; |
| 54 |
adx |
61 |
#endif |
| 55 |
|
|
|
| 56 |
|
|
#ifdef USE_SYSLOG |
| 57 |
|
|
static const int sysLogLevel[] = |
| 58 |
|
|
{ |
| 59 |
|
|
LOG_CRIT, |
| 60 |
|
|
LOG_ERR, |
| 61 |
|
|
LOG_WARNING, |
| 62 |
|
|
LOG_NOTICE, |
| 63 |
|
|
LOG_INFO, |
| 64 |
|
|
LOG_INFO, |
| 65 |
|
|
LOG_INFO |
| 66 |
|
|
}; |
| 67 |
|
|
#endif |
| 68 |
|
|
|
| 69 |
|
|
static const char *logLevelToString[] = |
| 70 |
|
|
{ |
| 71 |
|
|
"L_CRIT", |
| 72 |
|
|
"L_ERROR", |
| 73 |
|
|
"L_WARN", |
| 74 |
|
|
"L_NOTICE", |
| 75 |
|
|
"L_TRACE", |
| 76 |
|
|
"L_INFO", |
| 77 |
|
|
"L_DEBUG" |
| 78 |
|
|
}; |
| 79 |
|
|
|
| 80 |
|
|
/* |
| 81 |
|
|
* open_log - open ircd logging file |
| 82 |
|
|
* returns true (1) if successful, false (0) otherwise |
| 83 |
|
|
*/ |
| 84 |
|
|
static int |
| 85 |
|
|
open_log(const char *filename) |
| 86 |
|
|
{ |
| 87 |
|
|
logFile = fbopen(filename, "a"); |
| 88 |
|
|
|
| 89 |
|
|
if (logFile == NULL) |
| 90 |
|
|
{ |
| 91 |
|
|
#ifdef USE_SYSLOG |
| 92 |
|
|
syslog(LOG_ERR, "Unable to open log file: %s: %s", |
| 93 |
|
|
filename, strerror(errno)); |
| 94 |
|
|
#endif |
| 95 |
|
|
return (0); |
| 96 |
|
|
} |
| 97 |
|
|
|
| 98 |
|
|
return (1); |
| 99 |
|
|
} |
| 100 |
|
|
|
| 101 |
|
|
static void |
| 102 |
|
|
write_log(const char *message) |
| 103 |
|
|
{ |
| 104 |
|
|
char buf[LOG_BUFSIZE]; |
| 105 |
|
|
size_t nbytes = 0; |
| 106 |
|
|
|
| 107 |
|
|
if (logFile == NULL) |
| 108 |
|
|
return; |
| 109 |
|
|
|
| 110 |
|
|
#ifdef _WIN32 |
| 111 |
|
|
nbytes = snprintf(buf, sizeof(buf), "[%s] %s\r\n", |
| 112 |
|
|
#else |
| 113 |
|
|
nbytes = snprintf(buf, sizeof(buf), "[%s] %s\n", |
| 114 |
|
|
#endif |
| 115 |
|
|
smalldate(CurrentTime), message); |
| 116 |
|
|
fbputs(buf, logFile, nbytes); |
| 117 |
|
|
} |
| 118 |
|
|
|
| 119 |
|
|
void |
| 120 |
|
|
ilog(const int priority, const char *fmt, ...) |
| 121 |
|
|
{ |
| 122 |
|
|
char buf[LOG_BUFSIZE]; |
| 123 |
|
|
va_list args; |
| 124 |
|
|
|
| 125 |
|
|
assert(priority > -1); |
| 126 |
|
|
|
| 127 |
|
|
if (fmt == NULL) |
| 128 |
|
|
return; |
| 129 |
|
|
|
| 130 |
|
|
if (priority > logLevel) |
| 131 |
|
|
return; |
| 132 |
|
|
|
| 133 |
|
|
va_start(args, fmt); |
| 134 |
|
|
vsprintf(buf, fmt, args); |
| 135 |
|
|
va_end(args); |
| 136 |
|
|
|
| 137 |
|
|
#ifdef USE_SYSLOG |
| 138 |
|
|
if (priority <= L_DEBUG) |
| 139 |
|
|
syslog(sysLogLevel[priority], "%s", buf); |
| 140 |
|
|
#endif |
| 141 |
adx |
65 |
write_log(buf); |
| 142 |
adx |
61 |
} |
| 143 |
|
|
|
| 144 |
|
|
void |
| 145 |
|
|
init_log(const char *filename) |
| 146 |
|
|
{ |
| 147 |
|
|
open_log(filename); |
| 148 |
|
|
#ifdef USE_SYSLOG |
| 149 |
|
|
openlog("ircd", LOG_PID | LOG_NDELAY, LOG_FACILITY); |
| 150 |
|
|
#endif |
| 151 |
|
|
#ifndef SYSLOG_USERS |
| 152 |
|
|
eventAddIsh("user_log_resync", user_log_resync, NULL, 60); |
| 153 |
|
|
#endif |
| 154 |
|
|
} |
| 155 |
|
|
|
| 156 |
|
|
void |
| 157 |
|
|
reopen_log(const char *filename) |
| 158 |
|
|
{ |
| 159 |
|
|
if (logFile != NULL) |
| 160 |
|
|
fbclose(logFile); |
| 161 |
|
|
open_log(filename); |
| 162 |
|
|
} |
| 163 |
|
|
|
| 164 |
|
|
void |
| 165 |
|
|
set_log_level(const int level) |
| 166 |
|
|
{ |
| 167 |
|
|
if (L_ERROR < level && level <= L_DEBUG) |
| 168 |
|
|
logLevel = level; |
| 169 |
|
|
} |
| 170 |
|
|
|
| 171 |
|
|
int |
| 172 |
|
|
get_log_level(void) |
| 173 |
|
|
{ |
| 174 |
|
|
return(logLevel); |
| 175 |
|
|
} |
| 176 |
|
|
|
| 177 |
|
|
const char * |
| 178 |
|
|
get_log_level_as_string(int level) |
| 179 |
|
|
{ |
| 180 |
|
|
if (level > L_DEBUG) |
| 181 |
|
|
level = L_DEBUG; |
| 182 |
|
|
else if (level < L_ERROR) |
| 183 |
|
|
level = L_ERROR; |
| 184 |
|
|
|
| 185 |
|
|
return(logLevelToString[level]); |
| 186 |
|
|
} |
| 187 |
|
|
|
| 188 |
|
|
#ifndef SYSLOG_USERS |
| 189 |
|
|
/* user_log_resync() |
| 190 |
|
|
* |
| 191 |
|
|
* inputs - NONE |
| 192 |
|
|
* output - NONE |
| 193 |
|
|
* side effects - |
| 194 |
|
|
*/ |
| 195 |
|
|
static void |
| 196 |
|
|
user_log_resync(void *notused) |
| 197 |
|
|
{ |
| 198 |
|
|
if (user_log_fb != NULL) |
| 199 |
|
|
{ |
| 200 |
|
|
fbclose(user_log_fb); |
| 201 |
|
|
user_log_fb = NULL; |
| 202 |
|
|
} |
| 203 |
|
|
} |
| 204 |
|
|
#endif |