修改/dev/kmsg时间戳格式(printk.c)

修改 cat /dev/kmsg 输出log的时间戳,达到与dmesg输出的格式效果一致

6,64,2484633,-;Calibrating delay loop (skipped), value calculated using timer frequency.. 48.00 BogoMIPS (lpj=240000)
6,65,2484669,-;pid_max: default: 32768 minimum: 301
6,66,2484938,-;Mount-cache hash table entries: 512
6,67,2485940,-;CPU: Testing write buffer coherency: ok
3,68,2486353,-;/cpus/cpu@f00 missing clock-frequency property
6,69,2486481,-;CPU0: thread -1, cpu 0, socket 15, mpidr 80000f00
6,70,2486528,-;Setting up static identity map for 0xc057a5a0 - 0xc057a5f8
6,71,2487613,-;last_log: 0x63480000 map to 0xc8814000 and copy to 0xc8836000, size 0x20000 early 0x14ce (version 3.1)
6,72,2541319,-;Brought up 1 CPUs

 

 

 修改kernel/kernel/printk.c

diff --git a/kernel/kernel/printk.c b/kernel/kernel/printk.c
index 7874259..8ae14bb 100644
--- a/kernel/kernel/printk.c
+++ b/kernel/kernel/printk.c
@@ -588,9 +588,9 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
                 ((user->prev & LOG_CONT) && !(msg->flags & LOG_PREFIX)))
                cont = '+';
 
-       len = sprintf(user->buf, "%u,%llu,%llu,%c;",
+       len = sprintf(user->buf, "[%2u]:[%3llu]:[%6llu.%06llu] ",
                      (msg->facility << 3) | msg->level,
-                     user->seq, ts_usec, cont);
+                     user->seq, (ts_usec / 1000000), (ts_usec % 1000000));
        user->prev = msg->flags;
 
        /* escape non-printable characters */

 

 

修改后

diff --git a/kernel/kernel/printk.c b/kernel/kernel/printk.c
index 7874259..9953166 100644
--- a/kernel/kernel/printk.c
+++ b/kernel/kernel/printk.c
@@ -534,6 +534,8 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
        struct devkmsg_user *user = file->private_data;
        struct log *msg;
        u64 ts_usec;
+    u64 ts_sec = 0;
+    u64 ts_msec= 0;
        size_t i;
        char cont = '-';
        size_t len;
@@ -574,6 +576,11 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
        ts_usec = msg->ts_nsec;
        do_div(ts_usec, 1000);
 
+    ts_sec = ts_usec;
+    do_div(ts_sec, 1000000);
+
+    ts_msec = ts_usec - ts_sec * 1000000;
+
        /*
         * If we couldn't merge continuation line fragments during the print,
         * export the stored flags to allow an optional external merge of the
@@ -588,9 +595,9 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
                 ((user->prev & LOG_CONT) && !(msg->flags & LOG_PREFIX)))
                cont = '+';
 
-       len = sprintf(user->buf, "%u,%llu,%llu,%c;",
+       len = sprintf(user->buf, "[%2u]:[%3llu]:[%6llu.%06llu] ",
                      (msg->facility << 3) | msg->level,
-                     user->seq, ts_usec, cont);
+                     user->seq, ts_sec, ts_msec);
        user->prev = msg->flags;
 
        /* escape non-printable characters */

 

 

编译通过,烧录重新 cat /deg/kmsg

[ 6]:[ 64]:[     2.484905] Calibrating delay loop (skipped), value calculated using timer frequency.. 48.00 BogoMIPS (lpj=240000)
[ 6]:[ 65]:[     2.484937] pid_max: default: 32768 minimum: 301
[ 6]:[ 66]:[     2.485205] Mount-cache hash table entries: 512
[ 6]:[ 67]:[     2.486235] CPU: Testing write buffer coherency: ok
[ 3]:[ 68]:[     2.486642] /cpus/cpu@f00 missing clock-frequency property
[ 6]:[ 69]:[     2.486762] CPU0: thread -1, cpu 0, socket 15, mpidr 80000f00
[ 6]:[ 70]:[     2.486807] Setting up static identity map for 0xc057a640 - 0xc057a698
[ 6]:[ 71]:[     2.487896] last_log: 0x63480000 map to 0xc8814000 and copy to 0xc8836000, size 0x20000 early 0x14ce (version 3.1)
[ 6]:[ 72]:[     2.541550] Brought up 1 CPUs

 

posted @ 2022-02-19 12:23  mcdull^0^  阅读(993)  评论(0编辑  收藏  举报