Mercurial > audlegacy
annotate src/audacious/logger.c @ 3190:dcc92b58f06e trunk
Un-shortcircuit some code that I didn't mean to.
author | William Pitcock <nenolod@atheme-project.org> |
---|---|
date | Fri, 27 Jul 2007 19:44:14 -0500 |
parents | f1c756f39e6c |
children | 978fbda4855f |
rev | line source |
---|---|
3112
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
1 /* Audacious - Cross-platform multimedia player |
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
2 * Copyright (C) 2005-2007 Audacious development team |
2313 | 3 * |
3112
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
4 * Based on BMP: |
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
5 * Copyright (C) 2003-2004 BMP development team |
2313 | 6 * |
3112
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
7 * This program is free software; you can redistribute it and/or modify |
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
8 * it under the terms of the GNU General Public License as published by |
3121
3b6d316f8b09
GPL3 relicensing.
William Pitcock <nenolod@atheme-project.org>
parents:
3112
diff
changeset
|
9 * the Free Software Foundation; under version 3 of the License. |
2313 | 10 * |
3112
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
11 * This program is distributed in the hope that it will be useful, |
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
14 * GNU General Public License for more details. |
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
15 * |
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
16 * You should have received a copy of the GNU General Public License |
3121
3b6d316f8b09
GPL3 relicensing.
William Pitcock <nenolod@atheme-project.org>
parents:
3112
diff
changeset
|
17 * along with this program. If not, see <http://www.gnu.org/licenses>. |
3123
f1c756f39e6c
Invoke "Plugins are not derived work" clause provided by GPL3.
William Pitcock <nenolod@atheme-project.org>
parents:
3121
diff
changeset
|
18 * |
f1c756f39e6c
Invoke "Plugins are not derived work" clause provided by GPL3.
William Pitcock <nenolod@atheme-project.org>
parents:
3121
diff
changeset
|
19 * The Audacious team does not consider modular code linking to |
f1c756f39e6c
Invoke "Plugins are not derived work" clause provided by GPL3.
William Pitcock <nenolod@atheme-project.org>
parents:
3121
diff
changeset
|
20 * Audacious or using our public API to be a derived work. |
2313 | 21 */ |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 # include "config.h" | |
25 #endif | |
26 | |
27 #include "logger.h" | |
28 | |
29 #include <glib.h> | |
30 #include <glib/gi18n.h> | |
31 #include <glib/gprintf.h> | |
32 #include <stdio.h> | |
33 #include <stdlib.h> | |
34 #include <sys/stat.h> | |
35 #include <time.h> | |
36 | |
37 #include "main.h" | |
38 | |
39 | |
40 #define LOG_ALL_LEVELS \ | |
41 (G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION) | |
42 | |
43 | |
44 struct _LogHandler { | |
45 gchar *domain; | |
46 GLogLevelFlags level; | |
47 guint id; | |
48 }; | |
49 | |
50 typedef struct _LogHandler LogHandler; | |
51 | |
52 | |
53 static FILE *bmp_log_file = NULL; | |
54 | |
55 G_LOCK_DEFINE_STATIC(bmp_log_file); | |
56 | |
57 static LogHandler log_handlers[] = { | |
58 {NULL, LOG_ALL_LEVELS, 0}, | |
59 {"Glib", LOG_ALL_LEVELS, 0}, | |
60 {"Gtk", LOG_ALL_LEVELS, 0} | |
61 }; | |
62 | |
63 static guint log_handler_count = G_N_ELEMENTS(log_handlers); | |
64 | |
65 | |
66 static const gchar * | |
67 get_timestamp_str(void) | |
68 { | |
69 time_t current_time = time(NULL); | |
70 return ctime(¤t_time); | |
71 } | |
72 | |
73 static size_t | |
74 get_filesize(const gchar *filename) | |
75 { | |
76 struct stat info; | |
77 | |
78 if (stat(filename, &info)) | |
79 return 0; | |
80 | |
81 return info.st_size; | |
82 } | |
83 | |
84 static void | |
85 log_to_file(const gchar * domain, GLogLevelFlags level, | |
86 const gchar * message, gpointer data) | |
87 { | |
88 FILE *file = (FILE *) data; | |
89 | |
90 if (!file) { | |
91 g_printerr(G_STRLOC ": file is NULL!\n"); | |
92 return; | |
93 } | |
94 | |
95 G_LOCK(bmp_log_file); | |
96 | |
97 if (domain) | |
98 g_fprintf(file, "(%s) ", domain); | |
99 | |
100 if (message) | |
101 g_fprintf(file, "%s\n", message); | |
102 else | |
103 g_fprintf(file, "message is NULL!\n"); | |
104 | |
105 fflush(file); | |
106 | |
107 G_UNLOCK(bmp_log_file); | |
108 } | |
109 | |
110 gboolean | |
111 bmp_logger_start(const gchar * filename) | |
112 { | |
113 guint i; | |
114 | |
115 g_return_val_if_fail(filename != NULL, FALSE); | |
116 | |
117 /* truncate file when size limit is reached */ | |
118 if (get_filesize(filename) < BMP_LOGGER_FILE_MAX_SIZE) | |
119 bmp_log_file = fopen(filename, "at"); | |
120 else | |
121 bmp_log_file = fopen(filename, "w+t"); | |
122 | |
123 if (!bmp_log_file) { | |
124 g_printerr(_("Unable to create log file (%s)!\n"), filename); | |
125 return FALSE; | |
126 } | |
127 | |
128 for (i = 0; i < log_handler_count; i++) { | |
129 log_handlers[i].id = g_log_set_handler(log_handlers[i].domain, | |
130 log_handlers[i].level, | |
131 log_to_file, bmp_log_file); | |
132 } | |
133 | |
134 g_message("\n** LOGGING STARTED AT %s", get_timestamp_str()); | |
135 | |
136 return TRUE; | |
137 } | |
138 | |
139 void | |
140 bmp_logger_stop(void) | |
141 { | |
142 guint i; | |
143 | |
144 if (!bmp_log_file) | |
145 return; | |
146 | |
147 g_message("\n** LOGGING STOPPED AT %s", get_timestamp_str()); | |
148 | |
149 for (i = 0; i < log_handler_count; i++) | |
150 g_log_remove_handler(log_handlers[i].domain, log_handlers[i].id); | |
151 | |
152 fclose(bmp_log_file); | |
153 bmp_log_file = NULL; | |
154 } |