Move logging, updating and testing utils into common directory

This commit is contained in:
Bryan Boreham committed 2015-01-02 14:49:49 +00:00
1 parent 5da2219dc8
commit 82b54aabcd
8 files changed
+69 -52

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
package common
import (
"io"
"io/ioutil"
"log"
"os"
)
const (
standardLogFlags = log.Ldate | log.Ltime | log.Lmicroseconds
)
// Largely taken from
// http://www.goinggo.net/2013/11/using-log-package-in-go.html
var (
Debug *log.Logger
Info *log.Logger
Warning *log.Logger
Error *log.Logger
)
func InitLogging(debugHandle io.Writer,
infoHandle io.Writer,
warningHandle io.Writer,
errorHandle io.Writer) {
Debug = log.New(debugHandle, "DEBUG: ", standardLogFlags)
Info = log.New(infoHandle, "INFO: ", standardLogFlags)
Warning = log.New(warningHandle, "WARNING: ", standardLogFlags)
Error = log.New(errorHandle, "ERROR: ", standardLogFlags)
}
func InitDefaultLogging(debug bool) {
debugOut := ioutil.Discard
if debug {
debugOut = os.Stderr
}
InitLogging(debugOut, os.Stdout, os.Stdout, os.Stderr)
}
+51
View File
@@ -0,0 +1,51 @@
package common
import (
"fmt"
"reflect"
"runtime"
"strings"
"testing"
)
func CallSite() string {
_, file, line, ok := runtime.Caller(2)
if ok {
// Truncate file name at last file name separator.
if index := strings.LastIndex(file, "/"); index >= 0 {
file = file[index+1:]
} else if index = strings.LastIndex(file, "\\"); index >= 0 {
file = file[index+1:]
}
} else {
file = "???"
line = 1
}
return fmt.Sprintf("%s:%d: ", file, line)
}
func AssertNoErr(t *testing.T, err error) {
if err != nil {
t.Fatal(CallSite(), err)
}
}
func AssertStatus(t *testing.T, got int, wanted int, desc string) {
if got != wanted {
t.Fatalf("%s: Expected %s %d but got %d", CallSite(), desc, wanted, got)
}
}
func AssertErrorInterface(t *testing.T, got interface{}, wanted interface{}, desc string) {
gotT, wantedT := reflect.TypeOf(got), reflect.TypeOf(wanted).Elem()
if !gotT.Implements(wantedT) {
t.Fatalf("%s: Expected %s but got %s (%s)", CallSite(), wantedT.String(), gotT.String(), desc)
}
}
func AssertErrorType(t *testing.T, got interface{}, wanted interface{}, desc string) {
gotT, wantedT := reflect.TypeOf(got), reflect.TypeOf(wanted).Elem()
if gotT != wantedT {
t.Fatalf("%s: Expected %s but got %s (%s)", CallSite(), wantedT.String(), gotT.String(), desc)
}
}