/
log.c
82 lines (66 loc) · 2.23 KB
/
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
/*
Copyright (C) 2009 Conrad Parker
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include "params.h"
#include "http-reqline.h"
#define ACCESS_LOG "/var/log/sighttpd/access.log"
#define ERROR_LOG "/var/log/sighttpd/error.log"
#define DATE_FMT "[%s] "
#define LOG_FMT DATE_FMT "\"%s\" 200 %s \"%s\"\r\n"
FILE * access_log=NULL, * error_log=NULL;
static int log_date (FILE * stream)
{
char date[256];
httpdate_snprint (date, 256, time(NULL));
return fprintf (stream, DATE_FMT, date);
}
int log_open (void)
{
access_log = fopen (ACCESS_LOG, "a");
error_log = fopen (ERROR_LOG, "a");
if (error_log != NULL) {
log_date (error_log);
fprintf (error_log, "Sighttpd/" VERSION " resuming normal operations\n");
fflush (error_log);
}
}
int log_close (void)
{
if (access_log != NULL) {
fflush (access_log);
fclose (access_log);
}
if (error_log != NULL) {
log_date (error_log);
fprintf (error_log, "Sighttpd/" VERSION " shutting down\n");
fflush (error_log);
fclose (error_log);
}
}
void log_access (http_request * request, params_t * request_headers, params_t * response_headers)
{
char canon[1024];
char * date, * user_agent, * content_length;
/* Dump request headers to stdout */
params_snprint (canon, 1024, request_headers, PARAMS_HEADERS);
puts (canon);
/* Apache-style logging */
if ((date = params_get (response_headers, "Date")) == NULL)
date = "";
if ((user_agent = params_get (request_headers, "User-Agent")) == NULL)
user_agent = "";
if ((content_length = params_get (response_headers, "Content-Length")) == NULL)
content_length = "";
if (access_log != NULL) {
fprintf (access_log, LOG_FMT, date, request->original_reqline,
content_length, user_agent);
fflush (access_log);
} else {
fprintf (stderr, LOG_FMT, date, request->original_reqline,
content_length, user_agent);
}
}