| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
| 3 |
* 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$ |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "stdinc.h" |
| 26 |
|
| 27 |
#include "log.h" |
| 28 |
#include "irc_string.h" |
| 29 |
#include "ircd.h" |
| 30 |
#include "conf.h" |
| 31 |
#include "s_misc.h" |
| 32 |
|
| 33 |
|
| 34 |
static struct { |
| 35 |
char path[HYB_PATH_MAX + 1]; |
| 36 |
size_t size; |
| 37 |
FILE *file; |
| 38 |
} log_type_table[LOG_TYPE_LAST]; |
| 39 |
|
| 40 |
|
| 41 |
void |
| 42 |
log_set_file(enum log_type type, size_t size, const char *path) |
| 43 |
{ |
| 44 |
strlcpy(log_type_table[type].path, path, sizeof(log_type_table[type].path)); |
| 45 |
log_type_table[type].size = size; |
| 46 |
|
| 47 |
if (type == LOG_TYPE_IRCD) |
| 48 |
log_type_table[type].file = fopen(log_type_table[type].path, "a"); |
| 49 |
} |
| 50 |
|
| 51 |
void |
| 52 |
log_del_all(void) |
| 53 |
{ |
| 54 |
unsigned int type = 0; |
| 55 |
|
| 56 |
while (++type < LOG_TYPE_LAST) |
| 57 |
log_type_table[type].path[0] = '\0'; |
| 58 |
} |
| 59 |
|
| 60 |
void |
| 61 |
log_reopen_all(void) |
| 62 |
{ |
| 63 |
unsigned int type = 0; |
| 64 |
|
| 65 |
while (++type < LOG_TYPE_LAST) |
| 66 |
{ |
| 67 |
if (log_type_table[type].file) |
| 68 |
{ |
| 69 |
fclose(log_type_table[type].file); |
| 70 |
log_type_table[type].file = NULL; |
| 71 |
} |
| 72 |
|
| 73 |
if (log_type_table[type].path[0]) |
| 74 |
log_type_table[type].file = fopen(log_type_table[type].path, "a"); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static int |
| 79 |
log_exceed_size(unsigned int type) |
| 80 |
{ |
| 81 |
struct stat sb; |
| 82 |
|
| 83 |
if (!log_type_table[type].size) |
| 84 |
return 0; |
| 85 |
|
| 86 |
if (stat(log_type_table[type].path, &sb) < 0) |
| 87 |
return -1; |
| 88 |
|
| 89 |
return (size_t)sb.st_size > log_type_table[type].size; |
| 90 |
} |
| 91 |
|
| 92 |
static void |
| 93 |
log_write(enum log_type type, const char *message) |
| 94 |
{ |
| 95 |
char buf[IRCD_BUFSIZE]; |
| 96 |
|
| 97 |
strftime(buf, sizeof(buf), "[%FT%H:%M:%S%z]", localtime(&CurrentTime)); |
| 98 |
|
| 99 |
fprintf(log_type_table[type].file, "%s %s\n", buf, message); |
| 100 |
fflush(log_type_table[type].file); |
| 101 |
} |
| 102 |
|
| 103 |
void |
| 104 |
ilog(enum log_type type, const char *fmt, ...) |
| 105 |
{ |
| 106 |
char buf[LOG_BUFSIZE]; |
| 107 |
va_list args; |
| 108 |
|
| 109 |
if (!log_type_table[type].file || !ConfigLoggingEntry.use_logging) |
| 110 |
return; |
| 111 |
|
| 112 |
va_start(args, fmt); |
| 113 |
vsnprintf(buf, sizeof(buf), fmt, args); |
| 114 |
va_end(args); |
| 115 |
|
| 116 |
log_write(type, buf); |
| 117 |
|
| 118 |
if (log_exceed_size(type) <= 0) |
| 119 |
return; |
| 120 |
|
| 121 |
snprintf(buf, sizeof(buf), "Rotating logfile %s", |
| 122 |
log_type_table[type].path); |
| 123 |
log_write(type, buf); |
| 124 |
|
| 125 |
fclose(log_type_table[type].file); |
| 126 |
log_type_table[type].file = NULL; |
| 127 |
|
| 128 |
snprintf(buf, sizeof(buf), "%s.old", log_type_table[type].path); |
| 129 |
unlink(buf); |
| 130 |
rename(log_type_table[type].path, buf); |
| 131 |
|
| 132 |
log_set_file(type, log_type_table[type].size, log_type_table[type].path); |
| 133 |
log_type_table[type].file = fopen(log_type_table[type].path, "a"); |
| 134 |
} |