annotate audacious/logger.c @ 340:c7a67ac8e19c trunk

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