如何在llinux中获取带时间戳的ttyUSB日志

时间:2016-04-20 07:47:57

标签: linux logging terminal

以下日志来自TeraTerm,一个用于Windows的终端模拟器。 我希望以下列方式从我的Linux中的ttyUSB0获取日志。

[Thu Apr 14 15:08:14.410 2016] Load U-Boot from eMMC/NAND Flash
[Thu Apr 14 15:08:14.410 2016] eMMC/NAND copy from 0x030A0000 to 0x49FB0000 (len:262144).
[Thu Apr 14 15:08:14.456 2016] Done.
[Thu Apr 14 15:08:14.456 2016] 
[Thu Apr 14 15:08:14.456 2016] 
[Thu Apr 14 15:08:14.456 2016] U-Boot 1.2.0 (Sep  2 2013 - 13:31:42)Puma6 - PSPU-Boot 2.0.0.24
[Thu Apr 14 15:08:14.503 2016] 
[Thu Apr 14 15:08:14.503 2016] DRAM:  128 MB
[Thu Apr 14 15:08:14.503 2016] *** Warning - Unsupported Flash detected, flash is unusable
[Thu Apr 14 15:08:14.503 2016] 

我找到了this document,但它在终端空闲时给出了时间戳。

-- 0:ttyUSB2 -- time-stamp -- 2016-04-15 17:23:13 --
-- 0:ttyUSB2 -- time-stamp -- 2016-04-15 17:23:53 --

1 个答案:

答案 0 :(得分:0)

您可以使用awk这样做。我有数据通过串行端口进入。原始输出(使用/ dev / ttyUSB0)如下所示。

Sending struct (10 bytes) ... 

A: -0.06.

Sending struct (10 bytes) ... 

A: -0.05.

Sending struct (10 bytes) ... 

A: -0.06.


Sending struct (10 bytes) ... 

A: -0.05.

使用以下命令后,我会得到时间戳记。

awk '{ print strftime("[%a %b %d %H:%M:%S.%f %y]"), $0; }' /dev/ttyUSB0 > /home/captain/ttyUSB0_v11.txt &

输出如下。

[Thu Sep 26 12:01:03.%f 19] 
[Thu Sep 26 12:01:03.%f 19] Transmitting at 868 Mhz...
[Thu Sep 26 12:01:03.%f 19] 
[Thu Sep 26 12:01:03.%f 19] SPI Flash Init OK ... UniqueID (MAC): A: -0.06.
[Thu Sep 26 12:01:03.%f 19] 
[Thu Sep 26 12:01:03.%f 19] Sending struct (10 bytes) ... 
[Thu Sep 26 12:01:03.%f 19] 
[Thu Sep 26 12:01:04.%f 19] A: -0.05.
[Thu Sep 26 12:01:04.%f 19] 
[Thu Sep 26 12:01:04.%f 19] Sending struct (10 bytes) ... 
[Thu Sep 26 12:01:04.%f 19] 
[Thu Sep 26 12:01:05.%f 19] A: -0.06.
[Thu Sep 26 12:01:05.%f 19] 
[Thu Sep 26 12:01:06.%f 19] Sending struct (10 bytes) ... 
[Thu Sep 26 12:01:06.%f 19] 
[Thu Sep 26 12:01:07.%f 19] A: -0.06.
[Thu Sep 26 12:01:07.%f 19] 
[Thu Sep 26 12:01:07.%f 19] Sending struct (10 bytes) ... 
[Thu Sep 26 12:01:07.%f 19] 
[Thu Sep 26 12:01:08.%f 19] A: -0.06.
[Thu Sep 26 12:01:08.%f 19] 
[Thu Sep 26 12:01:08.%f 19] Sending struct (10 bytes) ... 
[Thu Sep 26 12:01:08.%f 19] 
[Thu Sep 26 12:01:09.%f 19] A: -0.05.

出于某些奇怪的原因,毫秒(%f)无效。但是您应该能够弄清楚它是否非常重要。

欢呼

OS- Ubuntu14.04

串行设备-Moteino(Arduino)