--- /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(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
"os"
"flag"
//~ "time"
- "log/syslog"
- "log"
pubsub "github.com/tuxychandru/pubsub"
"./r3events"
)
keylookup_addr_ string
brain_listen_addr_ string
use_syslog_ bool
- Syslog_ *log.Logger
+ enable_debuglog_ bool
)
func usage() {
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()
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)
//~ 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 {
//~ "./brain"
pubsub "github.com/tuxychandru/pubsub"
"./r3events"
- //~ "log"
)
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) {
} 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")
import (
zmq "github.com/vaughan0/go-zmq"
"./r3events"
- "log"
)
type hippocampus map[string]interface{}
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)
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")}
//~ "./brain"
pubsub "github.com/tuxychandru/pubsub"
zmq "github.com/vaughan0/go-zmq"
- "log"
"./r3events"
)
case "opening":
case "closing":
default:
- if Syslog_ != nil {Syslog_.Print("parseSocketInputLine_State: Unexpected State:", lines)}
+ Syslog_.Print("parseSocketInputLine_State: Unexpected State:", lines)
}
}
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 }