forked from yaoweibin/nginx_tcp_proxy_module
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ngx_tcp_log.c
233 lines (167 loc) · 5.69 KB
/
ngx_tcp_log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_tcp.h>
static u_char * ngx_tcp_time(u_char *buf, time_t t);
static u_char *ngx_tcp_log_fill(ngx_tcp_session_t *s, u_char *buf);
static void ngx_tcp_log_write(ngx_tcp_session_t *s, ngx_tcp_log_t *log,
u_char *buf, size_t len);
ngx_int_t
ngx_tcp_log_handler(ngx_tcp_session_t *s)
{
u_char *line, *p;
size_t len;
ngx_uint_t l;
ngx_connection_t *c;
ngx_tcp_log_t *log;
ngx_open_file_t *file;
#if (nginx_version) >= 1003010 || (nginx_version) >= 1002007 && (nginx_version) < 1003000
ngx_tcp_log_buf_t *buffer;
#endif
ngx_tcp_log_srv_conf_t *lscf;
ngx_tcp_core_srv_conf_t *cscf;
ngx_log_debug0(NGX_LOG_DEBUG_TCP, s->connection->log, 0,
"tcp access log handler");
cscf = ngx_tcp_get_module_srv_conf(s, ngx_tcp_core_module);
lscf = cscf->access_log;
if (lscf->off) {
return NGX_OK;
}
c = s->connection;
log = lscf->logs->elts;
for (l = 0; l < lscf->logs->nelts; l++) {
if (ngx_time() == log[l].disk_full_time) {
/*
* on FreeBSD writing to a full filesystem with enabled softupdates
* may block process for much longer time than writing to non-full
* filesystem, so we skip writing to a log for one second
*/
continue;
}
len = 0;
/* Calculate the length */
len += sizeof("1970/09/28 12:00:00"); /* log time */
len += NGX_INT64_LEN + 2; /* [ngx_pid] */
len += c->addr_text.len + 1; /* client address */
len += s->addr_text->len + 1; /* this session address */
len += sizeof("1970/09/28 12:00:00"); /* accept time */
len += sizeof("255.255.255.255:65536"); /* upstream address */
len += NGX_OFF_T_LEN + 1; /* read bytes from client */
len += NGX_OFF_T_LEN + 1; /* write bytes to client */
len += NGX_LINEFEED_SIZE;
file = log[l].file;
#if (nginx_version) >= 1003010 || (nginx_version) >= 1002007 && (nginx_version) < 1003000
if (file && file->data) {
buffer = file->data;
if (len > (size_t) (buffer->last - buffer->pos)) {
ngx_tcp_log_write(s, &log[l], buffer->start,
buffer->pos - buffer->start);
buffer->pos = buffer->start;
}
if (len <= (size_t) (buffer->last - buffer->pos)) {
p = buffer->pos;
p = ngx_tcp_log_fill(s, p);
buffer->pos = p;
continue;
}
}
#else
if (file && file->buffer) {
if (len > (size_t) (file->last - file->pos)) {
ngx_tcp_log_write(s, &log[l], file->buffer,
file->pos - file->buffer);
file->pos = file->buffer;
}
if (len <= (size_t) (file->last - file->pos)) {
p = file->pos;
p = ngx_tcp_log_fill(s, p);
file->pos = p;
continue;
}
}
#endif
line = ngx_pnalloc(s->pool, len);
if (line == NULL) {
return NGX_ERROR;
}
p = line;
p = ngx_tcp_log_fill(s, p);
ngx_tcp_log_write(s, &log[l], line, p - line);
}
return NGX_OK;
}
static u_char *
ngx_tcp_time(u_char *buf, time_t t)
{
ngx_tm_t tm;
ngx_localtime(t, &tm);
return ngx_sprintf(buf, "%4d/%02d/%02d %02d:%02d:%02d",
tm.ngx_tm_year, tm.ngx_tm_mon,
tm.ngx_tm_mday, tm.ngx_tm_hour,
tm.ngx_tm_min, tm.ngx_tm_sec);
}
static u_char *
ngx_tcp_log_fill(ngx_tcp_session_t *s, u_char *buf)
{
u_char *last;
ngx_str_t *name;
ngx_connection_t *c;
ngx_tcp_upstream_t *u;
c = s->connection;
last = ngx_cpymem(buf, ngx_cached_err_log_time.data,
ngx_cached_err_log_time.len);
last = ngx_sprintf(last, " [%P]", ngx_pid);
last = ngx_sprintf(last, " %V", &c->addr_text);
last = ngx_sprintf(last, " %V ", s->addr_text);
last = ngx_tcp_time(last, s->start_sec);
name = NULL;
if (s->upstream) {
u = s->upstream;
if (u->peer.connection) {
name = u->peer.name;
}
}
if (name) {
last = ngx_sprintf(last, " %V", name);
}
else {
last = ngx_sprintf(last, " -");
}
last = ngx_sprintf(last, " %O", s->bytes_read);
last = ngx_sprintf(last, " %O", s->bytes_write);
ngx_linefeed(last);
return last;
}
static void
ngx_tcp_log_write(ngx_tcp_session_t *s, ngx_tcp_log_t *log, u_char *buf,
size_t len)
{
u_char *name;
time_t now;
ssize_t n;
ngx_err_t err;
if(len == 0) return;
name = log->file->name.data;
n = ngx_write_fd(log->file->fd, buf, len);
if (n == (ssize_t) len) {
return;
}
now = ngx_time();
if (n == -1) {
err = ngx_errno;
if (err == NGX_ENOSPC) {
log->disk_full_time = now;
}
if (now - log->error_log_time > 59) {
ngx_log_error(NGX_LOG_ALERT, s->connection->log, err,
ngx_write_fd_n " to \"%s\" failed", name);
log->error_log_time = now;
}
return;
}
if (now - log->error_log_time > 59) {
ngx_log_error(NGX_LOG_ALERT, s->connection->log, 0,
ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
name, n, len);
log->error_log_time = now;
}
}