2017-04-10 16:54:12 +00:00
|
|
|
package logging
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This database type is just for,
|
|
|
|
* - debugging without a influxconn
|
|
|
|
* - example for other developers for new databases
|
|
|
|
*/
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/database"
|
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Connection struct {
|
|
|
|
database.Connection
|
|
|
|
config Config
|
|
|
|
file *os.File
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config map[string]interface{}
|
|
|
|
|
|
|
|
func (c Config) Enable() bool {
|
|
|
|
return c["enable"].(bool)
|
|
|
|
}
|
|
|
|
func (c Config) Path() string {
|
|
|
|
return c["path"].(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-04-17 18:42:06 +00:00
|
|
|
database.RegisterAdapter("logging", Connect)
|
2017-04-10 16:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Connect(configuration interface{}) (database.Connection, error) {
|
|
|
|
var config Config
|
|
|
|
config = configuration.(map[string]interface{})
|
|
|
|
if !config.Enable() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.OpenFile(config.Path(), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Connection{config: config, file: file}, nil
|
|
|
|
}
|
|
|
|
|
2017-04-17 23:48:38 +00:00
|
|
|
func (conn *Connection) InsertNode(node *runtime.Node) {
|
|
|
|
conn.log("InsertNode: [", node.Statistics.NodeID, "] clients: ", node.Statistics.Clients.Total)
|
2017-04-10 16:54:12 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 11:55:02 +00:00
|
|
|
func (conn *Connection) InsertLink(link *runtime.Link, time time.Time) {
|
|
|
|
conn.log("InsertLink: ", link)
|
|
|
|
}
|
|
|
|
|
2017-04-17 23:48:38 +00:00
|
|
|
func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time) {
|
|
|
|
conn.log("InsertGlobals: [", time.String(), "] nodes: ", stats.Nodes, ", clients: ", stats.Clients, " models: ", len(stats.Models))
|
2017-04-10 16:54:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 18:42:06 +00:00
|
|
|
func (conn *Connection) PruneNodes(deleteAfter time.Duration) {
|
|
|
|
conn.log("PruneNodes")
|
2017-04-10 16:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Connection) Close() {
|
|
|
|
conn.log("Close")
|
|
|
|
conn.file.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Connection) log(v ...interface{}) {
|
|
|
|
log.Println(v)
|
|
|
|
conn.file.WriteString(fmt.Sprintln("[", time.Now().String(), "]", v))
|
|
|
|
}
|