aboutsummaryrefslogtreecommitdiff
path: root/sys/kern/kern_syslog.c
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-05-26 23:40:02 -0400
committerIan Moffett <ian@osmora.org>2024-05-26 23:41:10 -0400
commita59842033c0a29f774f895278597c8fc8141f7ac (patch)
treed15f40ccf3a9ba7c50a2968b280177e3cdf822b8 /sys/kern/kern_syslog.c
parent97ccc71cfd0b15b8cc7b54405c1231e0e63c7f9c (diff)
kernel: syslog: Add timestamp to logging
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'sys/kern/kern_syslog.c')
-rw-r--r--sys/kern/kern_syslog.c32
1 files changed, 31 insertions, 1 deletions
diff --git a/sys/kern/kern_syslog.c b/sys/kern/kern_syslog.c
index 712b69b..21b90af 100644
--- a/sys/kern/kern_syslog.c
+++ b/sys/kern/kern_syslog.c
@@ -31,6 +31,7 @@
#include <sys/machdep.h>
#include <sys/tty.h>
#include <sys/cdefs.h>
+#include <sys/timer.h>
#include <dev/vcons/vcons.h>
#include <fs/procfs.h>
#include <string.h>
@@ -104,9 +105,38 @@ void
kprintf(const char *fmt, ...)
{
va_list ap;
+ char timestamp[64] = "[ 0.000000] ";
+ bool has_counter = true;
+ bool use_timestamp = true;
+ const char *fmt_p = fmt;
+ struct timer tmr = {0};
+
+ /*
+ * If the first char is OMIT_TIMESTAMP, than we won't
+ * print out the timestamp.
+ */
+ if (*fmt_p == OMIT_TIMESTAMP[0]) {
+ ++fmt_p;
+ use_timestamp = false;
+ }
+
+ /* See if we can use the counter */
+ if (req_timer(TIMER_GP, &tmr) != 0)
+ has_counter = false;
+
+ if (has_counter) {
+ if (tmr.get_time_sec != NULL && tmr.get_time_usec != NULL)
+ snprintf(timestamp, sizeof(timestamp), "[ %d.%06d] ",
+ tmr.get_time_sec(), tmr.get_time_usec());
+
+ }
+
+ if (use_timestamp) {
+ syslog_write(timestamp, strlen(timestamp));
+ }
va_start(ap, fmt);
- vkprintf(fmt, &ap);
+ vkprintf(fmt_p, &ap);
va_end(ap);
}