1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Recipient logs now in own subdirectories

Filename is based on date, in a sortable order
This commit is contained in:
James Booth 2012-10-09 00:46:58 +01:00
parent 3c5a50a664
commit 18c6bb9219

View File

@ -22,6 +22,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "glib.h" #include "glib.h"
@ -30,64 +31,60 @@
#include "log.h" #include "log.h"
static GHashTable *logs; static GHashTable *logs;
static GTimeZone *tz;
struct dated_chat_log { struct dated_chat_log {
gpointer logpp; FILE *logp;
GDateTime *date; GDateTime *date;
}; };
static void _close_file(gpointer key, gpointer value, gpointer user_data);
static gboolean _log_roll_needed(struct dated_chat_log *dated_log); static gboolean _log_roll_needed(struct dated_chat_log *dated_log);
static struct dated_chat_log *_create_log(char *other, const char * const login); static struct dated_chat_log *_create_log(char *other, const char * const login);
static void _free_chat_log(struct dated_chat_log *dated_log);
void void
chat_log_init(void) chat_log_init(void)
{ {
log_info("Initialising chat logs"); log_info("Initialising chat logs");
tz = g_time_zone_new_local(); logs = g_hash_table_new_full(NULL, (GEqualFunc) g_strcmp0, g_free,
logs = g_hash_table_new(NULL, (GEqualFunc) g_strcmp0); (GDestroyNotify)_free_chat_log);
} }
void void
chat_log_chat(const gchar * const login, gchar *other, chat_log_chat(const gchar * const login, gchar *other,
const gchar * const msg, chat_log_direction_t direction) const gchar * const msg, chat_log_direction_t direction)
{ {
struct dated_chat_log *dated_log = g_hash_table_lookup(logs, other); gchar *other_copy = strdup(other);
FILE *logp; struct dated_chat_log *dated_log = g_hash_table_lookup(logs, other_copy);
// no log for user // no log for user
if (dated_log == NULL) { if (dated_log == NULL) {
dated_log = _create_log(other, login); dated_log = _create_log(other_copy, login);
g_hash_table_insert(logs, other, dated_log); g_hash_table_insert(logs, other_copy, dated_log);
// log exists but needs rolling // log exists but needs rolling
} else if (_log_roll_needed(dated_log)) { } else if (_log_roll_needed(dated_log)) {
fclose(dated_log->logpp); dated_log = _create_log(other_copy, login);
dated_log = _create_log(other, login); g_hash_table_replace(logs, other_copy, dated_log);
g_hash_table_replace(logs, other, dated_log);
} }
logp = (FILE *) dated_log->logpp; GDateTime *dt = g_date_time_new_now_local();
GDateTime *dt = g_date_time_new_now(tz);
gchar *date_fmt = g_date_time_format(dt, "%H:%M:%S"); gchar *date_fmt = g_date_time_format(dt, "%H:%M:%S");
if (direction == IN) { if (direction == IN) {
fprintf(logp, "%s - %s: %s\n", date_fmt, other, msg); fprintf(dated_log->logp, "%s - %s: %s\n", date_fmt, other_copy, msg);
} else { } else {
fprintf(logp, "%s - me: %s\n", date_fmt, msg); fprintf(dated_log->logp, "%s - me: %s\n", date_fmt, msg);
} }
fflush(logp); fflush(dated_log->logp);
g_free(date_fmt);
g_date_time_unref(dt); g_date_time_unref(dt);
} }
void void
chat_log_close(void) chat_log_close(void)
{ {
g_hash_table_foreach(logs, (GHFunc) _close_file, NULL); g_hash_table_remove_all(logs);
g_time_zone_unref(tz);
} }
static struct dated_chat_log * static struct dated_chat_log *
@ -104,21 +101,19 @@ _create_log(char *other, const char * const login)
gchar *other_file = str_replace(other, "@", "_at_"); gchar *other_file = str_replace(other, "@", "_at_");
g_string_append_printf(log_file, "/%s", other_file); g_string_append_printf(log_file, "/%s", other_file);
create_dir(log_file->str);
free(other_file);
GDateTime *dt = g_date_time_new_now_local(); GDateTime *dt = g_date_time_new_now_local();
//gchar *date = g_date_time_format(dt, "_%d_%m_%Y.log"); gchar *date = g_date_time_format(dt, "/%Y_%m_%d.log");
gchar *date = g_date_time_format(dt, "_%d_%m_%Y_%H_%M.log");
g_string_append_printf(log_file, date); g_string_append_printf(log_file, date);
gpointer logp = fopen(log_file->str, "a");
free(other_file);
g_string_free(log_file, TRUE);
struct dated_chat_log *new_log = malloc(sizeof(struct dated_chat_log)); struct dated_chat_log *new_log = malloc(sizeof(struct dated_chat_log));
new_log->logpp = logp; new_log->logp = fopen(log_file->str, "a");
new_log->date = dt; new_log->date = dt;
g_string_free(log_file, TRUE);
return new_log; return new_log;
} }
@ -128,13 +123,8 @@ _log_roll_needed(struct dated_chat_log *dated_log)
{ {
gboolean result = FALSE; gboolean result = FALSE;
GDateTime *now = g_date_time_new_now_local(); GDateTime *now = g_date_time_new_now_local();
if (g_date_time_get_day_of_year(dated_log->date) !=
g_date_time_get_day_of_year(now)) {
if (g_date_time_get_minute(dated_log->date) !=
g_date_time_get_minute(now)) {
// if (g_date_time_get_day_of_year(dated_log->date) !=
// g_date_time_get_day_of_year(now)) {
result = TRUE; result = TRUE;
} }
g_date_time_unref(now); g_date_time_unref(now);
@ -143,7 +133,17 @@ _log_roll_needed(struct dated_chat_log *dated_log)
} }
static void static void
_close_file(gpointer key, gpointer value, gpointer user_data) _free_chat_log(struct dated_chat_log *dated_log)
{ {
fclose(value); if (dated_log != NULL) {
if (dated_log->logp != NULL) {
fclose(dated_log->logp);
dated_log->logp = NULL;
}
if (dated_log->date != NULL) {
g_date_time_unref(dated_log->date);
dated_log->date = NULL;
}
}
dated_log = NULL;
} }