Add log cycling on HUP signals. Currently reopens log files in append mode
[xiph/unicode.git] / icecast / src / logging.c
blob784739a0e20f2d86f6ffb44033dfd4cbca7311ca
1 #include <stdio.h>
2 #include <time.h>
3 #include <string.h>
5 #include "thread.h"
6 #include "httpp.h"
7 #include "log.h"
9 #include "connection.h"
10 #include "refbuf.h"
11 #include "client.h"
13 #include "os.h"
14 #include "config.h"
15 #include "logging.h"
17 #ifdef _WIN32
18 #define snprintf _snprintf
19 #endif
21 /* the global log descriptors */
22 int errorlog = 0;
23 int accesslog = 0;
25 /*
26 ** ADDR USER AUTH DATE REQUEST CODE BYTES REFERER AGENT [TIME]
28 ** ADDR = client->con->ip
29 ** USER = -
30 ** we should do this for real once we support authentication
31 ** AUTH = -
32 ** DATE = _make_date(client->con->con_time)
33 ** REQUEST = build from client->parser
34 ** CODE = client->respcode
35 ** BYTES = client->con->sent_bytes
36 ** REFERER = get from client->parser
37 ** AGENT = get from client->parser
38 ** TIME = timing_get_time() - client->con->con_time
40 void logging_access(client_t *client)
42 char datebuf[128];
43 char reqbuf[1024];
44 struct tm *thetime;
45 time_t now;
46 time_t stayed;
48 now = time(NULL);
50 /* build the data */
51 /* TODO: localtime is not threadsafe on all platforms
52 ** we should probably use localtime_r if it's available
54 PROTECT_CODE(thetime = localtime(&now); strftime(datebuf, 128, LOGGING_FORMAT_CLF, thetime))
56 /* build the request */
57 snprintf(reqbuf, 1024, "%s %s %s/%s", httpp_getvar(client->parser, HTTPP_VAR_REQ_TYPE), httpp_getvar(client->parser, HTTPP_VAR_URI),
58 httpp_getvar(client->parser, HTTPP_VAR_PROTOCOL), httpp_getvar(client->parser, HTTPP_VAR_VERSION));
60 stayed = now - client->con->con_time;
62 log_write_direct(accesslog, "%s - - [%s] \"%s\" %d %lld \"%s\" \"%s\" %d",
63 client->con->ip,
64 datebuf,
65 reqbuf,
66 client->respcode,
67 client->con->sent_bytes,
68 (httpp_getvar(client->parser, "referer") != NULL) ? httpp_getvar(client->parser, "referer") : "-",
69 (httpp_getvar(client->parser, "user-agent") != NULL) ? httpp_getvar(client->parser, "user-agent") : "-",
70 (int)stayed);
75 void restart_logging ()
77 ice_config_t *config = config_get_config_unlocked();
79 if (strcmp (config->error_log, "-"))
81 char fn_error[FILENAME_MAX];
82 snprintf (fn_error, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->error_log);
83 log_set_filename (errorlog, fn_error);
84 log_set_level (errorlog, config->loglevel);
85 log_reopen (errorlog);
88 if (strcmp (config->access_log, "-"))
90 char fn_error[FILENAME_MAX];
91 snprintf (fn_error, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->access_log);
92 log_set_filename (accesslog, fn_error);
93 log_reopen (accesslog);