Skip to content

Commit

Permalink
refact(log): Use a replaceable logger
Browse files Browse the repository at this point in the history
Previously it was impossible to mute fiano without
affecting the global logger "log". Now it is possible.

Signed-off-by: Dmitrii Okunev <[email protected]>
  • Loading branch information
xaionaro committed Apr 23, 2021
1 parent 91b79e9 commit 86d0011
Show file tree
Hide file tree
Showing 20 changed files with 127 additions and 59 deletions.
14 changes: 7 additions & 7 deletions cmds/fmap/fmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ import (
"hash"
"io"
"io/ioutil"
"log"
"os"
"strconv"
"text/template"

"github.com/linuxboot/fiano/pkg/fmap"
"github.com/linuxboot/fiano/pkg/log"
)

var cmds = map[string]struct {
Expand Down Expand Up @@ -308,7 +308,7 @@ func verify(a cmdArgs) error {
for i, area := range a.f.Areas {
if area.Offset+area.Size > a.f.Size {
err = errors.New("Invalid flash map")
log.Printf("Area %d is out of range", i)
log.Errorf("Area %d is out of range", i)
}
}
return err
Expand All @@ -330,11 +330,11 @@ func main() {
}
cmd, ok := cmds[os.Args[1]]
if !ok {
log.Printf("Invalid command %#v\n", os.Args[1])
log.Errorf("Invalid command %#v\n", os.Args[1])
printUsage()
}
if len(os.Args) != cmd.nArgs+3 {
log.Printf("Expected %d arguments, got %d\n", cmd.nArgs+3, len(os.Args))
log.Errorf("Expected %d arguments, got %d\n", cmd.nArgs+3, len(os.Args))
printUsage()
}

Expand All @@ -348,7 +348,7 @@ func main() {
// Open file.
r, err := os.Open(os.Args[len(os.Args)-1])
if err != nil {
log.Fatal(err)
log.Fatalf("%v", err)
}
a.r = r
defer r.Close()
Expand All @@ -359,13 +359,13 @@ func main() {
// Parse fmap.
f, m, err := fmap.Read(a.r)
if err != nil {
log.Fatal(err)
log.Fatalf("%v", err)
}
a.f, a.m = f, m
}

// Execute command.
if err := cmd.f(a); err != nil {
log.Fatal(err)
log.Fatalf("%v", err)
}
}
10 changes: 5 additions & 5 deletions cmds/fspinfo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"flag"
"fmt"
"io/ioutil"
"log"

"github.com/linuxboot/fiano/pkg/fsp"
"github.com/linuxboot/fiano/pkg/log"
"github.com/linuxboot/fiano/pkg/uefi"
)

Expand Down Expand Up @@ -54,20 +54,20 @@ func extractFirstFSPHeader(b []byte) (*fsp.InfoHeaderRev3, error) {
func main() {
flag.Parse()
if flag.Arg(0) == "" {
log.Fatal("Error: missing file name")
log.Fatalf("missing file name")
}
data, err := ioutil.ReadFile(flag.Arg(0))
if err != nil {
log.Fatalf("Error: cannot read input file: %v", err)
log.Fatalf("cannot read input file: %v", err)
}
hdr, err := extractFirstFSPHeader(data)
if err != nil {
log.Fatal(err)
log.Fatalf("%v", err)
}

j, err := json.MarshalIndent(hdr, "", " ")
if err != nil {
log.Fatalf("Error: cannot marshal JSON: %v", err)
log.Fatalf("cannot marshal JSON: %v", err)
}
if *flagJSON {
fmt.Println(string(j))
Expand Down
14 changes: 7 additions & 7 deletions cmds/glzma/glzma.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ package main
import (
"flag"
"io/ioutil"
"log"

"github.com/linuxboot/fiano/pkg/compression"
"github.com/linuxboot/fiano/pkg/log"
)

var (
Expand All @@ -33,13 +33,13 @@ func main() {
flag.Parse()

if *d == *e {
log.Fatal("either decode (-d) or encode (-e) must be set")
log.Fatalf("either decode (-d) or encode (-e) must be set")
}
if *o == "" {
log.Fatal("output file must be set")
log.Fatalf("output file must be set")
}
if flag.NArg() != 1 {
log.Fatal("expected one input file")
log.Fatalf("expected one input file")
}

var compressor compression.Compressor
Expand All @@ -58,13 +58,13 @@ func main() {

in, err := ioutil.ReadFile(flag.Args()[0])
if err != nil {
log.Fatal(err)
log.Fatalf("%v", err)
}
out, err := op(in)
if err != nil {
log.Fatal(err)
log.Fatalf("%v", err)
}
if err := ioutil.WriteFile(*o, out, 0666); err != nil {
log.Fatal(err)
log.Fatalf("%v", err)
}
}
6 changes: 3 additions & 3 deletions cmds/guid2english/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ package main
import (
"flag"
"io"
"log"
"os"
"text/template"

"github.com/linuxboot/fiano/pkg/guid2english"
"github.com/linuxboot/fiano/pkg/log"
"golang.org/x/text/transform"
)

Expand All @@ -48,12 +48,12 @@ func main() {
}
defer r.Close()
default:
log.Fatal("Error: At most 1 positional arguments expected")
log.Fatalf("At most 1 positional arguments expected")
}

t, err := template.New("guid2english").Parse(*tmpl)
if err != nil {
log.Fatalf("Error: Template not valid: %v", err)
log.Fatalf("Template not valid: %v", err)
}

trans := guid2english.New(guid2english.NewTemplateMapper(t))
Expand Down
4 changes: 2 additions & 2 deletions cmds/utk/utk.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ package main
import (
"flag"
"fmt"
"log"

"github.com/linuxboot/fiano/pkg/log"
"github.com/linuxboot/fiano/pkg/utk"
"github.com/linuxboot/fiano/pkg/visitors"
)
Expand All @@ -73,6 +73,6 @@ func init() {
func main() {
flag.Parse()
if err := utk.Run(flag.Args()...); err != nil {
log.Fatal(err)
log.Fatalf("%v", err)
}
}
5 changes: 3 additions & 2 deletions pkg/fsp/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"bytes"
"encoding/binary"
"fmt"
"log"
"strings"

"github.com/linuxboot/fiano/pkg/log"
)

// TODO support FSP versions < 2.0
Expand Down Expand Up @@ -222,7 +223,7 @@ func NewInfoHeader(b []byte) (*InfoHeaderRev3, error) {
}
// reserved bytes must be zero'ed
if !bytes.Equal(f.Reserved1[:], []byte{0, 0}) {
log.Printf("warning: reserved bytes must be zero, got %v", f.Reserved1)
log.Warnf("reserved bytes must be zero, got %v", f.Reserved1)
}
// check spec version
// TODO currently, only FSP 2.0 is supported
Expand Down
5 changes: 3 additions & 2 deletions pkg/guid/guid.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"log"
"strings"

"github.com/linuxboot/fiano/pkg/log"
)

const (
Expand Down Expand Up @@ -66,7 +67,7 @@ func Parse(s string) (*GUID, error) {
func MustParse(s string) *GUID {
guid, err := Parse(s)
if err != nil {
log.Fatal(err)
log.Fatalf("%v", err)
}
return guid
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/guid2english/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ package guid2english

import (
"bytes"
"log"
"regexp"
"text/template"

"github.com/linuxboot/fiano/pkg/guid"
"github.com/linuxboot/fiano/pkg/knownguids"
"github.com/linuxboot/fiano/pkg/log"
"golang.org/x/text/transform"
)

Expand Down Expand Up @@ -66,7 +66,7 @@ func (f *TemplateMapper) Map(g guid.GUID) []byte {
if err != nil {
// There is likely a bug in the template. We do not want to
// interrupt the byte stream, so just log the error.
log.Printf("Error in template: %v", err)
log.Errorf("Error in template: %v", err)
}
return b.Bytes()
}
Expand Down
65 changes: 65 additions & 0 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2021 the LinuxBoot Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package log

import (
"log"
"os"
)

// Logger describes a logger to be used in fiano.
type Logger interface {
// Warnf logs an warning message.
Warnf(format string, args ...interface{})

// Errorf logs an error message.
Errorf(format string, args ...interface{})

// Fatalf logs a fatal message and immediately exits the application
// with os.Exit.
Fatalf(format string, args ...interface{})
}

// DefaultLogger is the logger used by default everywhere within fiano.
var DefaultLogger Logger

func init() {
DefaultLogger = logWrapper{Logger: log.New(os.Stderr, "", log.LstdFlags)}
}

type logWrapper struct {
Logger *log.Logger
}

// Warnf implements Logger.
func (logger logWrapper) Warnf(format string, args ...interface{}) {
logger.Logger.Printf("[fiano][WARN] "+format, args...)
}

// Errorf implements Logger.
func (logger logWrapper) Errorf(format string, args ...interface{}) {
logger.Logger.Printf("[fiano][ERROR] "+format, args...)
}

// Fatalf implements Logger.
func (logger logWrapper) Fatalf(format string, args ...interface{}) {
logger.Logger.Fatalf("[fiano][FATAL] "+format, args...)
}

// Warnf logs an warning message.
func Warnf(format string, args ...interface{}) {
DefaultLogger.Warnf(format, args...)
}

// Errorf logs an error message.
func Errorf(format string, args ...interface{}) {
DefaultLogger.Errorf(format, args...)
}

// Fatalf logs a fatal message and immediately exits the application
// with os.Exit (which is expected to be called by the DefaultLogger.Fatalf).
func Fatalf(format string, args ...interface{}) {
DefaultLogger.Fatalf(format, args...)
}
4 changes: 2 additions & 2 deletions pkg/uefi/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"bytes"
"encoding/binary"
"fmt"
"log"

"github.com/linuxboot/fiano/pkg/guid"
"github.com/linuxboot/fiano/pkg/log"
)

// FVFileType represents the different types possible in an EFI file.
Expand Down Expand Up @@ -450,7 +450,7 @@ func NewFile(buf []byte) (*File, error) {
if f.Header.Type == FVFileTypeRaw && f.Header.GUID == *NVAR {
ns, err := NewNVarStore(f.buf[f.DataOffset:])
if err != nil {
log.Printf("error parsing NVAR store in file %v: %v", f.Header.GUID, err)
log.Errorf("error parsing NVAR store in file %v: %v", f.Header.GUID, err)
}
// Note that ns is nil if there was an error, so this assign is fine either way.
f.NVarStore = ns
Expand Down
4 changes: 2 additions & 2 deletions pkg/uefi/firmwarevolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"encoding/binary"
"errors"
"fmt"
"log"

"github.com/linuxboot/fiano/pkg/guid"
"github.com/linuxboot/fiano/pkg/log"
)

// FirmwareVolume constants
Expand Down Expand Up @@ -273,7 +273,7 @@ func NewFirmwareVolume(data []byte, fvOffset uint64, resizable bool) (*FirmwareV
// Start from the end of the fv header.
// Test if the fv type is supported.
if _, ok := supportedFVs[fv.FileSystemGUID]; !ok {
log.Printf("warning unsupported fv type %v,%v not parsing it", fv.FileSystemGUID.String(), fv.FVType)
log.Warnf("unsupported fv type %v,%v not parsing it", fv.FileSystemGUID.String(), fv.FVType)
return &fv, nil
}
lh := fv.Length - FileHeaderMinLength
Expand Down
5 changes: 3 additions & 2 deletions pkg/uefi/firmwarevolume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ package uefi
import (
"fmt"
"io/ioutil"
"log"
"testing"

"github.com/linuxboot/fiano/pkg/log"
)

var (
Expand All @@ -21,7 +22,7 @@ func init() {
var err error
sampleFV, err = ioutil.ReadFile("../../integration/roms/ovmfSECFV.fv")
if err != nil {
log.Fatal(err)
log.Fatalf("%v", err)
}
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/uefi/meregion.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"log"

"github.com/linuxboot/fiano/pkg/log"
)

// ME Partition parsing, the goal is to spot a padding in the ME Region
Expand Down Expand Up @@ -192,7 +193,7 @@ func NewMERegion(buf []byte, r *FlashRegion, rt FlashRegionType) (Region, error)
copy(rr.buf, buf)
fp, err := NewMEFPT(buf)
if err != nil {
log.Printf("error parsing ME Flash Partition Table: %v", err)
log.Errorf("error parsing ME Flash Partition Table: %v", err)
return rr, nil
}
rr.FPT = fp
Expand Down
Loading

0 comments on commit 86d0011

Please sign in to comment.