mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-22 21:23:01 +01:00
refactor logging functions to only handle chatlog pointers
This commit is contained in:
parent
1e503b1080
commit
7109b8fa18
51
src/chat.c
51
src/chat.c
@ -33,7 +33,7 @@
|
||||
#include "misc_tools.h"
|
||||
#include "friendlist.h"
|
||||
#include "toxic_strings.h"
|
||||
#include "chat.h"
|
||||
#include "log.h"
|
||||
|
||||
extern char *DATA_FILE;
|
||||
extern int store_data(Tox *m, char *path);
|
||||
@ -79,7 +79,7 @@ void kill_chat_window(ToxWindow *self)
|
||||
ChatContext *ctx = self->chatwin;
|
||||
StatusBar *statusbar = self->stb;
|
||||
|
||||
chat_disable_log(self);
|
||||
log_disable(ctx->log);
|
||||
|
||||
int f_num = self->num;
|
||||
delwin(ctx->linewin);
|
||||
@ -87,30 +87,11 @@ void kill_chat_window(ToxWindow *self)
|
||||
del_window(self);
|
||||
disable_chatwin(f_num);
|
||||
|
||||
free(ctx->log);
|
||||
free(ctx);
|
||||
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)
|
||||
{
|
||||
if (self->num != num)
|
||||
@ -134,7 +115,7 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1
|
||||
} else
|
||||
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);
|
||||
}
|
||||
|
||||
@ -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);
|
||||
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);
|
||||
}
|
||||
|
||||
@ -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");
|
||||
wattroff(ctx->history, COLOR_PAIR(RED));
|
||||
} 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");
|
||||
wattroff(ctx->history, COLOR_PAIR(RED));
|
||||
} 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);
|
||||
scrollok(ctx->history, 1);
|
||||
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)
|
||||
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);
|
||||
|
||||
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
|
||||
}
|
||||
|
||||
ToxWindow new_chat(Tox *m, int friendnum)
|
||||
|
@ -25,8 +25,6 @@
|
||||
|
||||
#include "toxic_windows.h"
|
||||
|
||||
void chat_enable_log(ToxWindow *self);
|
||||
void chat_disable_log(ToxWindow *self);
|
||||
void kill_chat_window(ToxWindow *self);
|
||||
ToxWindow new_chat(Tox *m, int friendnum);
|
||||
|
||||
|
@ -230,7 +230,7 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
|
||||
ChatContext *ctx = self->chatwin;
|
||||
|
||||
if (argc == 0) {
|
||||
if (ctx->log.log_on) {
|
||||
if (ctx->log->log_on) {
|
||||
wprintw(window, "Logging for this chat is ");
|
||||
wattron(window, COLOR_PAIR(GREEN) | A_BOLD);
|
||||
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 *ident = NULL;
|
||||
|
||||
if (!strcmp(swch, "1") || !strcmp(swch, "on")) {
|
||||
if (self->is_chat) {
|
||||
chat_enable_log(self);
|
||||
friends[self->num].logging_on = true;
|
||||
} else if (self->is_groupchat) {
|
||||
groupchat_enable_log(self);
|
||||
ident = friends[self->num].pub_key;
|
||||
}
|
||||
|
||||
log_enable(ctx->log, self->name, ident);
|
||||
|
||||
wprintw(window, "Logging ");
|
||||
wattron(window, COLOR_PAIR(GREEN) | A_BOLD);
|
||||
wprintw(window, "[on]\n");
|
||||
wattroff(window, COLOR_PAIR(GREEN) | A_BOLD);
|
||||
return;
|
||||
} else if (!strcmp(swch, "0") || !strcmp(swch, "off")) {
|
||||
if (self->is_chat) {
|
||||
chat_disable_log(self);
|
||||
if (self->is_chat)
|
||||
friends[self->num].logging_on = false;
|
||||
} else if (self->is_groupchat) {
|
||||
groupchat_disable_log(self);
|
||||
}
|
||||
|
||||
log_disable(ctx->log);
|
||||
|
||||
wprintw(window, "Logging ");
|
||||
wattron(window, COLOR_PAIR(RED) | A_BOLD);
|
||||
|
@ -34,6 +34,7 @@
|
||||
#include "groupchat.h"
|
||||
#include "prompt.h"
|
||||
#include "toxic_strings.h"
|
||||
#include "log.h"
|
||||
|
||||
extern char *DATA_FILE;
|
||||
extern int store_data(Tox *m, char *path);
|
||||
@ -75,9 +76,10 @@ void kill_groupchat_window(ToxWindow *self)
|
||||
{
|
||||
ChatContext *ctx = self->chatwin;
|
||||
|
||||
groupchat_disable_log(self);
|
||||
log_disable(ctx->log);
|
||||
delwin(ctx->linewin);
|
||||
del_window(self);
|
||||
free(ctx->log);
|
||||
free(ctx);
|
||||
}
|
||||
|
||||
@ -98,30 +100,9 @@ static void close_groupchat(ToxWindow *self, Tox *m, int groupnum)
|
||||
}
|
||||
|
||||
max_groupchat_index = i;
|
||||
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
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,
|
||||
@ -223,7 +204,7 @@ static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int p
|
||||
wprintw(ctx->history, "* %s %s\n", nick, action);
|
||||
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 */
|
||||
@ -301,7 +282,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu
|
||||
wprintw(ctx->history, " %s\n", event);
|
||||
wattroff(ctx->history, COLOR_PAIR(GREEN));
|
||||
|
||||
add_to_log_buf(event, peername, ctx, true);
|
||||
add_to_log_buf(event, peername, ctx->log, true);
|
||||
break;
|
||||
|
||||
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)
|
||||
--groupchats[self->num].side_pos;
|
||||
|
||||
add_to_log_buf(event, oldpeername, ctx, true);
|
||||
add_to_log_buf(event, oldpeername, ctx->log, true);
|
||||
break;
|
||||
|
||||
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];
|
||||
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;
|
||||
}
|
||||
|
||||
@ -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->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);
|
||||
execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE);
|
||||
|
||||
|
@ -32,8 +32,6 @@ typedef struct {
|
||||
uint8_t *oldpeer_names;
|
||||
} GroupChat;
|
||||
|
||||
void groupchat_enable_log(ToxWindow *self);
|
||||
void groupchat_disable_log(ToxWindow *self);
|
||||
void kill_groupchat_window(ToxWindow *self);
|
||||
int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum);
|
||||
ToxWindow new_group_chat(Tox *m, int groupnum);
|
||||
|
56
src/log.c
56
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,
|
||||
"%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;
|
||||
}
|
||||
}
|
||||
|
13
src/log.h
13
src/log.h
@ -21,16 +21,15 @@
|
||||
*/
|
||||
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
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.
|
||||
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.
|
||||
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);
|
@ -70,8 +70,8 @@ void print_time(WINDOW *window)
|
||||
wattroff(window,COLOR_PAIR(BLUE));
|
||||
}
|
||||
|
||||
/* Returns 1 if the string is empty, 0 otherwise */
|
||||
int string_is_empty(char *string)
|
||||
/* Returns true if the string is empty, false otherwise */
|
||||
bool string_is_empty(char *string)
|
||||
{
|
||||
return string[0] == '\0';
|
||||
}
|
||||
|
@ -32,8 +32,8 @@ struct tm *get_time(void);
|
||||
/* Prints the time to given window */
|
||||
void print_time(WINDOW *window);
|
||||
|
||||
/* Returns 1 if the string is empty, 0 otherwise */
|
||||
int string_is_empty(char *string);
|
||||
/* Returns true if the string is empty, false otherwise */
|
||||
bool string_is_empty(char *string);
|
||||
|
||||
/* 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);
|
||||
|
@ -161,7 +161,7 @@ struct ChatContext {
|
||||
|
||||
bool self_is_typing;
|
||||
|
||||
struct chatlog log;
|
||||
struct chatlog *log;
|
||||
|
||||
WINDOW *history;
|
||||
WINDOW *linewin;
|
||||
|
Loading…
Reference in New Issue
Block a user