0

I am making a logging function for logging data arrives from CANBUS.

I have used prints to see if I can enter every condition and I can recieve I am in message but I can't append and log in to my file that I have created. All not declared variables are globals.


package main

import (
    Filter "go/src/Filter/FiltreLib"
    "flag"
    "fmt"
    "github.com/go-daq/canbus"
    "log"
    "os"
    "strconv"
    "sync"
    "time"
)


func main(){
sck, err := canbus.New()
        if err != nil {
            log.Fatal(err)
        }
        defer sck.Close()

        addr := "can 0"
        err = sck.Bind(addr)
        if err != nil {
            log.Fatalf("error binding to [%s]: %v\n", addr, err)
        }
        state = "s" //Silent as default

        go func() {
            for {
                fmt.Scanln(&newstate)
                switch true {
                case state == "s" && "v" == newstate: // if silence -> verbose
                    state = "v"
                    fmt.Println(state)
                    break
                case state == "v" && "s" == newstate: // if Verbose -> Silence
                    state = "s"
                    fmt.Println(state)
                    break
                }
            }
        }()
        for {
            CANtoDATA(sck, 4)
        }
}
func CANtoDATA(sck *canbus.Socket, IDDS int)() { //Takes Frames from can and logs them in xlsx

    id,msg,err = sck.Recv() //gets new frame
    FileCreatorWriter(IDDS,msg)
    framestoString(IDDS)

}

func FileCreatorWriter(DS int, data []byte)(err error){
    if datecheck != time.Now().Format("01-02-2006") {
        token = false
    }
    if token == false {
        datecheck = time.Now().Format("01-02-2006")
        date = time.Now().Format(" 01-02-2006 - 15:04:05")
        f, err = os.OpenFile("IDDS: "+strconv.Itoa(DS)+" - "+date+".txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
        if err != nil {
            log.Fatalf("error opening file: %v", err)
        }
        defer f.Close()

        if err != nil {
            fmt.Printf(err.Error())
        }
        token = true
    }
    _,frameID = CityFilter.FrameIden(id)
    IDds := CityFilter.ReadIDDS(Converter(data))

    if  DS == IDds && 1 == frameID {
        fmt.Printf("I am in!!!!!!")
        log.SetOutput(f)
        log.Println(datamap)
    }
    return
}


I want to see the data map logged in the created file and append with every iteration.

  • You haven't changed the logger destination so it's not logging to file, it's logging to stderr (unless you're piping stderr to file). – Adrian Apr 09 '19 at 16:00
  • @Adrian `log.SetOutput(f)` sets destionation to created file but I have no logs whatsoever. I was inspired by this : https://stackoverflow.com/questions/19965795/go-golang-write-log-to-file – Deniz Tohumcu Apr 09 '19 at 16:06
  • Ah I just saw that, but it happens after almost all of your logging calls. – Adrian Apr 09 '19 at 16:24
  • @Adrian Yeah, every single time, I tried without the `defer f.close` now it works but why would the defer function would block my destination? – Deniz Tohumcu Apr 09 '19 at 16:37

0 Answers0