github.com/afumu/libc@v0.0.6/dmesg.go (about)

     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  
     5  //go:build libc.dmesg
     6  // +build libc.dmesg
     7  
     8  package libc // import "github.com/afumu/libc"
     9  
    10  import (
    11  	"fmt"
    12  	"os"
    13  	"path/filepath"
    14  	"strings"
    15  	"time"
    16  )
    17  
    18  const dmesgs = true
    19  
    20  var (
    21  	pid  = fmt.Sprintf("[%v %v] ", os.Getpid(), filepath.Base(os.Args[0]))
    22  	logf *os.File
    23  )
    24  
    25  func init() {
    26  	var err error
    27  	if logf, err = os.OpenFile("/tmp/libc.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY|os.O_SYNC, 0644); err != nil {
    28  		panic(err.Error())
    29  	}
    30  
    31  	dmesg("%v", time.Now())
    32  }
    33  
    34  func dmesg(s string, args ...interface{}) {
    35  	if s == "" {
    36  		s = strings.Repeat("%v ", len(args))
    37  	}
    38  	s = fmt.Sprintf(pid+s, args...)
    39  	switch {
    40  	case len(s) != 0 && s[len(s)-1] == '\n':
    41  		fmt.Fprint(logf, s)
    42  	default:
    43  		fmt.Fprintln(logf, s)
    44  	}
    45  }