1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 01:53:02 +01:00

refactor logging functions to only handle chatlog pointers

This commit is contained in:
Jfreegman 2014-03-01 07:10:44 -05:00
parent 1e503b1080
commit 7109b8fa18
10 changed files with 95 additions and 103 deletions

View File

@ -33,7 +33,7 @@
#include "misc_tools.h" #include "misc_tools.h"
#include "friendlist.h" #include "friendlist.h"
#include "toxic_strings.h" #include "toxic_strings.h"
#include "chat.h" #include "log.h"
extern char *DATA_FILE; extern char *DATA_FILE;
extern int store_data(Tox *m, char *path); extern int store_data(Tox *m, char *path);
@ -79,7 +79,7 @@ void kill_chat_window(ToxWindow *self)
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
StatusBar *statusbar = self->stb; StatusBar *statusbar = self->stb;
chat_disable_log(self); log_disable(ctx->log);
int f_num = self->num; int f_num = self->num;
delwin(ctx->linewin); delwin(ctx->linewin);
@ -87,30 +87,11 @@ void kill_chat_window(ToxWindow *self)
del_window(self); del_window(self);
disable_chatwin(f_num); disable_chatwin(f_num);
free(ctx->log);
free(ctx); free(ctx);
free(statusbar); free(statusbar);
} }
void chat_enable_log(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
ctx->log.log_on = true;
if (!ctx->log.log_path[0])
init_logging_session(self->name, friends[self->num].pub_key, ctx);
}
void chat_disable_log(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
if (ctx->log.log_on)
write_to_log(ctx);
ctx->log.log_on = false;
}
static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len) static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len)
{ {
if (self->num != num) if (self->num != num)
@ -134,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, false); add_to_log_buf(msg, nick, ctx->log, false);
alert_window(self, WINDOW_ALERT_1, true); alert_window(self, WINDOW_ALERT_1, true);
} }
@ -178,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, true); add_to_log_buf(action, nick, ctx->log, true);
alert_window(self, WINDOW_ALERT_1, true); alert_window(self, WINDOW_ALERT_1, true);
} }
@ -360,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, true); add_to_log_buf(action, selfname, ctx->log, true);
} }
} }
@ -551,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, false); add_to_log_buf(line, selfname, ctx->log, false);
} }
} }
@ -688,15 +669,25 @@ static void chat_onInit(ToxWindow *self, Tox *m)
ctx->history = subwin(self->window, y2-CHATBOX_HEIGHT+1, x2, 0, 0); ctx->history = subwin(self->window, y2-CHATBOX_HEIGHT+1, x2, 0, 0);
scrollok(ctx->history, 1); scrollok(ctx->history, 1);
ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x2, y2-CHATBOX_HEIGHT, 0); ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x2, y2-CHATBOX_HEIGHT, 0);
wprintw(ctx->history, "\n\n");
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE); ctx->log = malloc(sizeof(struct chatlog));
if (ctx->log == NULL) {
endwin();
fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE);
}
memset(ctx->log, 0, sizeof(struct chatlog));
if (friends[self->num].logging_on) if (friends[self->num].logging_on)
chat_enable_log(self); log_enable(ctx->log, self->name, friends[self->num].pub_key);
wprintw(ctx->history, "\n\n");
execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE);
execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} }
ToxWindow new_chat(Tox *m, int friendnum) ToxWindow new_chat(Tox *m, int friendnum)

View File

@ -25,8 +25,6 @@
#include "toxic_windows.h" #include "toxic_windows.h"
void chat_enable_log(ToxWindow *self);
void chat_disable_log(ToxWindow *self);
void kill_chat_window(ToxWindow *self); void kill_chat_window(ToxWindow *self);
ToxWindow new_chat(Tox *m, int friendnum); ToxWindow new_chat(Tox *m, int friendnum);

View File

