Changes in / [48848ae:048b1ff]
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
commands.c
rf809cad r39a531d 490 490 "\n" 491 491 "SEE ALSO: aaway, zaway"), 492 493 OWLCMD_VOID("flush-logs", owl_command_flushlogs, OWL_CTX_ANY,494 "flush the queue of messages waiting to be logged",495 "",496 "If BarnOwl failed to log a file, this command tells\n"497 "BarnOwl to try logging the messages that have since\n"498 "come in, and to resume logging normally."),499 492 500 493 OWLCMD_ARGS("load-subs", owl_command_loadsubs, OWL_CTX_ANY, … … 1443 1436 { 1444 1437 owl_function_unsuball(); 1445 }1446 1447 void owl_command_flushlogs(void)1448 {1449 owl_log_flush_logs();1450 1438 } 1451 1439 -
logging.c
rf809cad r0792d99 11 11 static GMainLoop *log_loop; 12 12 static GThread *logging_thread; 13 bool defer_logs;14 static GQueue *deferred_entry_queue;15 13 16 14 /* This is now the one function that should be called to log a … … 57 55 /* skip login/logout messages if appropriate */ 58 56 if (!owl_global_is_loglogins(&g) && owl_message_is_loginout(m)) return(0); 59 57 60 58 /* check direction */ 61 59 if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) && owl_message_is_direction_out(m)) { … … 85 83 buffer = g_string_new(""); 86 84 tmp = short_zuser(owl_message_get_sender(m)); 87 g_string_append_printf(buffer, "Class: %s Instance: %s", 88 owl_message_get_class(m), 85 g_string_append_printf(buffer, "Class: %s Instance: %s", 86 owl_message_get_class(m), 89 87 owl_message_get_instance(m)); 90 88 if (strcmp(owl_message_get_opcode(m), "")) { 91 g_string_append_printf(buffer, " Opcode: %s", 89 g_string_append_printf(buffer, " Opcode: %s", 92 90 owl_message_get_opcode(m)); 93 91 } 94 92 g_string_append_printf(buffer, "\n"); 95 g_string_append_printf(buffer, "Time: %s Host: %s\n", 96 owl_message_get_timestr(m), 93 g_string_append_printf(buffer, "Time: %s Host: %s\n", 94 owl_message_get_timestr(m), 97 95 owl_message_get_hostname(m)); 98 g_string_append_printf(buffer, "From: %s <%s>\n\n", 96 g_string_append_printf(buffer, "From: %s <%s>\n\n", 99 97 owl_message_get_zsig(m), tmp); 100 98 g_string_append_printf(buffer, "%s\n\n", owl_message_get_body(m)); … … 107 105 GString *buffer = NULL; 108 106 buffer = g_string_new(""); 109 g_string_append_printf(buffer, "From: <%s> To: <%s>\n", 107 g_string_append_printf(buffer, "From: <%s> To: <%s>\n", 110 108 owl_message_get_sender(m), owl_message_get_recipient(m)); 111 g_string_append_printf(buffer, "Time: %s\n\n", 109 g_string_append_printf(buffer, "Time: %s\n\n", 112 110 owl_message_get_timestr(m)); 113 111 if (owl_message_is_login(m)) { … … 126 124 buffer = g_string_new(""); 127 125 g_string_append_printf(buffer, "From: <%s> To: <%s>\n", 128 owl_message_get_sender(m), 126 owl_message_get_sender(m), 129 127 owl_message_get_recipient(m)); 130 g_string_append_printf(buffer, "Time: %s\n\n", 128 g_string_append_printf(buffer, "Time: %s\n\n", 131 129 owl_message_get_timestr(m)); 132 130 g_string_append_printf(buffer, "%s\n\n", owl_message_get_body(m)); … … 138 136 GString *buffer; 139 137 buffer = g_string_new(""); 140 g_string_append_printf(buffer, "From: <%s> To: <%s>\n", 141 owl_message_get_sender(m), 138 g_string_append_printf(buffer, "From: <%s> To: <%s>\n", 139 owl_message_get_sender(m), 142 140 owl_message_get_recipient(m)); 143 g_string_append_printf(buffer, "Time: %s\n\n", 141 g_string_append_printf(buffer, "Time: %s\n\n", 144 142 owl_message_get_timestr(m)); 145 g_string_append_printf(buffer, "%s\n\n", 143 g_string_append_printf(buffer, "%s\n\n", 146 144 owl_message_get_body(m)); 147 145 return g_string_free(buffer, FALSE); … … 160 158 } 161 159 162 static CALLER_OWN owl_log_entry *owl_log_new_entry(const char *buffer, const char *filename) 163 { 164 owl_log_entry *log_msg = g_new(owl_log_entry, 1); 165 log_msg->message = g_strdup(buffer); 166 log_msg->filename = g_strdup(filename); 167 return log_msg; 168 } 169 170 static void owl_log_deferred_enqueue_message(const char *buffer, const char *filename) 171 { 172 g_queue_push_tail(deferred_entry_queue, owl_log_new_entry(buffer, filename)); 173 } 174 175 /* write out the entry if possible 176 * return 0 on success, errno on failure to open 177 */ 178 static int owl_log_try_write_entry(owl_log_entry *msg) 179 { 160 static void owl_log_write_entry(gpointer data) 161 { 162 owl_log_entry *msg = (owl_log_entry*)data; 180 163 FILE *file = NULL; 181 164 file = fopen(msg->filename, "a"); 182 165 if (!file) { 183 return errno; 166 owl_log_error("Unable to open file for logging"); 167 return; 184 168 } 185 169 fprintf(file, "%s", msg->message); 186 170 fclose(file); 187 return 0;188 171 } 189 172 … … 198 181 } 199 182 200 static void owl_log_entry_free_gfunc(gpointer data, gpointer user_data)201 {202 owl_log_entry_free(data);203 }204 205 /* If we are deferring log messages, enqueue this entry for206 * writing. Otherwise, try to write this log message, and,207 * if it fails with EPERM or EACCES, go into deferred logging208 * mode and queue an admin message. If it fails with anything209 * else, display an error message, but do not go into deferred210 * logging mode. */211 static void owl_log_eventually_write_entry(gpointer data)212 {213 int ret;214 gchar *errmsg;215 owl_log_entry *msg = (owl_log_entry*)data;216 if (defer_logs) {217 owl_log_deferred_enqueue_message(msg->message, msg->filename);218 } else {219 ret = owl_log_try_write_entry(msg);220 if (ret == EPERM || ret == EACCES) {221 defer_logs = true;222 owl_log_error("Unable to open file for logging; you do not have \n"223 "permission. Consider renewing your tickets. \n"224 "Logging has been suspended, and your messages \n"225 "will be saved. To resume logging, use the \n"226 "command :flush-logs.\n\n");227 owl_log_deferred_enqueue_message(msg->message, msg->filename);228 } else if (ret != 0) {229 errmsg = g_strdup_printf("Unable to open file for logging: %s", g_strerror(ret));230 owl_log_error(errmsg);231 g_free(errmsg);232 }233 }234 }235 236 /* tries to the deferred log entries */237 static void owl_log_write_deferred_entries(gpointer data)238 {239 owl_log_entry *entry;240 241 defer_logs = false;242 while (!g_queue_is_empty(deferred_entry_queue) && !defer_logs) {243 entry = (owl_log_entry*)g_queue_pop_head(deferred_entry_queue);244 owl_log_eventually_write_entry(entry);245 owl_log_entry_free(entry);246 }247 }248 249 void owl_log_flush_logs(void)250 {251 owl_select_post_task(owl_log_write_deferred_entries, NULL, NULL, log_context);252 }253 254 183 void owl_log_enqueue_message(const char *buffer, const char *filename) 255 184 { 256 owl_log_entry *log_msg = owl_log_new_entry(buffer, filename); 257 owl_select_post_task(owl_log_eventually_write_entry, log_msg, 185 owl_log_entry *log_msg = NULL; 186 log_msg = g_new(owl_log_entry,1); 187 log_msg->message = g_strdup(buffer); 188 log_msg->filename = g_strdup(filename); 189 owl_select_post_task(owl_log_write_entry, log_msg, 258 190 owl_log_entry_free, log_context); 259 191 } … … 421 353 } else if (owl_message_is_type_jabber(m)) { 422 354 if (personal) { 423 from=frombuff=g_strdup_printf("jabber:%s", 355 from=frombuff=g_strdup_printf("jabber:%s", 424 356 owl_message_get_sender(m)); 425 357 } else { 426 from=frombuff=g_strdup_printf("jabber:%s", 358 from=frombuff=g_strdup_printf("jabber:%s", 427 359 owl_message_get_recipient(m)); 428 360 } … … 430 362 from=frombuff=g_strdup("unknown"); 431 363 } 432 364 433 365 /* check for malicious sender formats */ 434 366 len=strlen(frombuff); … … 497 429 static gpointer owl_log_thread_func(gpointer data) 498 430 { 499 log_context = g_main_context_new();500 431 log_loop = g_main_loop_new(log_context, FALSE); 501 432 g_main_loop_run(log_loop); … … 503 434 } 504 435 505 void owl_log_init(void) 436 void owl_log_init(void) 506 437 { 507 438 log_context = g_main_context_new(); … … 523 454 } 524 455 #endif 525 526 deferred_entry_queue = g_queue_new(); 456 527 457 } 528 458 529 459 static void owl_log_quit_func(gpointer data) 530 460 { 531 /* flush the deferred logs queue, trying to write the532 * entries to the disk one last time */533 owl_log_write_deferred_entries(NULL);534 #if GLIB_CHECK_VERSION(2, 32, 0)535 g_queue_free_full(deferred_entry_queue, owl_log_entry_free_gfunc);536 #else537 g_queue_foreach(deferred_entry_queue, owl_log_entry_free_gfunc, NULL);538 g_queue_free(deferred_entry_queue);539 #endif540 541 461 g_main_loop_quit(log_loop); 542 462 }
Note: See TracChangeset
for help on using the changeset viewer.