Repository navigation
Expand file tree
/
Copy pathlog.c
More file actions
126 lines (103 loc) · 2.68 KB
/
Copy pathlog.c
File metadata and controls
126 lines (103 loc) · 2.68 KB
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
#include "log.h"
#include <sys/file.h>
int
open_log_file(const char *path)
{
return open(path, O_CREAT|O_APPEND|O_WRONLY, 0744);
}
void
write_error_log(char *file_name, int line)
{
char buf[64];
PyObject *f = PySys_GetObject("stderr");
FILE *fp = PyFile_AsFile(f);
int fd = fileno(fp);
flock(fd, LOCK_EX);
cache_time_update();
fputs((char *)err_log_time, fp);
fputs(" [error] ", fp);
sprintf(buf, "pid %d, File \"%s\", line %d \n", getpid(), file_name, line);
fputs(buf, fp);
PyErr_Print();
PyErr_Clear();
fflush(fp);
flock(fd, LOCK_UN);
}
static int
write_log(const char *new_path, int fd, const char *data, size_t len)
{
int openfd;
flock(fd, LOCK_EX);
if(write(fd, data, len) < 0){
flock(fd, LOCK_UN);
//reopen
openfd = open_log_file(new_path);
if(openfd < 0){
//fail
return -1;
}
flock(openfd, LOCK_EX);
if(write(openfd, data, len) < 0){
flock(openfd, LOCK_UN);
// write fail
return -1;
}
flock(openfd, LOCK_UN);
return openfd;
}
flock(fd, LOCK_UN);
return fd;
}
int
write_access_log(client_t *cli, int log_fd, const char *log_path)
{
char buf[1024*4];
if(log_fd > 0){
PyObject *obj;
char *method, *path, *version, *referer, *ua;
obj = PyDict_GetItemString(cli->environ, "REQUEST_METHOD");
if(obj){
method = PyString_AS_STRING(obj);
}else{
method = "-";
}
obj = PyDict_GetItemString(cli->environ, "PATH_INFO");
if(obj){
path = PyString_AS_STRING(obj);
}else{
path = "-";
}
obj = PyDict_GetItemString(cli->environ, "SERVER_PROTOCOL");
if(obj){
version = PyString_AS_STRING(obj);
}else{
version = "-";
}
obj = PyDict_GetItemString(cli->environ, "HTTP_USER_AGENT");
if(obj){
ua = PyString_AS_STRING(obj);
}else{
ua = "-";
}
obj = PyDict_GetItemString(cli->environ, "HTTP_REFERER");
if(obj){
referer = PyString_AS_STRING(obj);
}else{
referer = "-";
}
//update
cache_time_update();
sprintf(buf, "%s - - [%s] \"%s %s %s\" %d %d \"%s\" \"%s\"\n",
cli->remote_addr,
http_log_time,
method,
path,
version,
cli->status_code,
cli->write_bytes,
referer,
ua);
return write_log(log_path, log_fd, buf, strlen(buf));
}
return 0;
}