Posted to tcl by colin at Tue Aug 28 23:06:17 GMT 2012view raw
- # syslog using Udp extension
- # Colin McCormack
- lappend auto_path [pwd]/Udp
- package require Udp
- package require TclOO
- package provide Syslog 1.0
- oo::class create Syslog {
- method timestamp {time} {
- # return a timestamp of $time
- if {![string is integer -strict $time]} {
- set time [clock scan $time -timezone :UTC]
- }
- return [clock format $time -format "%Y-%m-%dT%H:%M:%SZ"]
- }
- method log {message args} {
- variable template
- set props [dict merge $template $args]
- foreach {p script} {
- timestamp {clock seconds}
- } {
- if {![dict exists $props $p]} {
- dict set props $p [eval $script]
- }
- }
- dict with props {
- set timestamp [my timestamp $timestamp]
- if {![string is integer -strict $facility]} {
- variable facilities
- set facility [dict get $facilities $facility]
- }
- if {![string is integer -strict $priority]} {
- variable priorities
- set priority [dict get $priorities $priority]
- }
- set PRI [expr {($facility * 8) + $priority}]
- set line "<$PRI>1 $timestamp $hostname $appname $procid $msgid - $message"
- #puts stderr $line
- }
- variable syslog; variable server; variable port
- ::udp send $syslog $server $port $line
- }
- destructor {
- variable syslog
- catch {chan close $syslog}
- }
- constructor {args} {
- variable facility user
- variable priority debug
- variable hostname [info host]
- variable procid [pid]
- variable appname $::argv0
- variable msgid -
- variable template {}
- variable port 514 ;# syslog's port
- variable {*}$args
- if {![info exists server]} {
- error "must specify Syslog server"
- }
- variable syslog [::udp create]
- foreach v {hostname procid appname msgid facility priority} {
- if {![dict exists $template $v]} {
- dict set template $v [set $v]
- }
- }
- variable facilities
- set i 0
- foreach f {
- kern user mail daemon auth syslog
- lrp news uucp cron authpriv ftp ntp audit alert clock
- local0 local1 local2 local3 local4 local5 local6 local7
- } {
- dict set facilities $f $i
- dict set facilities $i $f
- incr i
- }
- variable priorities
- set i 0
- foreach f {emergency alert critical error warning notice info debug} {
- dict set priorities $f $i
- dict set priorities $i $f
- incr i
- }
- }
- }
- if {[info exists argv0] && ($argv0 eq [info script])} {
- Syslog create syslog server box
- syslog log "This is a test"
- }