ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/log.c
Revision: 1592
Committed: Sat Oct 27 21:02:32 2012 UTC (12 years, 10 months ago) by michael
Content type: text/x-csrc
File size: 2941 byte(s)
Log Message:
- Second time's the charm? Moving svnroot/ircd-hybrid-8 to
  svnroot/ircd-hybrid/trunk

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 "irc_string.h"
29     #include "ircd.h"
30 michael 1309 #include "conf.h"
31 michael 1319 #include "s_misc.h"
32 adx 30
33    
34 michael 1247 static struct {
35     char path[PATH_MAX + 1];
36     size_t size;
37 michael 1325 FILE *file;
38 michael 1247 } log_type_table[LOG_TYPE_LAST];
39 adx 30
40    
41 michael 1247 int
42 michael 1319 log_add_file(enum log_type type, size_t size, const char *path)
43 adx 30 {
44 michael 1247 if (log_type_table[type].file)
45 michael 1325 fclose(log_type_table[type].file);
46 adx 30
47 michael 1247 strlcpy(log_type_table[type].path, path, sizeof(log_type_table[type].path));
48 michael 1248 log_type_table[type].size = size;
49 adx 30
50 michael 1325 return (log_type_table[type].file = fopen(path, "a")) != NULL;
51 michael 1247 }
52    
53     void
54     log_close_all(void)
55 adx 30 {
56 michael 1247 unsigned int type = 0;
57 adx 30
58 michael 1247 while (++type < LOG_TYPE_LAST)
59 adx 30 {
60 michael 1247 if (log_type_table[type].file == NULL)
61     continue;
62    
63 michael 1325 fclose(log_type_table[type].file);
64 michael 1248 log_type_table[type].file = NULL;
65 adx 30 }
66     }
67    
68 michael 1248 static int
69     log_exceed_size(unsigned int type)
70     {
71     struct stat sb;
72    
73     if (!log_type_table[type].size)
74     return 0;
75    
76     if (stat(log_type_table[type].path, &sb) < 0)
77     return -1;
78    
79     return (size_t)sb.st_size > log_type_table[type].size;
80     }
81    
82 adx 30 static void
83 michael 1327 log_write(enum log_type type, const char *message)
84 adx 30 {
85 michael 1327 char buf[IRCD_BUFSIZE];
86 adx 30
87 michael 1327 strftime(buf, sizeof(buf), "[%FT%H:%M:%S%z]", localtime(&CurrentTime));
88 michael 1001
89 michael 1327 fprintf(log_type_table[type].file, "%s %s\n", buf, message);
90 michael 1325 fflush(log_type_table[type].file);
91 adx 30 }
92 michael 1325
93 adx 30 void
94 michael 1319 ilog(enum log_type type, const char *fmt, ...)
95 adx 30 {
96     char buf[LOG_BUFSIZE];
97     va_list args;
98    
99 michael 1327 if (!log_type_table[type].file || !ConfigLoggingEntry.use_logging)
100 michael 1319 return;
101 adx 30
102     va_start(args, fmt);
103 michael 1086 vsnprintf(buf, sizeof(buf), fmt, args);
104 adx 30 va_end(args);
105    
106 michael 1327 log_write(type, buf);
107 michael 1248
108 michael 1327 if (log_exceed_size(type) <= 0)
109     return;
110 michael 1248
111 michael 1327 snprintf(buf, sizeof(buf), "Rotating logfile %s",
112     log_type_table[type].path);
113     log_write(type, buf);
114 michael 1248
115 michael 1327 fclose(log_type_table[type].file);
116     log_type_table[type].file = NULL;
117    
118     snprintf(buf, sizeof(buf), "%s.old", log_type_table[type].path);
119     unlink(buf);
120     rename(log_type_table[type].path, buf);
121    
122     log_add_file(type, log_type_table[type].size, log_type_table[type].path);
123 michael 1248 }

Properties

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