@ -230,7 +230,7 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
if (argc == 0) { if (argc == 0) {
if (ctx->log.log_on) { if (ctx->log->log_on) {
wprintw(window, "Logging for this chat is "); wprintw(window, "Logging for this chat is ");
wattron(window, COLOR_PAIR(GREEN) | A_BOLD); wattron(window, COLOR_PAIR(GREEN) | A_BOLD);
wprintw(window, "[on]"); wprintw(window, "[on]");
@ -248,27 +248,26 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
} }
uint8_t *swch = argv[1]; uint8_t *swch = argv[1];
uint8_t *ident = NULL;
if (!strcmp(swch, "1") || !strcmp(swch, "on")) { if (!strcmp(swch, "1") || !strcmp(swch, "on")) {
if (self->is_chat) { if (self->is_chat) {
chat_enable_log(self);
friends[self->num].logging_on = true; friends[self->num].logging_on = true;
} else if (self->is_groupchat) { ident = friends[self->num].pub_key;
groupchat_enable_log(self);
} }
log_enable(ctx->log, self->name, ident);
wprintw(window, "Logging "); wprintw(window, "Logging ");
wattron(window, COLOR_PAIR(GREEN) | A_BOLD); wattron(window, COLOR_PAIR(GREEN) | A_BOLD);
wprintw(window, "[on]\n"); wprintw(window, "[on]\n");
wattroff(window, COLOR_PAIR(GREEN) | A_BOLD); wattroff(window, COLOR_PAIR(GREEN) | A_BOLD);
return; return;
} else if (!strcmp(swch, "0") || !strcmp(swch, "off")) { } else if (!strcmp(swch, "0") || !strcmp(swch, "off")) {
if (self->is_chat) { if (self->is_chat)
chat_disable_log(self);
friends[self->num].logging_on = false; friends[self->num].logging_on = false;
} else if (self->is_groupchat) {
groupchat_disable_log(self); log_disable(ctx->log);
}
wprintw(window, "Logging "); wprintw(window, "Logging ");
wattron(window, COLOR_PAIR(RED) | A_BOLD); wattron(window, COLOR_PAIR(RED) | A_BOLD);

View File

@ -34,6 +34,7 @@
#include "groupchat.h" #include "groupchat.h"
#include "prompt.h" #include "prompt.h"
#include "toxic_strings.h" #include "toxic_strings.h"
#include "log.h"
extern char *DATA_FILE; extern char *DATA_FILE;
extern int store_data(Tox *m, char *path); extern int store_data(Tox *m, char *path);
@ -75,9 +76,10 @@ void kill_groupchat_window(ToxWindow *self)
{ {
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
groupchat_disable_log(self); log_disable(ctx->log);
delwin(ctx->linewin); delwin(ctx->linewin);
del_window(self); del_window(self);
free(ctx->log);
free(ctx); free(ctx);
} }
@ -98,30 +100,9 @@ static void close_groupchat(ToxWindow *self, Tox *m, int groupnum)
} }
max_groupchat_index = i; max_groupchat_index = i;
kill_groupchat_window(self); kill_groupchat_window(self);
} }
void groupchat_enable_log(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
ctx->log.log_on = true;
if (!ctx->log.log_path[0])
init_logging_session(self->name, NULL, ctx);
}
void groupchat_disable_log(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
if (ctx->log.log_on) {
write_to_log(ctx);
ctx->log.log_on = false;
}
}
static void print_groupchat_help(ChatContext *ctx) static void print_groupchat_help(ChatContext *ctx)
{ {
wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD); wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
@ -187,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, false); add_to_log_buf(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,
@ -223,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, true); add_to_log_buf(action, nick, ctx->log, true);
} }
/* Puts two copies of peerlist in chat instance */ /* Puts two copies of peerlist in chat instance */
@ -301,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, true); add_to_log_buf(event, peername, ctx->log, true);
break; break;
case TOX_CHAT_CHANGE_PEER_DEL: case TOX_CHAT_CHANGE_PEER_DEL:
@ -315,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, true); add_to_log_buf(event, oldpeername, ctx->log, true);
break; break;
case TOX_CHAT_CHANGE_PEER_NAME: case TOX_CHAT_CHANGE_PEER_NAME:
@ -333,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, true); add_to_log_buf(tmp_event, oldpeername, ctx->log, true);
break; break;
} }
@ -622,6 +603,16 @@ static void groupchat_onInit(ToxWindow *self, Tox *m)
ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x, y-CHATBOX_HEIGHT, 0); ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x, y-CHATBOX_HEIGHT, 0);
ctx->sidebar = subwin(self->window, y-CHATBOX_HEIGHT+1, SIDEBAR_WIDTH, 0, x-SIDEBAR_WIDTH); ctx->sidebar = subwin(self->window, y-CHATBOX_HEIGHT+1, SIDEBAR_WIDTH, 0, x-SIDEBAR_WIDTH);
ctx->log = malloc(sizeof(struct chatlog));
if (ctx->log == NULL) {
endwin();
fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE);
}
memset(ctx->log, 0, sizeof(struct chatlog));
print_groupchat_help(ctx); print_groupchat_help(ctx);
execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE);

View File

@ -32,8 +32,6 @@ typedef struct {
uint8_t *oldpeer_names; uint8_t *oldpeer_names;
} GroupChat; } GroupChat;
void groupchat_enable_log(ToxWindow *self);
void groupchat_disable_log(ToxWindow *self);
void kill_groupchat_window(ToxWindow *self); void kill_groupchat_window(ToxWindow *self);
int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum); int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum);
ToxWindow new_group_chat(Tox *m, int groupnum); ToxWindow new_group_chat(Tox *m, int groupnum);

