gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

dmesg.go (1253B)


      1 // Copyright 2023 The Sqlite 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 sqlite.dmesg
      6 // +build sqlite.dmesg
      7 
      8 package sqlite // import "modernc.org/sqlite"
      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 	t := time.Now()
     27 	// 01/02 03:04:05PM '06 -0700
     28 	dn := t.Format("sqlite-dmesg-2006-01-02-03-150405")
     29 	dn = filepath.Join(os.TempDir(), fmt.Sprintf("%s.%d", dn, os.Getpid()))
     30 	if err := os.Mkdir(dn, 0770); err != nil {
     31 		panic(err.Error())
     32 	}
     33 
     34 	fn := filepath.Join(dn, "dmesg.log")
     35 	var err error
     36 	if logf, err = os.OpenFile(fn, os.O_APPEND|os.O_CREATE|os.O_WRONLY|os.O_SYNC, 0644); err != nil {
     37 		panic(err.Error())
     38 	}
     39 
     40 	dmesg("%v", time.Now())
     41 	fmt.Fprintf(os.Stderr, "debug messages in %s\n", fn)
     42 }
     43 
     44 func dmesg(s string, args ...interface{}) {
     45 	if s == "" {
     46 		s = strings.Repeat("%v ", len(args))
     47 	}
     48 	s = fmt.Sprintf(pid+s, args...)
     49 	s += fmt.Sprintf(" (%v: %v:)", origin(3), origin(2))
     50 	switch {
     51 	case len(s) != 0 && s[len(s)-1] == '\n':
     52 		fmt.Fprint(logf, s)
     53 	default:
     54 		fmt.Fprintln(logf, s)
     55 	}
     56 }