From 0d2bd96d2c019a7c0e0282898809fb03962ca701 Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Fri, 4 Oct 2013 17:22:18 +0000 Subject: [PATCH] better logging --- go/r3-eventbroker_zmq/logging.go | 30 ++++++++++++++++++++++++++++++ go/r3-eventbroker_zmq/main.go | 25 +++++++++++-------------- go/r3-eventbroker_zmq/presence.go | 5 ++--- go/r3-eventbroker_zmq/smallbrain.go | 8 ++++---- go/r3-eventbroker_zmq/sockettoevent.go | 5 ++--- 5 files changed, 49 insertions(+), 24 deletions(-) create mode 100644 go/r3-eventbroker_zmq/logging.go diff --git a/go/r3-eventbroker_zmq/logging.go b/go/r3-eventbroker_zmq/logging.go new file mode 100644 index 0000000..0c0cd40 --- /dev/null +++ b/go/r3-eventbroker_zmq/logging.go @@ -0,0 +1,30 @@ +// (c) Bernhard Tittelbach, 2013 + +package main + +import "os" +import "log" +import "log/syslog" + +type NullWriter struct {} +func (n *NullWriter) Write(p []byte) (int, error) {return len(p),nil} + +var ( + Syslog_ *log.Logger + Debug_ *log.Logger +) + +func init() { + Syslog_ = log.New(os.Stdout, "", log.LstdFlags) + Debug_ = log.New(&NullWriter{}, "", 0) +} + +func LogEnableSyslog() { + var logerr error + Syslog_, logerr = syslog.NewLogger(syslog.LOG_INFO | (18<<3), 0) + if logerr != nil { panic(logerr) } +} + +func LogEnableDebuglog() { + Debug_ = log.New(os.Stderr, "DEBUG", log.LstdFlags) +} \ No newline at end of file diff --git a/go/r3-eventbroker_zmq/main.go b/go/r3-eventbroker_zmq/main.go index 447ac1c..9c253f2 100644 --- a/go/r3-eventbroker_zmq/main.go +++ b/go/r3-eventbroker_zmq/main.go @@ -7,8 +7,6 @@ import ( "os" "flag" //~ "time" - "log/syslog" - "log" pubsub "github.com/tuxychandru/pubsub" "./r3events" ) @@ -30,7 +28,7 @@ var ( keylookup_addr_ string brain_listen_addr_ string use_syslog_ bool - Syslog_ *log.Logger + enable_debuglog_ bool ) func usage() { @@ -45,11 +43,19 @@ func init() { flag.StringVar(&keylookup_addr_, "keylookupaddr", "ipc:///run/tuer/door_keyname.ipc", "address to use for key/name lookups") flag.StringVar(&brain_listen_addr_, "brainlisten", "tcp://*:4245", "address to listen for requests about latest stored event") flag.BoolVar(&use_syslog_, "syslog", false, "log to syslog local2 facility") + flag.BoolVar(&enable_debuglog_, "debug", false, "enable debug logging") flag.Usage = usage flag.Parse() } func main() { + if enable_debuglog_ { LogEnableDebuglog() } + if use_syslog_ { + LogEnableSyslog() + Syslog_.Print("started") + defer Syslog_.Print("exiting") + } + zmqctx, sub_in_chans, pub_out_socket, keylookup_socket := ZmqsInit(doorsub_addr_, sensorssub_port_, pub_port_, keylookup_addr_) if sub_in_chans != nil {defer sub_in_chans.Close()} defer zmqctx.Close() @@ -59,15 +65,6 @@ func main() { panic("zmq sockets must not be nil !!") } - if use_syslog_ { - var logerr error - Syslog_, logerr = syslog.NewLogger(syslog.LOG_INFO | (18<<3), 0) - //~ Syslog_, logerr = syslog.NewLogger(syslog.LOG_INFO | syslog.LOG_LOCAL2, 0) - if logerr != nil { panic(logerr) } - Syslog_.Print("started") - defer Syslog_.Print("exiting") - } - ps := pubsub.New(10) defer ps.Shutdown() // ps.Shutdown should be called before zmq_ctx.Close(), since it will cause goroutines to shutdown and close zqm_sockets which is needed for zmq_ctx.Close() to return //~ ticker := time.NewTicker(time.Duration(5) * time.Minute) @@ -87,9 +84,9 @@ func main() { //~ MakeTimeTick(ps) case event_interface := <- publish_these_events_chan: data, err := r3events.MarshalEvent2ByteByte(event_interface) - log.Printf("publishing %s",data) + Debug_.Printf("publishing %s",data) if err != nil { - if Syslog_ != nil {Syslog_.Print(err)} + Syslog_.Print(err) continue } if err := pub_out_socket.Send(data); err != nil { diff --git a/go/r3-eventbroker_zmq/presence.go b/go/r3-eventbroker_zmq/presence.go index c8553e2..ecaa146 100644 --- a/go/r3-eventbroker_zmq/presence.go +++ b/go/r3-eventbroker_zmq/presence.go @@ -7,7 +7,6 @@ import ( //~ "./brain" pubsub "github.com/tuxychandru/pubsub" "./r3events" - //~ "log" ) func MetaEventRoutine_Presence(ps *pubsub.PubSub) { @@ -20,7 +19,7 @@ func MetaEventRoutine_Presence(ps *pubsub.PubSub) { defer ps.Unsub(events_chan, "door", "doorcmd", "buttons", "movement") for event := range(events_chan) { - //~ log.Printf("Presence: %s - %s", event, doorstatemap) + //~ Debug_.Printf("Presence: %s - %s", event, doorstatemap) new_presence := last_presence ts := time.Now().Unix() switch evnt := event.(type) { @@ -55,7 +54,7 @@ func MetaEventRoutine_Presence(ps *pubsub.PubSub) { } else { new_presence = false } - //~ log.Printf("Presence: new: %s , last:%s", new_presence, last_presence) + //~ Debug_.Printf("Presence: new: %s , last:%s", new_presence, last_presence) if new_presence != last_presence { last_presence = new_presence ps.Pub(r3events.PresenceUpdate{new_presence, ts} , "presence") diff --git a/go/r3-eventbroker_zmq/smallbrain.go b/go/r3-eventbroker_zmq/smallbrain.go index 0996d78..cdd0c5a 100644 --- a/go/r3-eventbroker_zmq/smallbrain.go +++ b/go/r3-eventbroker_zmq/smallbrain.go @@ -5,7 +5,6 @@ package main import ( zmq "github.com/vaughan0/go-zmq" "./r3events" - "log" ) type hippocampus map[string]interface{} @@ -20,12 +19,12 @@ func BrainCenter( zmq_ctx *zmq.Context, listen_addr string, event_chan <- chan i case event, ec_still_open := <- event_chan: if ! ec_still_open { return } h[r3events.NameOfStruct(event)] = event - log.Printf("Brain: stored %s, %s", r3events.NameOfStruct(event), event) + Debug_.Printf("Brain: stored %s, %s", r3events.NameOfStruct(event), event) case brain_request := <- zbrain_chans.In(): if len(brain_request) == 0 { continue } requested_eventname := string(brain_request[0]) - log.Printf("Brain: received request: %s", requested_eventname) + Debug_.Printf("Brain: received request: %s", requested_eventname) retr_event, is_in_map := h[requested_eventname] if is_in_map { data, err := r3events.MarshalEvent2ByteByte(retr_event) @@ -33,7 +32,8 @@ func BrainCenter( zmq_ctx *zmq.Context, listen_addr string, event_chan <- chan i zbrain_chans.Out() <- data continue } else { - if Syslog_ != nil {Syslog_.Print("BrainCenter", err)} + Syslog_.Print("BrainCenter", err) + Debug_.Print("BrainCenter", err) } } zbrain_chans.Out() <- [][]byte{[]byte("UNKNOWN")} diff --git a/go/r3-eventbroker_zmq/sockettoevent.go b/go/r3-eventbroker_zmq/sockettoevent.go index 6511ff0..fbd74d5 100644 --- a/go/r3-eventbroker_zmq/sockettoevent.go +++ b/go/r3-eventbroker_zmq/sockettoevent.go @@ -9,7 +9,6 @@ import ( //~ "./brain" pubsub "github.com/tuxychandru/pubsub" zmq "github.com/vaughan0/go-zmq" - "log" "./r3events" ) @@ -41,7 +40,7 @@ func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) { case "opening": case "closing": default: - if Syslog_ != nil {Syslog_.Print("parseSocketInputLine_State: Unexpected State:", lines)} + Syslog_.Print("parseSocketInputLine_State: Unexpected State:", lines) } } @@ -49,7 +48,7 @@ func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) { func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *zmq.Socket) { //, brn *brain.Brain) { ts := time.Now().Unix() if len(lines) < 1 { return } - log.Printf("ParseSocketInputLine: %s %s",string(lines[0]), lines[1:]) + Debug_.Printf("ParseSocketInputLine: %s %s",string(lines[0]), lines[1:]) switch string(lines[0]) { case "State:": if len(lines) < 2 { return } -- 1.7.10.4