Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can a timestamp be added to the log_file of expect?

343 views
Skip to first unread message

Svenn Are Bjerkem

unread,
Nov 7, 2019, 3:26:18 AM11/7/19
to
I have tried to search the internet to see if the log_file command of expect can add a timestamp to the resulting log file.

I have found that I can use send_log to have generic text added to the log, but it would have been nice to prepend each logged line with a timestamp.

--
Svenn

heinrichmartin

unread,
Nov 7, 2019, 4:20:33 AM11/7/19
to
On Thursday, November 7, 2019 at 9:26:18 AM UTC+1, Svenn Are Bjerkem wrote:
> I have tried to search the internet to see if the log_file command of expect can add a timestamp to the resulting log file.

Tcl allows to redirect procs. For all lines that are actually logged using send_log, you can

# untested
proc send_log args {
# consider optional "--", but leave syntax check to legacy proc
lset args end [string cat \[ [clock format ...] "\] " [lindex $args end]]
exp_send_log {*}$args
}
# optionally [rename] send_log and exp_send_log first and override both of them

> I have found that I can use send_log to have generic text added to the log, but it would have been nice to prepend each logged line with a timestamp.

My guts feeling says that a timestamp to other log messages (other than logged via send_log) requires a patch at C level.

Svenn Are Bjerkem

unread,
Nov 7, 2019, 5:29:34 AM11/7/19
to
On Thursday, November 7, 2019 at 10:20:33 AM UTC+1, heinrichmartin wrote:
> On Thursday, November 7, 2019 at 9:26:18 AM UTC+1, Svenn Are Bjerkem wrote:
> > I have tried to search the internet to see if the log_file command of expect can add a timestamp to the resulting log file.
>
> Tcl allows to redirect procs. For all lines that are actually logged using send_log, you can
>
> # untested
> proc send_log args {
> # consider optional "--", but leave syntax check to legacy proc
> lset args end [string cat \[ [clock format ...] "\] " [lindex $args end]]
> exp_send_log {*}$args
> }
> # optionally [rename] send_log and exp_send_log first and override both of them
>

Your proc worked as described when adjusting the clock format to my liking.

>
> My guts feeling says that a timestamp to other log messages (other than logged via send_log) requires a patch at C level.

Ok, I think I will have to be satisfied with a few time-stamped headers here and there and just accept that log_file can't be easily overridden for this purpose.

--
Svenn

onem...@gmail.com

unread,
Nov 7, 2019, 5:27:42 PM11/7/19
to
You may wish to try using Heinrich's proc redirection trick to redirect through the logger package, which does timestamp all messages. However, it uses its own particular logging format to wrap the logged messages, which may not be to your taste.

--Erik

Kenny McCormack

unread,
Nov 21, 2019, 3:28:29 PM11/21/19
to
In article <0b69dbb9-a6f0-4c99...@googlegroups.com>,
This may or may not be what you are looking for, but this is a script that
I wrote decades ago called "Instrument", that I use to run a program with a
timestamp on each line in the log file:

--- Cut Here ---
#!/bin/expect --
# Post process the log file with:
# awk '-F\r' '{print $2,$1}' /tmp/typescript
log_file -noappend /tmp/typescript
eval spawn $argv

interact -o "\n" { send_user [timestamp -format %c\n] }
--- Cut Here ---

I know this worked long ago; I haven't had the need to use it any time recently.

Note: In case it isn't clear from the comments in the code, you run the thing like:

$ Instrument some command line

Which produces a log file, which you then read via the AWK command line shown.

--
I am not a troll.
Rick C. Hodgin
I am not a crook.
Rick M. Nixon
0 new messages