github.com/unclejack/drone@v0.2.1-0.20140918182345-831b034aa33b/pkg/build/log/log.go (about)

     1  package log
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"sync"
     8  )
     9  
    10  const (
    11  	LOG_EMERG = iota
    12  	LOG_ALERT
    13  	LOG_CRIT
    14  	LOG_ERR
    15  	LOG_WARNING
    16  	LOG_NOTICE
    17  	LOG_INFO
    18  	LOG_DEBUG
    19  )
    20  
    21  var mu sync.Mutex
    22  
    23  // the default Log priority
    24  var priority int = LOG_DEBUG
    25  
    26  // the default Log output destination
    27  var output io.Writer = os.Stdout
    28  
    29  // the log prefix
    30  var prefix string
    31  
    32  // the log suffix
    33  var suffix string = "\n"
    34  
    35  // SetPriority sets the default log level.
    36  func SetPriority(level int) {
    37  	mu.Lock()
    38  	defer mu.Unlock()
    39  	priority = level
    40  }
    41  
    42  // SetOutput sets the output destination.
    43  func SetOutput(w io.Writer) {
    44  	mu.Lock()
    45  	defer mu.Unlock()
    46  	output = w
    47  }
    48  
    49  // SetPrefix sets the prefix for the log message.
    50  func SetPrefix(pre string) {
    51  	mu.Lock()
    52  	defer mu.Unlock()
    53  	prefix = pre
    54  }
    55  
    56  // SetSuffix sets the suffix for the log message.
    57  func SetSuffix(suf string) {
    58  	mu.Lock()
    59  	defer mu.Unlock()
    60  	suffix = suf
    61  }
    62  
    63  func Write(out string, level int) {
    64  	mu.Lock()
    65  	defer mu.Unlock()
    66  
    67  	// append the prefix and suffix
    68  	out = prefix + out + suffix
    69  
    70  	if priority >= level {
    71  		output.Write([]byte(out))
    72  	}
    73  }
    74  
    75  func Debug(out string) {
    76  	Write(out, LOG_DEBUG)
    77  }
    78  
    79  func Debugf(format string, a ...interface{}) {
    80  	Debug(fmt.Sprintf(format, a...))
    81  }
    82  
    83  func Info(out string) {
    84  	Write(out, LOG_INFO)
    85  }
    86  
    87  func Infof(format string, a ...interface{}) {
    88  	Info(fmt.Sprintf(format, a...))
    89  }
    90  
    91  func Err(out string) {
    92  	Write(out, LOG_ERR)
    93  }
    94  
    95  func Errf(format string, a ...interface{}) {
    96  	Err(fmt.Sprintf(format, a...))
    97  }
    98  
    99  func Notice(out string) {
   100  	Write(out, LOG_NOTICE)
   101  }
   102  
   103  func Noticef(format string, a ...interface{}) {
   104  	Notice(fmt.Sprintf(format, a...))
   105  }