coreboot/util/intelp2m/logs/logs.go
Maxim Polyakov be7eb06131 util/intelp2m: Add logger
Add logging to a file, ./logs.txt by default. --logs option is used to
override this path. Error messages are duplicated to the console.

Change-Id: I97aba146b6d8866a7fa46bac80c27c0896b26cf7
Signed-off-by: Maxim Polyakov <max.senia.poliak@gmail.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/70542
Reviewed-by: David Hendricks <david.hendricks@gmail.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
2025-03-03 21:43:28 +00:00

50 lines
1,010 B
Go

package logs
import (
"fmt"
"log"
"os"
"review.coreboot.org/coreboot.git/util/intelp2m/config/p2m"
)
var (
linfo *log.Logger
lwarning *log.Logger
lerror *log.Logger
)
func Init() (*os.File, error) {
flags := os.O_RDWR | os.O_CREATE | os.O_APPEND
file, err := os.OpenFile(p2m.Config.LogsPath, flags, 0666)
if err != nil {
fmt.Printf("logs: error opening %s file: %v", p2m.Config.LogsPath, err)
return nil, err
}
attributes := log.Lshortfile
linfo = log.New(file, "INFO: ", attributes)
lwarning = log.New(file, "WARNING: ", attributes)
lerror = log.New(file, "ERROR: ", attributes)
return file, nil
}
func Infof(format string, v ...any) {
if linfo != nil {
linfo.Output(2, fmt.Sprintf(format, v...))
}
}
func Warnf(format string, v ...any) {
if lwarning != nil {
lwarning.Output(2, fmt.Sprintf(format, v...))
}
}
func Errorf(format string, v ...any) {
if lerror != nil {
lerror.Output(2, fmt.Sprintf(format, v...))
}
log.Output(2, fmt.Sprintf(format, v...))
}