aboutsummaryrefslogblamecommitdiff
path: root/clog/clog.go
blob: 04835859fd1d5e5fbdb55b0a31a520fc485181aa (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                 







                                     










                                                                 

                 
                               


                                        




                                               


                                        
                                           
 

                                       
                                         
 

                                                  
                                             

                     
// Package clog provides basic logging utilities.
package clog

import (
	"fmt"
	"os"
)

func log(str string, keyvals []any) {
	fmt.Fprint(os.Stderr, str)
	if len(keyvals) != 0 {
		fmt.Fprint(os.Stderr, " ")
		for i, j := range keyvals {
			if i&1 == 0 {
				fmt.Fprintf(os.Stderr, "%v=", j)
			} else if i == len(keyvals)-1 {
				fmt.Fprintf(os.Stderr, "%#v", j)
			} else {
				fmt.Fprintf(os.Stderr, "%#v ", j)
			}
		}
	}
	fmt.Fprintln(os.Stderr)
}

func Error(str string, keyvals ...any) {
	log("\x1b[1;91mERROR: "+str, keyvals)
}

func Warn(str string, keyvals ...any) {
	log("\x1b[1;93mWARNING: "+str, keyvals)
}

func Debug(str string, keyvals ...any) {
	log("\x1b[90mDEBUG: "+str, keyvals)
}

func Info(str string, keyvals ...any) {
	log("\x1b[0mINFO: "+str, keyvals)
}

func Fatal(exit int, str string, keyvals ...any) {
	log("\x1b[1;35mFATAL: "+str, keyvals)
	os.Exit(exit)
}