summaryrefslogtreecommitdiff
path: root/internal/logging
diff options
context:
space:
mode:
Diffstat (limited to 'internal/logging')
-rw-r--r--internal/logging/logging.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/internal/logging/logging.go b/internal/logging/logging.go
new file mode 100644
index 0000000..2e4bbc8
--- /dev/null
+++ b/internal/logging/logging.go
@@ -0,0 +1,53 @@
+package logging
+
+import (
+ "fmt"
+ "log"
+)
+
+// ANSI color utilities shared across Hexai.
+const (
+ AnsiBgBlack = "\x1b[40m"
+ AnsiGrey = "\x1b[90m"
+ AnsiCyan = "\x1b[36m"
+ AnsiGreen = "\x1b[32m"
+ AnsiRed = "\x1b[31m"
+ AnsiReset = "\x1b[0m"
+)
+
+// AnsiBase is the default style: black background + grey foreground.
+const AnsiBase = AnsiBgBlack + AnsiGrey
+
+// singleton logger used across the codebase
+var std *log.Logger
+
+// Bind sets the underlying standard logger to use for Logf.
+func Bind(l *log.Logger) { std = l }
+
+// Logf prints a formatted message with a module prefix and base ANSI style.
+func Logf(prefix, format string, args ...any) {
+ if std == nil {
+ return
+ }
+ msg := fmt.Sprintf(format, args...)
+ std.Print(AnsiBase + prefix + msg + AnsiReset)
+}
+
+// Logging configuration for previews (shared)
+var logPreviewLimit int // 0 means unlimited
+
+// SetLogPreviewLimit sets the maximum number of characters to log for
+// request/response previews. Set to 0 for unlimited.
+func SetLogPreviewLimit(n int) { logPreviewLimit = n }
+
+// PreviewForLog returns the string truncated to the configured preview limit.
+func PreviewForLog(s string) string {
+ if logPreviewLimit > 0 {
+ if len(s) <= logPreviewLimit {
+ return s
+ }
+ return s[:logPreviewLimit] + "…"
+ }
+ return s
+}
+