github.com/puellanivis/breton@v0.2.16/lib/glog/api.go (about)

     1  package glog
     2  
     3  import (
     4  	"runtime"
     5  	"sync/atomic"
     6  )
     7  
     8  // Verbose is a boolean type that implements Infof (like Printf) etc.
     9  // See the documentation of V for more information.
    10  type Verbose bool
    11  
    12  // V reports whether verbosity at the call site is at least the requested level.
    13  // The returned value is a boolean of type Verbose, which implements Info, Infoln
    14  // and Infof. These methods will write to the Info log if called.
    15  // Thus, one may write either
    16  //
    17  //	if glog.V(2) { glog.Info("log this") }
    18  //
    19  // or
    20  //
    21  //	glog.V(2).Info("log this")
    22  //
    23  // The second form is shorter but the first is cheaper if logging is off because it does
    24  // not evaluate its arguments.
    25  //
    26  // Whether an individual call to V generates a log record depends on the setting of
    27  // the --verbosity and --vmodule flags; both are off by default. If the level in the call to
    28  // V is at least the value of --verbosity, or of --vmodule for the source file containing the
    29  // call, the V call will glog.
    30  func V(level Level) Verbose {
    31  	// This function tries hard to be cheap unless there's work to do.
    32  	// The fast path is two atomic loads and compares.
    33  
    34  	// Here is a cheap but safe test to see if V logging is enabled globally.
    35  	if logging.Verbosity.get() >= level {
    36  		return true
    37  	}
    38  
    39  	// It's off globally but it vmodule may still be set.
    40  	// Here is another cheap but safe test to see if vmodule is enabled.
    41  	if !logging.Vmodule.isSet() {
    42  		return false
    43  	}
    44  
    45  	var pc [1]uintptr
    46  	if runtime.Callers(2, pc[:]) == 0 {
    47  		return false
    48  	}
    49  
    50  	return logging.Vmodule.getV(pc[0]) >= level
    51  }
    52  
    53  // Info is equivalent to the global Info function, guarded by the value of v.
    54  // See the documentation of V for usage.
    55  func (v Verbose) Info(args ...interface{}) {
    56  	if v {
    57  		logging.print(infoLog, args...)
    58  	}
    59  }
    60  
    61  // Infoln is equivalent to the global Infoln function, guarded by the value of v.
    62  // See the documentation of V for usage.
    63  func (v Verbose) Infoln(args ...interface{}) {
    64  	if v {
    65  		logging.println(infoLog, args...)
    66  	}
    67  }
    68  
    69  // Infof is equivalent to the global Infof function, guarded by the value of v.
    70  // See the documentation of V for usage.
    71  func (v Verbose) Infof(format string, args ...interface{}) {
    72  	if v {
    73  		logging.printf(infoLog, format, args...)
    74  	}
    75  }
    76  
    77  // Info logs to the INFO log.
    78  // Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
    79  func Info(args ...interface{}) {
    80  	logging.print(infoLog, args...)
    81  }
    82  
    83  // InfoDepth acts as Info but uses depth to determine which call frame to log.
    84  // InfoDepth(0, "msg") is the same as Info("msg").
    85  func InfoDepth(depth int, args ...interface{}) {
    86  	logging.printDepth(infoLog, depth, args...)
    87  }
    88  
    89  // Infoln logs to the INFO log.
    90  // Arguments are handled in the manner of fmt.Println; a newline is appended if missing.
    91  func Infoln(args ...interface{}) {
    92  	logging.println(infoLog, args...)
    93  }
    94  
    95  // Infof logs to the INFO log.
    96  // Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
    97  func Infof(format string, args ...interface{}) {
    98  	logging.printf(infoLog, format, args...)
    99  }
   100  
   101  // Warning logs to the WARNING and INFO logs.
   102  // Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
   103  func Warning(args ...interface{}) {
   104  	logging.print(warningLog, args...)
   105  }
   106  
   107  // WarningDepth acts as Warning but uses depth to determine which call frame to log.
   108  // WarningDepth(0, "msg") is the same as Warning("msg").
   109  func WarningDepth(depth int, args ...interface{}) {
   110  	logging.printDepth(warningLog, depth, args...)
   111  }
   112  
   113  // Warningln logs to the WARNING and INFO logs.
   114  // Arguments are handled in the manner of fmt.Println; a newline is appended if missing.
   115  func Warningln(args ...interface{}) {
   116  	logging.println(warningLog, args...)
   117  }
   118  
   119  // Warningf logs to the WARNING and INFO logs.
   120  // Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
   121  func Warningf(format string, args ...interface{}) {
   122  	logging.printf(warningLog, format, args...)
   123  }
   124  
   125  // Error logs to the ERROR, WARNING, and INFO logs.
   126  // Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
   127  func Error(args ...interface{}) {
   128  	logging.print(errorLog, args...)
   129  }
   130  
   131  // ErrorDepth acts as Error but uses depth to determine which call frame to log.
   132  // ErrorDepth(0, "msg") is the same as Error("msg").
   133  func ErrorDepth(depth int, args ...interface{}) {
   134  	logging.printDepth(errorLog, depth, args...)
   135  }
   136  
   137  // Errorln logs to the ERROR, WARNING, and INFO logs.
   138  // Arguments are handled in the manner of fmt.Println; a newline is appended if missing.
   139  func Errorln(args ...interface{}) {
   140  	logging.println(errorLog, args...)
   141  }
   142  
   143  // Errorf logs to the ERROR, WARNING, and INFO logs.
   144  // Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
   145  func Errorf(format string, args ...interface{}) {
   146  	logging.printf(errorLog, format, args...)
   147  }
   148  
   149  // Fatal logs to the FATAL, ERROR, WARNING, and INFO logs,
   150  // including a stack trace of all running goroutines, then calls os.Exit(255).
   151  // Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
   152  func Fatal(args ...interface{}) {
   153  	logging.print(fatalLog, args...)
   154  }
   155  
   156  // FatalDepth acts as Fatal but uses depth to determine which call frame to log.
   157  // FatalDepth(0, "msg") is the same as Fatal("msg").
   158  func FatalDepth(depth int, args ...interface{}) {
   159  	logging.printDepth(fatalLog, depth, args...)
   160  }
   161  
   162  // Fatalln logs to the FATAL, ERROR, WARNING, and INFO logs,
   163  // including a stack trace of all running goroutines, then calls os.Exit(255).
   164  // Arguments are handled in the manner of fmt.Println; a newline is appended if missing.
   165  func Fatalln(args ...interface{}) {
   166  	logging.println(fatalLog, args...)
   167  }
   168  
   169  // Fatalf logs to the FATAL, ERROR, WARNING, and INFO logs,
   170  // including a stack trace of all running goroutines, then calls os.Exit(255).
   171  // Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
   172  func Fatalf(format string, args ...interface{}) {
   173  	logging.printf(fatalLog, format, args...)
   174  }
   175  
   176  // fatalNoStacks is non-zero if we are to exit without dumping goroutine stacks.
   177  // It allows Exit and relatives to use the Fatal logs.
   178  var fatalNoStacks uint32
   179  
   180  // Exit logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).
   181  // Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
   182  func Exit(args ...interface{}) {
   183  	atomic.StoreUint32(&fatalNoStacks, 1)
   184  	logging.print(fatalLog, args...)
   185  }
   186  
   187  // ExitDepth acts as Exit but uses depth to determine which call frame to log.
   188  // ExitDepth(0, "msg") is the same as Exit("msg").
   189  func ExitDepth(depth int, args ...interface{}) {
   190  	atomic.StoreUint32(&fatalNoStacks, 1)
   191  	logging.printDepth(fatalLog, depth, args...)
   192  }
   193  
   194  // Exitln logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).
   195  func Exitln(args ...interface{}) {
   196  	atomic.StoreUint32(&fatalNoStacks, 1)
   197  	logging.println(fatalLog, args...)
   198  }
   199  
   200  // Exitf logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).
   201  // Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
   202  func Exitf(format string, args ...interface{}) {
   203  	atomic.StoreUint32(&fatalNoStacks, 1)
   204  	logging.printf(fatalLog, format, args...)
   205  }