From 1c16467eb9fd8a18bf2954f0344c8baaa17f47d2 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 22 Sep 2014 17:09:39 -0400 Subject: [PATCH] don't share chatlogs across data files (changes log naming convention) --- src/chat.c | 5 ++- src/global_commands.c | 10 ++--- src/groupchat.c | 7 +++- src/log.c | 89 ++++++++++++++++++++++++++----------------- src/log.h | 9 +++-- src/prompt.c | 2 +- 6 files changed, 74 insertions(+), 48 deletions(-) diff --git a/src/chat.c b/src/chat.c index ca2aa8a..190bfb1 100644 --- a/src/chat.c +++ b/src/chat.c @@ -1091,7 +1091,10 @@ static void chat_onInit(ToxWindow *self, Tox *m) line_info_init(ctx->hst); - log_enable(nick, Friends.list[self->num].pub_key, ctx->log); + char myid[TOX_FRIEND_ADDRESS_SIZE]; + tox_get_address(m, (uint8_t *) myid); + + log_enable(nick, myid, Friends.list[self->num].pub_key, ctx->log, LOG_CHAT); load_chat_history(self, ctx->log); if (!Friends.list[self->num].logging_on) diff --git a/src/global_commands.c b/src/global_commands.c index 231e7b9..18bdee1 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -286,16 +286,16 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX const char *swch = argv[1]; if (!strcmp(swch, "1") || !strcmp(swch, "on")) { + char myid[TOX_FRIEND_ADDRESS_SIZE]; + tox_get_address(m, (uint8_t *) myid); if (self->is_chat) { Friends.list[self->num].logging_on = true; - log_enable(self->name, Friends.list[self->num].pub_key, log); + log_enable(self->name, myid, Friends.list[self->num].pub_key, log, LOG_CHAT); } else if (self->is_prompt) { - char myid[TOX_FRIEND_ADDRESS_SIZE]; - tox_get_address(m, (uint8_t *) myid); - log_enable(self->name, myid, log); + log_enable(self->name, myid, NULL, log, LOG_PROMPT); } else if (self->is_groupchat) { - log_enable(self->name, NULL, log); + log_enable(self->name, myid, NULL, log, LOG_GROUP); } msg = "Logging enabled"; diff --git a/src/groupchat.c b/src/groupchat.c index 7ff85d1..b17c96a 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -503,8 +503,11 @@ static void groupchat_onInit(ToxWindow *self, Tox *m) line_info_init(ctx->hst); - if (user_settings_->autolog == AUTOLOG_ON) - log_enable(self->name, NULL, ctx->log); + if (user_settings_->autolog == AUTOLOG_ON) { + char myid[TOX_FRIEND_ADDRESS_SIZE]; + tox_get_address(m, (uint8_t *) myid); + log_enable(self->name, myid, NULL, ctx->log, LOG_GROUP); + } execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); diff --git a/src/log.c b/src/log.c index 238db1f..27be5c1 100644 --- a/src/log.c +++ b/src/log.c @@ -35,56 +35,71 @@ extern struct user_settings *user_settings_; -/* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */ -void init_logging_session(char *name, const char *key, struct chatlog *log) +/* Opens log file or creates a new one */ +static int init_logging_session(char *name, const char *selfkey, const char *otherkey, struct chatlog *log, int logtype) { - if (!log->log_on) - return; + if (selfkey == NULL || (logtype == LOG_CHAT && otherkey == NULL)) + return -1; if (!valid_nick(name)) name = UNKNOWN_NAME; + const char *namedash = "-"; const char *set_path = user_settings_->chatlogs_path; char *user_config_dir = get_user_config_dir(); - int path_len = strlen(set_path) + strlen(name) ? *set_path - : strlen(user_config_dir) + strlen(LOGDIR) + strlen(name); + int path_len = strlen(name) + strlen(".log") + strlen("-") + strlen(namedash); + path_len += strlen(set_path) ? *set_path : strlen(user_config_dir) + strlen(LOGDIR); - /* use first 4 digits of key as log ident. If no key use a timestamp */ - char ident[32]; + /* first 4 digits of selfkey */ + char self_id[32]; + path_len += (KEY_IDENT_DIGITS * 2 + 5); + sprintf(&self_id[0], "%02X", selfkey[0] & 0xff); + sprintf(&self_id[2], "%02X", selfkey[1] & 0xff); + self_id[KEY_IDENT_DIGITS * 2 + 1] = '\0'; - if (key != NULL) { - path_len += (KEY_IDENT_DIGITS * 2 + 5); - sprintf(&ident[0], "%02X", key[0] & 0xff); - sprintf(&ident[2], "%02X", key[1] & 0xff); - ident[KEY_IDENT_DIGITS * 2 + 1] = '\0'; - } else { - strftime(ident, sizeof(ident), "%Y-%m-%d[%H:%M:%S]", get_time()); - path_len += strlen(ident) + 1; - } + char other_id[32] = {0}; - if (path_len >= MAX_STR_SIZE) { - log->log_on = false; - free(user_config_dir); - return; + switch (logtype) { + case LOG_CHAT: + path_len += (KEY_IDENT_DIGITS * 2 + 5); + sprintf(&other_id[0], "%02X", otherkey[0] & 0xff); + sprintf(&other_id[2], "%02X", otherkey[1] & 0xff); + other_id[KEY_IDENT_DIGITS * 2 + 1] = '\0'; + break; + + case LOG_GROUP: + strftime(other_id, sizeof(other_id), "%Y-%m-%d[%H:%M:%S]", get_time()); + path_len += strlen(other_id); + break; + + case LOG_PROMPT: + namedash = ""; + --path_len; + break; } char log_path[MAX_STR_SIZE]; - if (*set_path) - snprintf(log_path, sizeof(log_path), "%s%s-%s.log", set_path, name, ident); + if (path_len >= sizeof(log_path)) { + free(user_config_dir); + return -1; + } + + if (!string_is_empty(set_path)) + snprintf(log_path, sizeof(log_path), "%s%s-%s%s%s.log", set_path, self_id, name, namedash, other_id); else - snprintf(log_path, sizeof(log_path), "%s%s%s-%s.log", user_config_dir, LOGDIR, name, ident); + snprintf(log_path, sizeof(log_path), "%s%s%s-%s%s%s.log", user_config_dir, LOGDIR, self_id, name, namedash, other_id); free(user_config_dir); log->file = fopen(log_path, "a+"); snprintf(log->path, sizeof(log->path), "%s", log_path); - if (log->file == NULL) { - log->log_on = false; - return; - } + if (log->file == NULL) + return -1; + + return 0; } #define LOG_FLUSH_LIMIT 1 /* limits calls to fflush to a max of one per LOG_FLUSH_LIMIT seconds */ @@ -119,14 +134,6 @@ void write_to_log(const char *msg, const char *name, struct chatlog *log, bool e } } -void log_enable(char *name, const char *key, struct chatlog *log) -{ - log->log_on = true; - - if (log->file == NULL) - init_logging_session(name, key, log); -} - void log_disable(struct chatlog *log) { log->log_on = false; @@ -137,6 +144,16 @@ void log_disable(struct chatlog *log) } } +void log_enable(char *name, const char *selfkey, const char *otherkey, struct chatlog *log, int logtype) +{ + log->log_on = true; + + if (log->file == NULL) { + if (init_logging_session(name, selfkey, otherkey, log, logtype) == -1) + log_disable(log); + } +} + /* Loads previous history from chat log */ void load_chat_history(ToxWindow *self, struct chatlog *log) { diff --git a/src/log.h b/src/log.h index 2a9018b..d212b4c 100644 --- a/src/log.h +++ b/src/log.h @@ -30,14 +30,17 @@ struct chatlog { bool log_on; /* specific to current chat window */ }; -/* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */ -void init_logging_session(char *name, const char *key, struct chatlog *log); +enum { + LOG_GROUP, + LOG_PROMPT, + LOG_CHAT, +} LOG_TYPE; /* formats/writes line to log file */ void write_to_log(const char *msg, const char *name, struct chatlog *log, bool event); /* enables logging for specified log and creates/fetches file if necessary */ -void log_enable(char *name, const char *key, struct chatlog *log); +void log_enable(char *name, const char *selfkey, const char *otherkey, struct chatlog *log, int logtype); /* disables logging for specified log and closes file */ void log_disable(struct chatlog *log); diff --git a/src/prompt.c b/src/prompt.c index 79c11e9..eb684a0 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -458,7 +458,7 @@ static void prompt_onInit(ToxWindow *self, Tox *m) if (user_settings_->autolog == AUTOLOG_ON) { char myid[TOX_FRIEND_ADDRESS_SIZE]; tox_get_address(m, (uint8_t *) myid); - log_enable(self->name, myid, ctx->log); + log_enable(self->name, myid, NULL, ctx->log, LOG_PROMPT); } scrollok(ctx->history, 0);