logging.c 2.71 KB
Newer Older
1 2 3 4
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
5 6
#include <stdio.h>
#include <time.h>
7
#include <string.h>
Jack Moffitt's avatar
Jack Moffitt committed
8

Karl Heyes's avatar
Karl Heyes committed
9 10
#include "thread/thread.h"
#include "httpp/httpp.h"
Jack Moffitt's avatar
Jack Moffitt committed
11 12 13 14 15

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

16
#include "os.h"
17
#include "cfgfile.h"
Jack Moffitt's avatar
Jack Moffitt committed
18 19
#include "logging.h"

20 21 22 23
#ifdef _WIN32
#define snprintf _snprintf
#endif

Jack Moffitt's avatar
Jack Moffitt committed
24
/* the global log descriptors */
Michael Smith's avatar
Michael Smith committed
25 26
int errorlog = 0;
int accesslog = 0;
Jack Moffitt's avatar
Jack Moffitt committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

/* 
** 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)
{
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
    char datebuf[128];
    char reqbuf[1024];
    struct tm *thetime;
    time_t now;
    time_t stayed;

    now = time(NULL);

    /* build the data */
    /* TODO: localtime is not threadsafe on all platforms
    ** we should probably use localtime_r if it's available
    */
    PROTECT_CODE(thetime = localtime(&now); strftime(datebuf, 128, LOGGING_FORMAT_CLF, thetime))

    /* build the request */
    snprintf(reqbuf, 1024, "%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));

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

    log_write_direct(accesslog, "%s - - [%s] \"%s\" %d %lld \"%s\" \"%s\" %d",
             client->con->ip,
             datebuf,
             reqbuf,
             client->respcode,
             client->con->sent_bytes,
             (httpp_getvar(client->parser, "referer") != NULL) ? httpp_getvar(client->parser, "referer") : "-",
             (httpp_getvar(client->parser, "user-agent") != NULL) ? httpp_getvar(client->parser, "user-agent") : "-",
             (int)stayed);
Jack Moffitt's avatar
Jack Moffitt committed
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
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);
    }
}