dmesgon.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2024 The tk9.0-go 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 tk.dmesg
  5. // +build tk.dmesg
  6. package tk9_0 // import "modernc.org/tk9.0"
  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. fn := filepath.Join(os.TempDir(), fmt.Sprintf("%s-%v-%s", filepath.Base(os.Args[0]), os.Getpid(), time.Now().Format("20060102-150405")))
  21. var err error
  22. if logf, err = os.OpenFile(fn, os.O_APPEND|os.O_CREATE|os.O_WRONLY|os.O_SYNC, 0660); err != nil {
  23. panic(err.Error())
  24. }
  25. dmesg("enter")
  26. fmt.Println(fn)
  27. }
  28. func dmesg(s string, args ...interface{}) {
  29. if s == "" {
  30. s = strings.Repeat("%v ", len(args))
  31. }
  32. s = fmt.Sprintf(pid+s, args...)
  33. s = fmt.Sprintf("%s %v", s, []string{origin(2), origin(3), origin(4)})
  34. switch {
  35. case len(s) != 0 && s[len(s)-1] == '\n':
  36. fmt.Fprint(logf, s)
  37. default:
  38. fmt.Fprintln(logf, s)
  39. }
  40. }