git.gammaspectra.live/P2Pool/p2pool-observer@v0.0.0-20240403172525-d7dfbf7231c8/utils/logger.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"runtime"
     7  	"strings"
     8  	"time"
     9  )
    10  
    11  type LogLevel int
    12  
    13  var LogFile bool
    14  var LogFunc bool
    15  
    16  const (
    17  	LogLevelError = LogLevel(1 << iota)
    18  	LogLevelInfo
    19  	LogLevelNotice
    20  	LogLevelDebug
    21  )
    22  
    23  var GlobalLogLevel = LogLevelError | LogLevelInfo
    24  
    25  func Panic(v ...any) {
    26  	s := fmt.Sprint(v...)
    27  	innerPrint("", "PANIC", s)
    28  	panic(s)
    29  }
    30  
    31  func Panicf(format string, v ...any) {
    32  	s := fmt.Sprintf(format, v...)
    33  	innerPrint("", "PANIC", s)
    34  	panic(s)
    35  }
    36  
    37  func Fatalf(format string, v ...any) {
    38  	innerPrint("", "FATAL", fmt.Sprintf(format, v...))
    39  	os.Exit(1)
    40  }
    41  
    42  func Error(v ...any) {
    43  	if GlobalLogLevel&LogLevelError == 0 {
    44  		return
    45  	}
    46  	innerPrint("", "ERROR", fmt.Sprint(v...))
    47  }
    48  
    49  func Errorf(prefix, format string, v ...any) {
    50  	if GlobalLogLevel&LogLevelError == 0 {
    51  		return
    52  	}
    53  	innerPrint(prefix, "ERROR", fmt.Sprintf(format, v...))
    54  }
    55  
    56  func Print(v ...any) {
    57  	if GlobalLogLevel&LogLevelInfo == 0 {
    58  		return
    59  	}
    60  	innerPrint("", "INFO", fmt.Sprint(v...))
    61  }
    62  
    63  func Logf(prefix, format string, v ...any) {
    64  	if GlobalLogLevel&LogLevelInfo == 0 {
    65  		return
    66  	}
    67  	innerPrint(prefix, "INFO", fmt.Sprintf(format, v...))
    68  }
    69  
    70  func Noticef(format string, v ...any) {
    71  	if GlobalLogLevel&LogLevelNotice == 0 {
    72  		return
    73  	}
    74  	innerPrint("", "NOTICE", fmt.Sprintf(format, v...))
    75  }
    76  
    77  func Debugf(format string, v ...any) {
    78  	if GlobalLogLevel&LogLevelDebug == 0 {
    79  		return
    80  	}
    81  	innerPrint("", "DEBUG", fmt.Sprintf(format, v...))
    82  }
    83  
    84  func innerPrint(prefix, class, v string) {
    85  	timestamp := time.Now().UTC().Format("2006-01-02 15:04:05.000")
    86  	if LogFile {
    87  		var function string
    88  		pc, file, line, ok := runtime.Caller(2)
    89  		if !ok {
    90  			file = "???"
    91  			line = 0
    92  			pc = 0
    93  		}
    94  		short := file
    95  		for i := len(file) - 1; i > 0; i-- {
    96  			if file[i] == '/' {
    97  				short = file[i+1:]
    98  				break
    99  			}
   100  		}
   101  
   102  		if LogFunc {
   103  			if pc != 0 {
   104  				if details := runtime.FuncForPC(pc); details != nil {
   105  					function = details.Name()
   106  				}
   107  			}
   108  			shortFunc := function
   109  			for i := len(function) - 1; i > 0; i-- {
   110  				if function[i] == '/' {
   111  					shortFunc = function[i+1:]
   112  					break
   113  				}
   114  			}
   115  			funcItems := strings.Split(shortFunc, ".")
   116  			fmt.Printf("%s %s:%d:%s [%s] %s %s\n", timestamp, short, line, funcItems[len(funcItems)-1], prefix, class, strings.TrimSpace(v))
   117  		} else {
   118  			fmt.Printf("%s %s:%d [%s] %s %s\n", timestamp, short, line, prefix, class, strings.TrimSpace(v))
   119  		}
   120  	} else {
   121  		fmt.Printf("%s [%s] %s %s\n", timestamp, prefix, class, strings.TrimSpace(v))
   122  	}
   123  }