ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/log.c
Revision: 1324
Committed: Fri Mar 30 21:40:38 2012 UTC (13 years, 4 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/log.c
File size: 3102 byte(s)
Log Message:
- Remove log::timestamp configuration directive. Timestamps are now enabled by default.
- Timestamps are iso8601 now

File Contents

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

Properties

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