/[hydra]/hydra/src/log.c
ViewVC logotype

Contents of /hydra/src/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Sat Sep 21 13:53:23 2002 UTC (21 years, 7 months ago) by nmav
Branch: boas
CVS Tags: start
Changes since 1.1: +0 -0 lines
File MIME type: text/plain
Imported sources

1 /*
2 * Boa, an http server
3 * Copyright (C) 1995 Paul Phillips <paulp@go2net.com>
4 * Some changes Copyright (C) 1996 Larry Doolittle <ldoolitt@boa.org>
5 * Some changes Copyright (C) 1999 Jon Nelson <jnelson@boa.org>
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 1, or (at your option)
10 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22
23 /* $Id: log.c,v 1.36.2.3 2002/07/26 03:04:48 jnelson Exp $*/
24
25 #include "boa.h"
26
27 FILE *access_log;
28
29 char *error_log_name;
30 char *access_log_name;
31 char *cgi_log_name;
32 int cgi_log_fd;
33
34 FILE *fopen_gen_fd(char *spec, const char *mode);
35
36 FILE *fopen_gen_fd(char *spec, const char *mode)
37 {
38 int fd;
39 if (!spec || *spec == '\0')
40 return NULL;
41 fd = open_gen_fd(spec);
42 if (fd == -1)
43 return NULL;
44 return fdopen(fd, mode);
45 }
46
47 /*
48 * Name: open_logs
49 *
50 * Description: Opens access log, error log, and if specified, cgi log
51 * Ties stderr to error log, except during cgi execution, at which
52 * time cgi log is the stderr for cgis.
53 *
54 * Access log is line buffered, error log is not buffered.
55 *
56 */
57
58 void open_logs(void)
59 {
60 int error_log;
61
62 /* if error_log_name is set, dup2 stderr to it */
63 /* otherwise, leave stderr alone */
64 /* we don't want to tie stderr to /dev/null */
65 if (error_log_name) {
66 /* open the log file */
67 if (!(error_log = open_gen_fd(error_log_name))) {
68 DIE("unable to open error log");
69 }
70
71 /* redirect stderr to error_log */
72 if (dup2(error_log, STDERR_FILENO) == -1) {
73 DIE("unable to dup2 the error log");
74 }
75 close(error_log);
76 }
77
78 /* set the close-on-exec to true */
79 if (fcntl(STDERR_FILENO, F_SETFD, 1) == -1) {
80 DIE("unable to fcntl the error log");
81 }
82
83 if (access_log_name) {
84 /* Used the "a" flag with fopen, but fopen_gen_fd builds that in
85 * implicitly when used as a file, and "a" is incompatible with
86 * pipes and network sockets. */
87 if (!(access_log = fopen_gen_fd(access_log_name, "w"))) {
88 int errno_save = errno;
89 fprintf(stderr, "Cannot open %s for logging: ",
90 access_log_name);
91 errno = errno_save;
92 perror("logfile open");
93 exit(errno);
94 }
95 /* line buffer the access log */
96 #ifdef SETVBUF_REVERSED
97 setvbuf(access_log, _IOLBF, (char *) NULL, 0);
98 #else
99 setvbuf(access_log, (char *) NULL, _IOLBF, 0);
100 #endif
101 } else
102 access_log = NULL;
103
104 if (cgi_log_name) {
105 cgi_log_fd = open_gen_fd(cgi_log_name);
106 if (cgi_log_fd == -1) {
107 WARN("open cgi_log");
108 free(cgi_log_name);
109 cgi_log_name = NULL;
110 cgi_log_fd = 0;
111 } else {
112 if (fcntl(cgi_log_fd, F_SETFD, 1) == -1) {
113 WARN("unable to set close-on-exec flag for cgi_log");
114 close(cgi_log_fd);
115 cgi_log_fd = 0;
116 free(cgi_log_name);
117 cgi_log_name = NULL;
118 }
119 }
120 }
121 }
122
123 /*
124 * Name: close_access_log
125 *
126 * Description: closes access_log file
127 */
128 void close_access_log(void)
129 {
130 if (access_log)
131 fclose(access_log);
132 }
133
134 /*
135 * Name: log_access
136 *
137 * Description: Writes log data to access_log.
138 */
139
140 void log_access(request * req)
141 {
142 if (access_log) {
143 if (virtualhost)
144 fprintf(access_log, "%s ", req->local_ip_addr);
145 fprintf(access_log, "%s - - %s\"%s\" %d %ld \"%s\" \"%s\"\n",
146 req->remote_ip_addr,
147 get_commonlog_time(),
148 req->logline,
149 req->response_status,
150 req->filepos,
151 (req->header_referer ? req->header_referer : "-"),
152 (req->header_user_agent ? req->header_user_agent : "-"));
153
154 }
155 }
156
157 /*
158 * Name: log_error_doc
159 *
160 * Description: Logs the current time and transaction identification
161 * to the stderr (the error log):
162 * should always be followed by an fprintf to stderr
163 *
164 * This function used to be implemented with a big fprintf, but not
165 * all fprintf's are reliable in the face of null string pointers
166 * (SunOS, in particular). As long as I had to add the checks for
167 * null pointers, I changed from fprintf to fputs.
168 *
169 * Example output:
170 [08/Nov/1997:01:05:03 -0600] request from 192.228.331.232 "GET /~joeblow/dir/ HTTP/1.0" ("/usr/user1/joeblow/public_html/dir/"): write: Broken pipe
171 */
172
173 void log_error_doc(request * req)
174 {
175 int errno_save = errno;
176
177 fprintf(stderr, "%srequest from %s \"%s\" (\"%s\"): ",
178 get_commonlog_time(),
179 req->remote_ip_addr,
180 (req->logline != NULL ?
181 req->logline : "(null)"),
182 (req->pathname != NULL ? req->pathname : "(null)"));
183
184 errno = errno_save;
185 }
186
187 /*
188 * Name: boa_perror
189 *
190 * Description: logs an error to user and error file both
191 *
192 */
193 void boa_perror(request * req, char *message)
194 {
195 log_error_doc(req);
196 perror(message); /* don't need to save errno because log_error_doc does */
197 send_r_error(req);
198 }
199
200 /*
201 * Name: log_error_time
202 *
203 * Description: Logs the current time to the stderr (the error log):
204 * should always be followed by an fprintf to stderr
205 */
206
207 void log_error_time()
208 {
209 int errno_save = errno;
210 fputs(get_commonlog_time(), stderr);
211 errno = errno_save;
212 }
213
214 /*
215 * Name: log_error_mesg
216 *
217 * Description: performs a log_error_time, writes the file and lineno
218 * to stderr (saving errno), and then a perror with message
219 *
220 */
221
222 void log_error_mesg(char *file, int line, char *mesg)
223 {
224 int errno_save = errno;
225 fprintf(stderr, "%s%s:%d - ", get_commonlog_time(), file, line);
226 errno = errno_save;
227 perror(mesg);
228 errno = errno_save;
229 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26