modernc.org/ccgo/v3@v3.16.14/lib/dmesg.go (about)

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