ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/libio/misc/log.c
Revision: 69
Committed: Tue Oct 4 16:09:51 2005 UTC (20 years, 10 months ago) by adx
Content type: text/x-csrc
File size: 3662 byte(s)
Log Message:
- splitted ircd/libio, all headers connected with libio sources have been
  moved for internal use only. To use libio interface, include "libio.h"
  (which is already done in "stdinc.h")


File Contents

# User Rev Content
1 adx 61 /*
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     * $Id: s_log.c 33 2005-10-02 20:50:00Z knight $
23     */
24    
25     #include "stdinc.h"
26    
27     #ifdef USE_SYSLOG
28     # ifdef HAVE_SYS_SYSLOG_H
29     # include <sys/syslog.h>
30     # else
31     # ifdef HAVE_SYSLOG_H
32     # include <syslog.h>
33     # endif
34     # endif
35     #endif
36    
37    
38     /* some older syslogs would overflow at 2024 */
39     #define LOG_BUFSIZE 2000
40    
41 adx 65 static FBFILE *logFile = NULL;
42 adx 61 static int logLevel = INIT_LOG_LEVEL;
43    
44     #ifndef SYSLOG_USERS
45     static EVH user_log_resync;
46 adx 69 void *user_log_fb = NULL;
47 adx 61 #endif
48    
49     #ifdef USE_SYSLOG
50     static const int sysLogLevel[] =
51     {
52     LOG_CRIT,
53     LOG_ERR,
54     LOG_WARNING,
55     LOG_NOTICE,
56     LOG_INFO,
57     LOG_INFO,
58     LOG_INFO
59     };
60     #endif
61    
62     static const char *logLevelToString[] =
63     {
64     "L_CRIT",
65     "L_ERROR",
66     "L_WARN",
67     "L_NOTICE",
68     "L_TRACE",
69     "L_INFO",
70     "L_DEBUG"
71     };
72    
73     /*
74     * open_log - open ircd logging file
75     * returns true (1) if successful, false (0) otherwise
76     */
77     static int
78     open_log(const char *filename)
79     {
80     logFile = fbopen(filename, "a");
81    
82     if (logFile == NULL)
83     {
84     #ifdef USE_SYSLOG
85     syslog(LOG_ERR, "Unable to open log file: %s: %s",
86     filename, strerror(errno));
87     #endif
88     return (0);
89     }
90    
91     return (1);
92     }
93    
94     static void
95     write_log(const char *message)
96     {
97     char buf[LOG_BUFSIZE];
98     size_t nbytes = 0;
99    
100     if (logFile == NULL)
101     return;
102    
103     #ifdef _WIN32
104     nbytes = snprintf(buf, sizeof(buf), "[%s] %s\r\n",
105     #else
106     nbytes = snprintf(buf, sizeof(buf), "[%s] %s\n",
107     #endif
108     smalldate(CurrentTime), message);
109     fbputs(buf, logFile, nbytes);
110     }
111    
112     void
113     ilog(const int priority, const char *fmt, ...)
114     {
115     char buf[LOG_BUFSIZE];
116     va_list args;
117    
118     assert(priority > -1);
119    
120     if (fmt == NULL)
121     return;
122    
123     if (priority > logLevel)
124     return;
125    
126     va_start(args, fmt);
127     vsprintf(buf, fmt, args);
128     va_end(args);
129    
130     #ifdef USE_SYSLOG
131     if (priority <= L_DEBUG)
132     syslog(sysLogLevel[priority], "%s", buf);
133     #endif
134 adx 65 write_log(buf);
135 adx 61 }
136    
137     void
138     init_log(const char *filename)
139     {
140     open_log(filename);
141     #ifdef USE_SYSLOG
142     openlog("ircd", LOG_PID | LOG_NDELAY, LOG_FACILITY);
143     #endif
144     #ifndef SYSLOG_USERS
145     eventAddIsh("user_log_resync", user_log_resync, NULL, 60);
146     #endif
147     }
148    
149     void
150     reopen_log(const char *filename)
151     {
152     if (logFile != NULL)
153     fbclose(logFile);
154     open_log(filename);
155     }
156    
157     void
158     set_log_level(const int level)
159     {
160     if (L_ERROR < level && level <= L_DEBUG)
161     logLevel = level;
162     }
163    
164     int
165     get_log_level(void)
166     {
167     return(logLevel);
168     }
169    
170     const char *
171     get_log_level_as_string(int level)
172     {
173     if (level > L_DEBUG)
174     level = L_DEBUG;
175     else if (level < L_ERROR)
176     level = L_ERROR;
177    
178     return(logLevelToString[level]);
179     }
180    
181     #ifndef SYSLOG_USERS
182     /* user_log_resync()
183     *
184     * inputs - NONE
185     * output - NONE
186     * side effects -
187     */
188     static void
189     user_log_resync(void *notused)
190     {
191     if (user_log_fb != NULL)
192     {
193     fbclose(user_log_fb);
194     user_log_fb = NULL;
195     }
196     }
197     #endif

Properties

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