yanic/database/logging/file.go

71 lines
1.6 KiB
Go
Raw Normal View History

package logging
/**
* This database type is just for,
* - debugging without a influxconn
* - example for other developers for new databases
*/
import (
"fmt"
2022-03-28 03:56:00 +02:00
"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) Path() string {
return c["path"].(string)
}
func init() {
2017-04-17 20:42:06 +02:00
database.RegisterAdapter("logging", Connect)
}
func Connect(configuration map[string]interface{}) (database.Connection, error) {
2022-03-28 03:56:00 +02:00
config := Config(configuration)
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-18 01:48:38 +02:00
func (conn *Connection) InsertNode(node *runtime.Node) {
conn.log("InsertNode: [", node.Statistics.NodeID, "] clients: ", node.Statistics.Clients.Total)
}
func (conn *Connection) InsertLink(link *runtime.Link, time time.Time) {
conn.log("InsertLink: ", link)
}
func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time, site string, domain string) {
conn.log("InsertGlobals: [", time.String(), "] site: ", site, " domain: ", domain, ", nodes: ", stats.Nodes, ", clients: ", stats.Clients, " models: ", len(stats.Models))
}
2017-04-17 20:42:06 +02:00
func (conn *Connection) PruneNodes(deleteAfter time.Duration) {
conn.log("PruneNodes")
}
func (conn *Connection) Close() {
conn.log("Close")
conn.file.Close()
}
func (conn *Connection) log(v ...interface{}) {
2022-03-28 03:56:00 +02:00
_, err := fmt.Fprintf(conn.file, "[%s] %v", time.Now().String(), v)
if err != nil {
log.Println(err)
}
}