ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/log.c
Revision: 8508
Committed: Fri Apr 6 12:41:58 2018 UTC (7 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 2879 byte(s)
Log Message:
- log.c: further cleanups

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id