PageRenderTime 31ms CodeModel.GetById 20ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/logger/logger.go

https://code.google.com/p/gobuild/
Go | 100 lines | 52 code | 13 blank | 35 comment | 7 complexity | 585cf3f08a98086d6c5dffc568dbaf80 MD5 | raw file
  1// Copyright 2009-2012 by Maurice Gilden. All rights reserved.
  2// Use of this source code is governed by a BSD-style
  3// license that can be found in the LICENSE file.
  4
  5/*
  6 A collection of helper functions for console output.
  7*/
  8package logger
  9
 10import "fmt"
 11import "os"
 12
 13const (
 14	DEBUG   = -1 + iota // start with -1
 15	DEFAULT             // should be 0
 16	WARN
 17	ERROR
 18)
 19
 20var verbosity int
 21
 22/*
 23 Sets the verbosity level. Possible values are DEBUG, DEFAULT, WARN and ERROR.
 24 DEFAULT is the standard value which will print everything but debug messages.
 25*/
 26func SetVerbosityLevel(level int) { verbosity = level }
 27
 28/*
 29 Prints debug messages. Same syntax as fmt.Printf.
 30*/
 31func Debug(format string, v ...interface{}) {
 32	if verbosity <= DEBUG {
 33		fmt.Printf("DEBUG: ")
 34		fmt.Printf(format, v...)
 35	}
 36}
 37
 38/*
 39 Prints a debug message if enabled but without the "DEBUG: " prefix.
 40 Same syntax as fmt.Printf.
 41*/
 42func DebugContinue(format string, v ...interface{}) {
 43	if verbosity <= DEBUG {
 44		fmt.Printf("       ")
 45		fmt.Printf(format, v...)
 46	}
 47}
 48
 49/*
 50 Prints an info message if enabled. This is for general feedback about what
 51 gobuild is currently doing. Same syntax as fmt.Printf.
 52*/
 53func Info(format string, v ...interface{}) {
 54	if verbosity <= DEFAULT {
 55		fmt.Printf(format, v...)
 56	}
 57}
 58
 59
 60/*
 61 Prints a warning if warnings are enabled. Same syntax as fmt.Printf.
 62*/
 63func Warn(format string, v ...interface{}) {
 64	if verbosity <= WARN {
 65		fmt.Print("WARNING: ")
 66		fmt.Printf(format, v...)
 67	}
 68}
 69
 70/*
 71 Prints a warning message if enabled but without the "WARNING: " prefix.
 72 Same syntax as fmt.Printf.
 73*/
 74func WarnContinue(format string, v ...interface{}) {
 75	if verbosity <= WARN {
 76		fmt.Print("         ")
 77		fmt.Printf(format, v...)
 78	}
 79}
 80
 81/*
 82 Prints an error message. Same syntax as fmt.Printf.
 83*/
 84func Error(format string, v ...interface{}) {
 85	if verbosity <= ERROR {
 86		fmt.Fprint(os.Stderr, "ERROR: ")
 87		fmt.Fprintf(os.Stderr, format, v...)
 88	}
 89}
 90
 91/*
 92 Prints an error message but without the "ERROR: " prefix.
 93 Same syntax as fmt.Printf.
 94*/
 95func ErrorContinue(format string, v ...interface{}) {
 96	if verbosity <= ERROR {
 97		fmt.Fprint(os.Stderr, "       ")
 98		fmt.Fprintf(os.Stderr, format, v...)
 99	}
100}