| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
* |
| 4 |
* Copyright (c) 1997-2015 ircd-hybrid development team |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 19 |
* USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/*! \file log.c |
| 23 |
* \brief Logger functions. |
| 24 |
* \version $Id$ |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "stdinc.h" |
| 28 |
#include "log.h" |
| 29 |
#include "irc_string.h" |
| 30 |
#include "ircd.h" |
| 31 |
#include "conf.h" |
| 32 |
#include "misc.h" |
| 33 |
|
| 34 |
|
| 35 |
static struct |
| 36 |
{ |
| 37 |
char path[HYB_PATH_MAX + 1]; |
| 38 |
size_t size; |
| 39 |
FILE *file; |
| 40 |
} log_type_table[LOG_TYPE_LAST]; |
| 41 |
|
| 42 |
|
| 43 |
void |
| 44 |
log_set_file(enum log_type type, size_t size, const char *path) |
| 45 |
{ |
| 46 |
strlcpy(log_type_table[type].path, path, sizeof(log_type_table[type].path)); |
| 47 |
log_type_table[type].size = size; |
| 48 |
|
| 49 |
if (type == LOG_TYPE_IRCD) |
| 50 |
log_type_table[type].file = fopen(log_type_table[type].path, "a"); |
| 51 |
} |
| 52 |
|
| 53 |
void |
| 54 |
log_del_all(void) |
| 55 |
{ |
| 56 |
unsigned int type = 0; |
| 57 |
|
| 58 |
while (++type < LOG_TYPE_LAST) |
| 59 |
log_type_table[type].path[0] = '\0'; |
| 60 |
} |
| 61 |
|
| 62 |
void |
| 63 |
log_reopen_all(void) |
| 64 |
{ |
| 65 |
unsigned int type = 0; |
| 66 |
|
| 67 |
while (++type < LOG_TYPE_LAST) |
| 68 |
{ |
| 69 |
if (log_type_table[type].file) |
| 70 |
{ |
| 71 |
fclose(log_type_table[type].file); |
| 72 |
log_type_table[type].file = NULL; |
| 73 |
} |
| 74 |
|
| 75 |
if (log_type_table[type].path[0]) |
| 76 |
log_type_table[type].file = fopen(log_type_table[type].path, "a"); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
static int |
| 81 |
log_exceed_size(unsigned int type) |
| 82 |
{ |
| 83 |
struct stat sb; |
| 84 |
|
| 85 |
if (!log_type_table[type].size) |
| 86 |
return 0; |
| 87 |
|
| 88 |
if (stat(log_type_table[type].path, &sb) < 0) |
| 89 |
return -1; |
| 90 |
|
| 91 |
return (size_t)sb.st_size > log_type_table[type].size; |
| 92 |
} |
| 93 |
|
| 94 |
static void |
| 95 |
log_write(enum log_type type, const char *message) |
| 96 |
{ |
| 97 |
fprintf(log_type_table[type].file, "[%s] %s\n", date_iso8601(0), message); |
| 98 |
fflush(log_type_table[type].file); |
| 99 |
} |
| 100 |
|
| 101 |
void |
| 102 |
ilog(enum log_type type, const char *fmt, ...) |
| 103 |
{ |
| 104 |
char buf[LOG_BUFSIZE] = ""; |
| 105 |
va_list args; |
| 106 |
|
| 107 |
if (!log_type_table[type].file || !ConfigLog.use_logging) |
| 108 |
return; |
| 109 |
|
| 110 |
va_start(args, fmt); |
| 111 |
vsnprintf(buf, sizeof(buf), fmt, args); |
| 112 |
va_end(args); |
| 113 |
|
| 114 |
log_write(type, buf); |
| 115 |
|
| 116 |
if (log_exceed_size(type) <= 0) |
| 117 |
return; |
| 118 |
|
| 119 |
snprintf(buf, sizeof(buf), "Rotating logfile %s", |
| 120 |
log_type_table[type].path); |
| 121 |
log_write(type, buf); |
| 122 |
|
| 123 |
fclose(log_type_table[type].file); |
| 124 |
log_type_table[type].file = NULL; |
| 125 |
|
| 126 |
snprintf(buf, sizeof(buf), "%s.old", log_type_table[type].path); |
| 127 |
unlink(buf); |
| 128 |
rename(log_type_table[type].path, buf); |
| 129 |
|
| 130 |
log_set_file(type, log_type_table[type].size, log_type_table[type].path); |
| 131 |
log_type_table[type].file = fopen(log_type_table[type].path, "a"); |
| 132 |
} |