From: Bernhard Tittelbach Date: Fri, 4 Oct 2013 17:43:13 +0000 (+0000) Subject: better logging in r3-netstatus X-Git-Url: https://git.realraum.at/?a=commitdiff_plain;h=b7182b7f1d5dae86d81310beaa6ce6d693696572;p=svn42.git better logging in r3-netstatus --- diff --git a/go/r3-netstatus/logging.go b/go/r3-netstatus/logging.go new file mode 100644 index 0000000..b805aba --- /dev/null +++ b/go/r3-netstatus/logging.go @@ -0,0 +1,31 @@ +// (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(&NullWriter{}, "", 0) + 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() { + Syslog_ = log.New(os.Stdout, "", log.LstdFlags) + Debug_ = log.New(os.Stderr, "DEBUG", log.LstdFlags) +} \ No newline at end of file diff --git a/go/r3-netstatus/main.go b/go/r3-netstatus/main.go index a2917a5..b65e96e 100644 --- a/go/r3-netstatus/main.go +++ b/go/r3-netstatus/main.go @@ -27,6 +27,7 @@ var ( r3eventssub_port_ string button_press_timeout_ int64 = 3600 brain_connect_addr_ string + enable_syslog_, enable_debug_ bool ) //------- @@ -38,7 +39,11 @@ func init() { flag.StringVar(&xmpp_state_save_dir_,"xstatedir","/flash/var/lib/r3netstatus/", "Directory to save XMPP bot state in") flag.StringVar(&r3eventssub_port_, "eventsubport", "tcp://wuzzler.realraum.at:4244", "zmq address to subscribe r3events") flag.StringVar(&brain_connect_addr_, "brainconnect", "tcp://wuzzler.realraum.at:4245", "address to ask about most recent stored events") + flag.BoolVar(&enable_syslog_, "syslog", false, "enable logging to syslog") + flag.BoolVar(&enable_debug_, "debug", false, "enable debug logging") flag.Parse() + if enable_syslog_ { LogEnableSyslog(); r3xmppbot.LogEnableSyslog() } + if enable_debug_ { LogEnableDebuglog(); r3xmppbot.LogEnableDebuglog() } } //------- @@ -61,7 +66,7 @@ func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface defer func() { if x := recover(); x != nil { - fmt.Printf("handleIncomingXMPPStanzas: run time panic: %v", x) + Debug_.Printf("handleIncomingXMPPStanzas: run time panic: %v", x) ps.Unsub(events, "presence","door","buttons","updateinterval") close(xmpp_presence_events_chan_) } @@ -78,7 +83,7 @@ func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface xmpp_presence_events_chan_ <- r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe, "Nobody is here"} for eventinterface := range(events) { - fmt.Println("event2xmpp", eventinterface) + Debug_.Println("event2xmpp", eventinterface) switch event := eventinterface.(type) { case r3events.PresenceUpdate: present = event.Present @@ -138,12 +143,12 @@ func main() { } else { fmt.Println(xmpperr) fmt.Println("XMPP Bot disabled") + Syslog_.Printf("XMPP Bot disabled due to error: %s", xmpperr) } // --- get update on most recent events --- answ := ZmqsAskQuestionsAndClose(zmqctx, brain_connect_addr_, [][][]byte{[][]byte{[]byte("DoorLockUpdate")}, [][]byte{[]byte("DoorAjarUpdate")}, [][]byte{[]byte("DoorCommandEvent")}, [][]byte{[]byte("PresenceUpdate")}, [][]byte{[]byte("IlluminationSensorUpdate")}, [][]byte{[]byte("TempSensorUpdate")}}) for _, a := range(answ) { - //~ fmt.Println("recent event:", a) ParseZMQr3Event(a, ps) } diff --git a/go/r3-netstatus/r3xmppbot/logging.go b/go/r3-netstatus/r3xmppbot/logging.go new file mode 100644 index 0000000..83a7dad --- /dev/null +++ b/go/r3-netstatus/r3xmppbot/logging.go @@ -0,0 +1,31 @@ +// (c) Bernhard Tittelbach, 2013 + +package r3xmppbot + +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(&NullWriter{}, "", 0) + 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() { + Syslog_ = log.New(os.Stdout, "", log.LstdFlags) + Debug_ = log.New(os.Stderr, "DEBUG", log.LstdFlags) +} \ No newline at end of file diff --git a/go/r3-netstatus/r3xmppbot/r3xmppbot.go b/go/r3-netstatus/r3xmppbot/r3xmppbot.go index 9963485..7863b70 100644 --- a/go/r3-netstatus/r3xmppbot/r3xmppbot.go +++ b/go/r3-netstatus/r3xmppbot/r3xmppbot.go @@ -4,7 +4,6 @@ package r3xmppbot import ( xmpp "code.google.com/p/goexmpp" - "log" "crypto/tls" "os" "time" @@ -12,16 +11,16 @@ import ( "path" ) -type StdLogger struct { -} +//~ type StdLogger struct { +//~ } -func (s *StdLogger) Log(v ...interface{}) { - log.Println(v...) -} +//~ func (s *StdLogger) Log(v ...interface{}) { + //~ log.Println(v...) +//~ } -func (s *StdLogger) Logf(fmt string, v ...interface{}) { - log.Printf(fmt, v...) -} +//~ func (s *StdLogger) Logf(fmt string, v ...interface{}) { + //~ log.Printf(fmt, v...) +//~ } func (botdata *XmppBot) makeXMPPMessage(to string, message interface{}, subject interface{}) *xmpp.Message { @@ -143,13 +142,13 @@ type XmppBot struct { func (data RealraumXmppNotifierConfig) saveTo(filepath string) () { fh, err := os.Create(filepath) if err != nil { - log.Println(err) + Syslog_.Println(err) return } defer fh.Close() enc := json.NewEncoder(fh) if err = enc.Encode(&data); err != nil { - log.Println(err) + Syslog_.Println(err) return } } @@ -157,13 +156,13 @@ func (data RealraumXmppNotifierConfig) saveTo(filepath string) () { func (data RealraumXmppNotifierConfig) loadFrom(filepath string) () { fh, err := os.Open(filepath) if err != nil { - log.Println(err) + Syslog_.Println(err) return } defer fh.Close() dec := json.NewDecoder(fh) if err = dec.Decode(&data); err != nil { - log.Println(err) + Syslog_.Println(err) return } for to, jiddata := range data { @@ -185,7 +184,7 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc defer func() { if x := recover(); x != nil { - log.Printf("handleEventsforXMPP: run time panic: %v", x) + Syslog_.Printf("handleEventsforXMPP: run time panic: %v", x) } }() @@ -249,7 +248,6 @@ func removeJIDResource(jid string) string { func (botdata *XmppBot) isAuthenticated(jid string) bool { authtime, in_map := botdata.jid_lastauthtime_[jid] - //~ log.Println("isAuthenticated", in_map, authtime, time.Now().Unix(), auth_timeout_, time.Now().Unix() - authtime > auth_timeout_) return in_map && time.Now().Unix() - authtime < botdata.auth_timeout_ } @@ -263,7 +261,6 @@ func (botdata *XmppBot) handleIncomingMessageDialog(inmsg xmpp.Message, xmppout return } bodytext :=inmsg.Body.Chardata - //~ log.Println("Message Body:", bodytext) if botdata.isAuthenticated(inmsg.GetHeader().From) { switch bodytext { case "on", "*on*": @@ -316,7 +313,7 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm defer func() { if x := recover(); x != nil { - log.Printf("handleIncomingXMPPStanzas: run time panic: %v", x) + Syslog_.Printf("handleIncomingXMPPStanzas: run time panic: %v", x) close(jabber_events) } }() @@ -367,8 +364,6 @@ func NewStartedBot(loginjid, loginpwd, password, state_save_dir string, insecure botdata.config_file_ = path.Join(state_save_dir, "r3xmpp."+removeJIDResource(loginjid)+".json") - //~ log.Println(botdata.config_file_) - //~ logger := &StdLogger{} //~ xmpp.Debug = logger //~ xmpp.Info = logger @@ -381,13 +376,13 @@ func NewStartedBot(loginjid, loginpwd, password, state_save_dir string, insecure client_jid.Set(botdata.my_jid_) botdata.xmppclient_, err = xmpp.NewClient(client_jid, botdata.my_login_password_, nil) if err != nil { - log.Println("Error connecting to xmpp server", err) + Syslog_.Println("Error connecting to xmpp server", err) return nil, nil, err } err = botdata.xmppclient_.StartSession(true, &xmpp.Presence{}) if err != nil { - log.Println("'Error StartSession:", err) + Syslog_.Println("'Error StartSession:", err) return nil, nil, err } diff --git a/go/r3-netstatus/sockettoevent.go b/go/r3-netstatus/sockettoevent.go index d3ffd22..463838c 100644 --- a/go/r3-netstatus/sockettoevent.go +++ b/go/r3-netstatus/sockettoevent.go @@ -9,12 +9,11 @@ import ( //~ "./brain" //~ "net" "encoding/json" - "log" r3events "svn.spreadspace.org/realraum/go.svn/r3-eventbroker_zmq/r3events" ) func ParseZMQr3Event(lines [][]byte, ps *pubsub.PubSub) { //, brn *brain.Brain) { - //log.Printf("ParseZMQr3Event: len: %d lines: %s", len(lines), lines) + //Debug_.Printf("ParseZMQr3Event: len: %d lines: %s", len(lines), lines) if len(lines) != 2 { return } @@ -40,7 +39,6 @@ func ParseZMQr3Event(lines [][]byte, ps *pubsub.PubSub) { //, brn *brain.Brain) err := json.Unmarshal(lines[1],evnt) if err == nil {ps.Pub(*evnt, "buttons")} case "DoorLockUpdate" : - log.Print("DoorLockUpdate received") evnt := new(r3events.DoorLockUpdate) err := json.Unmarshal(lines[1],evnt) if err == nil {ps.Pub(*evnt, "door")} diff --git a/go/r3-netstatus/webstatus.go b/go/r3-netstatus/webstatus.go index 4ebfbfb..53ff92d 100644 --- a/go/r3-netstatus/webstatus.go +++ b/go/r3-netstatus/webstatus.go @@ -8,7 +8,6 @@ import ( "regexp" "net/http" "net/url" - "log" "time" r3events "svn.spreadspace.org/realraum/go.svn/r3-eventbroker_zmq/r3events" ) @@ -52,7 +51,7 @@ func publishStateToWeb() { updateStatusString() jsondata_b, err := spaceapidata.MakeJSON() if err != nil { - log.Println("Error:", err) + Syslog_.Println("Error:", err) return } //jsondata_b := re_querystresc_.ReplaceAllFunc(jsondata_b, func(in []byte) []byte { @@ -64,7 +63,7 @@ func publishStateToWeb() { jsondata := url.QueryEscape(string(jsondata_b)) resp, err := http.Get("http://www.realraum.at/cgi/status.cgi?pass=jako16&set=" + jsondata) if err != nil { - log.Println("Error publishing realraum info", err) + Syslog_.Println("Error publishing realraum info", err) return } resp.Body.Close() @@ -74,7 +73,7 @@ func EventToWeb(ps *pubsub.PubSub) { events := ps.Sub("presence","door","sensors","buttons","updateinterval") for eventinterface := range(events) { - //log.Printf("EventToWeb: %s" , eventinterface) + //Debug_.Printf("EventToWeb: %s" , eventinterface) switch event := eventinterface.(type) { case r3events.TimeTick: publishStateToWeb()