ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/log.c
Revision: 1248
Committed: Sat Oct 1 10:02:53 2011 UTC (13 years, 10 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/s_log.c
File size: 3317 byte(s)
Log Message:
- add log file rotation

File Contents

# User Rev Content
1 adx 30 /*
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 knight 31 * $Id$
23 adx 30 */
24    
25     #include "stdinc.h"
26    
27     #include "s_log.h"
28     #include "fileio.h"
29     #include "irc_string.h"
30     #include "sprintf_irc.h"
31     #include "ircd.h"
32     #include "s_misc.h"
33     #include "s_conf.h"
34     #include "memory.h"
35 michael 1248 #include "send.h"
36 adx 30
37     /* some older syslogs would overflow at 2024 */
38     #define LOG_BUFSIZE 2000
39    
40 michael 1247 static struct {
41     char path[PATH_MAX + 1];
42     size_t size;
43     FBFILE *file;
44     } log_type_table[LOG_TYPE_LAST];
45 adx 30
46    
47 michael 1247 int
48     log_add_file(unsigned int type, size_t size, const char *path)
49 adx 30 {
50 michael 1247 if (log_type_table[type].file)
51     fbclose(log_type_table[type].file);
52 adx 30
53 michael 1247 strlcpy(log_type_table[type].path, path, sizeof(log_type_table[type].path));
54 michael 1248 log_type_table[type].size = size;
55 adx 30
56 michael 1247 return (log_type_table[type].file = fbopen(path, "a")) != NULL;
57     }
58    
59     void
60     log_close_all(void)
61 adx 30 {
62 michael 1247 unsigned int type = 0;
63 adx 30
64 michael 1247 while (++type < LOG_TYPE_LAST)
65 adx 30 {
66 michael 1247 if (log_type_table[type].file == NULL)
67     continue;
68    
69     fbclose(log_type_table[type].file);
70 michael 1248 log_type_table[type].file = NULL;
71 adx 30 }
72     }
73    
74 michael 1248 static int
75     log_exceed_size(unsigned int type)
76     {
77     struct stat sb;
78    
79     if (!log_type_table[type].size)
80     return 0;
81    
82     if (stat(log_type_table[type].path, &sb) < 0)
83     return -1;
84    
85     return (size_t)sb.st_size > log_type_table[type].size;
86     }
87    
88    
89 adx 30 static void
90 michael 1247 write_log(unsigned int type, const char *message)
91 adx 30 {
92     char buf[LOG_BUFSIZE];
93     size_t nbytes = 0;
94    
95 michael 1247 if (log_type_table[type].file == NULL)
96 adx 30 return;
97    
98 michael 1247 if (ConfigLoggingEntry.timestamp)
99     nbytes = snprintf(buf, sizeof(buf), "[%s] %s\n",
100     smalldate(CurrentTime), message);
101     else
102     nbytes = snprintf(buf, sizeof(buf), "%s\n", message);
103 michael 1001
104 michael 1247 fbputs(buf, log_type_table[type].file, nbytes);
105 adx 30 }
106    
107     void
108 michael 1247 ilog(unsigned int type, const char *fmt, ...)
109 adx 30 {
110     char buf[LOG_BUFSIZE];
111     va_list args;
112    
113 michael 1247 assert(fmt);
114     assert(type >= 0 && type < LOG_TYPE_LAST);
115 adx 30
116     va_start(args, fmt);
117 michael 1086 vsnprintf(buf, sizeof(buf), fmt, args);
118 adx 30 va_end(args);
119    
120     if (ConfigLoggingEntry.use_logging)
121 michael 1248 {
122 michael 1247 write_log(type, buf);
123 michael 1248
124     if (log_exceed_size(type) <= 0)
125     return;
126    
127     snprintf(buf, sizeof(buf), "Rotating logfile %s",
128     log_type_table[type].path);
129     write_log(type, buf);
130     fbclose(log_type_table[type].file);
131     log_type_table[type].file = NULL;
132    
133     snprintf(buf, sizeof(buf), "%s.old", log_type_table[type].path);
134     unlink(buf);
135     rename(log_type_table[type].path, buf);
136     log_add_file(type, log_type_table[type].size, log_type_table[type].path);
137     }
138     }

Properties

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