ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/log.c
Revision: 5347
Committed: Sun Jan 11 12:42:20 2015 UTC (10 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 3233 byte(s)
Log Message:
- Update copyright years

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 5347 * Copyright (c) 1997-2015 ircd-hybrid development team
5 adx 30 *
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 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 adx 30 * USA
20     */
21    
22 michael 2916 /*! \file log.c
23     * \brief Logger functions.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 michael 1309 #include "log.h"
29 adx 30 #include "irc_string.h"
30     #include "ircd.h"
31 michael 1309 #include "conf.h"
32 michael 3347 #include "misc.h"
33 adx 30
34    
35 michael 2916 static struct
36     {
37 michael 1737 char path[HYB_PATH_MAX + 1];
38 michael 1247 size_t size;
39 michael 1325 FILE *file;
40 michael 1247 } log_type_table[LOG_TYPE_LAST];
41 adx 30
42    
43 michael 1831 void
44     log_set_file(enum log_type type, size_t size, const char *path)
45 adx 30 {
46 michael 1247 strlcpy(log_type_table[type].path, path, sizeof(log_type_table[type].path));
47 michael 1248 log_type_table[type].size = size;
48 adx 30
49 michael 1831 if (type == LOG_TYPE_IRCD)
50     log_type_table[type].file = fopen(log_type_table[type].path, "a");
51 michael 1247 }
52    
53     void
54 michael 1831 log_del_all(void)
55 adx 30 {
56 michael 1247 unsigned int type = 0;
57 adx 30
58 michael 1247 while (++type < LOG_TYPE_LAST)
59 michael 1831 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 adx 30 {
69 michael 1831 if (log_type_table[type].file)
70     {
71     fclose(log_type_table[type].file);
72     log_type_table[type].file = NULL;
73     }
74 michael 1247
75 michael 1831 if (log_type_table[type].path[0])
76     log_type_table[type].file = fopen(log_type_table[type].path, "a");
77 adx 30 }
78     }
79    
80 michael 1248 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 michael 2916 static void
95 michael 1327 log_write(enum log_type type, const char *message)
96 adx 30 {
97 michael 3215 char buf[IRCD_BUFSIZE] = "";
98 adx 30
99 michael 1327 strftime(buf, sizeof(buf), "[%FT%H:%M:%S%z]", localtime(&CurrentTime));
100 michael 1001
101 michael 1327 fprintf(log_type_table[type].file, "%s %s\n", buf, message);
102 michael 1325 fflush(log_type_table[type].file);
103 adx 30 }
104 michael 1325
105 adx 30 void
106 michael 1319 ilog(enum log_type type, const char *fmt, ...)
107 adx 30 {
108 michael 3215 char buf[LOG_BUFSIZE] = "";
109 adx 30 va_list args;
110    
111 michael 4340 if (!log_type_table[type].file || !ConfigLog.use_logging)
112 michael 1319 return;
113 adx 30
114     va_start(args, fmt);
115 michael 1086 vsnprintf(buf, sizeof(buf), fmt, args);
116 adx 30 va_end(args);
117    
118 michael 1327 log_write(type, buf);
119 michael 1248
120 michael 1327 if (log_exceed_size(type) <= 0)
121     return;
122 michael 1248
123 michael 1327 snprintf(buf, sizeof(buf), "Rotating logfile %s",
124     log_type_table[type].path);
125     log_write(type, buf);
126 michael 1248
127 michael 1327 fclose(log_type_table[type].file);
128     log_type_table[type].file = NULL;
129    
130     snprintf(buf, sizeof(buf), "%s.old", log_type_table[type].path);
131     unlink(buf);
132     rename(log_type_table[type].path, buf);
133    
134 michael 1831 log_set_file(type, log_type_table[type].size, log_type_table[type].path);
135     log_type_table[type].file = fopen(log_type_table[type].path, "a");
136 michael 1248 }

Properties

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