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