go.dedis.ch/onet/v3@v3.2.11-0.20210930124529-e36530bca7ef/log/ui.go (about)

     1  package log
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  )
     7  
     8  func lvlUI(l int, args ...interface{}) {
     9  	if isVisible(l) {
    10  		lvl(l, 3, args...)
    11  	}
    12  }
    13  
    14  // Info prints the arguments given with a 'info'-format
    15  func Info(args ...interface{}) {
    16  	lvlUI(lvlInfo, args...)
    17  }
    18  
    19  // Print directly sends the arguments to the stdout
    20  func Print(args ...interface{}) {
    21  	lvlUI(lvlPrint, args...)
    22  }
    23  
    24  // Warn prints out the warning message and quits
    25  func Warn(args ...interface{}) {
    26  	lvlUI(lvlWarning, args...)
    27  }
    28  
    29  // Error prints out the error message. If the
    30  // argument is an error, it will print it using "%+v".
    31  func Error(args ...interface{}) {
    32  	last := len(args) - 1
    33  	if last >= 0 {
    34  		err, ok := args[last].(error)
    35  		if ok {
    36  			args[last] = fmt.Sprintf("%+v", err)
    37  		}
    38  	}
    39  
    40  	lvlUI(lvlError, args...)
    41  }
    42  
    43  // Panic prints out the panic message and panics
    44  func Panic(args ...interface{}) {
    45  	lvlUI(lvlPanic, args...)
    46  	panic(fmt.Sprint(args...))
    47  }
    48  
    49  // Fatal prints out the fatal message and quits with os.Exit(1)
    50  func Fatal(args ...interface{}) {
    51  	lvlUI(lvlFatal, args...)
    52  	os.Exit(1)
    53  }
    54  
    55  // Infof takes a format-string and calls Info
    56  func Infof(f string, args ...interface{}) {
    57  	lvlUI(lvlInfo, fmt.Sprintf(f, args...))
    58  }
    59  
    60  // Printf is like Print but takes a formatting-argument first
    61  func Printf(f string, args ...interface{}) {
    62  	lvlUI(lvlPrint, fmt.Sprintf(f, args...))
    63  }
    64  
    65  // Warnf is like Warn but with a format-string
    66  func Warnf(f string, args ...interface{}) {
    67  	lvlUI(lvlWarning, fmt.Sprintf(f, args...))
    68  }
    69  
    70  // Errorf is like Error but with a format-string
    71  func Errorf(f string, args ...interface{}) {
    72  	lvlUI(lvlError, fmt.Sprintf(f, args...))
    73  }
    74  
    75  // Panicf is like Panic but with a format-string
    76  func Panicf(f string, args ...interface{}) {
    77  	lvlUI(lvlWarning, fmt.Sprintf(f, args...))
    78  	panic(args)
    79  }
    80  
    81  // Fatalf is like Fatal but with a format-string
    82  func Fatalf(f string, args ...interface{}) {
    83  	lvlUI(lvlFatal, fmt.Sprintf(f, args...))
    84  	os.Exit(-1)
    85  }
    86  
    87  // ErrFatal calls log.Fatal in the case err != nil
    88  func ErrFatal(err error, args ...interface{}) {
    89  	if err != nil {
    90  		lvlUI(lvlFatal, fmt.Sprint(args...)+" "+err.Error())
    91  		os.Exit(1)
    92  	}
    93  }
    94  
    95  // ErrFatalf will call Fatalf when the error is non-nil
    96  func ErrFatalf(err error, f string, args ...interface{}) {
    97  	if err != nil {
    98  		lvlUI(lvlFatal, fmt.Sprintf(f+" ", args...)+err.Error())
    99  		os.Exit(1)
   100  	}
   101  }
   102  
   103  // TraceID helps the tracing-simulation to know which trace it should attach
   104  // to.
   105  // The TraceID will be stored with the corresponding go-routine,
   106  // and any new go-routines spawned from the method will be attached to the
   107  // same TraceID.
   108  func TraceID(id []byte) {
   109  	for _, l := range loggers {
   110  		if t, ok := l.(Tracer); ok {
   111  			t.TraceID(id)
   112  		}
   113  	}
   114  }