mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-23 06:13:03 +01:00
don't share chatlogs across data files (changes log naming convention)
This commit is contained in:
parent
3c74385f5c
commit
1c16467eb9
@ -1091,7 +1091,10 @@ static void chat_onInit(ToxWindow *self, Tox *m)
|
|||||||
|
|
||||||
line_info_init(ctx->hst);
|
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);
|
load_chat_history(self, ctx->log);
|
||||||
|
|
||||||
if (!Friends.list[self->num].logging_on)
|
if (!Friends.list[self->num].logging_on)
|
||||||
|
@ -286,16 +286,16 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
|
|||||||
const char *swch = argv[1];
|
const char *swch = argv[1];
|
||||||
|
|
||||||
if (!strcmp(swch, "1") || !strcmp(swch, "on")) {
|
if (!strcmp(swch, "1") || !strcmp(swch, "on")) {
|
||||||
|
char myid[TOX_FRIEND_ADDRESS_SIZE];
|
||||||
|
tox_get_address(m, (uint8_t *) myid);
|
||||||
|
|
||||||
if (self->is_chat) {
|
if (self->is_chat) {
|
||||||
Friends.list[self->num].logging_on = true;
|
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) {
|
} else if (self->is_prompt) {
|
||||||
char myid[TOX_FRIEND_ADDRESS_SIZE];
|
log_enable(self->name, myid, NULL, log, LOG_PROMPT);
|
||||||
tox_get_address(m, (uint8_t *) myid);
|
|
||||||
log_enable(self->name, myid, log);
|
|
||||||
} else if (self->is_groupchat) {
|
} else if (self->is_groupchat) {
|
||||||
log_enable(self->name, NULL, log);
|
log_enable(self->name, myid, NULL, log, LOG_GROUP);
|
||||||
}
|
}
|
||||||
|
|
||||||
msg = "Logging enabled";
|
msg = "Logging enabled";
|
||||||
|
@ -503,8 +503,11 @@ static void groupchat_onInit(ToxWindow *self, Tox *m)
|
|||||||
|
|
||||||
line_info_init(ctx->hst);
|
line_info_init(ctx->hst);
|
||||||
|
|
||||||
if (user_settings_->autolog == AUTOLOG_ON)
|
if (user_settings_->autolog == AUTOLOG_ON) {
|
||||||
log_enable(self->name, NULL, ctx->log);
|
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);
|
execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE);
|
||||||
|
|
||||||
|
89
src/log.c
89
src/log.c
@ -35,56 +35,71 @@
|
|||||||
|
|
||||||
extern struct user_settings *user_settings_;
|
extern struct user_settings *user_settings_;
|
||||||
|
|
||||||
/* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */
|
/* Opens log file or creates a new one */
|
||||||
void init_logging_session(char *name, const char *key, struct chatlog *log)
|
static int init_logging_session(char *name, const char *selfkey, const char *otherkey, struct chatlog *log, int logtype)
|
||||||
{
|
{
|
||||||
if (!log->log_on)
|
if (selfkey == NULL || (logtype == LOG_CHAT && otherkey == NULL))
|
||||||
return;
|
return -1;
|
||||||
|
|
||||||
if (!valid_nick(name))
|
if (!valid_nick(name))
|
||||||
name = UNKNOWN_NAME;
|
name = UNKNOWN_NAME;
|
||||||
|
|
||||||
|
const char *namedash = "-";
|
||||||
const char *set_path = user_settings_->chatlogs_path;
|
const char *set_path = user_settings_->chatlogs_path;
|
||||||
|
|
||||||
char *user_config_dir = get_user_config_dir();
|
char *user_config_dir = get_user_config_dir();
|
||||||
int path_len = strlen(set_path) + strlen(name) ? *set_path
|
int path_len = strlen(name) + strlen(".log") + strlen("-") + strlen(namedash);
|
||||||
: strlen(user_config_dir) + strlen(LOGDIR) + strlen(name);
|
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 */
|
/* first 4 digits of selfkey */
|
||||||
char ident[32];
|
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) {
|
char other_id[32] = {0};
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (path_len >= MAX_STR_SIZE) {
|
switch (logtype) {
|
||||||
log->log_on = false;
|
case LOG_CHAT:
|
||||||
free(user_config_dir);
|
path_len += (KEY_IDENT_DIGITS * 2 + 5);
|
||||||
return;
|
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];
|
char log_path[MAX_STR_SIZE];
|
||||||
|
|
||||||
if (*set_path)
|
if (path_len >= sizeof(log_path)) {
|
||||||
snprintf(log_path, sizeof(log_path), "%s%s-%s.log", set_path, name, ident);
|
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
|
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);
|
free(user_config_dir);
|
||||||
|
|
||||||
log->file = fopen(log_path, "a+");
|
log->file = fopen(log_path, "a+");
|
||||||
snprintf(log->path, sizeof(log->path), "%s", log_path);
|
snprintf(log->path, sizeof(log->path), "%s", log_path);
|
||||||
|
|
||||||
if (log->file == NULL) {
|
if (log->file == NULL)
|
||||||
log->log_on = false;
|
return -1;
|
||||||
return;
|
|
||||||
}
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define LOG_FLUSH_LIMIT 1 /* limits calls to fflush to a max of one per LOG_FLUSH_LIMIT seconds */
|
#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)
|
void log_disable(struct chatlog *log)
|
||||||
{
|
{
|
||||||
log->log_on = false;
|
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 */
|
/* Loads previous history from chat log */
|
||||||
void load_chat_history(ToxWindow *self, struct chatlog *log)
|
void load_chat_history(ToxWindow *self, struct chatlog *log)
|
||||||
{
|
{
|
||||||
|
@ -30,14 +30,17 @@ struct chatlog {
|
|||||||
bool log_on; /* specific to current chat window */
|
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 */
|
enum {
|
||||||
void init_logging_session(char *name, const char *key, struct chatlog *log);
|
LOG_GROUP,
|
||||||
|
LOG_PROMPT,
|
||||||
|
LOG_CHAT,
|
||||||
|
} LOG_TYPE;
|
||||||
|
|
||||||
/* formats/writes line to log file */
|
/* formats/writes line to log file */
|
||||||
void write_to_log(const char *msg, const char *name, struct chatlog *log, bool event);
|
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 */
|
/* 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 */
|
/* disables logging for specified log and closes file */
|
||||||
void log_disable(struct chatlog *log);
|
void log_disable(struct chatlog *log);
|
||||||
|
@ -458,7 +458,7 @@ static void prompt_onInit(ToxWindow *self, Tox *m)
|
|||||||
if (user_settings_->autolog == AUTOLOG_ON) {
|
if (user_settings_->autolog == AUTOLOG_ON) {
|
||||||
char myid[TOX_FRIEND_ADDRESS_SIZE];
|
char myid[TOX_FRIEND_ADDRESS_SIZE];
|
||||||
tox_get_address(m, (uint8_t *) myid);
|
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);
|
scrollok(ctx->history, 0);
|
||||||
|
Loading…
Reference in New Issue
Block a user