summaryrefslogtreecommitdiff
path: root/src/audacious/logger.c
blob: 5215c71c65c47bdddcabc00e657bcb26c8c448ea (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*  Audacious - Cross-platform multimedia player
 *  Copyright (C) 2005-2007  Audacious development team
 *
 *  Based on BMP:
 *  Copyright (C) 2003-2004  BMP development team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; under version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses>.
 *
 *  The Audacious team does not consider modular code linking to
 *  Audacious or using our public API to be a derived work.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include "logger.h"

#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gprintf.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>

#include "main.h"


#define LOG_ALL_LEVELS \
    (G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION)


struct _LogHandler {
    gchar *domain;
    GLogLevelFlags level;
    guint id;
};

typedef struct _LogHandler LogHandler;


static FILE *aud_log_file = NULL;

G_LOCK_DEFINE_STATIC(aud_log_file);

static LogHandler log_handlers[] = {
    {NULL, LOG_ALL_LEVELS, 0},
    {"Glib", LOG_ALL_LEVELS, 0},
    {"GThread", LOG_ALL_LEVELS, 0},
    {"Gtk", LOG_ALL_LEVELS, 0}
};

static guint log_handler_count = G_N_ELEMENTS(log_handlers);


static const gchar *
get_timestamp_str(void)
{
    time_t current_time = time(NULL);
    return ctime(&current_time);
}

static size_t
get_filesize(const gchar *filename)
{
    struct stat info;

    if (stat(filename, &info))
        return 0;

    return info.st_size;
}

static void
log_to_file(const gchar * domain, GLogLevelFlags level,
            const gchar * message, gpointer data)
{
    FILE *file = (FILE *) data;

    if (!file) {
        g_printerr(G_STRLOC ": file is NULL!\n");
        return;
    }

    G_LOCK(aud_log_file);

    if (domain)
        g_fprintf(file, "(%s) ", domain);
    
    if (message)
        g_fprintf(file, "%s\n", message);
    else
        g_fprintf(file, "message is NULL!\n");

    fflush(file);

    G_UNLOCK(aud_log_file);
}

gboolean
aud_logger_start(const gchar * filename)
{
    guint i;

    g_return_val_if_fail(filename != NULL, FALSE);

    /* truncate file when size limit is reached */
    if (get_filesize(filename) < BMP_LOGGER_FILE_MAX_SIZE)
        aud_log_file = fopen(filename, "at");
    else
        aud_log_file = fopen(filename, "w+t");

    if (!aud_log_file) {
        g_printerr(_("Unable to create log file (%s)!\n"), filename);
        return FALSE;
    }

    for (i = 0; i < log_handler_count; i++) {
        log_handlers[i].id = g_log_set_handler(log_handlers[i].domain,
                                               log_handlers[i].level,
                                               log_to_file, aud_log_file);
    }

    g_message("\n** LOGGING STARTED AT %s", get_timestamp_str());

    return TRUE;
}

void
aud_logger_stop(void)
{
    guint i;

    if (!aud_log_file)
        return;

    g_message("\n** LOGGING STOPPED AT %s", get_timestamp_str());

    for (i = 0; i < log_handler_count; i++)
        g_log_remove_handler(log_handlers[i].domain, log_handlers[i].id);

    fclose(aud_log_file);
    aud_log_file = NULL;
}