Mercurial > pidgin
comparison src/log.c @ 11503:cd0c8830d881
[gaim-migrate @ 13748]
Making gaim_log_logger a varargs function so it can be expanded as GaimLogLogger expands, without breaking compatibility. I'm anticipating adding a find() function some day for a database logger. This commit also makes use of gaim_log_logger_new() everywhere it should be used, removing the old static structures.
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Sun, 11 Sep 2005 05:14:52 +0000 |
parents | 4db38b374d3f |
children | 8004885fabbe |
comparison
equal
deleted
inserted
replaced
11502:b858f992b566 | 11503:cd0c8830d881 |
---|---|
31 #include "util.h" | 31 #include "util.h" |
32 #include "stringref.h" | 32 #include "stringref.h" |
33 | 33 |
34 static GSList *loggers = NULL; | 34 static GSList *loggers = NULL; |
35 | 35 |
36 static GaimLogLogger html_logger; | 36 static GaimLogLogger *html_logger; |
37 static GaimLogLogger txt_logger; | 37 static GaimLogLogger *txt_logger; |
38 static GaimLogLogger old_logger; | 38 static GaimLogLogger *old_logger; |
39 | 39 |
40 struct _gaim_logsize_user { | 40 struct _gaim_logsize_user { |
41 char *name; | 41 char *name; |
42 GaimAccount *account; | 42 GaimAccount *account; |
43 }; | 43 }; |
44 static GHashTable *logsize_users = NULL; | 44 static GHashTable *logsize_users = NULL; |
45 | 45 |
46 static void log_get_log_sets_common(GHashTable *sets); | 46 static void log_get_log_sets_common(GHashTable *sets); |
47 | |
48 static void html_logger_write(GaimLog *log, GaimMessageFlags type, | |
49 const char *from, time_t time, const char *message); | |
50 static void html_logger_finalize(GaimLog *log); | |
51 static GList *html_logger_list(GaimLogType type, const char *sn, GaimAccount *account); | |
52 static GList *html_logger_list_syslog(GaimAccount *account); | |
53 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags); | |
54 | |
55 static GList *old_logger_list(GaimLogType type, const char *sn, GaimAccount *account); | |
56 static int old_logger_total_size(GaimLogType type, const char *name, GaimAccount *account); | |
57 static char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags); | |
58 static int old_logger_size (GaimLog *log); | |
59 static void old_logger_get_log_sets(GaimLogSetCallback cb, GHashTable *sets); | |
60 static void old_logger_finalize(GaimLog *log); | |
61 | |
62 static void txt_logger_write(GaimLog *log, | |
63 GaimMessageFlags type, | |
64 const char *from, time_t time, const char *message); | |
65 static void txt_logger_finalize(GaimLog *log); | |
66 static GList *txt_logger_list(GaimLogType type, const char *sn, GaimAccount *account); | |
67 static GList *txt_logger_list_syslog(GaimAccount *account); | |
68 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags); | |
47 | 69 |
48 /************************************************************************** | 70 /************************************************************************** |
49 * PUBLIC LOGGING FUNCTIONS *********************************************** | 71 * PUBLIC LOGGING FUNCTIONS *********************************************** |
50 **************************************************************************/ | 72 **************************************************************************/ |
51 | 73 |
229 gaim_log_logger_set(logger); | 251 gaim_log_logger_set(logger); |
230 return; | 252 return; |
231 } | 253 } |
232 l = l->next; | 254 l = l->next; |
233 } | 255 } |
234 gaim_log_logger_set(&txt_logger); | 256 gaim_log_logger_set(txt_logger); |
235 } | 257 } |
236 | 258 |
237 | 259 |
238 GaimLogLogger *gaim_log_logger_new( | 260 GaimLogLogger *gaim_log_logger_new(const char *id, const char *name, int functions, ...) |
261 { | |
262 #if 0 | |
239 void(*create)(GaimLog *), | 263 void(*create)(GaimLog *), |
240 void(*write)(GaimLog *, GaimMessageFlags, const char *, time_t, const char *), | 264 void(*write)(GaimLog *, GaimMessageFlags, const char *, time_t, const char *), |
241 void(*finalize)(GaimLog *), | 265 void(*finalize)(GaimLog *), |
242 GList*(*list)(GaimLogType type, const char*, GaimAccount*), | 266 GList*(*list)(GaimLogType type, const char*, GaimAccount*), |
243 char*(*read)(GaimLog*, GaimLogReadFlags*), | 267 char*(*read)(GaimLog*, GaimLogReadFlags*), |
244 int(*size)(GaimLog*), | 268 int(*size)(GaimLog*), |
245 int(*total_size)(GaimLogType type, const char *name, GaimAccount *account), | 269 int(*total_size)(GaimLogType type, const char *name, GaimAccount *account), |
246 GList*(*list_syslog)(GaimAccount *account), | 270 GList*(*list_syslog)(GaimAccount *account), |
247 void(*get_log_sets)(GaimLogSetCallback cb, GHashTable *sets)) | 271 void(*get_log_sets)(GaimLogSetCallback cb, GHashTable *sets)) |
248 { | 272 { |
249 GaimLogLogger *logger = g_new0(GaimLogLogger, 1); | 273 #endif |
250 | 274 GaimLogLogger *logger; |
251 logger->create = create; | 275 va_list args; |
252 logger->write = write; | 276 |
253 logger->finalize = finalize; | 277 g_return_val_if_fail(id != NULL, NULL); |
254 logger->list = list; | 278 g_return_val_if_fail(name != NULL, NULL); |
255 logger->read = read; | 279 g_return_val_if_fail(functions >= 1, NULL); |
256 logger->size = size; | 280 |
257 logger->total_size = total_size; | 281 logger = g_new0(GaimLogLogger, 1); |
258 logger->list_syslog = list_syslog; | 282 logger->id = g_strdup(id); |
259 logger->get_log_sets = get_log_sets; | 283 logger->name = g_strdup(name); |
284 | |
285 va_start(args, functions); | |
286 | |
287 if (functions >= 1) | |
288 logger->create = va_arg(args, void *); | |
289 if (functions >= 2) | |
290 logger->write = va_arg(args, void *); | |
291 if (functions >= 3) | |
292 logger->finalize = va_arg(args, void *); | |
293 if (functions >= 4) | |
294 logger->list = va_arg(args, void *); | |
295 if (functions >= 5) | |
296 logger->read = va_arg(args, void *); | |
297 if (functions >= 6) | |
298 logger->size = va_arg(args, void *); | |
299 if (functions >= 7) | |
300 logger->total_size = va_arg(args, void *); | |
301 if (functions >= 8) | |
302 logger->list_syslog = va_arg(args, void *); | |
303 if (functions >= 9) | |
304 logger->get_log_sets = va_arg(args, void *); | |
305 | |
306 if (functions > 9) | |
307 gaim_debug_info("log", "Dropping new functions for logger: %s (%s)\n", name, id); | |
308 | |
309 va_end(args); | |
260 | 310 |
261 return logger; | 311 return logger; |
262 } | 312 } |
263 | 313 |
264 void gaim_log_logger_free(GaimLogLogger *logger) | 314 void gaim_log_logger_free(GaimLogLogger *logger) |
265 { | 315 { |
316 g_free(logger->name); | |
317 g_free(logger->id); | |
266 g_free(logger); | 318 g_free(logger); |
267 } | 319 } |
268 | 320 |
269 void gaim_log_logger_add (GaimLogLogger *logger) | 321 void gaim_log_logger_add (GaimLogLogger *logger) |
270 { | 322 { |
440 gaim_prefs_add_bool("/core/logging/log_idle_state", FALSE); | 492 gaim_prefs_add_bool("/core/logging/log_idle_state", FALSE); |
441 gaim_prefs_add_bool("/core/logging/log_away_state", FALSE); | 493 gaim_prefs_add_bool("/core/logging/log_away_state", FALSE); |
442 gaim_prefs_add_bool("/core/logging/log_own_states", FALSE); | 494 gaim_prefs_add_bool("/core/logging/log_own_states", FALSE); |
443 | 495 |
444 gaim_prefs_add_string("/core/logging/format", "txt"); | 496 gaim_prefs_add_string("/core/logging/format", "txt"); |
445 gaim_log_logger_add(&html_logger); | 497 |
446 gaim_log_logger_add(&txt_logger); | 498 html_logger = gaim_log_logger_new("html", "HTML", 8, |
447 gaim_log_logger_add(&old_logger); | 499 NULL, |
500 html_logger_write, | |
501 html_logger_finalize, | |
502 html_logger_list, | |
503 html_logger_read, | |
504 gaim_log_common_sizer, | |
505 NULL, | |
506 html_logger_list_syslog); | |
507 gaim_log_logger_add(html_logger); | |
508 | |
509 txt_logger = gaim_log_logger_new("txt", "Plain text", 8, | |
510 NULL, | |
511 txt_logger_write, | |
512 txt_logger_finalize, | |
513 txt_logger_list, | |
514 txt_logger_read, | |
515 gaim_log_common_sizer, | |
516 NULL, | |
517 txt_logger_list_syslog); | |
518 gaim_log_logger_add(txt_logger); | |
519 | |
520 old_logger = gaim_log_logger_new("old", "Old Gaim", 9, | |
521 NULL, | |
522 NULL, | |
523 old_logger_finalize, | |
524 old_logger_list, | |
525 old_logger_read, | |
526 old_logger_size, | |
527 old_logger_total_size, | |
528 NULL, | |
529 old_logger_get_log_sets); | |
530 gaim_log_logger_add(old_logger); | |
531 | |
448 gaim_prefs_connect_callback(NULL, "/core/logging/format", | 532 gaim_prefs_connect_callback(NULL, "/core/logging/format", |
449 logger_pref_cb, NULL); | 533 logger_pref_cb, NULL); |
450 gaim_prefs_trigger_callback("/core/logging/format"); | 534 gaim_prefs_trigger_callback("/core/logging/format"); |
451 | 535 |
452 logsize_users = g_hash_table_new_full((GHashFunc)_gaim_logsize_user_hash, | 536 logsize_users = g_hash_table_new_full((GHashFunc)_gaim_logsize_user_hash, |
453 (GEqualFunc)_gaim_logsize_user_equal, | 537 (GEqualFunc)_gaim_logsize_user_equal, |
454 (GDestroyNotify)_gaim_logsize_user_free_key, NULL); | 538 (GDestroyNotify)_gaim_logsize_user_free_key, NULL); |
767 /**************************** | 851 /**************************** |
768 ** HTML LOGGER ************* | 852 ** HTML LOGGER ************* |
769 ****************************/ | 853 ****************************/ |
770 | 854 |
771 static void html_logger_write(GaimLog *log, GaimMessageFlags type, | 855 static void html_logger_write(GaimLog *log, GaimMessageFlags type, |
772 const char *from, time_t time, const char *message) | 856 const char *from, time_t time, const char *message) |
773 { | 857 { |
774 char *msg_fixed; | 858 char *msg_fixed; |
775 char date[64]; | 859 char date[64]; |
776 GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); | 860 GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); |
777 GaimLogCommonLoggerData *data = log->logger_data; | 861 GaimLogCommonLoggerData *data = log->logger_data; |
855 } | 939 } |
856 } | 940 } |
857 | 941 |
858 static GList *html_logger_list(GaimLogType type, const char *sn, GaimAccount *account) | 942 static GList *html_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
859 { | 943 { |
860 return gaim_log_common_lister(type, sn, account, ".html", &html_logger); | 944 return gaim_log_common_lister(type, sn, account, ".html", html_logger); |
861 } | 945 } |
862 | 946 |
863 static GList *html_logger_list_syslog(GaimAccount *account) | 947 static GList *html_logger_list_syslog(GaimAccount *account) |
864 { | 948 { |
865 return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".html", &html_logger); | 949 return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".html", html_logger); |
866 } | 950 } |
867 | 951 |
868 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) | 952 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) |
869 { | 953 { |
870 char *read, *minus_header; | 954 char *read, *minus_header; |
882 return minus_header; | 966 return minus_header; |
883 } | 967 } |
884 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); | 968 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); |
885 } | 969 } |
886 | 970 |
887 static GaimLogLogger html_logger = { | |
888 N_("HTML"), "html", | |
889 NULL, | |
890 html_logger_write, | |
891 html_logger_finalize, | |
892 html_logger_list, | |
893 html_logger_read, | |
894 gaim_log_common_sizer, | |
895 NULL, | |
896 html_logger_list_syslog, | |
897 NULL | |
898 }; | |
899 | |
900 | |
901 | 971 |
902 | 972 |
903 /**************************** | 973 /**************************** |
904 ** PLAIN TEXT LOGGER ******* | 974 ** PLAIN TEXT LOGGER ******* |
905 ****************************/ | 975 ****************************/ |
906 | 976 |
907 static void txt_logger_write(GaimLog *log, | 977 static void txt_logger_write(GaimLog *log, |
908 GaimMessageFlags type, | 978 GaimMessageFlags type, |
909 const char *from, time_t time, const char *message) | 979 const char *from, time_t time, const char *message) |
910 { | 980 { |
911 char date[64]; | 981 char date[64]; |
912 GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); | 982 GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); |
913 GaimLogCommonLoggerData *data = log->logger_data; | 983 GaimLogCommonLoggerData *data = log->logger_data; |
914 char *stripped = NULL; | 984 char *stripped = NULL; |
915 | 985 |
916 if(!data) { | 986 if (data == NULL) { |
917 /* This log is new. We could use the loggers 'new' function, but | 987 /* This log is new. We could use the loggers 'new' function, but |
918 * creating a new file there would result in empty files in the case | 988 * creating a new file there would result in empty files in the case |
919 * that you open a convo with someone, but don't say anything. | 989 * that you open a convo with someone, but don't say anything. |
920 */ | 990 */ |
921 const char *prpl = | 991 const char *prpl = |
986 } | 1056 } |
987 } | 1057 } |
988 | 1058 |
989 static GList *txt_logger_list(GaimLogType type, const char *sn, GaimAccount *account) | 1059 static GList *txt_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
990 { | 1060 { |
991 return gaim_log_common_lister(type, sn, account, ".txt", &txt_logger); | 1061 return gaim_log_common_lister(type, sn, account, ".txt", txt_logger); |
992 } | 1062 } |
993 | 1063 |
994 static GList *txt_logger_list_syslog(GaimAccount *account) | 1064 static GList *txt_logger_list_syslog(GaimAccount *account) |
995 { | 1065 { |
996 return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".txt", &txt_logger); | 1066 return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".txt", txt_logger); |
997 } | 1067 } |
998 | 1068 |
999 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) | 1069 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) |
1000 { | 1070 { |
1001 char *read, *minus_header, *minus_header2; | 1071 char *read, *minus_header, *minus_header2; |
1015 return minus_header2; | 1085 return minus_header2; |
1016 } | 1086 } |
1017 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); | 1087 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); |
1018 } | 1088 } |
1019 | 1089 |
1020 static GaimLogLogger txt_logger = { | 1090 |
1021 N_("Plain text"), "txt", | |
1022 NULL, | |
1023 txt_logger_write, | |
1024 txt_logger_finalize, | |
1025 txt_logger_list, | |
1026 txt_logger_read, | |
1027 gaim_log_common_sizer, | |
1028 NULL, | |
1029 txt_logger_list_syslog, | |
1030 NULL | |
1031 }; | |
1032 | 1091 |
1033 /**************** | 1092 /**************** |
1034 * OLD LOGGER *** | 1093 * OLD LOGGER *** |
1035 ****************/ | 1094 ****************/ |
1036 | 1095 |
1101 if(strchr(buf, '\r')) | 1160 if(strchr(buf, '\r')) |
1102 newlen--; | 1161 newlen--; |
1103 | 1162 |
1104 if (newlen != 0) { | 1163 if (newlen != 0) { |
1105 log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1); | 1164 log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1); |
1106 log->logger = &old_logger; | 1165 log->logger = old_logger; |
1107 log->time = lasttime; | 1166 log->time = lasttime; |
1108 data = g_new0(struct old_logger_data, 1); | 1167 data = g_new0(struct old_logger_data, 1); |
1109 data->offset = lastoff; | 1168 data->offset = lastoff; |
1110 data->length = newlen; | 1169 data->length = newlen; |
1111 data->pathref = gaim_stringref_ref(pathref); | 1170 data->pathref = gaim_stringref_ref(pathref); |
1152 } | 1211 } |
1153 | 1212 |
1154 if (logfound) { | 1213 if (logfound) { |
1155 if ((newlen = ftell(file) - lastoff) != 0) { | 1214 if ((newlen = ftell(file) - lastoff) != 0) { |
1156 log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1); | 1215 log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1); |
1157 log->logger = &old_logger; | 1216 log->logger = old_logger; |
1158 log->time = lasttime; | 1217 log->time = lasttime; |
1159 data = g_new0(struct old_logger_data, 1); | 1218 data = g_new0(struct old_logger_data, 1); |
1160 data->offset = lastoff; | 1219 data->offset = lastoff; |
1161 data->length = newlen; | 1220 data->length = newlen; |
1162 data->pathref = gaim_stringref_ref(pathref); | 1221 data->pathref = gaim_stringref_ref(pathref); |
1298 { | 1357 { |
1299 struct old_logger_data *data = log->logger_data; | 1358 struct old_logger_data *data = log->logger_data; |
1300 gaim_stringref_unref(data->pathref); | 1359 gaim_stringref_unref(data->pathref); |
1301 g_free(data); | 1360 g_free(data); |
1302 } | 1361 } |
1303 | |
1304 static GaimLogLogger old_logger = { | |
1305 "old logger", "old", | |
1306 NULL, NULL, | |
1307 old_logger_finalize, | |
1308 old_logger_list, | |
1309 old_logger_read, | |
1310 old_logger_size, | |
1311 old_logger_total_size, | |
1312 NULL, | |
1313 old_logger_get_log_sets | |
1314 }; |