Mercurial > pidgin
annotate 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 |
| rev | line source |
|---|---|
| 7431 | 1 /** |
| 2 * @file log.c Logging API | |
| 3 * @ingroup core | |
| 4 * | |
| 5 * gaim | |
| 6 * | |
| 8046 | 7 * Gaim is the legal property of its developers, whose names are too numerous |
| 8 * to list here. Please refer to the COPYRIGHT file distributed with this | |
| 9 * source distribution. | |
| 7436 | 10 * |
| 7431 | 11 * This program is free software; you can redistribute it and/or modify |
| 12 * it under the terms of the GNU General Public License as published by | |
| 13 * the Free Software Foundation; either version 2 of the License, or | |
| 14 * (at your option) any later version. | |
| 15 * | |
| 16 * This program is distributed in the hope that it will be useful, | |
| 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 19 * GNU General Public License for more details. | |
| 20 * | |
| 21 * You should have received a copy of the GNU General Public License | |
| 22 * along with this program; if not, write to the Free Software | |
| 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 4184 | 24 */ |
| 4195 | 25 |
| 7431 | 26 #include "account.h" |
|
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
27 #include "debug.h" |
| 7431 | 28 #include "internal.h" |
|
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
29 #include "log.h" |
| 5548 | 30 #include "prefs.h" |
|
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
31 #include "util.h" |
| 7764 | 32 #include "stringref.h" |
|
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
33 |
| 8096 | 34 static GSList *loggers = NULL; |
| 35 | |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
36 static GaimLogLogger *html_logger; |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
37 static GaimLogLogger *txt_logger; |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
38 static GaimLogLogger *old_logger; |
|
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
39 |
| 8635 | 40 struct _gaim_logsize_user { |
| 41 char *name; | |
| 42 GaimAccount *account; | |
| 43 }; | |
| 44 static GHashTable *logsize_users = NULL; | |
| 45 | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
46 static void log_get_log_sets_common(GHashTable *sets); |
| 8635 | 47 |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
48 static void html_logger_write(GaimLog *log, GaimMessageFlags type, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
49 const char *from, time_t time, const char *message); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
50 static void html_logger_finalize(GaimLog *log); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
51 static GList *html_logger_list(GaimLogType type, const char *sn, GaimAccount *account); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
52 static GList *html_logger_list_syslog(GaimAccount *account); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
53 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
54 |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
55 static GList *old_logger_list(GaimLogType type, const char *sn, GaimAccount *account); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
56 static int old_logger_total_size(GaimLogType type, const char *name, GaimAccount *account); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
57 static char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
58 static int old_logger_size (GaimLog *log); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
59 static void old_logger_get_log_sets(GaimLogSetCallback cb, GHashTable *sets); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
60 static void old_logger_finalize(GaimLog *log); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
61 |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
62 static void txt_logger_write(GaimLog *log, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
63 GaimMessageFlags type, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
64 const char *from, time_t time, const char *message); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
65 static void txt_logger_finalize(GaimLog *log); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
66 static GList *txt_logger_list(GaimLogType type, const char *sn, GaimAccount *account); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
67 static GList *txt_logger_list_syslog(GaimAccount *account); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
68 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
69 |
| 7431 | 70 /************************************************************************** |
| 71 * PUBLIC LOGGING FUNCTIONS *********************************************** | |
| 72 **************************************************************************/ | |
| 4184 | 73 |
|
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
74 GaimLog *gaim_log_new(GaimLogType type, const char *name, GaimAccount *account, |
|
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
75 GaimConversation *conv, time_t time) |
| 7431 | 76 { |
| 77 GaimLog *log = g_new0(GaimLog, 1); | |
| 8635 | 78 log->name = g_strdup(gaim_normalize(account, name)); |
| 7431 | 79 log->account = account; |
|
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
80 log->conv = conv; |
| 7431 | 81 log->time = time; |
| 8573 | 82 log->type = type; |
| 8096 | 83 log->logger_data = NULL; |
| 7431 | 84 log->logger = gaim_log_logger_get(); |
| 7440 | 85 if (log->logger && log->logger->create) |
| 86 log->logger->create(log); | |
| 7431 | 87 return log; |
| 4184 | 88 } |
| 89 | |
| 7431 | 90 void gaim_log_free(GaimLog *log) |
| 4184 | 91 { |
| 7431 | 92 g_return_if_fail(log); |
| 93 if (log->logger && log->logger->finalize) | |
| 94 log->logger->finalize(log); | |
| 95 g_free(log->name); | |
| 96 g_free(log); | |
| 97 } | |
| 7436 | 98 |
| 99 void gaim_log_write(GaimLog *log, GaimMessageFlags type, | |
| 7431 | 100 const char *from, time_t time, const char *message) |
| 101 { | |
| 10173 | 102 struct _gaim_logsize_user *lu; |
| 103 | |
| 7431 | 104 g_return_if_fail(log); |
| 105 g_return_if_fail(log->logger); | |
| 7442 | 106 g_return_if_fail(log->logger->write); |
| 7431 | 107 |
|
10171
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
108 (log->logger->write)(log, type, from, time, message); |
|
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
109 |
|
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
110 lu = g_new(struct _gaim_logsize_user, 1); |
| 9892 | 111 |
|
10171
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
112 lu->name = g_strdup(gaim_normalize(log->account, log->name)); |
|
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
113 lu->account = log->account; |
|
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
114 g_hash_table_remove(logsize_users, lu); |
|
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
115 g_free(lu->name); |
|
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
116 g_free(lu); |
| 9892 | 117 |
| 4184 | 118 } |
| 119 | |
| 7431 | 120 char *gaim_log_read(GaimLog *log, GaimLogReadFlags *flags) |
| 4184 | 121 { |
| 7542 | 122 GaimLogReadFlags mflags; |
| 7431 | 123 g_return_val_if_fail(log && log->logger, NULL); |
| 7462 | 124 if (log->logger->read) { |
| 7535 | 125 char *ret = (log->logger->read)(log, flags ? flags : &mflags); |
|
7478
3c21f3084ff0
[gaim-migrate @ 8091]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7473
diff
changeset
|
126 gaim_str_strip_cr(ret); |
| 7462 | 127 return ret; |
| 128 } | |
| 7470 | 129 return (_("<b><font color=\"red\">The logger has no read function</font></b>")); |
| 4184 | 130 } |
| 7616 | 131 |
| 7556 | 132 int gaim_log_get_size(GaimLog *log) |
| 133 { | |
| 134 g_return_val_if_fail(log && log->logger, 0); | |
| 8096 | 135 |
| 7556 | 136 if (log->logger->size) |
| 137 return log->logger->size(log); | |
| 138 return 0; | |
| 139 } | |
| 140 | |
| 8635 | 141 static guint _gaim_logsize_user_hash(struct _gaim_logsize_user *lu) |
| 142 { | |
| 143 return g_str_hash(lu->name); | |
| 144 } | |
| 145 | |
| 146 static guint _gaim_logsize_user_equal(struct _gaim_logsize_user *lu1, | |
| 147 struct _gaim_logsize_user *lu2) | |
| 148 { | |
| 149 return ((!strcmp(lu1->name, lu2->name)) && lu1->account == lu2->account); | |
| 150 } | |
| 151 | |
| 152 static void _gaim_logsize_user_free_key(struct _gaim_logsize_user *lu) | |
| 153 { | |
| 154 g_free(lu->name); | |
| 155 g_free(lu); | |
| 156 } | |
| 157 | |
| 8898 | 158 int gaim_log_get_total_size(GaimLogType type, const char *name, GaimAccount *account) |
| 7556 | 159 { |
| 9677 | 160 gpointer ptrsize; |
| 161 int size = 0; | |
| 8096 | 162 GSList *n; |
| 8635 | 163 struct _gaim_logsize_user *lu; |
| 8096 | 164 |
| 8635 | 165 lu = g_new(struct _gaim_logsize_user, 1); |
| 166 lu->name = g_strdup(gaim_normalize(account, name)); | |
| 167 lu->account = account; | |
| 168 | |
| 9677 | 169 if(g_hash_table_lookup_extended(logsize_users, lu, NULL, &ptrsize)) { |
| 170 size = GPOINTER_TO_INT(ptrsize); | |
| 8635 | 171 g_free(lu->name); |
| 172 g_free(lu); | |
| 173 } else { | |
| 174 for (n = loggers; n; n = n->next) { | |
| 175 GaimLogLogger *logger = n->data; | |
| 7616 | 176 |
| 8635 | 177 if(logger->total_size){ |
| 8898 | 178 size += (logger->total_size)(type, name, account); |
| 8635 | 179 } else if(logger->list) { |
| 8898 | 180 GList *logs = (logger->list)(type, name, account); |
| 8635 | 181 int this_size = 0; |
| 182 | |
| 183 while (logs) { | |
| 184 GList *logs2 = logs->next; | |
| 185 GaimLog *log = (GaimLog*)(logs->data); | |
| 186 this_size += gaim_log_get_size(log); | |
| 187 gaim_log_free(log); | |
| 188 g_list_free_1(logs); | |
| 189 logs = logs2; | |
| 190 } | |
| 191 | |
| 192 size += this_size; | |
| 8096 | 193 } |
| 8635 | 194 } |
| 8096 | 195 |
| 8635 | 196 g_hash_table_replace(logsize_users, lu, GINT_TO_POINTER(size)); |
| 7556 | 197 } |
| 198 return size; | |
| 199 } | |
| 4184 | 200 |
| 10822 | 201 char * |
| 10577 | 202 gaim_log_get_log_dir(GaimLogType type, const char *name, GaimAccount *account) |
| 203 { | |
| 204 GaimPlugin *prpl; | |
| 205 GaimPluginProtocolInfo *prpl_info; | |
| 206 const char *prpl_name; | |
| 207 char *acct_name; | |
| 9926 | 208 const char *target; |
| 10577 | 209 char *dir; |
| 9926 | 210 |
| 10577 | 211 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); |
| 212 if (!prpl) | |
| 213 return NULL; | |
| 214 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
| 215 prpl_name = prpl_info->list_icon(account, NULL); | |
| 216 | |
| 217 acct_name = g_strdup(gaim_escape_filename(gaim_normalize(account, | |
| 218 gaim_account_get_username(account)))); | |
| 9923 | 219 |
| 220 if (type == GAIM_LOG_CHAT) { | |
| 221 char *temp = g_strdup_printf("%s.chat", gaim_normalize(account, name)); | |
| 9926 | 222 target = gaim_escape_filename(temp); |
| 9923 | 223 g_free(temp); |
| 224 } else if(type == GAIM_LOG_SYSTEM) { | |
| 9926 | 225 target = ".system"; |
| 9923 | 226 } else { |
| 9926 | 227 target = gaim_escape_filename(gaim_normalize(account, name)); |
| 9923 | 228 } |
| 229 | |
| 10577 | 230 dir = g_build_filename(gaim_user_dir(), "logs", prpl_name, acct_name, target, NULL); |
| 9926 | 231 |
| 9923 | 232 g_free(acct_name); |
| 10577 | 233 |
| 9923 | 234 return dir; |
| 235 } | |
| 236 | |
| 7431 | 237 /**************************************************************************** |
| 238 * LOGGER FUNCTIONS ********************************************************* | |
| 239 ****************************************************************************/ | |
| 4184 | 240 |
| 7431 | 241 static GaimLogLogger *current_logger = NULL; |
| 7436 | 242 |
| 7431 | 243 static void logger_pref_cb(const char *name, GaimPrefType type, |
| 244 gpointer value, gpointer data) | |
| 245 { | |
| 246 GaimLogLogger *logger; | |
| 247 GSList *l = loggers; | |
| 248 while (l) { | |
| 249 logger = l->data; | |
| 250 if (!strcmp(logger->id, value)) { | |
| 251 gaim_log_logger_set(logger); | |
| 252 return; | |
| 4184 | 253 } |
| 7431 | 254 l = l->next; |
| 255 } | |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
256 gaim_log_logger_set(txt_logger); |
| 7431 | 257 } |
| 4184 | 258 |
| 259 | |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
260 GaimLogLogger *gaim_log_logger_new(const char *id, const char *name, int functions, ...) |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
261 { |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
262 #if 0 |
| 8898 | 263 void(*create)(GaimLog *), |
| 264 void(*write)(GaimLog *, GaimMessageFlags, const char *, time_t, const char *), | |
| 265 void(*finalize)(GaimLog *), | |
| 266 GList*(*list)(GaimLogType type, const char*, GaimAccount*), | |
| 267 char*(*read)(GaimLog*, GaimLogReadFlags*), | |
| 11025 | 268 int(*size)(GaimLog*), |
| 269 int(*total_size)(GaimLogType type, const char *name, GaimAccount *account), | |
| 270 GList*(*list_syslog)(GaimAccount *account), | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
271 void(*get_log_sets)(GaimLogSetCallback cb, GHashTable *sets)) |
| 7431 | 272 { |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
273 #endif |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
274 GaimLogLogger *logger; |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
275 va_list args; |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
276 |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
277 g_return_val_if_fail(id != NULL, NULL); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
278 g_return_val_if_fail(name != NULL, NULL); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
279 g_return_val_if_fail(functions >= 1, NULL); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
280 |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
281 logger = g_new0(GaimLogLogger, 1); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
282 logger->id = g_strdup(id); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
283 logger->name = g_strdup(name); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
284 |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
285 va_start(args, functions); |
| 11025 | 286 |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
287 if (functions >= 1) |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
288 logger->create = va_arg(args, void *); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
289 if (functions >= 2) |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
290 logger->write = va_arg(args, void *); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
291 if (functions >= 3) |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
292 logger->finalize = va_arg(args, void *); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
293 if (functions >= 4) |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
294 logger->list = va_arg(args, void *); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
295 if (functions >= 5) |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
296 logger->read = va_arg(args, void *); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
297 if (functions >= 6) |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
298 logger->size = va_arg(args, void *); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
299 if (functions >= 7) |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
300 logger->total_size = va_arg(args, void *); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
301 if (functions >= 8) |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
302 logger->list_syslog = va_arg(args, void *); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
303 if (functions >= 9) |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
304 logger->get_log_sets = va_arg(args, void *); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
305 |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
306 if (functions > 9) |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
307 gaim_debug_info("log", "Dropping new functions for logger: %s (%s)\n", name, id); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
308 |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
309 va_end(args); |
| 11025 | 310 |
| 7431 | 311 return logger; |
| 4184 | 312 } |
| 313 | |
| 7431 | 314 void gaim_log_logger_free(GaimLogLogger *logger) |
| 4184 | 315 { |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
316 g_free(logger->name); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
317 g_free(logger->id); |
| 7431 | 318 g_free(logger); |
| 319 } | |
| 4184 | 320 |
| 7431 | 321 void gaim_log_logger_add (GaimLogLogger *logger) |
| 322 { | |
| 323 g_return_if_fail(logger); | |
| 324 if (g_slist_find(loggers, logger)) | |
| 325 return; | |
| 326 loggers = g_slist_append(loggers, logger); | |
| 327 } | |
| 328 | |
| 329 void gaim_log_logger_remove (GaimLogLogger *logger) | |
| 330 { | |
| 331 g_return_if_fail(logger); | |
| 332 g_slist_remove(loggers, logger); | |
| 4184 | 333 } |
| 334 | |
| 7431 | 335 void gaim_log_logger_set (GaimLogLogger *logger) |
| 4184 | 336 { |
| 7431 | 337 g_return_if_fail(logger); |
| 338 current_logger = logger; | |
| 7436 | 339 } |
| 4184 | 340 |
| 7431 | 341 GaimLogLogger *gaim_log_logger_get() |
| 342 { | |
| 343 return current_logger; | |
| 344 } | |
| 4184 | 345 |
| 7431 | 346 GList *gaim_log_logger_get_options(void) |
| 347 { | |
| 348 GSList *n; | |
| 349 GList *list = NULL; | |
| 350 GaimLogLogger *data; | |
| 4184 | 351 |
| 7431 | 352 for (n = loggers; n; n = n->next) { |
| 353 data = n->data; | |
| 354 if (!data->write) | |
| 355 continue; | |
| 7494 | 356 list = g_list_append(list, _(data->name)); |
| 7431 | 357 list = g_list_append(list, data->id); |
| 4184 | 358 } |
| 359 | |
| 7431 | 360 return list; |
| 361 } | |
| 362 | |
| 8573 | 363 gint gaim_log_compare(gconstpointer y, gconstpointer z) |
| 7431 | 364 { |
| 7436 | 365 const GaimLog *a = y; |
| 366 const GaimLog *b = z; | |
| 367 | |
| 7431 | 368 return b->time - a->time; |
| 369 } | |
| 370 | |
| 8898 | 371 GList *gaim_log_get_logs(GaimLogType type, const char *name, GaimAccount *account) |
| 7431 | 372 { |
| 373 GList *logs = NULL; | |
| 374 GSList *n; | |
| 375 for (n = loggers; n; n = n->next) { | |
| 376 GaimLogLogger *logger = n->data; | |
| 377 if (!logger->list) | |
| 378 continue; | |
| 8898 | 379 logs = g_list_concat(logs, logger->list(type, name, account)); |
| 7431 | 380 } |
| 7436 | 381 |
| 8573 | 382 return g_list_sort(logs, gaim_log_compare); |
| 383 } | |
| 384 | |
| 11025 | 385 gint gaim_log_set_compare(gconstpointer y, gconstpointer z) |
| 386 { | |
| 387 const GaimLogSet *a = y; | |
| 388 const GaimLogSet *b = z; | |
| 389 gint ret = 0; | |
| 390 | |
| 391 /* This logic seems weird at first... | |
| 392 * If either account is NULL, we pretend the accounts are | |
| 393 * equal. This allows us to detect duplicates that will | |
| 394 * exist if one logger knows the account and another | |
| 395 * doesn't. */ | |
| 396 if (a->account != NULL && b->account != NULL) { | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
397 ret = strcmp(gaim_account_get_username(a->account), gaim_account_get_username(b->account)); |
| 11025 | 398 if (ret != 0) |
| 399 return ret; | |
| 400 } | |
| 401 | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
402 ret = strcmp(a->normalized_name, b->normalized_name); |
| 11025 | 403 if (ret != 0) |
| 404 return ret; | |
| 405 | |
| 406 return (gint)b->type - (gint)a->type; | |
| 407 } | |
| 408 | |
| 409 guint log_set_hash(gconstpointer key) | |
| 410 { | |
| 411 const GaimLogSet *set = key; | |
| 412 | |
| 413 /* The account isn't hashed because we need GaimLogSets with NULL accounts | |
| 414 * to be found when we search by a GaimLogSet that has a non-NULL account | |
| 415 * but the same type and name. */ | |
| 416 return g_int_hash((gint *)&set->type) + g_str_hash(set->name); | |
| 417 } | |
| 418 | |
| 419 gboolean log_set_equal(gconstpointer a, gconstpointer b) | |
| 420 { | |
| 421 /* I realize that the choices made for GList and GHashTable | |
| 422 * make sense for those data types, but I wish the comparison | |
| 423 * functions were compatible. */ | |
| 424 return !gaim_log_set_compare(a, b); | |
| 425 } | |
| 426 | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
427 void log_add_log_set_to_hash(GHashTable *sets, GaimLogSet *set) |
| 11025 | 428 { |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
429 GaimLogSet *existing_set = g_hash_table_lookup(sets, set); |
|
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
430 |
|
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
431 if (existing_set == NULL) |
|
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
432 g_hash_table_insert(sets, set, set); |
|
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
433 else if (existing_set->account == NULL && set->account != NULL) |
|
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
434 g_hash_table_replace(sets, set, set); |
|
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
435 else |
|
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
436 gaim_log_set_free(set); |
| 11025 | 437 } |
| 438 | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
439 GHashTable *gaim_log_get_log_sets(void) |
| 11025 | 440 { |
| 441 GSList *n; | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
442 GHashTable *sets = g_hash_table_new_full(log_set_hash, log_set_equal, |
|
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
443 (GDestroyNotify)gaim_log_set_free, NULL); |
| 11025 | 444 |
| 445 /* Get the log sets from all the loggers. */ | |
| 446 for (n = loggers; n; n = n->next) { | |
| 447 GaimLogLogger *logger = n->data; | |
| 448 | |
| 449 if (!logger->get_log_sets) | |
| 450 continue; | |
| 451 | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
452 logger->get_log_sets(log_add_log_set_to_hash, sets); |
| 11025 | 453 } |
| 454 | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
455 log_get_log_sets_common(sets); |
| 11025 | 456 |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
457 /* Return the GHashTable of unique GaimLogSets. */ |
|
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
458 return sets; |
|
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
459 } |
| 11025 | 460 |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
461 void gaim_log_set_free(GaimLogSet *set) |
|
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
462 { |
|
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
463 g_return_if_fail(set != NULL); |
| 11025 | 464 |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
465 g_free(set->name); |
|
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
466 if (set->normalized_name != set->name) |
|
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
467 g_free(set->normalized_name); |
|
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
468 g_free(set); |
| 11025 | 469 } |
| 470 | |
| 8573 | 471 GList *gaim_log_get_system_logs(GaimAccount *account) |
| 472 { | |
| 473 GList *logs = NULL; | |
| 474 GSList *n; | |
| 475 for (n = loggers; n; n = n->next) { | |
| 476 GaimLogLogger *logger = n->data; | |
| 477 if (!logger->list_syslog) | |
| 478 continue; | |
| 479 logs = g_list_concat(logs, logger->list_syslog(account)); | |
| 480 } | |
| 481 | |
| 482 return g_list_sort(logs, gaim_log_compare); | |
| 7431 | 483 } |
| 484 | |
| 485 void gaim_log_init(void) | |
| 7436 | 486 { |
| 7431 | 487 gaim_prefs_add_none("/core/logging"); |
| 7555 | 488 gaim_prefs_add_bool("/core/logging/log_ims", FALSE); |
| 489 gaim_prefs_add_bool("/core/logging/log_chats", FALSE); | |
| 8573 | 490 gaim_prefs_add_bool("/core/logging/log_system", FALSE); |
| 491 gaim_prefs_add_bool("/core/logging/log_signon_signoff", FALSE); | |
| 492 gaim_prefs_add_bool("/core/logging/log_idle_state", FALSE); | |
| 493 gaim_prefs_add_bool("/core/logging/log_away_state", FALSE); | |
| 494 gaim_prefs_add_bool("/core/logging/log_own_states", FALSE); | |
| 495 | |
| 7431 | 496 gaim_prefs_add_string("/core/logging/format", "txt"); |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
497 |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
498 html_logger = gaim_log_logger_new("html", "HTML", 8, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
499 NULL, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
500 html_logger_write, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
501 html_logger_finalize, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
502 html_logger_list, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
503 html_logger_read, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
504 gaim_log_common_sizer, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
505 NULL, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
506 html_logger_list_syslog); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
507 gaim_log_logger_add(html_logger); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
508 |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
509 txt_logger = gaim_log_logger_new("txt", "Plain text", 8, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
510 NULL, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
511 txt_logger_write, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
512 txt_logger_finalize, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
513 txt_logger_list, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
514 txt_logger_read, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
515 gaim_log_common_sizer, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
516 NULL, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
517 txt_logger_list_syslog); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
518 gaim_log_logger_add(txt_logger); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
519 |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
520 old_logger = gaim_log_logger_new("old", "Old Gaim", 9, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
521 NULL, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
522 NULL, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
523 old_logger_finalize, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
524 old_logger_list, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
525 old_logger_read, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
526 old_logger_size, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
527 old_logger_total_size, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
528 NULL, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
529 old_logger_get_log_sets); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
530 gaim_log_logger_add(old_logger); |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
531 |
| 10087 | 532 gaim_prefs_connect_callback(NULL, "/core/logging/format", |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
533 logger_pref_cb, NULL); |
| 7431 | 534 gaim_prefs_trigger_callback("/core/logging/format"); |
| 8635 | 535 |
| 536 logsize_users = g_hash_table_new_full((GHashFunc)_gaim_logsize_user_hash, | |
| 537 (GEqualFunc)_gaim_logsize_user_equal, | |
| 538 (GDestroyNotify)_gaim_logsize_user_free_key, NULL); | |
| 7431 | 539 } |
| 540 | |
| 541 /**************************************************************************** | |
| 542 * LOGGERS ****************************************************************** | |
| 543 ****************************************************************************/ | |
| 544 | |
|
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
545 void gaim_log_common_writer(GaimLog *log, const char *ext) |
| 9763 | 546 { |
| 547 char date[64]; | |
| 10822 | 548 GaimLogCommonLoggerData *data = log->logger_data; |
| 9763 | 549 |
|
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
550 if (data == NULL) |
|
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
551 { |
| 9763 | 552 /* This log is new */ |
| 9923 | 553 char *dir, *filename, *path; |
| 10577 | 554 |
| 9923 | 555 dir = gaim_log_get_log_dir(log->type, log->name, log->account); |
| 10577 | 556 if (dir == NULL) |
| 557 return; | |
| 558 | |
| 9923 | 559 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
| 9763 | 560 |
| 561 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S", localtime(&log->time)); | |
| 562 | |
| 563 filename = g_strdup_printf("%s%s", date, ext ? ext : ""); | |
| 564 | |
| 565 path = g_build_filename(dir, filename, NULL); | |
| 566 g_free(dir); | |
| 567 g_free(filename); | |
| 568 | |
| 10822 | 569 log->logger_data = data = g_new0(GaimLogCommonLoggerData, 1); |
| 9763 | 570 |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
571 data->file = g_fopen(path, "a"); |
|
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
572 if (data->file == NULL) |
|
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
573 { |
| 9763 | 574 gaim_debug(GAIM_DEBUG_ERROR, "log", |
| 9892 | 575 "Could not create log file %s\n", path); |
|
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
576 |
|
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
577 if (log->conv != NULL) |
|
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
578 gaim_conversation_write(log->conv, NULL, _("Logging of this conversation failed."), |
|
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
579 GAIM_MESSAGE_ERROR, time(NULL)); |
|
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
580 |
| 9763 | 581 g_free(path); |
| 582 return; | |
| 583 } | |
| 584 g_free(path); | |
| 10577 | 585 } |
| 9763 | 586 } |
| 587 | |
| 10822 | 588 GList *gaim_log_common_lister(GaimLogType type, const char *name, GaimAccount *account, const char *ext, GaimLogLogger *logger) |
| 7431 | 589 { |
| 590 GDir *dir; | |
| 591 GList *list = NULL; | |
| 7628 | 592 const char *filename; |
| 8111 | 593 char *path; |
| 594 | |
| 595 if(!account) | |
| 596 return NULL; | |
| 597 | |
| 9923 | 598 path = gaim_log_get_log_dir(type, name, account); |
| 10577 | 599 if (path == NULL) |
| 600 return NULL; | |
| 7447 | 601 |
| 7431 | 602 if (!(dir = g_dir_open(path, 0, NULL))) { |
| 603 g_free(path); | |
| 604 return NULL; | |
| 605 } | |
| 8898 | 606 |
| 7431 | 607 while ((filename = g_dir_read_name(dir))) { |
| 8577 | 608 if (gaim_str_has_suffix(filename, ext) && |
| 609 strlen(filename) == 17 + strlen(ext)) { | |
| 7431 | 610 GaimLog *log; |
| 10822 | 611 GaimLogCommonLoggerData *data; |
| 8577 | 612 time_t stamp = gaim_str_to_time(filename, FALSE); |
| 7431 | 613 |
|
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
614 log = gaim_log_new(type, name, account, NULL, stamp); |
| 7431 | 615 log->logger = logger; |
| 10822 | 616 log->logger_data = data = g_new0(GaimLogCommonLoggerData, 1); |
| 7616 | 617 data->path = g_build_filename(path, filename, NULL); |
| 7431 | 618 list = g_list_append(list, log); |
| 4184 | 619 } |
| 620 } | |
| 7431 | 621 g_dir_close(dir); |
| 7447 | 622 g_free(path); |
| 7431 | 623 return list; |
| 624 } | |
| 4184 | 625 |
| 10822 | 626 int gaim_log_common_sizer(GaimLog *log) |
| 7556 | 627 { |
| 628 struct stat st; | |
| 10822 | 629 GaimLogCommonLoggerData *data = log->logger_data; |
| 7556 | 630 |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
631 if (!data->path || g_stat(data->path, &st)) |
| 7556 | 632 st.st_size = 0; |
| 633 | |
| 634 return st.st_size; | |
| 635 } | |
| 636 | |
| 11025 | 637 /* This will build log sets for all loggers that use the common logger |
| 638 * functions because they use the same directory structure. */ | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
639 static void log_get_log_sets_common(GHashTable *sets) |
| 11025 | 640 { |
| 641 gchar *log_path = g_build_filename(gaim_user_dir(), "logs", NULL); | |
| 642 GDir *log_dir = g_dir_open(log_path, 0, NULL); | |
| 643 const gchar *protocol; | |
| 644 | |
| 645 if (log_dir == NULL) { | |
| 646 g_free(log_path); | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
647 return; |
| 11025 | 648 } |
| 649 | |
| 650 while ((protocol = g_dir_read_name(log_dir)) != NULL) { | |
| 651 gchar *protocol_path = g_build_filename(log_path, protocol, NULL); | |
| 652 GDir *protocol_dir; | |
| 653 const gchar *username; | |
| 654 gchar *protocol_unescaped; | |
| 655 GList *account_iter; | |
| 656 GList *accounts = NULL; | |
| 657 | |
| 658 if ((protocol_dir = g_dir_open(protocol_path, 0, NULL)) == NULL) { | |
| 659 g_free(protocol_path); | |
| 660 continue; | |
| 661 } | |
| 662 | |
| 663 /* Using g_strdup() to cover the one-in-a-million chance that a | |
| 664 * prpl's list_icon function uses gaim_unescape_filename(). */ | |
| 665 protocol_unescaped = g_strdup(gaim_unescape_filename(protocol)); | |
| 666 | |
| 667 /* Find all the accounts for protocol. */ | |
| 668 for (account_iter = gaim_accounts_get_all() ; account_iter != NULL ; account_iter = account_iter->next) { | |
| 669 GaimPlugin *prpl; | |
| 670 GaimPluginProtocolInfo *prpl_info; | |
| 671 | |
| 672 prpl = gaim_find_prpl(gaim_account_get_protocol_id((GaimAccount *)account_iter->data)); | |
| 673 if (!prpl) | |
| 674 continue; | |
| 675 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
| 676 | |
| 677 if (!strcmp(protocol_unescaped, prpl_info->list_icon((GaimAccount *)account_iter->data, NULL))) | |
| 678 accounts = g_list_append(accounts, account_iter->data); | |
| 679 } | |
| 680 g_free(protocol_unescaped); | |
| 681 | |
| 682 while ((username = g_dir_read_name(protocol_dir)) != NULL) { | |
| 683 gchar *username_path = g_build_filename(protocol_path, username, NULL); | |
| 684 GDir *username_dir; | |
| 685 const gchar *username_unescaped; | |
| 686 GaimAccount *account = NULL; | |
| 687 gchar *name; | |
| 688 | |
| 689 if ((username_dir = g_dir_open(username_path, 0, NULL)) == NULL) { | |
| 690 g_free(username_path); | |
| 691 continue; | |
| 692 } | |
| 693 | |
| 694 /* Find the account for username in the list of accounts for protocol. */ | |
| 695 username_unescaped = gaim_unescape_filename(username); | |
| 696 for (account_iter = g_list_first(accounts) ; account_iter != NULL ; account_iter = account_iter->next) { | |
| 697 if (!strcmp(((GaimAccount *)account_iter->data)->username, username_unescaped)) { | |
| 698 account = account_iter->data; | |
| 699 break; | |
| 700 } | |
| 701 } | |
| 702 | |
| 703 /* Don't worry about the cast, name will point to dynamically allocated memory shortly. */ | |
| 704 while ((name = (gchar *)g_dir_read_name(username_dir)) != NULL) { | |
| 705 size_t len; | |
| 706 GaimLogSet *set = g_new0(GaimLogSet, 1); | |
| 707 | |
| 708 /* Unescape the filename. */ | |
| 709 name = g_strdup(gaim_unescape_filename(name)); | |
| 710 | |
| 711 /* Get the (possibly new) length of name. */ | |
| 712 len = strlen(name); | |
| 713 | |
| 714 set->account = account; | |
| 715 set->name = name; | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
716 set->normalized_name = g_strdup(gaim_normalize(account, name)); |
| 11025 | 717 |
| 718 /* Chat for .chat or .system at the end of the name to determine the type. */ | |
| 719 set->type = GAIM_LOG_IM; | |
| 720 if (len > 7) { | |
| 721 gchar *tmp = &name[len - 7]; | |
| 722 if (!strcmp(tmp, ".system")) { | |
| 723 set->type = GAIM_LOG_SYSTEM; | |
| 724 *tmp = '\0'; | |
| 725 } | |
| 726 } | |
| 727 if (len > 5) { | |
| 728 gchar *tmp = &name[len - 5]; | |
| 729 if (!strcmp(tmp, ".chat")) { | |
| 730 set->type = GAIM_LOG_CHAT; | |
| 731 *tmp = '\0'; | |
| 732 } | |
| 733 } | |
| 734 | |
| 735 /* Determine if this (account, name) combination exists as a buddy. */ | |
|
11458
4db38b374d3f
[gaim-migrate @ 13697]
Richard Laager <rlaager@wiktel.com>
parents:
11292
diff
changeset
|
736 if (account != NULL) |
|
4db38b374d3f
[gaim-migrate @ 13697]
Richard Laager <rlaager@wiktel.com>
parents:
11292
diff
changeset
|
737 set->buddy = (gaim_find_buddy(account, name) != NULL); |
| 11025 | 738 |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
739 log_add_log_set_to_hash(sets, set); |
| 11025 | 740 } |
| 741 g_free(username_path); | |
| 742 g_dir_close(username_dir); | |
| 743 } | |
| 744 g_free(protocol_path); | |
| 745 g_dir_close(protocol_dir); | |
| 746 } | |
| 747 g_free(log_path); | |
| 748 g_dir_close(log_dir); | |
| 749 } | |
| 750 | |
| 7431 | 751 #if 0 /* Maybe some other time. */ |
| 7443 | 752 /**************** |
| 7431 | 753 ** XML LOGGER ** |
| 754 ****************/ | |
| 755 | |
| 756 static const char *str_from_msg_type (GaimMessageFlags type) | |
| 757 { | |
| 7443 | 758 |
| 7431 | 759 return ""; |
| 7443 | 760 |
| 7431 | 761 } |
| 762 | |
| 7443 | 763 static void xml_logger_write(GaimLog *log, |
| 764 GaimMessageFlags type, | |
| 7431 | 765 const char *from, time_t time, const char *message) |
| 766 { | |
| 767 char date[64]; | |
| 768 char *xhtml = NULL; | |
| 10577 | 769 |
| 7431 | 770 if (!log->logger_data) { |
| 771 /* This log is new. We could use the loggers 'new' function, but | |
| 772 * creating a new file there would result in empty files in the case | |
| 773 * that you open a convo with someone, but don't say anything. | |
| 774 */ | |
| 9923 | 775 char *dir = gaim_log_get_log_dir(log->type, log->name, log->account); |
| 7431 | 776 FILE *file; |
| 10577 | 777 |
| 778 if (dir == NULL) | |
| 779 return; | |
| 780 | |
| 7453 | 781 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.xml", localtime(&log->time)); |
| 7443 | 782 |
| 7612 | 783 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
| 7443 | 784 |
| 7431 | 785 char *filename = g_build_filename(dir, date, NULL); |
| 786 g_free(dir); | |
| 7443 | 787 |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
788 log->logger_data = g_fopen(filename, "a"); |
| 7431 | 789 if (!log->logger_data) { |
| 790 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
| 7564 | 791 g_free(filename); |
| 7431 | 792 return; |
| 793 } | |
| 7564 | 794 g_free(filename); |
| 7431 | 795 fprintf(log->logger_data, "<?xml version='1.0' encoding='UTF-8' ?>\n" |
| 796 "<?xml-stylesheet href='file:///usr/src/web/htdocs/log-stylesheet.xsl' type='text/xml' ?>\n"); | |
| 7443 | 797 |
| 7453 | 798 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
| 7431 | 799 fprintf(log->logger_data, "<conversation time='%s' screenname='%s' protocol='%s'>\n", |
| 800 date, log->name, prpl); | |
| 801 } | |
| 7443 | 802 |
| 9923 | 803 /* if we can't write to the file, give up before we hurt ourselves */ |
| 804 if(!data->file) | |
| 805 return; | |
| 806 | |
| 7453 | 807 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
| 7431 | 808 gaim_markup_html_to_xhtml(message, &xhtml, NULL); |
| 809 if (from) | |
| 7443 | 810 fprintf(log->logger_data, "<message %s %s from='%s' time='%s'>%s</message>\n", |
| 811 str_from_msg_type(type), | |
| 7431 | 812 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
| 813 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
| 814 from, date, xhtml); | |
| 815 else | |
| 7443 | 816 fprintf(log->logger_data, "<message %s %s time='%s'>%s</message>\n", |
| 817 str_from_msg_type(type), | |
| 7431 | 818 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
| 819 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
| 7443 | 820 date, xhtml): |
| 7431 | 821 fflush(log->logger_data); |
| 822 g_free(xhtml); | |
| 7443 | 823 } |
| 824 | |
| 7431 | 825 static void xml_logger_finalize(GaimLog *log) |
| 826 { | |
| 827 if (log->logger_data) { | |
| 828 fprintf(log->logger_data, "</conversation>\n"); | |
| 829 fclose(log->logger_data); | |
| 830 log->logger_data = NULL; | |
| 831 } | |
| 832 } | |
| 7443 | 833 |
| 8898 | 834 static GList *xml_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
| 7431 | 835 { |
| 10822 | 836 return gaim_log_common_lister(type, sn, account, ".xml", &xml_logger); |
| 4184 | 837 } |
| 838 | |
| 7431 | 839 static GaimLogLogger xml_logger = { |
| 840 N_("XML"), "xml", | |
| 841 NULL, | |
| 842 xml_logger_write, | |
| 843 xml_logger_finalize, | |
| 844 xml_logger_list, | |
| 8096 | 845 NULL, |
| 11025 | 846 NULL, |
| 7431 | 847 NULL |
| 848 }; | |
| 849 #endif | |
|
5563
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
850 |
| 7431 | 851 /**************************** |
| 7457 | 852 ** HTML LOGGER ************* |
| 853 ****************************/ | |
| 854 | |
| 855 static void html_logger_write(GaimLog *log, GaimMessageFlags type, | |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
856 const char *from, time_t time, const char *message) |
| 7457 | 857 { |
| 9763 | 858 char *msg_fixed; |
| 7457 | 859 char date[64]; |
| 9613 | 860 GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); |
| 10822 | 861 GaimLogCommonLoggerData *data = log->logger_data; |
| 9613 | 862 |
| 7618 | 863 if(!data) { |
| 9763 | 864 const char *prpl = |
| 865 GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon(log->account, NULL); | |
|
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
866 gaim_log_common_writer(log, ".html"); |
| 7457 | 867 |
| 9763 | 868 data = log->logger_data; |
| 7457 | 869 |
| 9763 | 870 /* if we can't write to the file, give up before we hurt ourselves */ |
| 871 if(!data->file) | |
| 872 return; | |
| 7616 | 873 |
| 7457 | 874 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
|
11092
68652f4ad6a7
[gaim-migrate @ 13115]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
875 fprintf(data->file, "<html><head>"); |
|
11094
59a1ff5a4bae
[gaim-migrate @ 13119]
Richard Laager <rlaager@wiktel.com>
parents:
11092
diff
changeset
|
876 fprintf(data->file, "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">"); |
|
11092
68652f4ad6a7
[gaim-migrate @ 13115]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
877 fprintf(data->file, "<title>"); |
| 7616 | 878 fprintf(data->file, "Conversation with %s at %s on %s (%s)", |
| 7457 | 879 log->name, date, gaim_account_get_username(log->account), prpl); |
| 7616 | 880 fprintf(data->file, "</title></head><body>"); |
| 881 fprintf(data->file, | |
| 7457 | 882 "<h3>Conversation with %s at %s on %s (%s)</h3>\n", |
| 883 log->name, date, gaim_account_get_username(log->account), prpl); | |
| 9763 | 884 |
| 7457 | 885 } |
| 7623 | 886 |
| 9892 | 887 /* if we can't write to the file, give up before we hurt ourselves */ |
| 888 if(!data->file) | |
| 889 return; | |
| 890 | |
| 7882 | 891 gaim_markup_html_to_xhtml(message, &msg_fixed, NULL); |
| 892 | |
| 8577 | 893 if(log->type == GAIM_LOG_SYSTEM){ |
| 9592 | 894 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&time)); |
| 8577 | 895 fprintf(data->file, "---- %s @ %s ----<br/>\n", msg_fixed, date); |
| 896 } else { | |
| 897 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
| 898 if (type & GAIM_MESSAGE_SYSTEM) | |
| 899 fprintf(data->file, "<font size=\"2\">(%s)</font><b> %s</b><br/>\n", date, msg_fixed); | |
| 900 else if (type & GAIM_MESSAGE_WHISPER) | |
| 901 fprintf(data->file, "<font color=\"#6C2585\"><font size=\"2\">(%s)</font><b> %s:</b></font> %s<br/>\n", | |
| 902 date, from, msg_fixed); | |
| 903 else if (type & GAIM_MESSAGE_AUTO_RESP) { | |
| 904 if (type & GAIM_MESSAGE_SEND) | |
| 905 fprintf(data->file, _("<font color=\"#16569E\"><font size=\"2\">(%s)</font> <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); | |
| 906 else if (type & GAIM_MESSAGE_RECV) | |
| 907 fprintf(data->file, _("<font color=\"#A82F2F\"><font size=\"2\">(%s)</font> <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); | |
| 908 } else if (type & GAIM_MESSAGE_RECV) { | |
| 909 if(gaim_message_meify(msg_fixed, -1)) | |
|
10735
a0edd89ddb83
[gaim-migrate @ 12337]
Luke Schierer <lschiere@pidgin.im>
parents:
10732
diff
changeset
|
910 fprintf(data->file, "<font color=\"#062585\"><font size=\"2\">(%s)</font> <b>***%s</b></font> %s<br/>\n", |
| 10645 | 911 date, from, msg_fixed); |
| 8577 | 912 else |
| 10645 | 913 fprintf(data->file, "<font color=\"#A82F2F\"><font size=\"2\">(%s)</font> <b>%s:</b></font> %s<br/>\n", |
| 914 date, from, msg_fixed); | |
| 8577 | 915 } else if (type & GAIM_MESSAGE_SEND) { |
| 916 if(gaim_message_meify(msg_fixed, -1)) | |
|
10735
a0edd89ddb83
[gaim-migrate @ 12337]
Luke Schierer <lschiere@pidgin.im>
parents:
10732
diff
changeset
|
917 fprintf(data->file, "<font color=\"#062585\"><font size=\"2\">(%s)</font> <b>***%s</b></font> %s<br/>\n", |
| 10645 | 918 date, from, msg_fixed); |
| 8577 | 919 else |
| 10645 | 920 fprintf(data->file, "<font color=\"#16569E\"><font size=\"2\">(%s)</font> <b>%s:</b></font> %s<br/>\n", |
| 921 date, from, msg_fixed); | |
| 8577 | 922 } |
| 7564 | 923 } |
| 8573 | 924 |
| 7882 | 925 g_free(msg_fixed); |
| 7616 | 926 fflush(data->file); |
| 7457 | 927 } |
| 928 | |
| 929 static void html_logger_finalize(GaimLog *log) | |
| 930 { | |
| 10822 | 931 GaimLogCommonLoggerData *data = log->logger_data; |
| 7616 | 932 if (data) { |
| 933 if(data->file) { | |
| 934 fprintf(data->file, "</body></html>"); | |
| 935 fclose(data->file); | |
| 936 } | |
| 937 g_free(data->path); | |
| 7752 | 938 g_free(data); |
| 7463 | 939 } |
| 7457 | 940 } |
| 941 | |
| 8898 | 942 static GList *html_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
| 7457 | 943 { |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
944 return gaim_log_common_lister(type, sn, account, ".html", html_logger); |
| 7457 | 945 } |
| 946 | |
| 8573 | 947 static GList *html_logger_list_syslog(GaimAccount *account) |
| 948 { | |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
949 return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".html", html_logger); |
| 8573 | 950 } |
| 951 | |
| 7457 | 952 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) |
| 953 { | |
| 954 char *read, *minus_header; | |
| 10822 | 955 GaimLogCommonLoggerData *data = log->logger_data; |
| 7457 | 956 *flags = GAIM_LOG_READ_NO_NEWLINE; |
| 7616 | 957 if (!data || !data->path) |
| 958 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
| 959 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
| 7457 | 960 minus_header = strchr(read, '\n'); |
| 961 if (!minus_header) | |
| 962 minus_header = g_strdup(read); | |
| 963 else | |
| 964 minus_header = g_strdup(minus_header + 1); | |
| 965 g_free(read); | |
| 966 return minus_header; | |
| 967 } | |
| 8578 | 968 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); |
| 7457 | 969 } |
| 970 | |
| 971 | |
| 972 | |
| 973 /**************************** | |
| 7431 | 974 ** PLAIN TEXT LOGGER ******* |
| 975 ****************************/ | |
| 4184 | 976 |
| 7436 | 977 static void txt_logger_write(GaimLog *log, |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
978 GaimMessageFlags type, |
|
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
979 const char *from, time_t time, const char *message) |
| 7431 | 980 { |
| 981 char date[64]; | |
| 9763 | 982 GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); |
| 10822 | 983 GaimLogCommonLoggerData *data = log->logger_data; |
| 9763 | 984 char *stripped = NULL; |
| 985 | |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
986 if (data == NULL) { |
| 7431 | 987 /* This log is new. We could use the loggers 'new' function, but |
| 988 * creating a new file there would result in empty files in the case | |
| 989 * that you open a convo with someone, but don't say anything. | |
| 990 */ | |
| 9763 | 991 const char *prpl = |
| 992 GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon(log->account, NULL); | |
|
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
993 gaim_log_common_writer(log, ".txt"); |
| 8898 | 994 |
| 9763 | 995 data = log->logger_data; |
| 7436 | 996 |
| 9763 | 997 /* if we can't write to the file, give up before we hurt ourselves */ |
| 998 if(!data->file) | |
| 999 return; | |
| 7616 | 1000 |
| 7453 | 1001 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
| 7616 | 1002 fprintf(data->file, "Conversation with %s at %s on %s (%s)\n", |
| 7431 | 1003 log->name, date, gaim_account_get_username(log->account), prpl); |
| 1004 } | |
| 7436 | 1005 |
| 7623 | 1006 /* if we can't write to the file, give up before we hurt ourselves */ |
| 1007 if(!data->file) | |
| 1008 return; | |
| 1009 | |
| 8573 | 1010 stripped = gaim_markup_strip_html(message); |
| 1011 | |
| 1012 if(log->type == GAIM_LOG_SYSTEM){ | |
| 9592 | 1013 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&time)); |
| 8573 | 1014 fprintf(data->file, "---- %s @ %s ----\n", stripped, date); |
| 1015 } else { | |
| 1016 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
| 1017 if (type & GAIM_MESSAGE_SEND || | |
| 1018 type & GAIM_MESSAGE_RECV) { | |
| 1019 if (type & GAIM_MESSAGE_AUTO_RESP) { | |
| 1020 fprintf(data->file, _("(%s) %s <AUTO-REPLY>: %s\n"), date, | |
| 1021 from, stripped); | |
| 1022 } else { | |
| 1023 if(gaim_message_meify(stripped, -1)) | |
| 1024 fprintf(data->file, "(%s) ***%s %s\n", date, from, | |
| 1025 stripped); | |
| 1026 else | |
| 1027 fprintf(data->file, "(%s) %s: %s\n", date, from, | |
| 1028 stripped); | |
| 1029 } | |
| 1030 } else if (type & GAIM_MESSAGE_SYSTEM) | |
| 1031 fprintf(data->file, "(%s) %s\n", date, stripped); | |
| 1032 else if (type & GAIM_MESSAGE_NO_LOG) { | |
| 1033 /* This shouldn't happen */ | |
| 1034 g_free(stripped); | |
| 1035 return; | |
| 1036 } else if (type & GAIM_MESSAGE_WHISPER) | |
| 1037 fprintf(data->file, "(%s) *%s* %s", date, from, stripped); | |
| 1038 else | |
| 1039 fprintf(data->file, "(%s) %s%s %s\n", date, from ? from : "", | |
| 1040 from ? ":" : "", stripped); | |
| 1041 } | |
| 1042 | |
| 1043 fflush(data->file); | |
| 1044 g_free(stripped); | |
| 7431 | 1045 } |
| 1046 | |
| 1047 static void txt_logger_finalize(GaimLog *log) | |
| 1048 { | |
| 10822 | 1049 GaimLogCommonLoggerData *data = log->logger_data; |
| 7616 | 1050 if (data) { |
| 1051 if(data->file) | |
| 1052 fclose(data->file); | |
| 1053 if(data->path) | |
| 1054 g_free(data->path); | |
| 7752 | 1055 g_free(data); |
| 7616 | 1056 } |
| 7431 | 1057 } |
| 1058 | |
| 8898 | 1059 static GList *txt_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
| 7431 | 1060 { |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
1061 return gaim_log_common_lister(type, sn, account, ".txt", txt_logger); |
| 7431 | 1062 } |
| 1063 | |
| 8573 | 1064 static GList *txt_logger_list_syslog(GaimAccount *account) |
| 1065 { | |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
1066 return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".txt", txt_logger); |
| 8573 | 1067 } |
| 1068 | |
| 7431 | 1069 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) |
| 1070 { | |
| 8517 | 1071 char *read, *minus_header, *minus_header2; |
| 10822 | 1072 GaimLogCommonLoggerData *data = log->logger_data; |
| 7457 | 1073 *flags = 0; |
| 7616 | 1074 if (!data || !data->path) |
| 1075 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
| 1076 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
| 7431 | 1077 minus_header = strchr(read, '\n'); |
| 1078 if (!minus_header) | |
| 1079 minus_header = g_strdup(read); | |
| 7436 | 1080 else |
| 7431 | 1081 minus_header = g_strdup(minus_header + 1); |
| 1082 g_free(read); | |
|
10732
c4cb90065e1d
[gaim-migrate @ 12334]
Luke Schierer <lschiere@pidgin.im>
parents:
10645
diff
changeset
|
1083 minus_header2 = g_markup_escape_text(minus_header, -1); |
| 8517 | 1084 g_free(minus_header); |
| 1085 return minus_header2; | |
| 7431 | 1086 } |
| 8578 | 1087 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); |
| 7436 | 1088 } |
| 7431 | 1089 |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
1090 |
| 7431 | 1091 |
| 1092 /**************** | |
| 1093 * OLD LOGGER *** | |
| 1094 ****************/ | |
| 1095 | |
| 1096 /* The old logger doesn't write logs, only reads them. This is to include | |
| 1097 * old logs in the log viewer transparently. | |
| 1098 */ | |
| 1099 | |
| 1100 struct old_logger_data { | |
| 7764 | 1101 GaimStringref *pathref; |
| 7431 | 1102 int offset; |
| 1103 int length; | |
| 1104 }; | |
| 1105 | |
| 8898 | 1106 static GList *old_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
| 7431 | 1107 { |
| 1108 FILE *file; | |
| 1109 char buf[BUF_LONG]; | |
| 1110 struct tm tm; | |
| 7761 | 1111 char month[4]; |
| 7431 | 1112 struct old_logger_data *data = NULL; |
| 1113 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, sn)); | |
| 7764 | 1114 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); |
| 1115 GaimStringref *pathref = gaim_stringref_new(pathstr); | |
| 7431 | 1116 char *newlog; |
| 7761 | 1117 int logfound = 0; |
| 1118 int lastoff = 0; | |
| 1119 int newlen; | |
| 7791 | 1120 time_t lasttime = 0; |
| 7431 | 1121 |
| 1122 GaimLog *log = NULL; | |
| 1123 GList *list = NULL; | |
| 1124 | |
| 7473 | 1125 g_free(logfile); |
| 7764 | 1126 g_free(pathstr); |
| 7473 | 1127 |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
1128 if (!(file = g_fopen(gaim_stringref_value(pathref), "rb"))) { |
| 7764 | 1129 gaim_stringref_unref(pathref); |
| 7431 | 1130 return NULL; |
| 7447 | 1131 } |
| 7436 | 1132 |
| 7431 | 1133 while (fgets(buf, BUF_LONG, file)) { |
| 1134 if ((newlog = strstr(buf, "---- New C"))) { | |
| 1135 int length; | |
| 1136 int offset; | |
| 1137 char convostart[32]; | |
| 1138 char *temp = strchr(buf, '@'); | |
| 7436 | 1139 |
| 7431 | 1140 if (temp == NULL || strlen(temp) < 2) |
| 1141 continue; | |
| 7436 | 1142 |
| 7431 | 1143 temp++; |
| 1144 length = strcspn(temp, "-"); | |
| 1145 if (length > 31) length = 31; | |
| 7436 | 1146 |
| 7431 | 1147 offset = ftell(file); |
| 7436 | 1148 |
| 7761 | 1149 if (logfound) { |
| 1150 newlen = offset - lastoff - length; | |
| 7436 | 1151 if(strstr(buf, "----</H3><BR>")) { |
| 7761 | 1152 newlen -= |
| 1153 sizeof("<HR><BR><H3 Align=Center> ---- New Conversation @ ") + | |
| 1154 sizeof("----</H3><BR>") - 2; | |
| 7436 | 1155 } else { |
| 7761 | 1156 newlen -= |
| 1157 sizeof("---- New Conversation @ ") + sizeof("----") - 2; | |
| 7436 | 1158 } |
| 1159 | |
| 7461 | 1160 if(strchr(buf, '\r')) |
| 7770 | 1161 newlen--; |
| 7461 | 1162 |
| 7761 | 1163 if (newlen != 0) { |
|
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
1164 log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1); |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
1165 log->logger = old_logger; |
| 7761 | 1166 log->time = lasttime; |
| 1167 data = g_new0(struct old_logger_data, 1); | |
| 1168 data->offset = lastoff; | |
| 1169 data->length = newlen; | |
| 7764 | 1170 data->pathref = gaim_stringref_ref(pathref); |
| 7761 | 1171 log->logger_data = data; |
| 7431 | 1172 list = g_list_append(list, log); |
| 7761 | 1173 } |
| 7431 | 1174 } |
| 1175 | |
| 7761 | 1176 logfound = 1; |
| 1177 lastoff = offset; | |
| 7436 | 1178 |
| 7431 | 1179 g_snprintf(convostart, length, "%s", temp); |
| 7676 | 1180 sscanf(convostart, "%*s %s %d %d:%d:%d %d", |
| 1181 month, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &tm.tm_year); | |
| 1182 /* Ugly hack, in case current locale is not English */ | |
| 1183 if (strcmp(month, "Jan") == 0) { | |
| 1184 tm.tm_mon= 0; | |
| 1185 } else if (strcmp(month, "Feb") == 0) { | |
| 1186 tm.tm_mon = 1; | |
| 1187 } else if (strcmp(month, "Mar") == 0) { | |
| 1188 tm.tm_mon = 2; | |
| 1189 } else if (strcmp(month, "Apr") == 0) { | |
| 1190 tm.tm_mon = 3; | |
| 1191 } else if (strcmp(month, "May") == 0) { | |
| 1192 tm.tm_mon = 4; | |
| 1193 } else if (strcmp(month, "Jun") == 0) { | |
| 1194 tm.tm_mon = 5; | |
| 1195 } else if (strcmp(month, "Jul") == 0) { | |
| 1196 tm.tm_mon = 6; | |
| 1197 } else if (strcmp(month, "Aug") == 0) { | |
| 1198 tm.tm_mon = 7; | |
| 1199 } else if (strcmp(month, "Sep") == 0) { | |
| 1200 tm.tm_mon = 8; | |
| 1201 } else if (strcmp(month, "Oct") == 0) { | |
| 1202 tm.tm_mon = 9; | |
| 1203 } else if (strcmp(month, "Nov") == 0) { | |
| 1204 tm.tm_mon = 10; | |
| 1205 } else if (strcmp(month, "Dec") == 0) { | |
| 1206 tm.tm_mon = 11; | |
| 1207 } | |
| 1208 tm.tm_year -= 1900; | |
| 7761 | 1209 lasttime = mktime(&tm); |
| 4184 | 1210 } |
| 1211 } | |
| 7613 | 1212 |
| 7761 | 1213 if (logfound) { |
| 1214 if ((newlen = ftell(file) - lastoff) != 0) { | |
|
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
1215 log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1); |
|
11503
cd0c8830d881
[gaim-migrate @ 13748]
Richard Laager <rlaager@wiktel.com>
parents:
11458
diff
changeset
|
1216 log->logger = old_logger; |
| 7761 | 1217 log->time = lasttime; |
| 1218 data = g_new0(struct old_logger_data, 1); | |
| 1219 data->offset = lastoff; | |
| 1220 data->length = newlen; | |
| 7764 | 1221 data->pathref = gaim_stringref_ref(pathref); |
| 7761 | 1222 log->logger_data = data; |
| 7613 | 1223 list = g_list_append(list, log); |
| 7761 | 1224 } |
| 7613 | 1225 } |
| 1226 | |
| 7764 | 1227 gaim_stringref_unref(pathref); |
| 7431 | 1228 fclose(file); |
| 1229 return list; | |
| 4184 | 1230 } |
|
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
1231 |
| 8898 | 1232 static int old_logger_total_size(GaimLogType type, const char *name, GaimAccount *account) |
| 8096 | 1233 { |
| 1234 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, name)); | |
| 1235 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); | |
| 1236 int size; | |
| 1237 struct stat st; | |
| 1238 | |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
1239 if (g_stat(pathstr, &st)) |
| 8096 | 1240 size = 0; |
| 1241 else | |
| 1242 size = st.st_size; | |
| 1243 | |
| 1244 g_free(logfile); | |
| 1245 g_free(pathstr); | |
| 1246 | |
| 1247 return size; | |
| 1248 } | |
| 1249 | |
| 7616 | 1250 static char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags) |
|
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
1251 { |
| 7431 | 1252 struct old_logger_data *data = log->logger_data; |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
1253 FILE *file = g_fopen(gaim_stringref_value(data->pathref), "rb"); |
| 10906 | 1254 char *tmp, *read = g_malloc(data->length + 1); |
| 7431 | 1255 fseek(file, data->offset, SEEK_SET); |
| 1256 fread(read, data->length, 1, file); | |
| 8370 | 1257 fclose(file); |
| 7431 | 1258 read[data->length] = '\0'; |
| 7436 | 1259 *flags = 0; |
| 1260 if(strstr(read, "<BR>")) | |
| 1261 *flags |= GAIM_LOG_READ_NO_NEWLINE; | |
| 10906 | 1262 else { |
| 1263 tmp = g_markup_escape_text(read, -1); | |
| 1264 g_free(read); | |
| 1265 read = tmp; | |
| 1266 } | |
| 7431 | 1267 return read; |
| 1268 } | |
|
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
1269 |
| 7616 | 1270 static int old_logger_size (GaimLog *log) |
| 7556 | 1271 { |
| 1272 struct old_logger_data *data = log->logger_data; | |
| 7616 | 1273 return data ? data->length : 0; |
| 1274 } | |
| 1275 | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
1276 static void old_logger_get_log_sets(GaimLogSetCallback cb, GHashTable *sets) |
| 11025 | 1277 { |
| 1278 char *log_path = g_build_filename(gaim_user_dir(), "logs", NULL); | |
| 1279 GDir *log_dir = g_dir_open(log_path, 0, NULL); | |
| 1280 gchar *name; | |
| 1281 GaimBlistNode *gnode, *cnode, *bnode; | |
| 1282 | |
| 1283 g_free(log_path); | |
| 1284 if (log_dir == NULL) | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
1285 return; |
| 11025 | 1286 |
| 1287 /* Don't worry about the cast, name will be filled with a dynamically allocated data shortly. */ | |
| 1288 while ((name = (gchar *)g_dir_read_name(log_dir)) != NULL) { | |
| 1289 size_t len; | |
| 1290 gchar *ext; | |
| 1291 GaimLogSet *set; | |
| 1292 gboolean found = FALSE; | |
| 1293 | |
| 1294 /* Unescape the filename. */ | |
| 1295 name = g_strdup(gaim_unescape_filename(name)); | |
| 1296 | |
| 1297 /* Get the (possibly new) length of name. */ | |
| 1298 len = strlen(name); | |
| 1299 | |
| 1300 if (len < 5) { | |
| 1301 g_free(name); | |
| 1302 continue; | |
| 1303 } | |
| 1304 | |
| 1305 /* Make sure we're dealing with a log file. */ | |
| 1306 ext = &name[len - 4]; | |
| 1307 if (strcmp(ext, ".log")) { | |
| 1308 g_free(name); | |
| 1309 continue; | |
| 1310 } | |
| 1311 | |
| 1312 set = g_new0(GaimLogSet, 1); | |
| 1313 | |
| 1314 /* Chat for .chat at the end of the name to determine the type. */ | |
| 1315 *ext = '\0'; | |
| 1316 set->type = GAIM_LOG_IM; | |
| 1317 if (len > 9) { | |
| 1318 char *tmp = &name[len - 9]; | |
| 1319 if (!strcmp(tmp, ".chat")) { | |
| 1320 set->type = GAIM_LOG_CHAT; | |
| 1321 *tmp = '\0'; | |
| 1322 } | |
| 1323 } | |
| 1324 | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
1325 set->name = set->normalized_name = name; |
| 11025 | 1326 |
| 1327 /* Search the buddy list to find the account and to determine if this is a buddy. */ | |
| 1328 for (gnode = gaim_get_blist()->root; !found && gnode != NULL; gnode = gnode->next) | |
| 1329 { | |
| 1330 if (!GAIM_BLIST_NODE_IS_GROUP(gnode)) | |
| 1331 continue; | |
| 1332 | |
| 1333 for (cnode = gnode->child; !found && cnode != NULL; cnode = cnode->next) | |
| 1334 { | |
| 1335 if (!GAIM_BLIST_NODE_IS_CONTACT(cnode)) | |
| 1336 continue; | |
| 1337 | |
| 1338 for (bnode = cnode->child; !found && bnode != NULL; bnode = bnode->next) | |
| 1339 { | |
| 1340 GaimBuddy *buddy = (GaimBuddy *)bnode; | |
| 1341 | |
| 1342 if (!strcmp(buddy->name, name)) { | |
| 1343 set->account = buddy->account; | |
| 1344 set->buddy = TRUE; | |
| 1345 found = TRUE; | |
| 1346 } | |
| 1347 } | |
| 1348 } | |
| 1349 } | |
| 1350 | |
|
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
1351 cb(sets, set); |
| 11025 | 1352 } |
| 1353 g_dir_close(log_dir); | |
| 1354 } | |
| 1355 | |
| 7616 | 1356 static void old_logger_finalize(GaimLog *log) |
| 1357 { | |
| 1358 struct old_logger_data *data = log->logger_data; | |
| 7764 | 1359 gaim_stringref_unref(data->pathref); |
| 7616 | 1360 g_free(data); |
| 7556 | 1361 } |
