annotate audacious/logger.c @ 2271:8282834d9580 trunk

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