go.dedis.ch/onet/v4@v4.0.0-pre1/log/log.go (about)

     1  // Package log is an output-library that can print nicely formatted
     2  // messages to the screen.
     3  //
     4  // There are log-level messages that will be printed according to the
     5  // current debug-level set. Furthermore a set of common messages exist
     6  // that are printed according to a chosen format.
     7  //
     8  // The log-level messages are:
     9  //	log.Lvl1("Important information")
    10  //	log.Lvl2("Less important information")
    11  //	log.Lvl3("Eventually flooding information")
    12  //	log.Lvl4("Definitively flooding information")
    13  //	log.Lvl5("I hope you never need this")
    14  // in your program, then according to the debug-level one or more levels of
    15  // output will be shown. To set the debug-level, use
    16  //	log.SetDebugVisible(3)
    17  // which will show all `Lvl1`, `Lvl2`, and `Lvl3`. If you want to turn
    18  // on just one output, you can use
    19  //	log.LLvl2("Less important information")
    20  // By adding a single 'L' to the method, it *always* gets printed.
    21  //
    22  // You can also add a 'f' to the name and use it like fmt.Printf:
    23  //	log.Lvlf1("Level: %d/%d", now, max)
    24  //
    25  // The common messages are:
    26  //	log.Print("Simple output")
    27  //	log.Info("For your information")
    28  //	log.Warn("Only a warning")
    29  //	log.Error("This is an error, but continues")
    30  //	log.Panic("Something really went bad - calls panic")
    31  //	log.Fatal("No way to continue - calls os.Exit")
    32  //
    33  // These messages are printed according to the value of 'Format':
    34  // - Format == FormatLvl - same as log.Lvl
    35  // - Format == FormatPython - with some nice python-style formatting
    36  // - Format == FormatNone - just as plain text
    37  //
    38  // The log-package also takes into account the following environment-variables:
    39  //	DEBUG_LVL // will act like SetDebugVisible
    40  //	DEBUG_TIME // if 'true' it will print the date and time
    41  //  DEBUG_FILEPATH // if 'true' it will print the absolute filepath
    42  //	DEBUG_COLOR // if 'false' it will not use colors
    43  //	DEBUG_PADDING // if 'false' it will not use padding
    44  // But for this the function ParseEnv() or AddFlags() has to be called.
    45  package log
    46  
    47  import (
    48  	"bytes"
    49  	"io"
    50  	"os"
    51  )
    52  
    53  // For testing we can change the output-writer
    54  var stdOut io.Writer
    55  var stdErr io.Writer
    56  
    57  var bufStdOut bytes.Buffer
    58  var bufStdErr bytes.Buffer
    59  
    60  func init() {
    61  	stdOut = os.Stdout
    62  	stdErr = os.Stderr
    63  }
    64  
    65  // OutputToBuf is called for sending all the log.*-outputs to internal buffers
    66  // that can be used for checking what the logger would've written. This is
    67  // mostly used for tests. The buffers are zeroed after this call.
    68  func OutputToBuf() {
    69  	debugMut.Lock()
    70  	defer debugMut.Unlock()
    71  	stdOut = &bufStdOut
    72  	stdErr = &bufStdErr
    73  	bufStdOut.Reset()
    74  	bufStdErr.Reset()
    75  }
    76  
    77  // OutputToOs redirects the output of the log.*-outputs again to the os.
    78  func OutputToOs() {
    79  	debugMut.Lock()
    80  	defer debugMut.Unlock()
    81  	stdOut = os.Stdout
    82  	stdErr = os.Stderr
    83  }
    84  
    85  // GetStdOut returns all log.*-outputs to StdOut since the last call.
    86  func GetStdOut() string {
    87  	debugMut.Lock()
    88  	defer debugMut.Unlock()
    89  	ret := bufStdOut.String()
    90  	bufStdOut.Reset()
    91  	return ret
    92  }
    93  
    94  // GetStdErr returns all log.*-outputs to StdErr since the last call.
    95  func GetStdErr() string {
    96  	debugMut.Lock()
    97  	defer debugMut.Unlock()
    98  	ret := bufStdErr.String()
    99  	bufStdErr.Reset()
   100  	return ret
   101  }