Andrey Smirnov 41d5f7859a chore: update golangci-lint to 1.28.3
Fixes #2272

`gofumpt` is now included into `golangci-lint`, but not the
`gofumports`, so we keep it using it as separate binary, but we keep
versions in sync with `golangci-lint`.

This contains fixes from:

* `gofumpt` (automated, mostly around octal constants)
* `exhaustive` in `switch` statements
* `noctx` (adding context with default timeout to http requests)

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2020-07-16 08:05:42 -07:00

49 lines
1.1 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Package kmsg provides access to kernel log.
//
// nolint: dupl
package kmsg
import (
"fmt"
"io"
"log"
"os"
"golang.org/x/sys/unix"
)
// SetupLogger configures the logger to write to the kernel ring buffer via
// /dev/kmsg.
//
// If logger is nil, default `log` logger is redirectred.
//
// If extraWriter is not nil, logs will be copied to it as well.
func SetupLogger(logger *log.Logger, prefix string, extraWriter io.Writer) error {
kmsg, err := os.OpenFile("/dev/kmsg", os.O_RDWR|unix.O_CLOEXEC|unix.O_NONBLOCK|unix.O_NOCTTY, 0o666)
if err != nil {
return fmt.Errorf("failed to open /dev/kmsg: %w", err)
}
var writer io.Writer = &Writer{KmsgWriter: kmsg}
if extraWriter != nil {
writer = io.MultiWriter(writer, extraWriter)
}
if logger != nil {
logger.SetOutput(writer)
logger.SetPrefix(prefix + " ")
logger.SetFlags(0)
} else {
log.SetOutput(writer)
log.SetPrefix(prefix + " ")
log.SetFlags(0)
}
return nil
}