commit 824e42a3188e2d9a6494f6df6f114f07d96defb6
Author: xuqiang <xuqiang@allwinnertech.com>
Date:   Mon Mar 21 17:33:40 2022 +0800

    busybox:syslogd: support format stamp
    
    Signed-off-by: xuqiang <xuqiang@allwinnertech.com>
    Change-Id: Ie3fa8f71649c5db0d2f06eab2cc2b67b099e5d02

diff --git a/utils/busybox/Makefile b/utils/busybox/Makefile
index 737b61828..8a6f7a9a5 100644
--- a/utils/busybox/Makefile
+++ b/utils/busybox/Makefile
@@ -90,6 +90,23 @@ ifeq ($(CONFIG_BUSYBOX_CONFIG_SELINUX),y)
   LDLIBS += pcre selinux sepol
 endif
 
+ifeq ($(CONFIG_BUSYBOX_SYSLOG_STAMP_KERNEL_FORMAT),y)
+  TARGET_CFLAGS+= -DSYSLOG_STAMP_KERNEL_FORMAT
+  # clock_gettime support the following clocks
+  ifeq ($(CONFIG_BUSYBOX_SYSLOG_STAMP_USE_CLOCK_REALTIME),y)
+    TARGET_CFLAGS+= -DSYSLOG_STAMP_USE_CLOCK_REALTIME
+  endif
+  ifeq ($(CONFIG_BUSYBOX_SYSLOG_STAMP_USE_CLOCK_MONOTONIC),y)
+    TARGET_CFLAGS+= -DSYSLOG_STAMP_USE_CLOCK_MONOTONIC
+  endif
+  ifeq ($(CONFIG_BUSYBOX_SYSLOG_STAMP_USE_CLOCK_MONOTONIC_RAW),y)
+    TARGET_CFLAGS+= -DSYSLOG_STAMP_USE_CLOCK_MONOTONIC_RAW
+  endif
+  ifeq ($(CONFIG_BUSYBOX_SYSLOG_STAMP_USE_CLOCK_BOOTTIME),y)
+    TARGET_CFLAGS+= -DSYSLOG_STAMP_USE_CLOCK_BOOTTIME
+  endif
+endif
+
 MAKE_VARS :=
 MAKE_FLAGS += \
 	EXTRA_CFLAGS="$(TARGET_CFLAGS) $(TARGET_CPPFLAGS)" \
diff --git a/utils/busybox/config/sysklogd/Config.in b/utils/busybox/config/sysklogd/Config.in
index 2045a7ae3..45fa8e5df 100644
--- a/utils/busybox/config/sysklogd/Config.in
+++ b/utils/busybox/config/sysklogd/Config.in
@@ -162,4 +162,49 @@ config BUSYBOX_CONFIG_FEATURE_KMSG_SYSLOG
 
 	  NOTICE: Syslog facilities in log entries needs kernel 3.5+.
 
+config BUSYBOX_SYSLOG_STAMP_KERNEL_FORMAT
+	bool "Support kernel stamp format."
+	default BUSYBOX_DEFAULT_FEATURE_KMSG_SYSLOG
+	depends on BUSYBOX_CONFIG_SYSLOGD
+	select BUSYBOX_CONFIG_PLATFORM_LINUX
+	help
+	  Say Y to enable kernel's stamp format, such as:
+	    [    0.000000] msg...
+	  Say N to use default format, such as:
+	    Jan  1 00:02:58 msg...
+	  And if say Y will choose more time source.
+
+choice
+	prompt "Choose Time Source"
+	default BUSYBOX_SYSLOG_STAMP_USE_CLOCK_MONOTONIC
+	depends on BUSYBOX_SYSLOG_STAMP_KERNEL_FORMAT
+	help
+		Additional time formats are supported by calling clock_gettime.
+
+	config BUSYBOX_SYSLOG_STAMP_USE_CLOCK_REALTIME
+	bool
+	prompt "CLOCK_REALTIME"
+	help
+	  See man clock_gettime, about clk_id.
+
+	config BUSYBOX_SYSLOG_STAMP_USE_CLOCK_MONOTONIC
+	bool
+	prompt "CLOCK_MONOTONIC"
+	help
+	  See man clock_gettime, about clk_id.
+
+	config BUSYBOX_SYSLOG_STAMP_USE_CLOCK_MONOTONIC_RAW
+	bool
+	prompt "CLOCK_MONOTONIC_RAW"
+	help
+	  See man clock_gettime, about clk_id.
+
+	config BUSYBOX_SYSLOG_STAMP_USE_CLOCK_BOOTTIME
+	bool
+	prompt "CLOCK_BOOTTIME"
+	help
+	  See man clock_gettime, about clk_id.
+
+endchoice
+
 endmenu
diff --git a/utils/busybox/patches/605-support-kernel-stamp.patch b/utils/busybox/patches/605-support-kernel-stamp.patch
new file mode 100644
index 000000000..d264246a7
--- /dev/null
+++ b/utils/busybox/patches/605-support-kernel-stamp.patch
@@ -0,0 +1,51 @@
+diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
+index d64ff27..df77823 100644
+--- a/sysklogd/syslogd.c
++++ b/sysklogd/syslogd.c
+@@ -816,6 +816,7 @@ static void timestamp_and_log(int pri, char *msg, int len)
+ 	char *timestamp;
+ 	time_t now;
+ 
++#ifndef SYSLOG_STAMP_KERNEL_FORMAT
+ 	/* Jan 18 00:11:22 msg... */
+ 	/* 01234567890123456 */
+ 	if (len < 16 || msg[3] != ' ' || msg[6] != ' '
+@@ -829,6 +830,38 @@ static void timestamp_and_log(int pri, char *msg, int len)
+ 		msg += 16;
+ 	}
+ 	timestamp[15] = '\0';
++#else
++	char buf[32] = {0};
++	struct timespec time_mon = {0};
++	clockid_t clkid = CLOCK_MONOTONIC;
++
++#ifdef SYSLOG_STAMP_USE_CLOCK_REALTIME
++	clkid = CLOCK_REALTIME;
++#endif
++#ifdef SYSLOG_STAMP_USE_CLOCK_MONOTONIC
++	clkid = CLOCK_MONOTONIC;
++#endif
++#ifdef SYSLOG_STAMP_USE_CLOCK_MONOTONIC_RAW
++	clkid = CLOCK_MONOTONIC_RAW;
++#endif
++#ifdef SYSLOG_STAMP_USE_CLOCK_BOOTTIME
++	clkid = CLOCK_BOOTTIME;
++#endif
++
++	if (len < 16 || msg[3] != ' ' || msg[6] != ' '
++	 || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
++	) {
++	} else {
++		msg += 16;
++	}
++
++	if (clock_gettime(clkid, &time_mon)) {
++		timestamp = "[format time error] ";
++	} else {
++		snprintf(buf, sizeof(buf), "[%8d.%06d] ", time_mon.tv_sec, time_mon.tv_nsec/1000);
++		timestamp = (char *)buf;
++	}
++#endif
+ 
+ 	if (option_mask32 & OPT_kmsg) {
+ 		log_to_kmsg(pri, msg);
