comparison src/audacious/logger.c @ 2313:3149d4b1a9a9 trunk

[svn] - objective-make autodepend fixes - move all sourcecode into src/ and adjust Makefiles accordingly
author nenolod
date Fri, 12 Jan 2007 11:43:40 -0800
parents
children d226b83fa329
comparison
equal deleted inserted replaced
2312:e1a5a66fb9cc 2313:3149d4b1a9a9
1 /* Audacious - Cross-platform multimedia player
2 * Copyright (C) 2005-2007 Audacious development team
3 *
4 * Based on BMP:
5 * Copyright (C) 2003-2004 BMP development team
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; under version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "logger.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <glib/gprintf.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <sys/stat.h>
33 #include <time.h>
34
35 #include "main.h"
36
37
38 #define LOG_ALL_LEVELS \
39 (G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION)
40
41
42 struct _LogHandler {
43 gchar *domain;
44 GLogLevelFlags level;
45 guint id;
46 };
47
48 typedef struct _LogHandler LogHandler;
49
50
51 static FILE *bmp_log_file = NULL;
52
53 G_LOCK_DEFINE_STATIC(bmp_log_file);
54
55 static LogHandler log_handlers[] = {
56 {NULL, LOG_ALL_LEVELS, 0},
57 {"Glib", LOG_ALL_LEVELS, 0},
58 {"Gtk", LOG_ALL_LEVELS, 0}
59 };
60
61 static guint log_handler_count = G_N_ELEMENTS(log_handlers);
62
63
64 static const gchar *
65 get_timestamp_str(void)
66 {
67 time_t current_time = time(NULL);
68 return ctime(&current_time);
69 }
70
71 static size_t
72 get_filesize(const gchar *filename)
73 {
74 struct stat info;
75
76 if (stat(filename, &info))
77 return 0;
78
79 return info.st_size;
80 }
81
82 static void
83 log_to_file(const gchar * domain, GLogLevelFlags level,
84 const gchar * message, gpointer data)
85 {
86 FILE *file = (FILE *) data;
87
88 if (!file) {
89 g_printerr(G_STRLOC ": file is NULL!\n");
90 return;
91 }
92
93 G_LOCK(bmp_log_file);
94
95 if (domain)
96 g_fprintf(file, "(%s) ", domain);
97
98 if (message)
99 g_fprintf(file, "%s\n", message);
100 else
101 g_fprintf(file, "message is NULL!\n");
102
103 fflush(file);
104
105 G_UNLOCK(bmp_log_file);
106 }
107
108 gboolean
109 bmp_logger_start(const gchar * filename)
110 {
111 guint i;
112
113 g_return_val_if_fail(filename != NULL, FALSE);
114
115 /* truncate file when size limit is reached */
116 if (get_filesize(filename) < BMP_LOGGER_FILE_MAX_SIZE)
117 bmp_log_file = fopen(filename, "at");
118 else
119 bmp_log_file = fopen(filename, "w+t");
120
121 if (!bmp_log_file) {
122 g_printerr(_("Unable to create log file (%s)!\n"), filename);
123 return FALSE;
124 }
125
126 for (i = 0; i < log_handler_count; i++) {
127 log_handlers[i].id = g_log_set_handler(log_handlers[i].domain,
128 log_handlers[i].level,
129 log_to_file, bmp_log_file);
130 }
131
132 g_message("\n** LOGGING STARTED AT %s", get_timestamp_str());
133
134 return TRUE;
135 }
136
137 void
138 bmp_logger_stop(void)
139 {
140 guint i;
141
142 if (!bmp_log_file)
143 return;
144
145 g_message("\n** LOGGING STOPPED AT %s", get_timestamp_str());
146
147 for (i = 0; i < log_handler_count; i++)
148 g_log_remove_handler(log_handlers[i].domain, log_handlers[i].id);
149
150 fclose(bmp_log_file);
151 bmp_log_file = NULL;
152 }