# TWAPI example
# Dump specified event log to standard output

package require twapi

if {$argc > 1} {
    puts stderr "Usage: [info nameofexecutable] $argv0 EVENTLOGNAME"
    exit
}

if {$argc == 1} {
    set source [lindex $argv 0]
} else {
    set source "Application"
}

# Open the specified event log
set hevl [twapi::eventlog_open -source $source]

# Make translation binary (lf), since records have embedded CR-LF,
# we will have to explicitly output CR-LF pairs
fconfigure stdout -translation lf
# Keep reading record until no more
while {[llength [set events [twapi::eventlog_read $hevl]]]} {
    # print out each record
    foreach eventrec $events {
        array set event $eventrec
        set timestamp [clock format $event(-timewritten) -format "%x %X"]
        set source   $event(-source)
        set category [twapi::eventlog_format_category $eventrec -width -1]
        set message  [twapi::eventlog_format_message $eventrec -width -1]
        puts -nonewline "Time: $timestamp\r\nSource: $source\r\nCategory: $category\r\n$message\r\n\r\n"
    }
}
