Mercurial > audlegacy
annotate src/audacious/logger.c @ 4506:f6bf0b7072c8
whoops, forgot to add those two files
author | mf0102 <0102@gmx.at> |
---|---|
date | Tue, 22 Apr 2008 12:14:24 +0200 |
parents | de33c83aa06f |
children |
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 | |
4413
de33c83aa06f
Huge renaming patch, changing BMP references to Audacious.
Matti Hamalainen <ccr@tnsp.org>
parents:
3581
diff
changeset
|
53 static FILE *aud_log_file = NULL; |
2313 | 54 |
4413
de33c83aa06f
Huge renaming patch, changing BMP references to Audacious.
Matti Hamalainen <ccr@tnsp.org>
parents:
3581
diff
changeset
|
55 G_LOCK_DEFINE_STATIC(aud_log_file); |
2313 | 56 |
57 static LogHandler log_handlers[] = { | |
58 {NULL, LOG_ALL_LEVELS, 0}, | |
59 {"Glib", LOG_ALL_LEVELS, 0}, | |
3581
978fbda4855f
Add GThread to domain logging targets.
William Pitcock <nenolod@atheme.org>
parents:
3123
diff
changeset
|
60 {"GThread", LOG_ALL_LEVELS, 0}, |
2313 | 61 {"Gtk", LOG_ALL_LEVELS, 0} |
62 }; | |
63 | |
64 static guint log_handler_count = G_N_ELEMENTS(log_handlers); | |
65 | |
66 | |
67 static const gchar * | |
68 get_timestamp_str(void) | |
69 { | |
70 time_t current_time = time(NULL); | |
71 return ctime(¤t_time); | |
72 } | |
73 | |
74 static size_t | |
75 get_filesize(const gchar *filename) | |
76 { | |
77 struct stat info; | |
78 | |
79 if (stat(filename, &info)) | |
80 return 0; | |
81 | |
82 return info.st_size; | |
83 } | |
84 | |
85 static void | |
86 log_to_file(const gchar * domain, GLogLevelFlags level, | |
87 const gchar * message, gpointer data) | |
88 { | |
89 FILE *file = (FILE *) data; | |
90 | |
91 if (!file) { | |
92 g_printerr(G_STRLOC ": file is NULL!\n"); | |
93 return; | |
94 } | |
95 | |
4413
de33c83aa06f
Huge renaming patch, changing BMP references to Audacious.
Matti Hamalainen <ccr@tnsp.org>
parents:
3581
diff
changeset
|
96 G_LOCK(aud_log_file); |
2313 | 97 |
98 if (domain) | |
99 g_fprintf(file, "(%s) ", domain); | |
100 | |
101 if (message) | |
102 g_fprintf(file, "%s\n", message); | |
103 else | |
104 g_fprintf(file, "message is NULL!\n"); | |
105 | |
106 fflush(file); | |
107 | |
4413
de33c83aa06f
Huge renaming patch, changing BMP references to Audacious.
Matti Hamalainen <ccr@tnsp.org>
parents:
3581
diff
changeset
|
108 G_UNLOCK(aud_log_file); |
2313 | 109 } |
110 | |
111 gboolean | |
4413
de33c83aa06f
Huge renaming patch, changing BMP references to Audacious.
Matti Hamalainen <ccr@tnsp.org>
parents:
3581
diff
changeset
|
112 aud_logger_start(const gchar * filename) |
2313 | 113 { |
114 guint i; | |
115 | |
116 g_return_val_if_fail(filename != NULL, FALSE); | |
117 | |
118 /* truncate file when size limit is reached */ | |
119 if (get_filesize(filename) < BMP_LOGGER_FILE_MAX_SIZE) | |
4413
de33c83aa06f
Huge renaming patch, changing BMP references to Audacious.
Matti Hamalainen <ccr@tnsp.org>
parents:
3581
diff
changeset
|
120 aud_log_file = fopen(filename, "at"); |
2313 | 121 else |
4413
de33c83aa06f
Huge renaming patch, changing BMP references to Audacious.
Matti Hamalainen <ccr@tnsp.org>
parents:
3581
diff
changeset
|
122 aud_log_file = fopen(filename, "w+t"); |
2313 | 123 |
4413
de33c83aa06f
Huge renaming patch, changing BMP references to Audacious.
Matti Hamalainen <ccr@tnsp.org>
parents:
3581
diff
changeset
|
124 if (!aud_log_file) { |
2313 | 125 g_printerr(_("Unable to create log file (%s)!\n"), filename); |
126 return FALSE; | |
127 } | |
128 | |
129 for (i = 0; i < log_handler_count; i++) { | |
130 log_handlers[i].id = g_log_set_handler(log_handlers[i].domain, | |
131 log_handlers[i].level, | |
4413
de33c83aa06f
Huge renaming patch, changing BMP references to Audacious.
Matti Hamalainen <ccr@tnsp.org>
parents:
3581
diff
changeset
|
132 log_to_file, aud_log_file); |
2313 | 133 } |
134 | |
135 g_message("\n** LOGGING STARTED AT %s", get_timestamp_str()); | |
136 | |
137 return TRUE; | |
138 } | |
139 | |
140 void | |
4413
de33c83aa06f
Huge renaming patch, changing BMP references to Audacious.
Matti Hamalainen <ccr@tnsp.org>
parents:
3581
diff
changeset
|
141 aud_logger_stop(void) |
2313 | 142 { |
143 guint i; | |
144 | |
4413
de33c83aa06f
Huge renaming patch, changing BMP references to Audacious.
Matti Hamalainen <ccr@tnsp.org>
parents:
3581
diff
changeset
|
145 if (!aud_log_file) |
2313 | 146 return; |
147 | |
148 g_message("\n** LOGGING STOPPED AT %s", get_timestamp_str()); | |
149 | |
150 for (i = 0; i < log_handler_count; i++) | |
151 g_log_remove_handler(log_handlers[i].domain, log_handlers[i].id); | |
152 | |
4413
de33c83aa06f
Huge renaming patch, changing BMP references to Audacious.
Matti Hamalainen <ccr@tnsp.org>
parents:
3581
diff
changeset
|
153 fclose(aud_log_file); |
de33c83aa06f
Huge renaming patch, changing BMP references to Audacious.
Matti Hamalainen <ccr@tnsp.org>
parents:
3581
diff
changeset
|
154 aud_log_file = NULL; |
2313 | 155 } |