commit 4f835b9f04d8b0de8b69f3960480c19e62c95363
parent e83d4265cac4452a63620a735faed3b82b6fcf14
Author: Drew DeVault <sir@cmpwn.com>
Date: Wed, 13 Apr 2022 17:10:02 +0200
log: new module
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
5 files changed, 128 insertions(+), 0 deletions(-)
diff --git a/log/funcs.ha b/log/funcs.ha
@@ -0,0 +1,21 @@
+use fmt;
+
+// Prints data to the log, with a newline.
+export fn lprintln(log: *logger, fields: fmt::formattable...) void = {
+ log.println(log, fields...);
+};
+
+// Formats and prints data to the log, with a newline.
+export fn lprintfln(log: *logger, fmt: str, fields: fmt::field...) void = {
+ log.printfln(log, fmt, fields...);
+};
+
+// Prints data to the global log, with a newline
+export fn println(fields: fmt::formattable...) void = {
+ lprintln(global, fields...);
+};
+
+// Formats and prints data to the global log, with a newline.
+export fn printfln(fmt: str, fields: fmt::field...) void = {
+ lprintfln(global, fmt, fields...);
+};
diff --git a/log/global.ha b/log/global.ha
@@ -0,0 +1,21 @@
+use io;
+use os;
+
+// The default global logger instance.
+export let global: *logger = &_global;
+
+let _global: stdlogger = stdlogger {
+ println = &log_println,
+ printfln = &log_printfln,
+ sink = 0: io::file,
+};
+
+@init fn init() void = {
+ // XXX: Would be nice not to have to do this
+ _global.sink = os::stdout;
+};
+
+// Sets the global logger instance to the provided logger.
+export fn setlogger(log: *logger) void = {
+ global = log;
+};
diff --git a/log/logger.ha b/log/logger.ha
@@ -0,0 +1,44 @@
+use datetime;
+use fmt;
+use io;
+use os;
+
+// Interface for implementing a logger.
+export type logger = struct {
+ println: *fn(logger: *logger, fields: fmt::formattable...) void,
+ printfln: *fn(logger: *logger, fmt: str, fields: fmt::field...) void,
+};
+
+export type stdlogger = struct {
+ logger,
+ sink: io::handle,
+};
+
+// Creates a new standard logger.
+export fn new(sink: io::handle) stdlogger = {
+ return stdlogger {
+ println = &log_println,
+ printfln = &log_printfln,
+ sink = sink,
+ };
+};
+
+fn log_println(sink: *logger, fields: fmt::formattable...) void = {
+ const sink = sink: *stdlogger;
+ assert(sink.println == &log_println);
+ const now = datetime::now();
+ fmt::fprint(sink.sink, "["): void;
+ datetime::format(sink.sink, datetime::STAMP, &now): void;
+ fmt::fprint(sink.sink, "] "): void;
+ fmt::fprintln(sink.sink, fields...): void;
+};
+
+fn log_printfln(sink: *logger, fmt: str, fields: fmt::field...) void = {
+ const sink = sink: *stdlogger;
+ assert(sink.printfln == &log_printfln);
+ const now = datetime::now();
+ fmt::fprint(sink.sink, "["): void;
+ datetime::format(sink.sink, datetime::STAMP, &now): void;
+ fmt::fprint(sink.sink, "] "): void;
+ fmt::fprintfln(sink.sink, fmt, fields...): void;
+};
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -827,6 +827,11 @@ linux_vdso() {
gen_ssa -plinux linux::vdso linux strings format::elf
}
+log() {
+ gen_srcs log logger.ha global.ha funcs.ha
+ gen_ssa log fmt io os
+}
+
gensrcs_math() {
gen_srcs math \
math.ha \
@@ -1333,6 +1338,7 @@ linux linux
linux::keyctl linux
linux::io_uring linux
linux::vdso linux
+log
math
math::random
net linux freebsd
diff --git a/stdlib.mk b/stdlib.mk
@@ -466,6 +466,12 @@ stdlib_deps_linux+=$(stdlib_linux_io_uring_linux)
stdlib_linux_vdso_linux=$(HARECACHE)/linux/vdso/linux_vdso-linux.o
stdlib_deps_linux+=$(stdlib_linux_vdso_linux)
+# gen_lib log (any)
+stdlib_log_any=$(HARECACHE)/log/log-any.o
+stdlib_deps_any+=$(stdlib_log_any)
+stdlib_log_linux=$(stdlib_log_any)
+stdlib_log_freebsd=$(stdlib_log_any)
+
# gen_lib math (any)
stdlib_math_any=$(HARECACHE)/math/math-any.o
stdlib_deps_any+=$(stdlib_math_any)
@@ -1382,6 +1388,18 @@ $(HARECACHE)/linux/vdso/linux_vdso-linux.ssa: $(stdlib_linux_vdso_linux_srcs) $(
@HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nlinux::vdso \
-t$(HARECACHE)/linux/vdso/linux_vdso.td $(stdlib_linux_vdso_linux_srcs)
+# log (+any)
+stdlib_log_any_srcs= \
+ $(STDLIB)/log/logger.ha \
+ $(STDLIB)/log/global.ha \
+ $(STDLIB)/log/funcs.ha
+
+$(HARECACHE)/log/log-any.ssa: $(stdlib_log_any_srcs) $(stdlib_rt) $(stdlib_fmt_$(PLATFORM)) $(stdlib_io_$(PLATFORM)) $(stdlib_os_$(PLATFORM))
+ @printf 'HAREC \t$@\n'
+ @mkdir -p $(HARECACHE)/log
+ @HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nlog \
+ -t$(HARECACHE)/log/log.td $(stdlib_log_any_srcs)
+
# math (+any)
stdlib_math_any_srcs= \
$(STDLIB)/math/math.ha \
@@ -2417,6 +2435,12 @@ testlib_deps_linux+=$(testlib_linux_io_uring_linux)
testlib_linux_vdso_linux=$(TESTCACHE)/linux/vdso/linux_vdso-linux.o
testlib_deps_linux+=$(testlib_linux_vdso_linux)
+# gen_lib log (any)
+testlib_log_any=$(TESTCACHE)/log/log-any.o
+testlib_deps_any+=$(testlib_log_any)
+testlib_log_linux=$(testlib_log_any)
+testlib_log_freebsd=$(testlib_log_any)
+
# gen_lib math (any)
testlib_math_any=$(TESTCACHE)/math/math-any.o
testlib_deps_any+=$(testlib_math_any)
@@ -3369,6 +3393,18 @@ $(TESTCACHE)/linux/vdso/linux_vdso-linux.ssa: $(testlib_linux_vdso_linux_srcs) $
@HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nlinux::vdso \
-t$(TESTCACHE)/linux/vdso/linux_vdso.td $(testlib_linux_vdso_linux_srcs)
+# log (+any)
+testlib_log_any_srcs= \
+ $(STDLIB)/log/logger.ha \
+ $(STDLIB)/log/global.ha \
+ $(STDLIB)/log/funcs.ha
+
+$(TESTCACHE)/log/log-any.ssa: $(testlib_log_any_srcs) $(testlib_rt) $(testlib_fmt_$(PLATFORM)) $(testlib_io_$(PLATFORM)) $(testlib_os_$(PLATFORM))
+ @printf 'HAREC \t$@\n'
+ @mkdir -p $(TESTCACHE)/log
+ @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nlog \
+ -t$(TESTCACHE)/log/log.td $(testlib_log_any_srcs)
+
# math (+any)
testlib_math_any_srcs= \
$(STDLIB)/math/math.ha \