From: <ro...@us...> - 2006-07-04 05:17:12
|
Revision: 16420 Author: roast Date: 2006-07-03 22:17:07 -0700 (Mon, 03 Jul 2006) ViewCVS: http://svn.sourceforge.net/gaim/?rev=16420&view=rev Log Message: ----------- preliminary (but completely functional) GIOChannel use for xmllogger without error checking Modified Paths: -------------- branches/soc-2006-file-loggers/src/log.c branches/soc-2006-file-loggers/src/log.h Modified: branches/soc-2006-file-loggers/src/log.c =================================================================== --- branches/soc-2006-file-loggers/src/log.c 2006-07-04 03:50:59 UTC (rev 16419) +++ branches/soc-2006-file-loggers/src/log.c 2006-07-04 05:17:07 UTC (rev 16420) @@ -720,15 +720,21 @@ log->logger_data = data = g_slice_new0(GaimLogCommonLoggerData); - data->file = g_fopen(path, "w+"); - if (data->file == NULL) - { + if (current_logger == xml_logger) { + data->channel = g_io_channel_new_file(path, "w+", NULL); + } + else { + data->file = g_fopen(path, "w+"); + } + + // they can't be /both/ null + if (data->channel == NULL && data->file == NULL) { gaim_debug(GAIM_DEBUG_ERROR, "log", - "Could not create log file %s\n", path); - + "Could not create log file %s\n", path); + if (log->conv != NULL) gaim_conversation_write(log->conv, NULL, _("Logging of this conversation failed."), - GAIM_MESSAGE_ERROR, time(NULL)); + GAIM_MESSAGE_ERROR, time(NULL)); g_free(dir); g_free(path); g_free(extname); @@ -1013,7 +1019,10 @@ GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); GaimLogCommonLoggerData *data = log->logger_data; - gsize written = 0; + gsize written = 0, written_buf = 0; + char writebuf[4096]; + GIOStatus iostat; + GError *gerror = NULL; /* This log is new. We could use the loggers 'new' function, but * creating a new file there would result in empty files in the case @@ -1027,36 +1036,36 @@ data = log->logger_data; // if file doesn't exist, die - if (!data->file) return 0; + if (!data->channel) return 0; + + g_snprintf(writebuf, sizeof(writebuf), + "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + "<chat service=\"%s\" account=\"%s\" version=\"0.3.1-04\"%s>\n" + "\t<event time=\"%s\" type=\"logStart\" />\n", + prpl, gaim_account_get_username(log->account), + log->type == GAIM_LOG_SYSTEM ? "gaim:logtype=\"system\"" : "", date); - fprintf(data->file, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); - - if (log->type == GAIM_LOG_SYSTEM) - written += fprintf(data->file, "<chat service=\"%s\" account=\"%s\" version=\"0.3.1-04\" gaim:logtype=\"system\">\n", - prpl, gaim_account_get_username(log->account)); - else - written += fprintf(data->file, "<chat service=\"%s\" account=\"%s\" version=\"0.3.1-04\">\n", - prpl, gaim_account_get_username(log->account)); - written += fprintf(data->file, "\t<event time=\"%s\" type=\"logStart\" />\n", date); + iostat = g_io_channel_write_chars(data->channel, writebuf, -1, &written_buf, &gerror); + written += written_buf; } /* if we can't write to the file, give up before we hurt ourselves */ - if(!data->file) + if(!data->channel) return 0; gaim_markup_html_to_xhtml(message, &msg_fixed, NULL); if (log->type == GAIM_LOG_SYSTEM) { - written += fprintf(data->file, "\t<event time=\"%s\" sender=\"%s\" gaim:type=\"system\">%s</event>\n", date, from, msg_fixed); + g_snprintf(writebuf, sizeof(writebuf), "\t<event time=\"%s\" sender=\"%s\" gaim:type=\"system\">%s</event>\n", date, from, msg_fixed); } else if (type & GAIM_MESSAGE_SYSTEM) { - written += fprintf(data->file, "\t<message time=\"%s\" sender=\"%s\" gaim:type=\"system\">%s</message>\n", date, from, msg_fixed); + g_snprintf(writebuf, sizeof(writebuf), "\t<message time=\"%s\" sender=\"%s\" gaim:type=\"system\">%s</message>\n", date, from, msg_fixed); } else if (type & GAIM_MESSAGE_ERROR) { - written += fprintf(data->file, "\t<message time=\"%s\" sender=\"%s\" gaim:type=\"error\">%s</message>\n", date, from, msg_fixed); + g_snprintf(writebuf, sizeof(writebuf), "\t<message time=\"%s\" sender=\"%s\" gaim:type=\"error\">%s</message>\n", date, from, msg_fixed); } else if (type & GAIM_MESSAGE_WHISPER) { - written += fprintf(data->file, "\t<message time=\"%s\" sender=\"%s\" gaim:type=\"whisper\">%s</message>\n", date, from, msg_fixed); + g_snprintf(writebuf, sizeof(writebuf), "\t<message time=\"%s\" sender=\"%s\" gaim:type=\"whisper\">%s</message>\n", date, from, msg_fixed); } else if (type & GAIM_MESSAGE_AUTO_RESP && (type & GAIM_MESSAGE_SEND || type & GAIM_MESSAGE_RECV)) { gboolean recv_from_self = FALSE; @@ -1070,7 +1079,7 @@ } } - written += fprintf(data->file, "\t<message time=\"%s\" sender=\"%s\"%s auto=\"true\">%s</message>\n", + g_snprintf(writebuf, sizeof(writebuf), "\t<message time=\"%s\" sender=\"%s\"%s auto=\"true\">%s</message>\n", date, from, recv_from_self ? " gaim:self=\"true\"" : "", msg_fixed); } else if (type & GAIM_MESSAGE_SEND || type & GAIM_MESSAGE_RECV) { @@ -1085,36 +1094,33 @@ } } - written += fprintf(data->file, "\t<message time=\"%s\" sender=\"%s\"%s>%s</message>\n", + g_snprintf(writebuf, sizeof(writebuf), "\t<message time=\"%s\" sender=\"%s\"%s>%s</message>\n", date, from, recv_from_self ? " gaim:self=\"true\"" : "", msg_fixed); } else { gaim_debug_error("log", "Unhandled message type."); - written += fprintf(data->file, "\t<message time=\"%s\" sender=\"%s\">%s</message>\n", date, from, msg_fixed); + g_snprintf(writebuf, sizeof(writebuf), "\t<message time=\"%s\" sender=\"%s\">%s</message>\n", date, from, msg_fixed); } + iostat = g_io_channel_write_chars(data->channel, writebuf, -1, &written_buf, &gerror); + written += written_buf; + + g_free(date); g_free(msg_fixed); // append suffix to file and seek back to keep a valid XML document out of the user space buffer - { - fpos_t tail; - const char *date = gaim_utf8_strftime("%Y-%m-%d %H:%M:%S%z", NULL); + g_snprintf(writebuf, sizeof(writebuf), + "\t<event time=\"%s\" type=\"logEnd\" />\n" + "</chat>\n", + gaim_utf8_strftime("%Y-%m-%d %H:%M:%S%z", NULL)); - if (fgetpos(data->file, &tail) != 0) { - gaim_debug_error("log", "Could not save logfile stream position. Not writing tail."); - } - else { - fprintf(data->file, "\t<event time=\"%s\" type=\"logEnd\" />\n", date); - fprintf(data->file, "</chat>\n"); + iostat = g_io_channel_write_chars(data->channel, writebuf, -1, &written_buf, &gerror); + iostat = g_io_channel_seek_position(data->channel, -1*(gint64)(written_buf), G_SEEK_CUR, &gerror); - if (fsetpos(data->file, &tail) != 0) { - gaim_debug_error("log", "Could not return logfile strem position after tail write."); - } - } - } + //gaim_debug_error("log", "Could not return logfile stream position after tail write."); - g_free(date); - fflush(data->file); + iostat = g_io_channel_flush(data->channel, &gerror); + return written; } @@ -1122,16 +1128,23 @@ { GaimLogCommonLoggerData *data = log->logger_data; if (data) { - if (data->file) { - const char *date = gaim_utf8_strftime("%Y-%m-%d %H:%M:%S%z", NULL); - fprintf(data->file, "\t<event time=\"%s\" type=\"logEnd\" />\n", date); - fprintf(data->file, "</chat>\n"); + if (data->channel) { + char writebuf[256]; + GIOStatus iostat; + GError *gerror = NULL; - fclose(data->file); - } - g_free(data->path); + g_snprintf(writebuf, sizeof(writebuf), + "\t<event time=\"%s\" type=\"logEnd\" />\n" + "</chat>\n", + gaim_utf8_strftime("%Y-%m-%d %H:%M:%S%z", NULL)); - g_slice_free(GaimLogCommonLoggerData, data); + iostat = g_io_channel_write_chars(data->channel, writebuf, -1, NULL, &gerror); + + iostat = g_io_channel_shutdown(data->channel, TRUE, &gerror); // this may cause hanging + } + g_free(data->path); + + g_slice_free(GaimLogCommonLoggerData, data); } } Modified: branches/soc-2006-file-loggers/src/log.h =================================================================== --- branches/soc-2006-file-loggers/src/log.h 2006-07-04 03:50:59 UTC (rev 16419) +++ branches/soc-2006-file-loggers/src/log.h 2006-07-04 05:17:07 UTC (rev 16420) @@ -138,6 +138,7 @@ struct _GaimLogCommonLoggerData { char *path; FILE *file; + GIOChannel *channel; void *extra_data; }; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |