--- /dev/null
+// (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
r3eventssub_port_ string
button_press_timeout_ int64 = 3600
brain_connect_addr_ string
+ enable_syslog_, enable_debug_ bool
)
//-------
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() }
}
//-------
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_)
}
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
} 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)
}
--- /dev/null
+// (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
import (
xmpp "code.google.com/p/goexmpp"
- "log"
"crypto/tls"
"os"
"time"
"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 {
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
}
}
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 {
defer func() {
if x := recover(); x != nil {
- log.Printf("handleEventsforXMPP: run time panic: %v", x)
+ Syslog_.Printf("handleEventsforXMPP: run time panic: %v", x)
}
}()
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_
}
return
}
bodytext :=inmsg.Body.Chardata
- //~ log.Println("Message Body:", bodytext)
if botdata.isAuthenticated(inmsg.GetHeader().From) {
switch bodytext {
case "on", "*on*":
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)
}
}()
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
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
}
//~ "./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
}
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")}
"regexp"
"net/http"
"net/url"
- "log"
"time"
r3events "svn.spreadspace.org/realraum/go.svn/r3-eventbroker_zmq/r3events"
)
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 {
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()
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()