mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-12 23:53:01 +01:00
log events
This commit is contained in:
parent
a61f5f6a6d
commit
8e4db369bc
@ -112,7 +112,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);
|
||||
add_to_log_buf(msg, nick, ctx, false);
|
||||
alert_window(self, WINDOW_ALERT_1, true);
|
||||
}
|
||||
|
||||
@ -156,7 +156,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);
|
||||
add_to_log_buf(action, nick, ctx, true);
|
||||
alert_window(self, WINDOW_ALERT_1, true);
|
||||
}
|
||||
|
||||
@ -338,7 +338,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);
|
||||
add_to_log_buf(action, selfname, ctx, true);
|
||||
}
|
||||
}
|
||||
|
||||
@ -529,7 +529,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);
|
||||
add_to_log_buf(line, selfname, ctx, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -168,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);
|
||||
add_to_log_buf(msg, nick, ctx, false);
|
||||
}
|
||||
|
||||
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);
|
||||
wattroff(ctx->history, COLOR_PAIR(YELLOW));
|
||||
|
||||
add_to_log_buf(action, nick, ctx);
|
||||
add_to_log_buf(action, nick, ctx, true);
|
||||
}
|
||||
|
||||
/* Puts two copies of peerlist in chat instance */
|
||||
@ -269,25 +269,36 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu
|
||||
ChatContext *ctx = self->chatwin;
|
||||
print_time(ctx->history);
|
||||
|
||||
uint8_t *event;
|
||||
|
||||
switch (change) {
|
||||
case TOX_CHAT_CHANGE_PEER_ADD:
|
||||
event = "has joined the room";
|
||||
|
||||
wattron(ctx->history, COLOR_PAIR(GREEN));
|
||||
wattron(ctx->history, A_BOLD);
|
||||
wprintw(ctx->history, "* %s", peername);
|
||||
wattroff(ctx->history, A_BOLD);
|
||||
wprintw(ctx->history, " has joined the room\n");
|
||||
wprintw(ctx->history, " %s\n", event);
|
||||
wattroff(ctx->history, COLOR_PAIR(GREEN));
|
||||
|
||||
add_to_log_buf(event, peername, ctx, true);
|
||||
break;
|
||||
|
||||
case TOX_CHAT_CHANGE_PEER_DEL:
|
||||
event = "has left the room";
|
||||
|
||||
wattron(ctx->history, A_BOLD);
|
||||
wprintw(ctx->history, "* %s", oldpeername);
|
||||
wattroff(ctx->history, A_BOLD);
|
||||
wprintw(ctx->history, " has left the room\n");
|
||||
wprintw(ctx->history, " %s\n", event);
|
||||
|
||||
if (groupchats[self->num].side_pos > 0)
|
||||
--groupchats[self->num].side_pos;
|
||||
|
||||
add_to_log_buf(event, peername, ctx, true);
|
||||
break;
|
||||
|
||||
case TOX_CHAT_CHANGE_PEER_NAME:
|
||||
wattron(ctx->history, COLOR_PAIR(MAGENTA));
|
||||
wattron(ctx->history, A_BOLD);
|
||||
@ -300,6 +311,10 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu
|
||||
wprintw(ctx->history, "%s\n", peername);
|
||||
wattroff(ctx->history, A_BOLD);
|
||||
wattroff(ctx->history, COLOR_PAIR(MAGENTA));
|
||||
|
||||
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, peername, ctx, true);
|
||||
break;
|
||||
}
|
||||
|
||||
|
17
src/log.c
17
src/log.c
@ -85,17 +85,24 @@ void write_to_log(ChatContext *ctx)
|
||||
fclose(logfile);
|
||||
}
|
||||
|
||||
/* 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_to_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.
|
||||
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)
|
||||
{
|
||||
if (!ctx->log.log_on)
|
||||
return;
|
||||
|
||||
uint8_t name_frmt[TOXIC_MAX_NAME_LENGTH + 3];
|
||||
|
||||
if (event)
|
||||
snprintf(name_frmt, sizeof(name_frmt), "* %s", name);
|
||||
else
|
||||
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(ctx->log.log_buf[ctx->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, msg);
|
||||
tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec, name_frmt, msg);
|
||||
|
||||
if (++(ctx->log.pos) >= MAX_LOG_BUF_LINES)
|
||||
write_to_log(ctx);
|
||||
|
@ -26,7 +26,11 @@ void init_logging_session(uint8_t *name, uint8_t *pub_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_to_log_buf(uint8_t *msg, uint8_t *name, ChatContext *ctx);
|
||||
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.
|
||||
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);
|
||||
|
||||
/* 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. */
|
||||
|
@ -136,7 +136,7 @@ struct StatusBar {
|
||||
};
|
||||
|
||||
#define MAX_LOG_BUF_LINES 10 /* write log_buf contents to log file after this many lines */
|
||||
#define MAX_LOG_LINE_SIZE MAX_STR_SIZE + TOXIC_MAX_NAME_LENGTH + 24 /* extra room for timestamp */
|
||||
#define MAX_LOG_LINE_SIZE MAX_STR_SIZE + TOXIC_MAX_NAME_LENGTH + 32 /* extra room for timestamp */
|
||||
|
||||
struct chatlog {
|
||||
uint8_t log_path[MAX_STR_SIZE];
|
||||
|
Loading…
Reference in New Issue
Block a user