import (
"fmt"
+ "io/ioutil"
"log"
"os"
"path"
)
const (
- EVENT_NEW = iota
- EVENT_DEL = iota
- EVENT_MSG = iota
- EVENT_TOPIC = iota
- EVENT_WHO = iota
- EVENT_MODE = iota
- FORMAT_MSG = "[%s] <%s> %s\n"
- FORMAT_META = "[%s] * %s %s\n"
+ EventNew = iota
+ EventDel = iota
+ EventMsg = iota
+ EventTopic = iota
+ EventWho = iota
+ EventMode = iota
+ FormatMsg = "[%s] <%s> %s\n"
+ FormatMeta = "[%s] * %s %s\n"
)
// Client events going from each of client
// They can be either NEW, DEL or unparsed MSG
type ClientEvent struct {
- client *Client
- event_type int
- text string
+ client *Client
+ eventType int
+ text string
}
func (m ClientEvent) String() string {
- return string(m.event_type) + ": " + m.client.String() + ": " + m.text
+ return string(m.eventType) + ": " + m.client.String() + ": " + m.text
}
// Logging in-room events
// Logging events logger itself
// Each room's events are written to separate file in logdir
// Events include messages, topic and keys changes, joining and leaving
-func Logger(logdir string, events chan LogEvent) {
+func Logger(logdir string, events <-chan LogEvent) {
mode := os.O_CREATE | os.O_WRONLY | os.O_APPEND
perm := os.FileMode(0660)
var format string
continue
}
if event.meta {
- format = FORMAT_META
+ format = FormatMeta
} else {
- format = FORMAT_MSG
+ format = FormatMsg
}
_, err = fd.WriteString(fmt.Sprintf(format, time.Now(), event.who, event.what))
fd.Close()
// Room state events saver
// Room states shows that either topic or key has been changed
// Each room's state is written to separate file in statedir
-func StateKeeper(statedir string, events chan StateEvent) {
- mode := os.O_CREATE | os.O_TRUNC | os.O_WRONLY
- perm := os.FileMode(0660)
+func StateKeeper(statedir string, events <-chan StateEvent) {
for event := range events {
- state_path := path.Join(statedir, event.where)
- fd, err := os.OpenFile(state_path, mode, perm)
+ fn := path.Join(statedir, event.where)
+ data := event.topic + "\n" + event.key + "\n"
+ err := ioutil.WriteFile(fn, []byte(data), os.FileMode(0660))
if err != nil {
- log.Println("Can not open statefile", state_path, err)
- continue
+ log.Printf("Can not write statefile %s: %v", fn, err)
}
- fd.WriteString(event.topic + "\n" + event.key + "\n")
- fd.Close()
}
}