Mercurial > hg > nginx
comparison src/http/modules/ngx_http_log_handler.c @ 119:cd54bcbaf3b5
nginx-0.0.1-2003-07-21-01:15:59 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Sun, 20 Jul 2003 21:15:59 +0000 |
parents | 571bcbff82c5 |
children |
comparison
equal
deleted
inserted
replaced
118:5bf52498665c | 119:cd54bcbaf3b5 |
---|---|
3 #include <ngx_core.h> | 3 #include <ngx_core.h> |
4 #include <ngx_http.h> | 4 #include <ngx_http.h> |
5 | 5 |
6 | 6 |
7 typedef struct { | 7 typedef struct { |
8 ngx_file_t file; | 8 ngx_open_file_t *file; |
9 } ngx_http_log_conf_t; | 9 } ngx_http_log_conf_t; |
10 | 10 |
11 | 11 |
12 static void *ngx_http_log_create_conf(ngx_pool_t *pool); | 12 static void *ngx_http_log_create_conf(ngx_conf_t *cf); |
13 static char *ngx_http_log_merge_conf(ngx_pool_t *p, void *parent, void *child); | 13 static char *ngx_http_log_merge_conf(ngx_conf_t *cf, void *parent, void *child); |
14 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, | 14 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, |
15 void *conf); | 15 void *conf); |
16 | 16 |
17 static ngx_command_t ngx_http_log_commands[] = { | 17 static ngx_command_t ngx_http_log_commands[] = { |
18 | 18 |
19 {ngx_string("access_log"), | 19 {ngx_string("access_log"), |
20 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | 20 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
47 NULL, /* init module */ | 47 NULL, /* init module */ |
48 NULL /* init child */ | 48 NULL /* init child */ |
49 }; | 49 }; |
50 | 50 |
51 | 51 |
52 static ngx_str_t http_access_log = ngx_string("access.log"); | |
53 | |
52 | 54 |
53 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", | 55 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
54 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; | 56 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
55 | 57 |
56 | 58 |
58 { | 60 { |
59 char *line, *p; | 61 char *line, *p; |
60 size_t len; | 62 size_t len; |
61 ngx_tm_t tm; | 63 ngx_tm_t tm; |
62 ngx_http_log_conf_t *lcf; | 64 ngx_http_log_conf_t *lcf; |
65 #if (WIN32) | |
66 int written; | |
67 #endif | |
63 | 68 |
64 ngx_log_debug(r->connection->log, "log handler"); | 69 ngx_log_debug(r->connection->log, "log handler"); |
65 | 70 |
66 lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module); | 71 lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module); |
67 | 72 |
133 p += r->headers_in.user_agent->value.len; | 138 p += r->headers_in.user_agent->value.len; |
134 } | 139 } |
135 *p++ = '"'; | 140 *p++ = '"'; |
136 | 141 |
137 #if (WIN32) | 142 #if (WIN32) |
143 | |
138 *p++ = CR; *p++ = LF; | 144 *p++ = CR; *p++ = LF; |
145 WriteFile(lcf->file->fd, line, p - line, &written, NULL); | |
146 | |
139 #else | 147 #else |
148 | |
140 *p++ = LF; | 149 *p++ = LF; |
150 write(lcf->file->fd, line, p - line); | |
151 | |
141 #endif | 152 #endif |
142 | 153 |
143 write(lcf->file.fd, line, p - line); | |
144 | 154 |
145 return NGX_OK; | 155 return NGX_OK; |
146 } | 156 } |
147 | 157 |
148 | 158 |
149 static void *ngx_http_log_create_conf(ngx_pool_t *pool) | 159 static void *ngx_http_log_create_conf(ngx_conf_t *cf) |
150 { | 160 { |
151 ngx_http_log_conf_t *conf; | 161 ngx_http_log_conf_t *conf; |
152 | 162 |
153 ngx_test_null(conf, ngx_pcalloc(pool, sizeof(ngx_http_log_conf_t)), | 163 ngx_test_null(conf, ngx_pcalloc(cf->pool, sizeof(ngx_http_log_conf_t)), |
154 NGX_CONF_ERROR); | 164 NGX_CONF_ERROR); |
155 | 165 |
156 return conf; | 166 return conf; |
157 } | 167 } |
158 | 168 |
159 | 169 |
160 static char *ngx_http_log_merge_conf(ngx_pool_t *p, void *parent, void *child) | 170 static char *ngx_http_log_merge_conf(ngx_conf_t *cf, void *parent, void *child) |
161 { | 171 { |
162 ngx_http_log_conf_t *prev = parent; | 172 ngx_http_log_conf_t *prev = parent; |
163 ngx_http_log_conf_t *conf = child; | 173 ngx_http_log_conf_t *conf = child; |
164 | 174 |
165 /* STUB */ | 175 if (conf->file == NULL) { |
166 *conf = *prev; | 176 if (prev->file) { |
177 conf->file = prev->file; | |
178 } else { | |
179 ngx_test_null(conf->file, | |
180 ngx_conf_open_file(cf->cycle, &http_access_log), | |
181 NGX_CONF_ERROR); | |
182 } | |
183 } | |
167 | 184 |
168 return NGX_CONF_OK; | 185 return NGX_CONF_OK; |
169 } | 186 } |
170 | 187 |
171 | 188 |
172 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, | 189 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, |
173 void *conf) | 190 void *conf) |
174 { | 191 { |
175 ngx_http_log_conf_t *lcf = conf; | 192 ngx_http_log_conf_t *lcf = conf; |
176 | 193 |
177 int len; | |
178 ngx_err_t err; | |
179 ngx_str_t *value; | 194 ngx_str_t *value; |
180 | 195 |
181 value = cf->args->elts; | 196 value = cf->args->elts; |
182 | 197 |
183 lcf->file.name.len = value[1].len; | 198 ngx_test_null(lcf->file, ngx_conf_open_file(cf->cycle, &value[1]), |
184 lcf->file.name.data = value[1].data; | 199 NGX_CONF_ERROR); |
185 | |
186 lcf->file.fd = ngx_open_file(lcf->file.name.data, | |
187 NGX_FILE_RDWR, | |
188 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND); | |
189 | |
190 if (lcf->file.fd == NGX_INVALID_FILE) { | |
191 err = ngx_errno; | |
192 len = ngx_snprintf(ngx_conf_errstr, sizeof(ngx_conf_errstr) - 1, | |
193 ngx_open_file_n " \"%s\" failed (%d: ", | |
194 lcf->file.name.data, err); | |
195 len += ngx_strerror_r(err, ngx_conf_errstr + len, | |
196 sizeof(ngx_conf_errstr) - len - 1); | |
197 ngx_conf_errstr[len++] = ')'; | |
198 ngx_conf_errstr[len++] = '\0'; | |
199 return ngx_conf_errstr; | |
200 } | |
201 | |
202 #if (WIN32) | |
203 if (ngx_file_append_mode(lcf->file.fd) == NGX_ERROR) { | |
204 err = ngx_errno; | |
205 len = ngx_snprintf(ngx_conf_errstr, sizeof(ngx_conf_errstr) - 1, | |
206 ngx_file_append_mode_n " \"%s\" failed (%d: ", | |
207 lcf->file.name.data, err); | |
208 len += ngx_strerror_r(err, ngx_conf_errstr + len, | |
209 sizeof(ngx_conf_errstr) - len - 1); | |
210 ngx_conf_errstr[len++] = ')'; | |
211 ngx_conf_errstr[len++] = '\0'; | |
212 return ngx_conf_errstr; | |
213 } | |
214 #endif | |
215 | 200 |
216 return NGX_CONF_OK; | 201 return NGX_CONF_OK; |
217 } | 202 } |