summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Russon <rich@flatcap.org>2023-10-03 23:49:01 +0100
committerRichard Russon <rich@flatcap.org>2023-10-05 01:11:29 +0100
commit7c78c0a53ed6d166249b5cd589cdcd825a5524f3 (patch)
tree98c73906050e0520d198127bc6e56df62b96aa1f
parent99ce265cb51b86b3b3eb704e1481853a714f3afa (diff)
debug: add --debug-logging
Add a new logging dispatcher that dumps everything to stdout. MuttLogger = log_disp_debug; Configure with: bool DebugLogColor; // Output ANSI colours bool DebugLogLevel; // Prefix log level, e.g. <E> bool DebugLogTimestamp; // Show the timestamp
-rw-r--r--Makefile.autosetup5
-rw-r--r--auto.def19
-rw-r--r--debug/lib.h8
-rw-r--r--debug/logging.c132
-rw-r--r--mutt/logging.c2
5 files changed, 158 insertions, 8 deletions
diff --git a/Makefile.autosetup b/Makefile.autosetup
index c60fc8e5a..ec158febb 100644
--- a/Makefile.autosetup
+++ b/Makefile.autosetup
@@ -356,13 +356,16 @@ LIBDEBUGOBJS+= debug/graphviz.o
@if USE_DEBUG_KEYMAP
LIBDEBUGOBJS+= debug/keymap.o
@endif
+@if USE_DEBUG_LOGGING
+LIBDEBUGOBJS+= debug/logging.o
+@endif
@if USE_DEBUG_NOTIFY
LIBDEBUGOBJS+= debug/notify.o
@endif
@if USE_DEBUG_WINDOW
LIBDEBUGOBJS+= debug/window.o
@endif
-@if USE_DEBUG_BACKTRACE || USE_DEBUG_COLOR || USE_DEBUG_EMAIL || USE_DEBUG_GRAPHVIZ || USE_DEBUG_KEYMAP || USE_DEBUG_NAMES || USE_DEBUG_NOTIFY || USE_DEBUG_WINDOW
+@if USE_DEBUG_BACKTRACE || USE_DEBUG_COLOR || USE_DEBUG_EMAIL || USE_DEBUG_GRAPHVIZ || USE_DEBUG_KEYMAP || USE_DEBUG_LOGGING || USE_DEBUG_NAMES || USE_DEBUG_NOTIFY || USE_DEBUG_WINDOW
LIBDEBUGOBJS+= debug/common.o debug/names.o
LIBDEBUG= libdebug.a
CLEANFILES+= $(LIBDEBUG) $(LIBDEBUGOBJS)
diff --git a/auto.def b/auto.def
index 48761b0d0..61db43417 100644
--- a/auto.def
+++ b/auto.def
@@ -123,10 +123,11 @@ set valid_options {
debug-email=0 => "DEBUG: Enable Email dump"
debug-graphviz=0 => "DEBUG: Enable Graphviz dump"
debug-keymap=0 => "DEBUG: Enable Key mappings dump"
+ debug-logging=0 => "DEBUG: Enable Debug logging"
debug-names=0 => "DEBUG: Enable Name lookup tables"
debug-notify=0 => "DEBUG: Enable Notifications dump"
debug-queue=0 => "DEBUG: Enable TAILQ debugging"
- debug-window=0 => "DEBUG: Enable windows dump"
+ debug-window=0 => "DEBUG: Enable Windows dump"
}
set deprecated_options {
@@ -170,11 +171,11 @@ if {1} {
# Keep sorted, please.
foreach opt {
asan autocrypt bdb compile-commands coverage debug-backtrace debug-color
- debug-email debug-graphviz debug-keymap debug-names debug-notify debug-queue
- debug-window doc everything fmemopen full-doc fuzzing gdbm gnutls gpgme
- gsasl gss homespool idn2 include-path-in-cflags inotify kyotocabinet lmdb
- locales-fix lua lz4 mixmaster nls notmuch pcre2 pgp qdbm rocksdb sasl
- smime sqlite ssl tdb tokyocabinet ubsan zlib zstd
+ debug-email debug-graphviz debug-keymap debug-logging debug-names
+ debug-notify debug-queue debug-window doc everything fmemopen full-doc
+ fuzzing gdbm gnutls gpgme gsasl gss homespool idn2 include-path-in-cflags
+ inotify kyotocabinet lmdb locales-fix lua lz4 mixmaster nls notmuch pcre2
+ pgp qdbm rocksdb sasl smime sqlite ssl tdb tokyocabinet ubsan zlib zstd
} {
define want-$opt [opt-bool $opt]
}
@@ -1054,6 +1055,12 @@ if {[get-define want-debug-keymap]} {
define debug_build 1
}
+# Logging
+if {[get-define want-debug-logging]} {
+ define USE_DEBUG_LOGGING 1
+ define debug_build 1
+}
+
# Name lookup tables
if {[get-define want-debug-names]} {
define USE_DEBUG_NAMES 1
diff --git a/debug/lib.h b/debug/lib.h
index a055d53b1..37b76d724 100644
--- a/debug/lib.h
+++ b/debug/lib.h
@@ -32,6 +32,7 @@
* | debug/email.c | @subpage debug_email |
* | debug/graphviz.c | @subpage debug_graphviz |
* | debug/keymap.c | @subpage debug_keymap |
+ * | debug/logging.c | @subpage debug_logging |
* | debug/notify.c | @subpage debug_notify |
* | debug/pager.c | @subpage debug_pager |
* | debug/window.c | @subpage debug_window |
@@ -81,6 +82,13 @@ void dump_graphviz_email (struct Email *e, const char *title);
// Keymap
void dump_keybindings (void);
+// Logging
+extern bool DebugLogColor;
+extern bool DebugLogLevel;
+extern bool DebugLogTimestamp;
+
+extern int log_disp_debug (time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format, ...);
+
// Names
const char *name_content_disposition(enum ContentDisposition disp);
const char *name_content_encoding (enum ContentEncoding enc);
diff --git a/debug/logging.c b/debug/logging.c
new file mode 100644
index 000000000..baf9aa1dd
--- /dev/null
+++ b/debug/logging.c
@@ -0,0 +1,132 @@
+/**
+ * @file
+ * Log everything to the terminal
+ *
+ * @authors
+ * Copyright (C) 2023 Richard Russon <rich@flatcap.org>
+ *
+ * @copyright
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 2 of the License, or (at your option) any later
+ * version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * @page debug_logging Log everything to the terminal
+ *
+ * Log everything to the terminal
+ */
+
+#include "config.h"
+#include <errno.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include "mutt/lib.h"
+#include "lib.h"
+
+bool DebugLogColor = false; ///< Output ANSI colours
+bool DebugLogLevel = false; ///< Prefix log level, e.g. [E]
+bool DebugLogTimestamp = false; ///< Show the timestamp
+
+extern const char *LevelAbbr;
+
+/**
+ * log_timestamp - Write a timestamp to a buffer
+ * @param buf Buffer for the result
+ * @param buflen Length of the buffer
+ * @param time Timestamp
+ * @retval num Bytes written to buffer
+ */
+static int log_timestamp(char *buf, size_t buflen, time_t time)
+{
+ return mutt_date_localtime_format(buf, buflen, "[%H:%M:%S]", time);
+}
+
+/**
+ * log_level - Write a log level to a buffer
+ * @param buf Buffer for the result
+ * @param buflen Length of the buffer
+ * @param level Log level
+ * @retval num Bytes written to buffer
+ *
+ * Write the log level, e.g. [E]
+ */
+static int log_level(char *buf, size_t buflen, enum LogLevel level)
+{
+ return snprintf(buf, buflen, "<%c>", LevelAbbr[level + 3]);
+}
+
+/**
+ * log_disp_debug - Display a log line on screen - Implements ::log_dispatcher_t - @ingroup logging_api
+ */
+int log_disp_debug(time_t stamp, const char *file, int line, const char *function,
+ enum LogLevel level, const char *format, ...)
+{
+ char buf[LOG_LINE_MAX_LEN] = { 0 };
+ size_t buflen = sizeof(buf);
+ int err = errno;
+ int colour = 0;
+ int bytes = 0;
+
+ if (DebugLogColor)
+ {
+ switch (level)
+ {
+ case LL_PERROR:
+ case LL_ERROR:
+ colour = 31;
+ break;
+ case LL_WARNING:
+ colour = 33;
+ break;
+ case LL_MESSAGE:
+ default:
+ break;
+ }
+
+ if (colour > 0)
+ {
+ bytes += snprintf(buf + bytes, buflen - bytes, "\033[1;%dm", colour); // Escape
+ }
+ }
+
+ if (DebugLogTimestamp)
+ {
+ bytes += log_timestamp(buf + bytes, buflen - bytes, stamp);
+ }
+
+ if (DebugLogLevel)
+ {
+ bytes += log_level(buf + bytes, buflen - bytes, level);
+ }
+
+ va_list ap;
+ va_start(ap, format);
+ bytes += vsnprintf(buf + bytes, buflen - bytes, format, ap);
+ va_end(ap);
+
+ if (level == LL_PERROR)
+ bytes += snprintf(buf + bytes, buflen - bytes, ": %s", strerror(err));
+
+ if (colour > 0)
+ {
+ bytes += snprintf(buf + bytes, buflen - bytes, "\033[0m"); // Escape
+ }
+
+ bytes += snprintf(buf + bytes, buflen - bytes, "\n");
+
+ fputs(buf, stdout);
+ return bytes;
+}
diff --git a/mutt/logging.c b/mutt/logging.c
index 3dc7f16a1..46b9b58d8 100644
--- a/mutt/logging.c
+++ b/mutt/logging.c
@@ -42,7 +42,7 @@
#include "queue.h"
#include "string2.h"
-static const char *LevelAbbr = "PEWM12345N"; ///< Abbreviations of logging level names
+const char *LevelAbbr = "PEWM12345N"; ///< Abbreviations of logging level names
/**
* MuttLogger - The log dispatcher - @ingroup logging_api