# HG changeset patch # User Richard Laager # Date 1141375559 0 # Node ID 0a8b72b12cef86fd57cec447464b54b34b422e24 # Parent d9dbb874a30cce94fc249b9f93d2ca0bc2c442ef [gaim-migrate @ 15761] Implement a total_size function for the text and HTML loggers. This way, when we call gaim_log_get_total_size(), we don't have to create and then immediately destroy a bunch of GaimLog instances. committer: Tailor Script diff -r d9dbb874a30c -r 0a8b72b12cef src/log.c --- a/src/log.c Fri Mar 03 07:02:59 2006 +0000 +++ b/src/log.c Fri Mar 03 08:45:59 2006 +0000 @@ -51,6 +51,7 @@ static GList *html_logger_list(GaimLogType type, const char *sn, GaimAccount *account); static GList *html_logger_list_syslog(GaimAccount *account); static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags); +static int html_logger_total_size(GaimLogType type, const char *name, GaimAccount *account); static GList *old_logger_list(GaimLogType type, const char *sn, GaimAccount *account); static int old_logger_total_size(GaimLogType type, const char *name, GaimAccount *account); @@ -66,6 +67,7 @@ static GList *txt_logger_list(GaimLogType type, const char *sn, GaimAccount *account); static GList *txt_logger_list_syslog(GaimAccount *account); static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags); +static int txt_logger_total_size(GaimLogType type, const char *name, GaimAccount *account); /************************************************************************** * PUBLIC LOGGING FUNCTIONS *********************************************** @@ -551,7 +553,7 @@ html_logger_list, html_logger_read, gaim_log_common_sizer, - NULL, + html_logger_total_size, html_logger_list_syslog); gaim_log_logger_add(html_logger); @@ -562,7 +564,7 @@ txt_logger_list, txt_logger_read, gaim_log_common_sizer, - NULL, + txt_logger_total_size, txt_logger_list_syslog); gaim_log_logger_add(txt_logger); @@ -738,6 +740,48 @@ return list; } +int gaim_log_common_total_sizer(GaimLogType type, const char *name, GaimAccount *account, const char *ext) +{ + GDir *dir; + int size = 0; + const char *filename; + char *path; + + if(!account) + return 0; + + path = gaim_log_get_log_dir(type, name, account); + if (path == NULL) + return 0; + + if (!(dir = g_dir_open(path, 0, NULL))) + { + g_free(path); + return 0; + } + + while ((filename = g_dir_read_name(dir))) + { + if (gaim_str_has_suffix(filename, ext) && + strlen(filename) >= (17 + strlen(ext))) + { + char *tmp = g_build_filename(path, filename, NULL); + struct stat st; + if (g_stat(tmp, &st)) + { + gaim_debug_error("log", "Error stating log file: %s\n", tmp); + g_free(tmp); + continue; + } + g_free(tmp); + size += st.st_size; + } + } + g_dir_close(dir); + g_free(path); + return size; +} + int gaim_log_common_sizer(GaimLog *log) { struct stat st; @@ -1104,6 +1148,10 @@ return g_strdup_printf(_("Could not read file: %s"), data->path); } +static int html_logger_total_size(GaimLogType type, const char *name, GaimAccount *account) +{ + return gaim_log_common_total_sizer(type, name, account, ".html"); +} /**************************** @@ -1228,6 +1276,10 @@ return g_strdup_printf(_("Could not read file: %s"), data->path); } +static int txt_logger_total_size(GaimLogType type, const char *name, GaimAccount *account) +{ + return gaim_log_common_total_sizer(type, name, account, ".txt"); +} /**************** diff -r d9dbb874a30c -r 0a8b72b12cef src/log.h --- a/src/log.h Fri Mar 03 07:02:59 2006 +0000 +++ b/src/log.h Fri Mar 03 08:45:59 2006 +0000 @@ -349,6 +349,25 @@ GaimLogLogger *logger); /** + * Returns the total size of all the logs for a given user, with + * a given extension. This is the "common" implemention of a + * logger's total_size function. + * This function should only be used with logs that are written + * with gaim_log_common_writer(). + * + * @param type The type of the logs being sized. + * @param name The name of the logs to size + * (e.g. the username or chat name). + * @param account The account of the log. + * @param ext The file extension this log format uses. + * + * @return The size of all the logs with the specified extension + * for the specified user. + */ +int gaim_log_common_total_sizer(GaimLogType type, const char *name, + GaimAccount *account, const char *ext); + +/** * Returns the size of a given GaimLog. * This function should only be used with logs that are written * with gaim_log_common_writer().