mirror of
https://github.com/Tha14/toxic.git
synced 2025-06-20 13:06:36 +02:00
refactor logging functions to only handle chatlog pointers
This commit is contained in:
58
src/log.c
58
src/log.c
@ -1,4 +1,4 @@
|
||||
/* ctx->log.c
|
||||
/* log->c
|
||||
*
|
||||
*
|
||||
* Copyright (C) 2014 Toxic All Rights Reserved.
|
||||
@ -29,9 +29,9 @@
|
||||
#include "misc_tools.h"
|
||||
|
||||
/* gets the log path by appending to the config dir the name and a pseudo-unique identity */
|
||||
void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx)
|
||||
void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log)
|
||||
{
|
||||
if (!ctx->log.log_on)
|
||||
if (!log->log_on)
|
||||
return;
|
||||
|
||||
char *user_config_dir = get_user_config_dir();
|
||||
@ -49,23 +49,23 @@ void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx)
|
||||
} else {
|
||||
struct tm *tminfo = get_time();
|
||||
snprintf(ident, sizeof(ident),
|
||||
"%04d-%02d-%02d-%02d:%02d:%02d", tminfo->tm_year+1900,tminfo->tm_mon+1, tminfo->tm_mday,
|
||||
tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec);
|
||||
"%04d-%02d-%02d[%02d:%02d:%02d]", tminfo->tm_year+1900,tminfo->tm_mon+1, tminfo->tm_mday,
|
||||
tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec);
|
||||
path_len += strlen(ident) + 1;
|
||||
}
|
||||
|
||||
if (path_len > MAX_STR_SIZE) {
|
||||
ctx->log.log_on = false;
|
||||
log->log_on = false;
|
||||
return;
|
||||
}
|
||||
|
||||
snprintf(ctx->log.log_path, MAX_STR_SIZE, "%s%s%s-%s.log",
|
||||
snprintf(log->log_path, MAX_STR_SIZE, "%s%s%s-%s.log",
|
||||
user_config_dir, CONFIGDIR, name, ident);
|
||||
|
||||
FILE *logfile = fopen(ctx->log.log_path, "a");
|
||||
FILE *logfile = fopen(log->log_path, "a");
|
||||
|
||||
if (logfile == NULL) {
|
||||
ctx->log.log_on = false;
|
||||
log->log_on = false;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -77,32 +77,32 @@ void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx)
|
||||
|
||||
/* writes contents from a chatcontext's log buffer to respective log file and resets log pos.
|
||||
This is triggered when the log buffer is full, but may be forced. */
|
||||
void write_to_log(ChatContext *ctx)
|
||||
void write_to_log(struct chatlog *log)
|
||||
{
|
||||
if (!ctx->log.log_on)
|
||||
if (!log->log_on)
|
||||
return;
|
||||
|
||||
FILE *logfile = fopen(ctx->log.log_path, "a");
|
||||
FILE *logfile = fopen(log->log_path, "a");
|
||||
|
||||
if (logfile == NULL) {
|
||||
ctx->log.log_on = false;
|
||||
log->log_on = false;
|
||||
return;
|
||||
}
|
||||
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ctx->log.pos; ++i)
|
||||
fprintf(logfile, "%s", ctx->log.log_buf[i]);
|
||||
for (i = 0; i < log->pos; ++i)
|
||||
fprintf(logfile, "%s", log->log_buf[i]);
|
||||
|
||||
ctx->log.pos = 0;
|
||||
log->pos = 0;
|
||||
fclose(logfile);
|
||||
}
|
||||
|
||||
/* Adds line/event to log_buf with timestamp and name. If buf is full, triggers write_to_log.
|
||||
If event is true, formats line as an event, e.g. * name has gone offline */
|
||||
void add_to_log_buf(uint8_t *msg, uint8_t *name, ChatContext *ctx, bool event)
|
||||
void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event)
|
||||
{
|
||||
if (!ctx->log.log_on)
|
||||
if (!log->log_on)
|
||||
return;
|
||||
|
||||
uint8_t name_frmt[TOXIC_MAX_NAME_LENGTH + 3];
|
||||
@ -113,10 +113,26 @@ void add_to_log_buf(uint8_t *msg, uint8_t *name, ChatContext *ctx, bool event)
|
||||
snprintf(name_frmt, sizeof(name_frmt), "%s:", name);
|
||||
|
||||
struct tm *tminfo = get_time();
|
||||
snprintf(ctx->log.log_buf[ctx->log.pos], MAX_LOG_LINE_SIZE, "%04d/%02d/%02d [%02d:%02d:%02d] %s %s\n",
|
||||
snprintf(log->log_buf[log->pos], MAX_LOG_LINE_SIZE, "%04d/%02d/%02d [%02d:%02d:%02d] %s %s\n",
|
||||
tminfo->tm_year + 1900, tminfo->tm_mon + 1, tminfo->tm_mday,
|
||||
tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec, name_frmt, msg);
|
||||
|
||||
if (++(ctx->log.pos) >= MAX_LOG_BUF_LINES)
|
||||
write_to_log(ctx);
|
||||
if (++(log->pos) >= MAX_LOG_BUF_LINES)
|
||||
write_to_log(log);
|
||||
}
|
||||
|
||||
void log_enable(struct chatlog *log, uint8_t *name, uint8_t *key)
|
||||
{
|
||||
log->log_on = true;
|
||||
|
||||
if (!log->log_path[0])
|
||||
init_logging_session(name, key, log);
|
||||
}
|
||||
|
||||
void log_disable(struct chatlog *log)
|
||||
{
|
||||
if (log->log_on) {
|
||||
write_to_log(log);
|
||||
log->log_on = false;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user