func writeToLogFile(ev *pubsub.Event) { name := ev.Topic device := services.Config.LookupDeviceName(ev) if device != "" { ev.Fields["device"] = device } p := path.Join(logDir, name) ensureDirectory(p) p = path.Join(p, "data.log") // reopen the log file each time, so that log rotation can happen in the // background. // TODO: could this be done more smartly by checking inode and only // reopening when it changes? fio, err := os.OpenFile(p, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660) defer fio.Close() if err != nil { log.Println("Couldn't write file:", err) return } fio.Write(ev.Bytes()) fio.WriteString("\n") }
// Emit an event func (pub *Publisher) Emit(ev *pubsub.Event) { // put all topics under gohome/ topic := "gohome/" + ev.Topic r := pub.client.Publish(MQTT.QOS_ONE, topic, ev.Bytes()) <-r }