Mercurial > audlegacy
annotate src/audacious/logger.c @ 3016:e84073b61ba5 trunk
fix tinyplayer skin
| author | Tomasz Mon <desowin@gmail.com> |
|---|---|
| date | Mon, 09 Jul 2007 13:02:50 +0200 |
| parents | d226b83fa329 |
| children | 4c758281fe8f |
| rev | line source |
|---|---|
|
2865
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
1 /* |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
2 * audacious: Cross-platform multimedia player. |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
3 * logger.c: Event logging. |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
4 * |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
5 * Copyright (c) 2005-2007 Audacious development team. |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
6 * Copyright (c) 2003-2005 BMP development team. |
| 2313 | 7 * |
|
2865
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
8 * Redistribution and use in source and binary forms, with or without |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
9 * modification, are permitted provided that the following conditions are |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
10 * met: |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
11 * |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
12 * 1. Redistributions of source code must retain the above copyright notice, |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
13 * this list of conditions and the following disclaimer. |
| 2313 | 14 * |
|
2865
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
15 * 2. Redistributions in binary form must reproduce the above copyright |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
16 * notice, this list of conditions and the following disclaimer in the |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
17 * documentation and/or other materials provided with the distribution. |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
18 * |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
19 * 3. The name of the author may not be used to endorse or promote products |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
20 * derived from this software without specific prior written permission. |
| 2313 | 21 * |
|
2865
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2313
diff
changeset
|
32 * POSSIBILITY OF SUCH DAMAGE. |
| 2313 | 33 */ |
| 34 | |
| 35 #ifdef HAVE_CONFIG_H | |
| 36 # include "config.h" | |
| 37 #endif | |
| 38 | |
| 39 #include "logger.h" | |
| 40 | |
| 41 #include <glib.h> | |
| 42 #include <glib/gi18n.h> | |
| 43 #include <glib/gprintf.h> | |
| 44 #include <stdio.h> | |
| 45 #include <stdlib.h> | |
| 46 #include <sys/stat.h> | |
| 47 #include <time.h> | |
| 48 | |
| 49 #include "main.h" | |
| 50 | |
| 51 | |
| 52 #define LOG_ALL_LEVELS \ | |
| 53 (G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION) | |
| 54 | |
| 55 | |
| 56 struct _LogHandler { | |
| 57 gchar *domain; | |
| 58 GLogLevelFlags level; | |
| 59 guint id; | |
| 60 }; | |
| 61 | |
| 62 typedef struct _LogHandler LogHandler; | |
| 63 | |
| 64 | |
| 65 static FILE *bmp_log_file = NULL; | |
| 66 | |
| 67 G_LOCK_DEFINE_STATIC(bmp_log_file); | |
| 68 | |
| 69 static LogHandler log_handlers[] = { | |
| 70 {NULL, LOG_ALL_LEVELS, 0}, | |
| 71 {"Glib", LOG_ALL_LEVELS, 0}, | |
| 72 {"Gtk", LOG_ALL_LEVELS, 0} | |
| 73 }; | |
| 74 | |
| 75 static guint log_handler_count = G_N_ELEMENTS(log_handlers); | |
| 76 | |
| 77 | |
| 78 static const gchar * | |
| 79 get_timestamp_str(void) | |
| 80 { | |
| 81 time_t current_time = time(NULL); | |
| 82 return ctime(¤t_time); | |
| 83 } | |
| 84 | |
| 85 static size_t | |
| 86 get_filesize(const gchar *filename) | |
| 87 { | |
| 88 struct stat info; | |
| 89 | |
| 90 if (stat(filename, &info)) | |
| 91 return 0; | |
| 92 | |
| 93 return info.st_size; | |
| 94 } | |
| 95 | |
| 96 static void | |
| 97 log_to_file(const gchar * domain, GLogLevelFlags level, | |
| 98 const gchar * message, gpointer data) | |
| 99 { | |
| 100 FILE *file = (FILE *) data; | |
| 101 | |
| 102 if (!file) { | |
| 103 g_printerr(G_STRLOC ": file is NULL!\n"); | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 G_LOCK(bmp_log_file); | |
| 108 | |
| 109 if (domain) | |
| 110 g_fprintf(file, "(%s) ", domain); | |
| 111 | |
| 112 if (message) | |
| 113 g_fprintf(file, "%s\n", message); | |
| 114 else | |
| 115 g_fprintf(file, "message is NULL!\n"); | |
| 116 | |
| 117 fflush(file); | |
| 118 | |
| 119 G_UNLOCK(bmp_log_file); | |
| 120 } | |
| 121 | |
| 122 gboolean | |
| 123 bmp_logger_start(const gchar * filename) | |
| 124 { | |
| 125 guint i; | |
| 126 | |
| 127 g_return_val_if_fail(filename != NULL, FALSE); | |
| 128 | |
| 129 /* truncate file when size limit is reached */ | |
| 130 if (get_filesize(filename) < BMP_LOGGER_FILE_MAX_SIZE) | |
| 131 bmp_log_file = fopen(filename, "at"); | |
| 132 else | |
| 133 bmp_log_file = fopen(filename, "w+t"); | |
| 134 | |
| 135 if (!bmp_log_file) { | |
| 136 g_printerr(_("Unable to create log file (%s)!\n"), filename); | |
| 137 return FALSE; | |
| 138 } | |
| 139 | |
| 140 for (i = 0; i < log_handler_count; i++) { | |
| 141 log_handlers[i].id = g_log_set_handler(log_handlers[i].domain, | |
| 142 log_handlers[i].level, | |
| 143 log_to_file, bmp_log_file); | |
| 144 } | |
| 145 | |
| 146 g_message("\n** LOGGING STARTED AT %s", get_timestamp_str()); | |
| 147 | |
| 148 return TRUE; | |
| 149 } | |
| 150 | |
| 151 void | |
| 152 bmp_logger_stop(void) | |
| 153 { | |
| 154 guint i; | |
| 155 | |
| 156 if (!bmp_log_file) | |
| 157 return; | |
| 158 | |
| 159 g_message("\n** LOGGING STOPPED AT %s", get_timestamp_str()); | |
| 160 | |
| 161 for (i = 0; i < log_handler_count; i++) | |
| 162 g_log_remove_handler(log_handlers[i].domain, log_handlers[i].id); | |
| 163 | |
| 164 fclose(bmp_log_file); | |
| 165 bmp_log_file = NULL; | |
| 166 } |
