2020-01-25 01:15:56 -05:00
|
|
|
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
|
2020-05-12 16:39:08 -04:00
|
|
|
package boba
|
2020-01-25 01:15:56 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"log/syslog"
|
2020-05-21 16:56:47 -04:00
|
|
|
"os"
|
2020-01-25 01:15:56 -05:00
|
|
|
)
|
|
|
|
|
2020-05-21 16:56:47 -04:00
|
|
|
// LogToFile sets up default logging to log to a file This is helpful as we
|
|
|
|
// can't print to the terminal since our TUI is occupying it. If the file
|
|
|
|
// doesn't exist it will be created.
|
|
|
|
//
|
|
|
|
// Don't forget to close the file when you're done with it.
|
|
|
|
//
|
|
|
|
// f, err := LogToFile("debug.log", "debug")
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println("fatal:", err)
|
|
|
|
// os.Exit(1)
|
|
|
|
// }
|
|
|
|
// defer f.Close()
|
|
|
|
func LogToFile(path string, prefix string) (*os.File, error) {
|
|
|
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
log.SetOutput(f)
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2020-01-25 01:15:56 -05:00
|
|
|
// UseSysLog sets up logging to log the system log. This becomes helpful when
|
2020-05-21 16:56:47 -04:00
|
|
|
// debugging since we can't print to the terminal since our TUI is occupying it.
|
2020-01-25 01:15:56 -05:00
|
|
|
//
|
|
|
|
// On macOS this is a just a matter of: tail -f /var/log/system.log
|
2020-05-21 16:56:47 -04:00
|
|
|
// On Linux this varies depending on distribution.
|
2020-01-25 01:15:56 -05:00
|
|
|
func UseSysLog(programName string) error {
|
|
|
|
l, err := syslog.New(syslog.LOG_NOTICE, programName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.SetOutput(l)
|
|
|
|
return nil
|
|
|
|
}
|