AmendHub

jcs

/

subtext

/

amendments

/

261

logger: There will only be one, stop passing object around

This also helps at shutdown to know when we can no longer log.

jcs made amendment 261 4 months ago
--- logger.c Fri Sep 23 23:58:06 2022 +++ logger.c Sat Oct 1 21:19:32 2022 @@ -22,19 +22,20 @@ #include "subtext.h" #include "util.h" -void logger_layout(struct logger *logger, bool init, Rect *init_bounds); +struct logger *logger = NULL; + +void logger_layout(bool init, Rect *init_bounds); void logger_key_down(struct focusable *focusable, EventRecord *event); void logger_mouse_down(struct focusable *focusable, EventRecord *event); void logger_update(struct focusable *focusable, EventRecord *event); void logger_resume(struct focusable *focusable, EventRecord *event); bool logger_quit(struct focusable *focusable); -void logger_flush_buffer(struct logger *logger); +void logger_flush_buffer(void); -struct logger * +void logger_init(void) { struct focusable *focusable; - struct logger *logger; Rect bounds = { 0 }; short padding = 5; @@ -63,7 +64,7 @@ logger_init(void) bounds.right -= bounds.left; bounds.bottom -= bounds.top; bounds.top = bounds.left = 0; - logger_layout(logger, true, &bounds); + logger_layout(true, &bounds); focusable = xmalloczero(sizeof(struct focusable), "logger focusable"); focusable->win = logger->win; @@ -76,12 +77,10 @@ logger_init(void) logger->focusable = focusable; DrawControls(logger->win); - - return logger; } void -logger_layout(struct logger *logger, bool init, Rect *init_bounds) +logger_layout(bool init, Rect *init_bounds) { Rect bounds, inset_bounds, win_bounds; @@ -124,8 +123,6 @@ logger_layout(struct logger *logger, bool init, Rect * void logger_resume(struct focusable *focusable, EventRecord *event) { - struct logger *logger = (struct logger *)(focusable->cookie); - show_focusable(focusable); InvalRect(logger->win->visRgn); } @@ -133,8 +130,6 @@ logger_resume(struct focusable *focusable, EventRecord bool logger_quit(struct focusable *focusable) { - struct logger *logger = (struct logger *)(focusable->cookie); - destroy_focusable(focusable); xfree(&logger->buffered_logs); xfree(&logger); @@ -145,7 +140,6 @@ logger_quit(struct focusable *focusable) void logger_update(struct focusable *focusable, EventRecord *event) { - struct logger *logger = (struct logger *)(focusable->cookie); GrafPtr old_port; Rect r; short what = -1; @@ -217,7 +211,6 @@ logger_update_title(void) void logger_mouse_down(struct focusable *focusable, EventRecord *event) { - struct logger *logger = (struct logger *)(focusable->cookie); Point p; ControlHandle control; Rect r; @@ -266,20 +259,23 @@ logger_mouse_down(struct focusable *focusable, EventRe } size_t -logger_printf(struct logger *logger, const char *format, ...) +logger_printf(const char *format, ...) { va_list va; size_t len; + if (!logger) + return 0; + va_start(va, format); - len = logger_vprintf(logger, format, va); + len = logger_vprintf(format, va); va_end(va); return len; } size_t -logger_vprintf(struct logger *logger, const char *format, va_list ap) +logger_vprintf(const char *format, va_list ap) { static char buf[600]; ssize_t left, len; @@ -302,17 +298,17 @@ logger_vprintf(struct logger *logger, const char *form } if (logger->buffered_logs_len + len > LOGGER_BUFFERED_LOG_SIZE) - logger_flush_buffer(logger); + logger_flush_buffer(); len = strlcat(logger->buffered_logs, buf, LOGGER_BUFFERED_LOG_SIZE); logger->buffered_logs_len = MIN(len, LOGGER_BUFFERED_LOG_SIZE); if (logger->autoflush) - logger_flush_buffer(logger); + logger_flush_buffer(); } void -logger_flush_buffer(struct logger *logger) +logger_flush_buffer(void) { RgnHandle savergn; Rect zerorect = { 0, 0, 0, 0 }; @@ -320,7 +316,7 @@ logger_flush_buffer(struct logger *logger) short line_height = 0, new_lines = 1, n; char *buf; - if (logger->buffered_logs_len == 0) + if (!logger || logger->buffered_logs_len == 0) return; for (n = 0; n < logger->buffered_logs_len; n++) { --- logger.h Fri Jan 1 12:46:48 1904 +++ logger.h Sat Oct 1 21:29:46 2022 @@ -35,10 +35,11 @@ struct logger { size_t buffered_logs_len; }; -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, ...); +extern struct logger *logger; + +void logger_init(void); +size_t logger_vprintf(const char *format, va_list ap); +size_t logger_printf(const char *format, ...); void logger_update_title(void); #endif --- main.c Wed Sep 21 17:33:25 2022 +++ main.c Sat Oct 1 21:08:28 2022 @@ -33,7 +33,6 @@ MenuHandle apple_menu, file_menu; short quitting = 0; struct db *db = NULL; -struct logger *logger = NULL; bool blanker_on = false; unsigned long blanker_last_blank = 0; WindowPtr blanker_win = NULL; @@ -98,17 +97,17 @@ main(void) _atexit(handle_exit); - logger = logger_init(); + logger_init(); logger_update_title(); zone_size = (unsigned long)CurStackBase - (unsigned long)ApplZone; stack_size = (unsigned long)CurStackBase - (unsigned long)ApplLimit; heap_size = (unsigned long)ApplLimit - (unsigned long)ApplZone; - logger_printf(logger, "Initialized with %ldKB zone, %ldKB stack for " + logger_printf("Initialized with %ldKB zone, %ldKB stack for " "%d threads, %ldKB heap", zone_size / 1024L, stack_size / 1024L, NUM_UTHREADS, heap_size / 1024L); - logger_printf(logger, "[db] Updating username cache"); + logger_printf("[db] Updating username cache"); user_cache_usernames(); if (db->config.telnet_port) @@ -417,6 +416,6 @@ blanker_unblank(void) * This will call back to blanker_unblank, so it must be done after * blanker_on = false */ - logger_printf(logger, "[blanker] Blanked screen for %ld second%s", + logger_printf("[blanker] Blanked screen for %ld second%s", rt, rt == 1 ? "" : "s"); } --- serial.c Sat Oct 1 20:26:42 2022 +++ serial.c Sat Oct 1 21:31:39 2022 @@ -31,6 +31,7 @@ #include <Serial.h> #include <Devices.h> +#include "logger.h" #include "serial_local.h" #include "session.h" #include "subtext.h" @@ -145,8 +146,7 @@ serial_init(void) baud = baud57600; break; default: - logger_printf(logger, - "[modem] Unsupported port speed %ld, using 19200", + logger_printf("[modem] Unsupported port speed %ld, using 19200", db->config.modem_speed); db->config.modem_speed = 19200; baud = baud19200; @@ -156,8 +156,7 @@ serial_init(void) parity = data7 + evenParity + stop10; } else { if (strcmp(db->config.modem_parity, "8N1") != 0) { - logger_printf(logger, - "[modem] Unknown parity \"%s\", using 8N1", + logger_printf("[modem] Unknown parity \"%s\", using 8N1", db->config.modem_parity); snprintf(db->config.modem_parity, sizeof(db->config.modem_parity), "8N1"); @@ -165,7 +164,7 @@ serial_init(void) parity = data8 + noParity + stop10; } - logger_printf(logger, "[modem] Initializing %s port at %ld (%s)", + logger_printf("[modem] Initializing %s port at %ld (%s)", (db->config.modem_port == 2 ? "printer" : "modem"), db->config.modem_speed, db->config.modem_parity); @@ -201,7 +200,7 @@ serial_reset(void) long m; /* reset */ - logger_printf(logger, "[modem] Hanging up"); + logger_printf("[modem] Hanging up"); serial_hangup(); serial_printf("ATZ\r"); Delay(TICKS_PER_SEC * 2, &m); @@ -273,7 +272,7 @@ serial_printf(const char *format, ...) format, ap); va_end(ap); - logger_printf(logger, "[modem] Sending: %s", serial_printf_tbuf); + logger_printf("[modem] Sending: %s", serial_printf_tbuf); pbr.ioParam.ioRefNum = serial_out_refnum; pbr.ioParam.ioBuffer = (Ptr)&serial_printf_tbuf; @@ -328,7 +327,7 @@ find_line: if (serial_cur_line[0] == '\0') goto find_line; - logger_printf(logger, "[modem] Read: %s", serial_cur_line); + logger_printf("[modem] Read: %s", serial_cur_line); return (char *)&serial_cur_line; } @@ -387,12 +386,11 @@ serial_idle(void) #if 0 SerStatus(serial_in_refnum, &status); if (status.ctsHold != 0) - logger_printf(logger, "[modem] CTS set"); + logger_printf("[modem] CTS set"); if (status.xOffHold != 0) - logger_printf(logger, "[modem] XOFF set"); + logger_printf("[modem] XOFF set"); if (status.cumErrs != 0) - logger_printf(logger, "[modem] Reported errors %d", - status.cumErrs); + logger_printf("[modem] Reported errors %d", status.cumErrs); #endif switch (the_serial_node.state) { @@ -407,8 +405,7 @@ serial_idle(void) the_serial_node.session = session_create(tty, "modem", &serial_node_funcs); if (the_serial_node.session == NULL) { - logger_printf(logger, "[modem] No free nodes, can't " - "answer call"); + logger_printf("[modem] No free nodes, can't answer call"); break; } @@ -417,7 +414,7 @@ serial_idle(void) the_serial_node.state = SERIAL_STATE_ANSWERED; the_serial_node.answered_at = Time; - logger_printf(logger, "[modem] Answering call"); + logger_printf("[modem] Answering call"); serial_printf("ATA\r"); } break; @@ -428,7 +425,7 @@ serial_idle(void) short count; if (Time - the_serial_node.answered_at > SERIAL_CONNECT_TIMEOUT) { - logger_printf(logger, "[modem] Timed out establishing a " + logger_printf("[modem] Timed out establishing a " "connection, aborting"); the_serial_node.session->ending = true; the_serial_node.state = SERIAL_STATE_HANGUP; --- session.c Wed Sep 21 16:56:41 2022 +++ session.c Sat Oct 1 21:33:04 2022 @@ -23,6 +23,7 @@ #include "board.h" #include "chat.h" #include "folder.h" +#include "logger.h" #include "mail.h" #include "subtext.h" #include "session.h" @@ -390,7 +391,7 @@ session_flush(struct session *session) size_t session_log(struct session *session, const char *str) { - return logger_printf(logger, "[%s] [%s] %s", session->node, + return logger_printf("[%s] [%s] %s", session->node, session->logged_in ? (session->user ? session->user->username : "guest") : "-", str); @@ -1492,8 +1493,7 @@ session_purge_logs(short days) secs = (60UL * 60UL * 24UL * (unsigned long)days); - logger_printf(logger, "[db] Purging session logs older than %d days", - days); + logger_printf("[db] Purging session logs older than %d days", days); for (n = 0; (obj = bile_get_nth_of_type(db->sessions_bile, n, SL_LOG_RTYPE)); @@ -1511,8 +1511,7 @@ session_purge_logs(short days) } if (count) { - logger_printf(logger, "[db] Deleting %ld of %ld log entries", - count, n); + logger_printf("[db] Deleting %ld of %ld log entries", count, n); for (n = 0; n < count; n++) { bile_delete(db->sessions_bile, SL_LOG_RTYPE, ids[n]); --- subtext.h Tue Jul 19 15:21:32 2022 +++ subtext.h Sat Oct 1 21:29:19 2022 @@ -68,11 +68,9 @@ #define BOARD_RETENTION_ID 8 #include "db.h" -#include "logger.h" extern struct db *db; extern MenuHandle file_menu; -extern struct logger *logger; void blanker_unblank(void); --- telnet.c Sat Sep 24 00:13:34 2022 +++ telnet.c Sat Oct 1 21:16:26 2022 @@ -159,7 +159,7 @@ telnet_init(void) if (!db->config.telnet_port) return; - logger_printf(logger, "[telnet] Initializing MacTCP"); + logger_printf("[telnet] Initializing MacTCP"); if (_TCPInit() != noErr) panic("Failed initializing MacTCP"); @@ -238,7 +238,7 @@ telnet_listen_on_node(struct telnet_node *node) if (!did_initial_log) { long2ip(ip, ip_s); - logger_printf(logger, "[telnet] Listening on %s:%d", ip_s, port); + logger_printf("[telnet] Listening on %s:%d", ip_s, port); did_initial_log = true; } @@ -302,8 +302,8 @@ telnet_idle(void) node->id); } - logger_printf(logger, "[%s] New telnet connection " - "from %s%s", node->name, node->ip_s, + logger_printf("[%s] New telnet connection from %s%s", + node->name, node->ip_s, node->from_trusted_proxy ? " (via trusted proxy)" : ""); node->state = TELNET_PB_STATE_CONNECTED; @@ -311,8 +311,8 @@ telnet_idle(void) node->from_trusted_proxy ? "web" : "telnet", &telnet_node_funcs); if (node->session == NULL) { - logger_printf(logger, "[%s] No free nodes, " - "disconnecting", node->name); + logger_printf("[%s] No free nodes, disconnecting", + node->name); telnet_print_busy(node); _TCPRelease(&node->listen_pb, node->stream, nil, nil, false); @@ -331,7 +331,7 @@ telnet_idle(void) else long2ip(telnet_status_pb.remoteHost, node->ip_s); - logger_printf(logger, "[%s] Telnet connection from " + logger_printf("[%s] Telnet connection from " "%s in state %d, aborting", node->name, node->ip_s, telnet_status_pb.connectionState);