View File

@ -1,4 +1,4 @@
/* ctx->log.c /* log->c
* *
* *
* Copyright (C) 2014 Toxic All Rights Reserved. * Copyright (C) 2014 Toxic All Rights Reserved.
@ -29,9 +29,9 @@
#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 */ /* 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; return;
char *user_config_dir = get_user_config_dir(); 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 { } else {
struct tm *tminfo = get_time(); struct tm *tminfo = get_time();
snprintf(ident, sizeof(ident), snprintf(ident, sizeof(ident),
"%04d-%02d-%02d-%02d:%02d:%02d", tminfo->tm_year+1900,tminfo->tm_mon+1, tminfo->tm_mday, "%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); tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec);
path_len += strlen(ident) + 1; path_len += strlen(ident) + 1;
} }
if (path_len > MAX_STR_SIZE) { if (path_len > MAX_STR_SIZE) {
ctx->log.log_on = false; log->log_on = false;
return; 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); user_config_dir, CONFIGDIR, name, ident);
FILE *logfile = fopen(ctx->log.log_path, "a"); FILE *logfile = fopen(log->log_path, "a");
if (logfile == NULL) { if (logfile == NULL) {
ctx->log.log_on = false; log->log_on = false;
return; 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. /* 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. */ 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; return;
FILE *logfile = fopen(ctx->log.log_path, "a"); FILE *logfile = fopen(log->log_path, "a");
if (logfile == NULL) { if (logfile == NULL) {
ctx->log.log_on = false; log->log_on = false;
return; return;
} }
int i; int i;
for (i = 0; i < ctx->log.pos; ++i) for (i = 0; i < log->pos; ++i)
fprintf(logfile, "%s", ctx->log.log_buf[i]); fprintf(logfile, "%s", log->log_buf[i]);
ctx->log.pos = 0; log->pos = 0;
fclose(logfile); fclose(logfile);
} }
/* Adds line/event to log_buf with timestamp and name. If buf is full, triggers write_to_log. /* 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 */ 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; return;
uint8_t name_frmt[TOXIC_MAX_NAME_LENGTH + 3]; 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); snprintf(name_frmt, sizeof(name_frmt), "%s:", name);
struct tm *tminfo = get_time(); 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_year + 1900, tminfo->tm_mon + 1, tminfo->tm_mday,
tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec, name_frmt, msg); tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec, name_frmt, msg);
if (++(ctx->log.pos) >= MAX_LOG_BUF_LINES) if (++(log->pos) >= MAX_LOG_BUF_LINES)
write_to_log(ctx); 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;
}
} }

View File

@ -21,16 +21,15 @@
*/ */
/* gets the log path by appending to the config dir the name and a pseudo-unique identity */ /* 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);
/* Adds msg to log_buf with timestamp and name.
If buf is full, triggers write_to_log (which sets buf pos to 0) */
void add_line_log_buf(uint8_t *msg, uint8_t *name, ChatContext *ctx);
/* Adds line/event to log_buf with timestamp and name. If buf is full, triggers write_to_log. /* 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 */ 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);
/* writes contents from a chatcontext's log buffer to respective log file and resets log pos. /* 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. */ This is triggered automatically when the log buffer is full, but may be forced. */
void write_to_log(ChatContext *ctx); void write_to_log(struct chatlog *log);
void log_enable(struct chatlog *log, uint8_t *name, uint8_t *key);
void log_disable(struct chatlog *log);

View File

@ -70,8 +70,8 @@ void print_time(WINDOW *window)
wattroff(window,COLOR_PAIR(BLUE)); wattroff(window,COLOR_PAIR(BLUE));
} }
/* Returns 1 if the string is empty, 0 otherwise */ /* Returns true if the string is empty, false otherwise */
int string_is_empty(char *string) bool string_is_empty(char *string)
{ {
return string[0] == '\0'; return string[0] == '\0';
} }

View File

@ -32,8 +32,8 @@ struct tm *get_time(void);
/* Prints the time to given window */ /* Prints the time to given window */
void print_time(WINDOW *window); void print_time(WINDOW *window);
/* Returns 1 if the string is empty, 0 otherwise */ /* Returns true if the string is empty, false otherwise */
int string_is_empty(char *string); bool string_is_empty(char *string);
/* convert a multibyte string to a wide character string (must provide buffer) */ /* convert a multibyte string to a wide character string (must provide buffer) */
int char_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n); int char_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n);

View File

@ -161,7 +161,7 @@ struct ChatContext {
bool self_is_typing; bool self_is_typing;
struct chatlog log; struct chatlog *log;
WINDOW *history; WINDOW *history;
WINDOW *linewin; WINDOW *linewin;