1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package main
- import (
- "encoding/json"
- "fmt"
- "io"
- "log"
- "os"
- "time"
- )
- // WriteToFile will print any string of text to a file safely by
- // checking for errors and syncing at the end.
- func writeToFile(filename string, data string) error {
- file, err := os.Create(filename)
- if err != nil {
- return err
- }
- defer file.Close()
- _, err = io.WriteString(file, data)
- if err != nil {
- return err
- }
- return file.Sync()
- }
- func handlepanic() {
- if a := recover(); a != nil {
- fmt.Println("OPS!: Recovering from:", a)
- }
- }
- func saveBayesToFile() {
- log.Println("Trying to write json file")
- defer handlepanic()
- var tmpJSON []byte
- Classifier.Matrix.busy.Lock()
- defer Classifier.Matrix.busy.Unlock()
- tmpJSON, err := json.MarshalIndent(Classifier.Matrix.bScores, "", " ")
- if err != nil {
- tmpJSON = []byte(err.Error())
- }
- statsJSON, err := json.MarshalIndent(Classifier.STATS, "", " ")
- if err != nil {
- statsJSON = []byte(err.Error())
- }
- dumpfile := os.Getenv("DUMPFILE")
- if dumpfile == "" {
- dumpfile = "bayes.json"
- }
- if DebugLog {
- log.Println("DUMP: ", string(tmpJSON))
- } else {
- writeToFile(dumpfile, string(tmpJSON)+string(statsJSON))
- log.Println("File saved: ", dumpfile)
- }
- }
- func jsonEngine() {
- for {
- log.Println("Zardoz Seniority: ", ProxyFlow.seniority)
- saveBayesToFile()
- time.Sleep(1 * time.Minute)
- }
- }
- func init() {
- log.Printf("File Engine Starting")
- go jsonEngine()
- log.Printf("FIle Engine Started")
- }
|