move to zerolog

This commit is contained in:
Mikaël Cluseau
2024-01-20 16:41:54 +01:00
parent 5ab8b74041
commit 6bf1d1ccf2
18 changed files with 205 additions and 170 deletions

47
main.go
View File

@ -3,7 +3,6 @@ package main
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
@ -12,10 +11,12 @@ import (
"time"
"github.com/pkg/term/termios"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/term"
"novit.nc/direktil/initrd/colorio"
"novit.nc/direktil/initrd/shio"
"novit.tech/direktil/initrd/colorio"
"novit.tech/direktil/initrd/shio"
)
const (
@ -41,11 +42,13 @@ func newPipe() (io.ReadCloser, io.WriteCloser) {
}
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
switch baseName := filepath.Base(os.Args[0]); baseName {
case "init":
runInit()
default:
log.Fatal("unknown sub-command: ", baseName)
log.Fatal().Msgf("unknown sub-command: %q", baseName)
}
}
@ -57,18 +60,19 @@ func runInit() {
runtime.LockOSThread()
if pid := os.Getpid(); pid != 1 {
log.Fatal("init must be PID 1, not ", pid)
}
// move log to shio
go io.Copy(os.Stdout, stdout.NewReader())
log.SetOutput(stderr)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: stderr})
// check the PID is 1
if pid := os.Getpid(); pid != 1 {
log.Fatal().Int("pid", pid).Msg("init must be PID 1")
}
// copy os.Stdin to my stdin pipe
go io.Copy(stdinPipe, os.Stdin)
log.Print("Welcome to ", VERSION)
log.Info().Msg("Welcome to " + VERSION)
// essential mounts
mount("none", "/proc", "proc", 0, "")
@ -78,7 +82,7 @@ func runInit() {
// get the "boot version"
bootVersion = param("version", "current")
log.Printf("booting system %q", bootVersion)
log.Info().Msgf("booting system %q", bootVersion)
os.Setenv("PATH", "/usr/bin:/bin:/usr/sbin:/sbin")
@ -107,19 +111,20 @@ func layerPath(name string) string {
}
func fatal(v ...interface{}) {
log.Print("*** FATAL ***")
log.Print(v...)
log.Error().Msg("*** FATAL ***")
log.Error().Msg(fmt.Sprint(v...))
die()
}
func fatalf(pattern string, v ...interface{}) {
log.Print("*** FATAL ***")
log.Printf(pattern, v...)
log.Error().Msg("*** FATAL ***")
log.Error().Msgf(pattern, v...)
die()
}
func die() {
log.SetOutput(os.Stderr)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
stdout.Close()
stdin.Close()
stdinPipe.Close()
@ -138,13 +143,13 @@ mainLoop:
deadline := time.Now().Add(time.Minute)
os.Stdin.SetReadDeadline(deadline)
termios.Tcflush(os.Stdin.Fd(), termios.TCIFLUSH)
term.MakeRaw(int(os.Stdin.Fd()))
termios.Tcflush(os.Stdin.Fd(), termios.TCIFLUSH)
b := make([]byte, 1)
_, err := os.Stdin.Read(b)
if err != nil {
log.Print("failed to read from stdin: ", err)
log.Error().Err(err).Msg("failed to read from stdin")
time.Sleep(5 * time.Second)
syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)
}
@ -184,10 +189,10 @@ mainLoop:
}
continue mainLoop
}
log.Print("failed to find a shell!")
log.Error().Msg("failed to find a shell!")
default:
log.Printf("unknown choice: %q", string(b))
log.Error().Msgf("unknown choice: %q", string(b))
}
}
}
@ -212,7 +217,7 @@ func mount(source, target, fstype string, flags uintptr, data string) {
if err := syscall.Mount(source, target, fstype, flags, data); err != nil {
fatalf("mount %q %q -t %q -o %q failed: %v", source, target, fstype, data, err)
}
log.Printf("mounted %q", target)
log.Info().Str("target", target).Msg("mounted")
}
func cp(srcPath, dstPath string) {