How do I convert dmesg timestamp to custom date format?

前端 未结 9 1219
感动是毒
感动是毒 2020-12-12 17:41

I am trying to understand the dmesg timestamp and find it hard to convert that to change it to java date/custom date format.

Sample dmesg log:

<         


        
相关标签:
9条回答
  • 2020-12-12 18:28

    So KevZero requested a less kludgy solution, so I came up with the following:

    sed -r 's#^\[([0-9]+\.[0-9]+)\](.*)#echo -n "[";echo -n $(date --date="@$(echo "$(grep btime /proc/stat|cut -d " " -f 2)+\1" | bc)" +"%c");echo -n "]";echo -n "\2"#e'
    

    Here's an example:

    $ dmesg|tail | sed -r 's#^\[([0-9]+\.[0-9]+)\](.*)#echo -n "[";echo -n $(date --date="@$(echo "$(grep btime /proc/stat|cut -d " " -f 2)+\1" | bc)" +"%c");echo -n "]";echo -n "\2"#e'
    [2015-12-09T04:29:20 COT] cfg80211:   (57240000 KHz - 63720000 KHz @ 2160000 KHz), (N/A, 0 mBm), (N/A)
    [2015-12-09T04:29:23 COT] wlp3s0: authenticate with dc:9f:db:92:d3:07
    [2015-12-09T04:29:23 COT] wlp3s0: send auth to dc:9f:db:92:d3:07 (try 1/3)
    [2015-12-09T04:29:23 COT] wlp3s0: authenticated
    [2015-12-09T04:29:23 COT] wlp3s0: associate with dc:9f:db:92:d3:07 (try 1/3)
    [2015-12-09T04:29:23 COT] wlp3s0: RX AssocResp from dc:9f:db:92:d3:07 (capab=0x431 status=0 aid=6)
    [2015-12-09T04:29:23 COT] wlp3s0: associated
    [2015-12-09T04:29:56 COT] thinkpad_acpi: EC reports that Thermal Table has changed
    [2015-12-09T04:29:59 COT] i915 0000:00:02.0: BAR 6: [??? 0x00000000 flags 0x2] has bogus alignment
    [2015-12-09T05:00:52 COT] thinkpad_acpi: EC reports that Thermal Table has changed
    

    If you want it to perform a bit better, put the timestamp from proc into a variable instead :)

    0 讨论(0)
  • 2020-12-12 18:37

    In recent versions of dmesg, you can just call dmesg -T.

    0 讨论(0)
  • 2020-12-12 18:37

    If you don't have the -T option for dmesg as for example on Andoid, you can use the busybox version. The following solves also some other issues:

    1. The [0.0000] format is preceded by something that looks like misplaced color information, prefixes like <6>.
    2. Make integers from floats.

    It is inspired by this blog post.

    #!/bin/sh                                                                                                               
    # Translate dmesg timestamps to human readable format                                                                   
    
    # uptime in seconds                                                                                                     
    uptime=$(cut -d " " -f 1 /proc/uptime)                                                                                  
    
    # remove fraction                                                                                                       
    uptime=$(echo $uptime | cut -d "." -f1)                                                                                 
    
    # run only if timestamps are enabled                                                                                    
    if [ "Y" = "$(cat /sys/module/printk/parameters/time)" ]; then                                                          
      dmesg | sed "s/[^\[]*\[/\[/" | sed "s/^\[[ ]*\?\([0-9.]*\)\] \(.*\)/\\1 \\2/" | while read timestamp message; do      
        timestamp=$(echo $timestamp | cut -d "." -f1)                                                                       
        ts1=$(( $(busybox date +%s) - $uptime + $timestamp ))                                                               
        ts2=$(busybox date -d "@${ts1}")                                                                                    
        printf "[%s] %s\n" "$ts2" "$message"                                                                                
      done                                                                                                                  
    else                                                                                                                    
      echo "Timestamps are disabled (/sys/module/printk/parameters/time)"                                                   
    fi                                                                                                                      
    

    Note, however, that this implementation is quite slow.

    0 讨论(0)
提交回复
热议问题