logging.c 3.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2000-2004, Jack Moffitt <jack@xiph.org, 
 *                      Michael Smith <msmith@xiph.org>,
 *                      oddsock <oddsock@xiph.org>,
 *                      Karl Heyes <karl@xiph.org>
 *                      and others (see AUTHORS for details).
 */

13 14 15 16
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
17 18
#include <stdio.h>
#include <time.h>
19
#include <string.h>
Jack Moffitt's avatar
Jack Moffitt committed
20

Karl Heyes's avatar
Karl Heyes committed
21 22
#include "thread/thread.h"
#include "httpp/httpp.h"
Jack Moffitt's avatar
Jack Moffitt committed
23 24 25 26 27

#include "connection.h"
#include "refbuf.h"
#include "client.h"

28
#include "os.h"
29
#include "cfgfile.h"
Jack Moffitt's avatar
Jack Moffitt committed
30
#include "logging.h"
31
#include "util.h"
Jack Moffitt's avatar
Jack Moffitt committed
32

33 34 35 36
#ifdef _WIN32
#define snprintf _snprintf
#endif

Jack Moffitt's avatar
Jack Moffitt committed
37
/* the global log descriptors */
Michael Smith's avatar
Michael Smith committed
38 39
int errorlog = 0;
int accesslog = 0;
Jack Moffitt's avatar
Jack Moffitt committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

/* 
** ADDR USER AUTH DATE REQUEST CODE BYTES REFERER AGENT [TIME]
**
** ADDR = client->con->ip
** USER = -      
**      we should do this for real once we support authentication
** AUTH = -
** DATE = _make_date(client->con->con_time)
** REQUEST = build from client->parser
** CODE = client->respcode
** BYTES = client->con->sent_bytes
** REFERER = get from client->parser
** AGENT = get from client->parser
** TIME = timing_get_time() - client->con->con_time
*/
void logging_access(client_t *client)
{
58 59
    char datebuf[128];
    char reqbuf[1024];
60
    struct tm thetime;
61 62
    time_t now;
    time_t stayed;
63
    char *referrer, *user_agent;
64 65 66 67

    now = time(NULL);

    /* build the data */
68 69
    localtime_r (&now, &thetime);
    strftime (datebuf, sizeof(datebuf), LOGGING_FORMAT_CLF, &thetime);
70 71

    /* build the request */
72 73 74 75 76
    snprintf (reqbuf, sizeof(reqbuf), "%s %s %s/%s",
            httpp_getvar (client->parser, HTTPP_VAR_REQ_TYPE),
            httpp_getvar (client->parser, HTTPP_VAR_URI),
            httpp_getvar (client->parser, HTTPP_VAR_PROTOCOL),
            httpp_getvar (client->parser, HTTPP_VAR_VERSION));
77 78 79

    stayed = now - client->con->con_time;

80 81 82 83 84 85 86 87 88
    referrer = httpp_getvar (client->parser, "referer");
    if (referrer == NULL)
        referrer = "-";

    user_agent = httpp_getvar (client->parser, "user-agent");
    if (user_agent == NULL)
        user_agent = "-";

    log_write_direct (accesslog, "%s - - [%s] \"%s\" %d %lld \"%s\" \"%s\" %u",
89 90 91 92 93
             client->con->ip,
             datebuf,
             reqbuf,
             client->respcode,
             client->con->sent_bytes,
94 95 96
             referrer,
             user_agent,
             stayed);
Jack Moffitt's avatar
Jack Moffitt committed
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
void restart_logging ()
{
    ice_config_t *config = config_get_config_unlocked();

    if (strcmp (config->error_log, "-"))
    {
        char fn_error[FILENAME_MAX];
        snprintf (fn_error, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->error_log);
        log_set_filename (errorlog, fn_error);
        log_set_level (errorlog, config->loglevel);
        log_reopen (errorlog);
    }

    if (strcmp (config->access_log, "-"))
    {
        char fn_error[FILENAME_MAX];
        snprintf (fn_error, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->access_log);
        log_set_filename (accesslog, fn_error);
        log_reopen (accesslog);
    }
}