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