changeset 10822:5c5120837bab

[gaim-migrate @ 12484] sf patch #1180568, from Kevin Stange Expose Gaim Common Logger Functions committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Thu, 14 Apr 2005 03:45:39 +0000
parents 5533f3131582
children c1bb12625b3f
files src/log.c src/log.h
diffstat 2 files changed, 95 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/src/log.c	Thu Apr 14 03:08:23 2005 +0000
+++ b/src/log.c	Thu Apr 14 03:45:39 2005 +0000
@@ -173,7 +173,7 @@
 	return size;
 }
 
-static char *
+char *
 gaim_log_get_log_dir(GaimLogType type, const char *name, GaimAccount *account)
 {
 	GaimPlugin *prpl;
@@ -361,16 +361,10 @@
  * LOGGERS ******************************************************************
  ****************************************************************************/
 
-struct generic_logger_data {
-	char *path;
-	FILE *file;
-};
-
-static void log_writer_common(GaimLog *log, GaimMessageFlags type,
-		time_t time, const char *ext)
+void gaim_log_common_writer(GaimLog *log, time_t time, const char *ext)
 {
 	char date[64];
-	struct generic_logger_data *data = log->logger_data;
+	GaimLogCommonLoggerData *data = log->logger_data;
 
 	if(!data) {
 		/* This log is new */
@@ -390,7 +384,7 @@
 		g_free(dir);
 		g_free(filename);
 
-		log->logger_data = data = g_new0(struct generic_logger_data, 1);
+		log->logger_data = data = g_new0(GaimLogCommonLoggerData, 1);
 
 		data->file = g_fopen(path, "a");
 		if (!data->file) {
@@ -403,7 +397,7 @@
 	}
 }
 
-static GList *log_lister_common(GaimLogType type, const char *name, GaimAccount *account, const char *ext, GaimLogLogger *logger)
+GList *gaim_log_common_lister(GaimLogType type, const char *name, GaimAccount *account, const char *ext, GaimLogLogger *logger)
 {
 	GDir *dir;
 	GList *list = NULL;
@@ -426,12 +420,12 @@
 		if (gaim_str_has_suffix(filename, ext) &&
 				strlen(filename) == 17 + strlen(ext)) {
 			GaimLog *log;
-			struct generic_logger_data *data;
+			GaimLogCommonLoggerData *data;
 			time_t stamp = gaim_str_to_time(filename, FALSE);
 
 			log = gaim_log_new(type, name, account, stamp);
 			log->logger = logger;
-			log->logger_data = data = g_new0(struct generic_logger_data, 1);
+			log->logger_data = data = g_new0(GaimLogCommonLoggerData, 1);
 			data->path = g_build_filename(path, filename, NULL);
 			list = g_list_append(list, log);
 		}
@@ -441,11 +435,10 @@
 	return list;
 }
 
-/* Only to be used with logs listed from log_lister_common */
-int log_sizer_common(GaimLog *log)
+int gaim_log_common_sizer(GaimLog *log)
 {
 	struct stat st;
-	struct generic_logger_data *data = log->logger_data;
+	GaimLogCommonLoggerData *data = log->logger_data;
 
 	if (!data->path || g_stat(data->path, &st))
 		st.st_size = 0;
@@ -538,7 +531,7 @@
 
 static GList *xml_logger_list(GaimLogType type, const char *sn, GaimAccount *account)
 {
-	return log_lister_common(type, sn, account, ".xml", &xml_logger);
+	return gaim_log_common_lister(type, sn, account, ".xml", &xml_logger);
 }
 
 static GaimLogLogger xml_logger =  {
@@ -562,12 +555,12 @@
 	char *msg_fixed;
 	char date[64];
 	GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account));
-	struct generic_logger_data *data = log->logger_data;
+	GaimLogCommonLoggerData *data = log->logger_data;
 
 	if(!data) {
 		const char *prpl =
 			GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon(log->account, NULL);
-		log_writer_common(log, type, time, ".html");
+		gaim_log_common_writer(log, time, ".html");
 
 		data = log->logger_data;
 
@@ -630,7 +623,7 @@
 
 static void html_logger_finalize(GaimLog *log)
 {
-	struct generic_logger_data *data = log->logger_data;
+	GaimLogCommonLoggerData *data = log->logger_data;
 	if (data) {
 		if(data->file) {
 			fprintf(data->file, "</body></html>");
@@ -643,18 +636,18 @@
 
 static GList *html_logger_list(GaimLogType type, const char *sn, GaimAccount *account)
 {
-	return log_lister_common(type, sn, account, ".html", &html_logger);
+	return gaim_log_common_lister(type, sn, account, ".html", &html_logger);
 }
 
 static GList *html_logger_list_syslog(GaimAccount *account)
 {
-	return log_lister_common(GAIM_LOG_SYSTEM, ".system", account, ".html", &html_logger);
+	return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".html", &html_logger);
 }
 
 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags)
 {
 	char *read, *minus_header;
-	struct generic_logger_data *data = log->logger_data;
+	GaimLogCommonLoggerData *data = log->logger_data;
 	*flags = GAIM_LOG_READ_NO_NEWLINE;
 	if (!data || !data->path)
 		return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>"));
@@ -677,7 +670,7 @@
 	html_logger_finalize,
 	html_logger_list,
 	html_logger_read,
-	log_sizer_common,
+	gaim_log_common_sizer,
 	NULL,
 	html_logger_list_syslog
 };
@@ -695,7 +688,7 @@
 {
 	char date[64];
 	GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account));
-	struct generic_logger_data *data = log->logger_data;
+	GaimLogCommonLoggerData *data = log->logger_data;
 	char *stripped = NULL;
 
 	if(!data) {
@@ -705,7 +698,7 @@
 		 */
 		const char *prpl =
 			GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon(log->account, NULL);
-		log_writer_common(log, type, time, ".txt");
+		gaim_log_common_writer(log, time, ".txt");
 
 		data = log->logger_data;
 
@@ -761,7 +754,7 @@
 
 static void txt_logger_finalize(GaimLog *log)
 {
-	struct generic_logger_data *data = log->logger_data;
+	GaimLogCommonLoggerData *data = log->logger_data;
 	if (data) {
 		if(data->file)
 			fclose(data->file);
@@ -773,18 +766,18 @@
 
 static GList *txt_logger_list(GaimLogType type, const char *sn, GaimAccount *account)
 {
-	return log_lister_common(type, sn, account, ".txt", &txt_logger);
+	return gaim_log_common_lister(type, sn, account, ".txt", &txt_logger);
 }
 
 static GList *txt_logger_list_syslog(GaimAccount *account)
 {
-	return log_lister_common(GAIM_LOG_SYSTEM, ".system", account, ".txt", &txt_logger);
+	return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".txt", &txt_logger);
 }
 
 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags)
 {
 	char *read, *minus_header, *minus_header2;
-	struct generic_logger_data *data = log->logger_data;
+	GaimLogCommonLoggerData *data = log->logger_data;
 	*flags = 0;
 	if (!data || !data->path)
 		return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>"));
@@ -809,7 +802,7 @@
 	txt_logger_finalize,
 	txt_logger_list,
 	txt_logger_read,
-	log_sizer_common,
+	gaim_log_common_sizer,
 	NULL,
 	txt_logger_list_syslog
 };
--- a/src/log.h	Thu Apr 14 03:08:23 2005 +0000
+++ b/src/log.h	Thu Apr 14 03:45:39 2005 +0000
@@ -34,6 +34,7 @@
 
 typedef struct _GaimLog GaimLog;
 typedef struct _GaimLogLogger GaimLogLogger;
+typedef struct _GaimLogCommonLoggerData GaimLogCommonLoggerData;
 
 typedef enum {
 	GAIM_LOG_IM,
@@ -107,6 +108,15 @@
 	void *logger_data;                    /**< Data used by the log logger */
 };
 
+/**
+ * A common logger_data struct containing a file handle and path, as well
+ * as a pointer to something else for additional data.
+ */
+struct _GaimLogCommonLoggerData {
+	char *path;
+	FILE *file;
+	void *extra_data;
+};
 
 #ifdef __cplusplus
 extern "C" {
@@ -182,7 +192,7 @@
 GList *gaim_log_get_system_logs(GaimAccount *account);
 
 /**
- * Returns the size of a log 
+ * Returns the size of a log
  *
  * @param log                 The log
  * @return                    The size of the log, in bytes
@@ -200,6 +210,18 @@
 int gaim_log_get_total_size(GaimLogType type, const char *name, GaimAccount *account);
 
 /**
+ * Returns the default logger directory Gaim uses for a given account
+ * and username.  This would be where Gaim stores logs created by
+ * the built-in text or HTML loggers.
+ *
+ * @param type                The type of the log.
+ * @param name                The name of the log.
+ * @param account             The account.
+ * @return                    The default logger directory for Gaim.
+ */
+char *gaim_log_get_log_dir(GaimLogType type, const char *name, GaimAccount *account);
+
+/**
  * Implements GCompareFunc
  *
  * @param y				   A GaimLog
@@ -210,6 +232,54 @@
 /*@}*/
 
 /******************************************/
+/** @name Common Logger Functions         */
+/******************************************/
+/*@{*/
+
+/**
+ * Opens a new log file in the standard Gaim log location
+ * with the given file extension, named for the current time,
+ * for writing.  If a log file is already open, the existing
+ * file handle is retained.  The log's logger_data value is
+ * set to a GaimLogCommonLoggerData struct containing the log
+ * file handle and log path.
+ *
+ * @param log   The log to write to.
+ * @param time  The time of the item being logged.
+ * @param ext   The file extension to give to this log file.
+ */
+void gaim_log_common_writer(GaimLog *log, time_t time, const char *ext);
+
+/**
+ * Returns a sorted GList of GaimLogs of the requested type.
+ * This function should only be used with logs that are written
+ * with gaim_log_common_writer().
+ *
+ * @param type     The type of the logs being listed.
+ * @param name     The name of the log.
+ * @param account  The account of the log.
+ * @param ext      The file extension this log format uses.
+ * @param logger   A reference to the logger struct for this log.
+ *
+ * @return A sorted GList of GaimLogs matching the parameters.
+ */
+GList *gaim_log_common_lister(GaimLogType type, const char *name,
+							  GaimAccount *account, const char *ext,
+							  GaimLogLogger *logger);
+
+/**
+ * Returns the size of a given GaimLog.
+ * This function should only be used with logs that are written
+ * with gaim_log_common_writer().
+ *
+ * @param log      The GaimLog to size.
+ *
+ * @return An integer indicating the size of the log in bytes.
+ */
+int gaim_log_common_sizer(GaimLog *log);
+/*@}*/
+
+/******************************************/
 /** @name Logger Functions                */
 /******************************************/
 /*@{*/