Mercurial > hg > nginx
comparison src/http/modules/ngx_http_log_handler.c @ 99:a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 02 Jun 2003 15:24:30 +0000 |
parents | e43f406e4525 |
children | 7ebc8b7fb816 |
comparison
equal
deleted
inserted
replaced
98:c9b243802a17 | 99:a059e1aa65d4 |
---|---|
1 | 1 |
2 #include <ngx_config.h> | 2 #include <ngx_config.h> |
3 #include <ngx_core.h> | 3 #include <ngx_core.h> |
4 #include <ngx_string.h> | |
5 #include <ngx_alloc.h> | |
6 #include <ngx_time.h> | |
7 #include <ngx_http.h> | 4 #include <ngx_http.h> |
8 #include <ngx_http_config.h> | 5 |
9 | 6 |
10 | 7 typedef struct { |
11 ngx_http_module_t ngx_http_log_module; | 8 ngx_file_t file; |
9 } ngx_http_log_conf_t; | |
10 | |
11 | |
12 static void *ngx_http_log_create_conf(ngx_pool_t *pool); | |
13 static char *ngx_http_log_merge_conf(ngx_pool_t *p, void *parent, void *child); | |
14 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, | |
15 void *conf); | |
16 | |
17 static ngx_command_t ngx_http_log_commands[] = { | |
18 | |
19 {ngx_string("access_log"), | |
20 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
21 ngx_http_log_set_log, | |
22 NGX_HTTP_LOC_CONF_OFFSET, | |
23 0, | |
24 NULL}, | |
25 | |
26 ngx_null_command | |
27 }; | |
28 | |
29 | |
30 ngx_http_module_t ngx_http_log_module_ctx = { | |
31 NULL, /* create main configuration */ | |
32 NULL, /* init main configuration */ | |
33 | |
34 NULL, /* create server configuration */ | |
35 NULL, /* merge server configuration */ | |
36 | |
37 ngx_http_log_create_conf, /* create location configration */ | |
38 ngx_http_log_merge_conf /* merge location configration */ | |
39 }; | |
40 | |
41 | |
42 ngx_module_t ngx_http_log_module = { | |
43 NGX_MODULE, | |
44 &ngx_http_log_module_ctx, /* module context */ | |
45 ngx_http_log_commands, /* module directives */ | |
46 NGX_HTTP_MODULE, /* module type */ | |
47 NULL, /* init module */ | |
48 }; | |
49 | |
12 | 50 |
13 | 51 |
14 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", | 52 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
15 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; | 53 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
16 | 54 |
17 | 55 |
18 int ngx_http_log_handler(ngx_http_request_t *r) | 56 int ngx_http_log_handler(ngx_http_request_t *r) |
19 { | 57 { |
20 size_t len; | 58 char *line, *p; |
21 char *line, *p; | 59 size_t len; |
22 ngx_tm_t tm; | 60 ngx_tm_t tm; |
61 ngx_http_log_conf_t *lcf; | |
23 | 62 |
24 ngx_log_debug(r->connection->log, "log handler"); | 63 ngx_log_debug(r->connection->log, "log handler"); |
25 | 64 |
26 /* 10:%con, 1:%pipe, 22:%date, 2:"%r", 3:%status, 20:%bytes, | 65 lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module); |
27 6*" ", 2/1: "\r\n" */ | 66 |
67 /* 10:%con, 1:%pipe, 22:%date, 2:"%r", 3:%status, 20:%bytes, 2:%user-agent, | |
68 7*" ", 2/1: "\r\n" */ | |
28 #if (WIN32) | 69 #if (WIN32) |
29 len = 10 + 1 + 22 + 2 + 3 + 20 + 6 + 2; | 70 len = 10 + 1 + 22 + 2 + 3 + 20 + 2 + 7 + 2; |
30 #else | 71 #else |
31 len = 10 + 1 + 22 + 2 + 3 + 20 + 6 + 1; | 72 len = 10 + 1 + 22 + 2 + 3 + 20 + 2 + 7 + 1; |
32 #endif | 73 #endif |
33 | 74 |
34 len += r->connection->addr_text.len; | 75 len += r->connection->addr_text.len; |
35 len += r->request_line.len; | 76 len += r->request_line.len; |
77 if (r->headers_in.user_agent) { | |
78 len += r->headers_in.user_agent->value.len; | |
79 } | |
36 | 80 |
37 ngx_test_null(line, ngx_palloc(r->pool, len), NGX_ERROR); | 81 ngx_test_null(line, ngx_palloc(r->pool, len), NGX_ERROR); |
38 p = line; | 82 p = line; |
39 | 83 |
40 ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len); | 84 ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len); |
77 | 121 |
78 *p++ = ' '; | 122 *p++ = ' '; |
79 | 123 |
80 p += ngx_snprintf(p, 21, OFF_FMT, r->connection->sent); | 124 p += ngx_snprintf(p, 21, OFF_FMT, r->connection->sent); |
81 | 125 |
126 *p++ = ' '; | |
127 | |
128 *p++ = '"'; | |
129 if (r->headers_in.user_agent) { | |
130 ngx_memcpy(p, r->headers_in.user_agent->value.data, | |
131 r->headers_in.user_agent->value.len); | |
132 p += r->headers_in.user_agent->value.len; | |
133 } | |
134 *p++ = '"'; | |
135 | |
82 #if (WIN32) | 136 #if (WIN32) |
83 *p++ = CR; *p++ = LF; | 137 *p++ = CR; *p++ = LF; |
84 #else | 138 #else |
85 *p++ = LF; | 139 *p++ = LF; |
86 #endif | 140 #endif |
87 | 141 |
88 write(1, line, p - line); | 142 write(lcf->file.fd, line, p - line); |
89 | 143 |
90 return NGX_OK; | 144 return NGX_OK; |
91 } | 145 } |
146 | |
147 | |
148 static void *ngx_http_log_create_conf(ngx_pool_t *pool) | |
149 { | |
150 ngx_http_log_conf_t *conf; | |
151 | |
152 ngx_test_null(conf, ngx_pcalloc(pool, sizeof(ngx_http_log_conf_t)), | |
153 NGX_CONF_ERROR); | |
154 | |
155 return conf; | |
156 } | |
157 | |
158 | |
159 static char *ngx_http_log_merge_conf(ngx_pool_t *p, void *parent, void *child) | |
160 { | |
161 ngx_http_log_conf_t *prev = parent; | |
162 ngx_http_log_conf_t *conf = child; | |
163 | |
164 /* STUB */ | |
165 *conf = *prev; | |
166 | |
167 return NGX_CONF_OK; | |
168 } | |
169 | |
170 | |
171 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, | |
172 void *conf) | |
173 { | |
174 ngx_http_log_conf_t *lcf = conf; | |
175 | |
176 int len; | |
177 ngx_err_t err; | |
178 ngx_str_t *value; | |
179 | |
180 value = cf->args->elts; | |
181 | |
182 lcf->file.name.len = value[1].len; | |
183 lcf->file.name.data = value[1].data; | |
184 | |
185 lcf->file.fd = ngx_open_file(lcf->file.name.data, | |
186 NGX_FILE_RDWR, | |
187 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND); | |
188 | |
189 if (lcf->file.fd == NGX_INVALID_FILE) { | |
190 err = ngx_errno; | |
191 len = ngx_snprintf(ngx_conf_errstr, sizeof(ngx_conf_errstr) - 1, | |
192 ngx_open_file_n " \"%s\" failed (%d: ", | |
193 lcf->file.name.data, err); | |
194 len += ngx_strerror_r(err, ngx_conf_errstr + len, | |
195 sizeof(ngx_conf_errstr) - len - 1); | |
196 ngx_conf_errstr[len++] = ')'; | |
197 ngx_conf_errstr[len++] = '\0'; | |
198 return ngx_conf_errstr; | |
199 } | |
200 | |
201 return NGX_CONF_OK; | |
202 } |