AmendHub

Download:

jcs

/

subtext

/

amendments

/

96

session: Add session_log to log with a common prefix


jcs made amendment 96 over 2 years ago
--- logger.c Wed Apr 13 16:41:09 2022 +++ logger.c Tue Apr 26 15:32:14 2022 @@ -234,11 +234,21 @@ logger_mouse_down(struct focusable *focusable, EventRe size_t logger_printf(struct logger *logger, const char *format, ...) { + va_list va; + size_t len; + + va_start(va, format); + len = logger_vprintf(logger, format, va); + va_end(argptr); +} + +size_t +logger_vprintf(struct logger *logger, const char *format, va_list ap) +{ static char buf[600]; RgnHandle savergn; Rect zerorect = { 0, 0, 0, 0 }; GrafPtr old_port; - va_list argptr; size_t len, n; time_t now = Time; short line_height = 0; @@ -257,9 +267,7 @@ logger_printf(struct logger *logger, const char *forma len += strftime(buf + len, sizeof(buf) - len, "[%H:%M] ", localtime(&now)); - va_start(argptr, format); - len += vsnprintf(buf + len, sizeof(buf) - len, format, argptr); - va_end(argptr); + len += vsnprintf(buf + len, sizeof(buf) - len, format, ap); while (buf[len - 1] == '\r') len--; --- logger.h Thu Mar 31 14:07:54 2022 +++ logger.h Tue Apr 26 15:42:38 2022 @@ -17,8 +17,8 @@ #ifndef __CHATTER_H__ #define __CHATTER_H__ +#include <stdarg.h> #include <stdio.h> - #include "util.h" #define LOGGER_FONT geneva @@ -32,6 +32,8 @@ struct logger { }; struct logger * logger_init(void); +size_t logger_vprintf(struct logger *logger, const char *format, + va_list ap); size_t logger_printf(struct logger *logger, const char *format, ...); #endif --- session.c Thu Apr 21 17:02:47 2022 +++ session.c Tue Apr 26 15:46:59 2022 @@ -26,6 +26,7 @@ #include "session.h" #include "signup.h" #include "user.h" +#include "user_settings.h" #include "uthread.h" #include "util.h" @@ -172,7 +173,7 @@ get_another_char: case 's': case 'S': if (s->user) - ; //user_settings_menu(s); + user_settings_menu(s); else if ((s->user = signup(s))) { session_printf(s, "\r\n" "Welcome, {{B}}%s{{/}}!\r\n", s->user->username); @@ -221,8 +222,7 @@ session_close(struct session *session) /* finalize session log */ session->log.logged_off_at = Time; - logger_printf(logger, "[%s] Closing session for user %s", - session->node, session->user ? session->user->username : "guest"); + session_log(session, "Closing session"); if (bile_write(db->sessions_bile, SL_LOG_RTYPE, session->log.id, &session->log, sizeof(session->log)) != sizeof(session->log)) @@ -270,6 +270,21 @@ session_flush(struct session *session) } size_t +session_log(struct session *session, const char *format, ...) +{ + static char session_log_tbuf[256]; + va_list ap; + size_t len; + + va_start(ap, format); + vsnprintf(session_log_tbuf, sizeof(session_log_tbuf), format, ap); + va_end(ap); + + return logger_printf(logger, "[%s] [%s] %s", session->node, + session->user ? session->user->username : "guest", session_log_tbuf); +} + +size_t session_output(struct session *session, const char *str, size_t len) { size_t chunk, olen = len, stroff = 0; @@ -444,7 +459,7 @@ done_consuming: return ret; idled_out: - logger_printf(logger, "[%s] Idle too long, logging out", session->node); + session_log(session, "Idle too long, logging out"); session_output_string(session, "\r\n\r\nYou have been idle too long, goodbye.\r\n\r\n"); session_flush(session); @@ -694,8 +709,7 @@ session_login(struct session *s) if (strcmp(username, GUEST_USERNAME) == 0) { free(username); - logger_printf(logger, "[%s] Successful login in as %s", - s->node, GUEST_USERNAME); + session_log(s, "Successful guest login in as %s", username); return AUTH_USER_OK; } @@ -710,8 +724,8 @@ session_login(struct session *s) if (user) { free(username); s->user = user; - logger_printf(logger, "[%s] Automatically logged in as %s", - s->node, s->autologin_username); + session_log(s, "Automatically logged in as %s", + s->autologin_username); return AUTH_USER_OK; } @@ -732,13 +746,13 @@ session_login(struct session *s) if (user_authenticate(user, password) == AUTH_USER_OK) s->user = user; else - logger_printf(logger, "[%s] Failed password login for %s", - s->node, user->username); + session_log(s, "Failed password login for %s", + user->username); } else { /* kill some time */ SHA256Data((const u_int8_t *)password, strlen(password), junk); - logger_printf(logger, "[%s] Failed password login for " - "invalid user %s", s->node, username); + session_log(s, "Failed password login for invalid user %s", + username); } len = strlen(username); @@ -752,8 +766,8 @@ session_login(struct session *s) password = NULL; if (s->user) { - logger_printf(logger, "[%s] Successful password login for " - "user %s", s->node, s->user->username); + session_log(s, "Successful password login for user %s", + s->user->username); return AUTH_USER_OK; } --- session.h Wed Apr 20 16:23:24 2022 +++ session.h Tue Apr 26 15:45:15 2022 @@ -101,6 +101,7 @@ void session_close(struct session *session); void session_idle(struct session *session); unsigned short session_input_char(struct session *session); void session_flush(struct session *session); +size_t session_log(struct session *session, const char *format, ...); size_t session_output(struct session *session, const char *str, size_t len); size_t session_output_string(struct session *session, const char *str); size_t session_printf(struct session *session, const char *format, ...); --- signup.c Thu Apr 21 16:52:17 2022 +++ signup.c Tue Apr 26 15:40:07 2022 @@ -35,7 +35,7 @@ signup(struct session *s) size_t len; short n; - logger_printf(logger, "[%s] Signing up for an account", s->node); + session_log(s, "Signing up for an account"); session_output_template(s, "{{B}}Create Account{{/}} " "(^C to cancel)\r\n" @@ -107,8 +107,7 @@ signup(struct session *s) user_set_password(user, password); user_save(user); - logger_printf(logger, "[%s] New user account created: %s", s->node, - user->username); + session_log(s, "New user account created for %s", user->username); signup_done: if (username)