dmesg.go 867 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2020 The Libc Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build libc.dmesg
  5. // +build libc.dmesg
  6. package libc // import "modernc.org/libc"
  7. import (
  8. "fmt"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "time"
  13. )
  14. const dmesgs = true
  15. var (
  16. pid = fmt.Sprintf("[%v %v] ", os.Getpid(), filepath.Base(os.Args[0]))
  17. logf *os.File
  18. )
  19. func init() {
  20. var err error
  21. if logf, err = os.OpenFile("/tmp/libc.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY|os.O_SYNC, 0644); err != nil {
  22. panic(err.Error())
  23. }
  24. dmesg("%v", time.Now())
  25. }
  26. func dmesg(s string, args ...interface{}) {
  27. if s == "" {
  28. s = strings.Repeat("%v ", len(args))
  29. }
  30. s = fmt.Sprintf(pid+s, args...)
  31. switch {
  32. case len(s) != 0 && s[len(s)-1] == '\n':
  33. fmt.Fprint(logf, s)
  34. default:
  35. fmt.Fprintln(logf, s)
  36. }
  37. }