mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-23 01:43:01 +01:00
simplify logging
This commit is contained in:
parent
7f38c3c6e7
commit
24b763bce6
@ -115,7 +115,7 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1
|
|||||||
} else
|
} else
|
||||||
wprintw(ctx->history, "%s\n", msg);
|
wprintw(ctx->history, "%s\n", msg);
|
||||||
|
|
||||||
add_to_log_buf(msg, nick, ctx->log, false);
|
write_to_log(msg, nick, ctx->log, false);
|
||||||
alert_window(self, WINDOW_ALERT_1, true);
|
alert_window(self, WINDOW_ALERT_1, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,7 +159,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin
|
|||||||
wprintw(ctx->history, "* %s %s\n", nick, action);
|
wprintw(ctx->history, "* %s %s\n", nick, action);
|
||||||
wattroff(ctx->history, COLOR_PAIR(YELLOW));
|
wattroff(ctx->history, COLOR_PAIR(YELLOW));
|
||||||
|
|
||||||
add_to_log_buf(action, nick, ctx->log, true);
|
write_to_log(action, nick, ctx->log, true);
|
||||||
alert_window(self, WINDOW_ALERT_1, true);
|
alert_window(self, WINDOW_ALERT_1, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -341,7 +341,7 @@ static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *acti
|
|||||||
wprintw(ctx->history, " * Failed to send action\n");
|
wprintw(ctx->history, " * Failed to send action\n");
|
||||||
wattroff(ctx->history, COLOR_PAIR(RED));
|
wattroff(ctx->history, COLOR_PAIR(RED));
|
||||||
} else {
|
} else {
|
||||||
add_to_log_buf(action, selfname, ctx->log, true);
|
write_to_log(action, selfname, ctx->log, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -532,7 +532,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
|
|||||||
wprintw(ctx->history, " * Failed to send message.\n");
|
wprintw(ctx->history, " * Failed to send message.\n");
|
||||||
wattroff(ctx->history, COLOR_PAIR(RED));
|
wattroff(ctx->history, COLOR_PAIR(RED));
|
||||||
} else {
|
} else {
|
||||||
add_to_log_buf(line, selfname, ctx->log, false);
|
write_to_log(line, selfname, ctx->log, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,7 +168,7 @@ static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int
|
|||||||
wprintw(ctx->history, "%s\n", msg);
|
wprintw(ctx->history, "%s\n", msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
add_to_log_buf(msg, nick, ctx->log, false);
|
write_to_log(msg, nick, ctx->log, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int peernum, uint8_t *action,
|
static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int peernum, uint8_t *action,
|
||||||
@ -204,7 +204,7 @@ static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int p
|
|||||||
wprintw(ctx->history, "* %s %s\n", nick, action);
|
wprintw(ctx->history, "* %s %s\n", nick, action);
|
||||||
wattroff(ctx->history, COLOR_PAIR(YELLOW));
|
wattroff(ctx->history, COLOR_PAIR(YELLOW));
|
||||||
|
|
||||||
add_to_log_buf(action, nick, ctx->log, true);
|
write_to_log(action, nick, ctx->log, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Puts two copies of peerlist in chat instance */
|
/* Puts two copies of peerlist in chat instance */
|
||||||
@ -282,7 +282,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu
|
|||||||
wprintw(ctx->history, " %s\n", event);
|
wprintw(ctx->history, " %s\n", event);
|
||||||
wattroff(ctx->history, COLOR_PAIR(GREEN));
|
wattroff(ctx->history, COLOR_PAIR(GREEN));
|
||||||
|
|
||||||
add_to_log_buf(event, peername, ctx->log, true);
|
write_to_log(event, peername, ctx->log, true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TOX_CHAT_CHANGE_PEER_DEL:
|
case TOX_CHAT_CHANGE_PEER_DEL:
|
||||||
@ -296,7 +296,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu
|
|||||||
if (groupchats[self->num].side_pos > 0)
|
if (groupchats[self->num].side_pos > 0)
|
||||||
--groupchats[self->num].side_pos;
|
--groupchats[self->num].side_pos;
|
||||||
|
|
||||||
add_to_log_buf(event, oldpeername, ctx->log, true);
|
write_to_log(event, oldpeername, ctx->log, true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TOX_CHAT_CHANGE_PEER_NAME:
|
case TOX_CHAT_CHANGE_PEER_NAME:
|
||||||
@ -314,7 +314,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu
|
|||||||
|
|
||||||
uint8_t tmp_event[TOXIC_MAX_NAME_LENGTH + 32];
|
uint8_t tmp_event[TOXIC_MAX_NAME_LENGTH + 32];
|
||||||
snprintf(tmp_event, sizeof(tmp_event), "is now known as %s", peername);
|
snprintf(tmp_event, sizeof(tmp_event), "is now known as %s", peername);
|
||||||
add_to_log_buf(tmp_event, oldpeername, ctx->log, true);
|
write_to_log(tmp_event, oldpeername, ctx->log, true);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
62
src/log.c
62
src/log.c
@ -28,7 +28,7 @@
|
|||||||
#include "toxic_windows.h"
|
#include "toxic_windows.h"
|
||||||
#include "misc_tools.h"
|
#include "misc_tools.h"
|
||||||
|
|
||||||
/* gets the log path by appending to the config dir the name and a pseudo-unique identity */
|
/* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */
|
||||||
void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log)
|
void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log)
|
||||||
{
|
{
|
||||||
if (!log->log_on)
|
if (!log->log_on)
|
||||||
@ -56,55 +56,36 @@ void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log)
|
|||||||
|
|
||||||
if (path_len > MAX_STR_SIZE) {
|
if (path_len > MAX_STR_SIZE) {
|
||||||
log->log_on = false;
|
log->log_on = false;
|
||||||
|
log->file = NULL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(log->log_path, MAX_STR_SIZE, "%s%s%s-%s.log",
|
uint8_t log_path[MAX_STR_SIZE];
|
||||||
|
|
||||||
|
snprintf(log_path, MAX_STR_SIZE, "%s%s%s-%s.log",
|
||||||
user_config_dir, CONFIGDIR, name, ident);
|
user_config_dir, CONFIGDIR, name, ident);
|
||||||
|
|
||||||
FILE *logfile = fopen(log->log_path, "a");
|
log->file = fopen(log_path, "a");
|
||||||
|
|
||||||
if (logfile == NULL) {
|
if (log->file == NULL) {
|
||||||
log->log_on = false;
|
log->log_on = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(logfile, "\n*** NEW SESSION ***\n\n");
|
fprintf(log->file, "\n*** NEW SESSION ***\n\n");
|
||||||
|
|
||||||
fclose(logfile);
|
|
||||||
free(user_config_dir);
|
free(user_config_dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* writes contents from a chatcontext's log buffer to respective log file and resets log pos.
|
void write_to_log(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event)
|
||||||
This is triggered when the log buffer is full, but may be forced. */
|
|
||||||
void write_to_log(struct chatlog *log)
|
|
||||||
{
|
{
|
||||||
if (!log->log_on)
|
if (!log->log_on)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
FILE *logfile = fopen(log->log_path, "a");
|
if (log->file == NULL) {
|
||||||
|
|
||||||
if (logfile == NULL) {
|
|
||||||
log->log_on = false;
|
log->log_on = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < log->pos; ++i)
|
|
||||||
fprintf(logfile, "%s", log->log_buf[i]);
|
|
||||||
|
|
||||||
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, struct chatlog *log, bool event)
|
|
||||||
{
|
|
||||||
if (!log->log_on)
|
|
||||||
return;
|
|
||||||
|
|
||||||
uint8_t name_frmt[TOXIC_MAX_NAME_LENGTH + 3];
|
uint8_t name_frmt[TOXIC_MAX_NAME_LENGTH + 3];
|
||||||
|
|
||||||
if (event)
|
if (event)
|
||||||
@ -113,26 +94,33 @@ void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event
|
|||||||
snprintf(name_frmt, sizeof(name_frmt), "%s:", name);
|
snprintf(name_frmt, sizeof(name_frmt), "%s:", name);
|
||||||
|
|
||||||
struct tm *tminfo = get_time();
|
struct tm *tminfo = get_time();
|
||||||
snprintf(log->log_buf[log->pos], MAX_LOG_LINE_SIZE, "%04d/%02d/%02d [%02d:%02d:%02d] %s %s\n",
|
fprintf(log->file,"%04d/%02d/%02d [%02d:%02d:%02d] %s %s\n", tminfo->tm_year+1900,
|
||||||
tminfo->tm_year + 1900, tminfo->tm_mon + 1, tminfo->tm_mday,
|
tminfo->tm_mon+1, tminfo->tm_mday, tminfo->tm_hour, tminfo->tm_min,
|
||||||
tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec, name_frmt, msg);
|
tminfo->tm_sec, name_frmt, msg);
|
||||||
|
|
||||||
|
uint64_t curtime = (uint64_t) time(NULL);
|
||||||
|
|
||||||
|
if (timed_out(log->lastflush, curtime, LOG_FLUSH_LIMIT))
|
||||||
|
fflush(log->file);
|
||||||
|
|
||||||
|
log->lastflush = curtime;
|
||||||
|
|
||||||
if (++(log->pos) >= MAX_LOG_BUF_LINES)
|
|
||||||
write_to_log(log);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log)
|
void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log)
|
||||||
{
|
{
|
||||||
log->log_on = true;
|
log->log_on = true;
|
||||||
|
|
||||||
if (!log->log_path[0])
|
if (log->file == NULL)
|
||||||
init_logging_session(name, key, log);
|
init_logging_session(name, key, log);
|
||||||
}
|
}
|
||||||
|
|
||||||
void log_disable(struct chatlog *log)
|
void log_disable(struct chatlog *log)
|
||||||
{
|
{
|
||||||
if (log->log_on) {
|
|
||||||
write_to_log(log);
|
|
||||||
log->log_on = false;
|
log->log_on = false;
|
||||||
|
|
||||||
|
if (log->file != NULL) {
|
||||||
|
fclose(log->file);
|
||||||
|
log->file = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
14
src/log.h
14
src/log.h
@ -20,16 +20,14 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* gets the log path by appending to the config dir the name and a pseudo-unique identity */
|
/* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */
|
||||||
void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log);
|
void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log);
|
||||||
|
|
||||||
/* Adds line/event to log_buf with timestamp and name. If buf is full, triggers write_to_log.
|
/* formats/writes line to log file */
|
||||||
If event is true, formats line as an event, e.g. * name has gone offline */
|
void write_to_log(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event);
|
||||||
void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event);
|
|
||||||
|
|
||||||
/* writes contents from a chatcontext's log buffer to respective log file and resets log pos.
|
|
||||||
This is triggered automatically when the log buffer is full, but may be forced. */
|
|
||||||
void write_to_log(struct chatlog *log);
|
|
||||||
|
|
||||||
|
/* enables logging for specified log and creates/fetches file if necessary */
|
||||||
void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log);
|
void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log);
|
||||||
|
|
||||||
|
/* disables logging for specified log and closes file */
|
||||||
void log_disable(struct chatlog *log);
|
void log_disable(struct chatlog *log);
|
||||||
|
16
src/prompt.c
16
src/prompt.c
@ -398,26 +398,26 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int friendnum , u
|
|||||||
uint8_t *msg;
|
uint8_t *msg;
|
||||||
|
|
||||||
if (status == 1) {
|
if (status == 1) {
|
||||||
msg = "has come online\n";
|
msg = "has come online";
|
||||||
wattron(self->window, COLOR_PAIR(GREEN));
|
wattron(self->window, COLOR_PAIR(GREEN));
|
||||||
wattron(self->window, A_BOLD);
|
wattron(self->window, A_BOLD);
|
||||||
wprintw(self->window, "* %s ", nick);
|
wprintw(self->window, "* %s ", nick);
|
||||||
wattroff(self->window, A_BOLD);
|
wattroff(self->window, A_BOLD);
|
||||||
wprintw(self->window, "%s", msg);
|
wprintw(self->window, "%s\n", msg);
|
||||||
wattroff(self->window, COLOR_PAIR(GREEN));
|
wattroff(self->window, COLOR_PAIR(GREEN));
|
||||||
|
|
||||||
add_to_log_buf(msg, nick, prt->log, true);
|
write_to_log(msg, nick, prt->log, true);
|
||||||
alert_window(self, WINDOW_ALERT_2, false);
|
alert_window(self, WINDOW_ALERT_2, false);
|
||||||
} else {
|
} else {
|
||||||
msg = "has gone offline\n";
|
msg = "has gone offline";
|
||||||
wattron(self->window, COLOR_PAIR(RED));
|
wattron(self->window, COLOR_PAIR(RED));
|
||||||
wattron(self->window, A_BOLD);
|
wattron(self->window, A_BOLD);
|
||||||
wprintw(self->window, "* %s ", nick);
|
wprintw(self->window, "* %s ", nick);
|
||||||
wattroff(self->window, A_BOLD);
|
wattroff(self->window, A_BOLD);
|
||||||
wprintw(self->window, "%s", msg);
|
wprintw(self->window, "%s\n", msg);
|
||||||
wattroff(self->window, COLOR_PAIR(RED));
|
wattroff(self->window, COLOR_PAIR(RED));
|
||||||
|
|
||||||
add_to_log_buf(msg, nick, prt->log, true);
|
write_to_log(msg, nick, prt->log, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -434,14 +434,14 @@ static void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data,
|
|||||||
uint8_t msg[MAX_STR_SIZE];
|
uint8_t msg[MAX_STR_SIZE];
|
||||||
snprintf(msg, sizeof(msg), "Friend request with the message '%s'\n", data);
|
snprintf(msg, sizeof(msg), "Friend request with the message '%s'\n", data);
|
||||||
wprintw(self->window, "%s", msg);
|
wprintw(self->window, "%s", msg);
|
||||||
add_to_log_buf(msg, "", prt->log, true);
|
write_to_log(msg, "", prt->log, true);
|
||||||
|
|
||||||
int n = add_friend_request(key);
|
int n = add_friend_request(key);
|
||||||
|
|
||||||
if (n == -1) {
|
if (n == -1) {
|
||||||
uint8_t *errmsg = "Friend request queue is full. Discarding request.\n";
|
uint8_t *errmsg = "Friend request queue is full. Discarding request.\n";
|
||||||
wprintw(self->window, "%s", errmsg);
|
wprintw(self->window, "%s", errmsg);
|
||||||
add_to_log_buf(errmsg, "", prt->log, true);
|
write_to_log(errmsg, "", prt->log, true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,12 +138,11 @@ struct StatusBar {
|
|||||||
bool is_online;
|
bool is_online;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define MAX_LOG_BUF_LINES 10 /* write log_buf contents to log file after this many lines */
|
#define LOG_FLUSH_LIMIT 2 /* limits calls to fflush(logfile) to a max of one per LOG_FLUSH_LIMIT seconds */
|
||||||
#define MAX_LOG_LINE_SIZE MAX_STR_SIZE + TOXIC_MAX_NAME_LENGTH + 32 /* extra room for timestamp */
|
|
||||||
|
|
||||||
struct chatlog {
|
struct chatlog {
|
||||||
uint8_t log_path[MAX_STR_SIZE];
|
FILE *file;
|
||||||
uint8_t log_buf[MAX_LOG_BUF_LINES][MAX_LOG_LINE_SIZE];
|
uint64_t lastflush;
|
||||||
int pos;
|
int pos;
|
||||||
bool log_on; /* specific to current chat window */
|
bool log_on; /* specific to current chat window */
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user