Golint fixes

This commit is contained in:
Harshavardhana
2015-03-25 13:23:07 -07:00
parent 9503a4df33
commit 2cbd15b690
9 changed files with 92 additions and 36 deletions

2
Godeps/Godeps.json generated
View File

@@ -20,7 +20,7 @@
},
{
"ImportPath": "github.com/minio-io/iodine",
"Rev": "b279ca8ea714fabc969883a4d1612a4e93d01611"
"Rev": "f92ca01c8671d9565c7aa58e3427364c5e187ccf"
},
{
"ImportPath": "gopkg.in/check.v1",

View File

@@ -1,5 +1,5 @@
/*
* Iodine, (C) 2014 Minio, Inc.
* Iodine, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,8 +21,10 @@ import (
"encoding/json"
"fmt"
"os"
"path"
"runtime"
"strconv"
"strings"
"sync"
)
@@ -43,25 +45,30 @@ type StackEntry struct {
Data map[string]string
}
var gopath string
var globalState = struct {
sync.RWMutex
m map[string]string
}{m: make(map[string]string)}
// SetGlobalState - set global state
func SetGlobalState(key, value string) {
globalState.Lock()
globalState.m[key] = value
globalState.Unlock()
}
// ClearGlobalState - clear info in globalState struct
func ClearGlobalState() {
globalState.Lock()
for k, _ := range globalState.m {
for k := range globalState.m {
delete(globalState.m, k)
}
globalState.Unlock()
}
// GetGlobalState - get map from globalState struct
func GetGlobalState() map[string]string {
result := make(map[string]string)
globalState.RLock()
@@ -72,7 +79,16 @@ func GetGlobalState() map[string]string {
return result
}
// Wrap an error, turning it into an iodine error.
// GetGlobalStateKey - get value for key from globalState struct
func GetGlobalStateKey(k string) string {
result, ok := globalState.m[k]
if !ok {
return ""
}
return result
}
// New - instantiate an error, turning it into an iodine error.
// Adds an initial stack trace.
func New(err error, data map[string]string) *Error {
entry := createStackEntry()
@@ -86,18 +102,46 @@ func New(err error, data map[string]string) *Error {
}
}
// createStackEntry - create stack entries
func createStackEntry() StackEntry {
host, _ := os.Hostname()
_, file, line, _ := runtime.Caller(2)
file = strings.TrimPrefix(file, gopath) // trim gopath from file
data := GetGlobalState()
for k, v := range getSystemData() {
data[k] = v
}
entry := StackEntry{
Host: host,
File: file,
Line: line,
Data: GetGlobalState(),
Data: data,
}
return entry
}
func getSystemData() map[string]string {
host, err := os.Hostname()
if err != nil {
host = ""
}
memstats := &runtime.MemStats{}
runtime.ReadMemStats(memstats)
return map[string]string{
"sys.host": host,
"sys.os": runtime.GOOS,
"sys.arch": runtime.GOARCH,
"sys.go": runtime.Version(),
"sys.cpus": strconv.Itoa(runtime.NumCPU()),
"sys.mem.used": strconv.FormatUint(memstats.Alloc, 10),
"sys.mem.allocated": strconv.FormatUint(memstats.TotalAlloc, 10),
"sys.mem.heap.used": strconv.FormatUint(memstats.HeapAlloc, 10),
"sys.mem.heap.allocated": strconv.FormatUint(memstats.HeapSys, 10),
}
}
// Annotate an error with a stack entry and returns itself
func (err *Error) Annotate(info map[string]string) *Error {
entry := createStackEntry()
@@ -127,3 +171,11 @@ func (err Error) EmitHumanReadable() string {
func (err Error) Error() string {
return err.EmbeddedError.Error()
}
func init() {
_, iodineFile, _, _ := runtime.Caller(0)
iodineFile = path.Dir(iodineFile) // trim iodine.go
iodineFile = path.Dir(iodineFile) // trim iodine
iodineFile = path.Dir(iodineFile) // trim minio-io
gopath = path.Dir(iodineFile) + "/" // trim github.com
}

View File

@@ -1,5 +1,5 @@
/*
* Iodine, (C) 2014 Minio, Inc.
* Iodine, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@ import (
"bytes"
"encoding/json"
"errors"
"log"
"testing"
)
@@ -38,16 +37,16 @@ func TestIodine(t *testing.T) {
}
var prettyBuffer bytes.Buffer
json.Indent(&prettyBuffer, jsonResult, "", " ")
if prettyBuffer.String() == "" {
t.Fail()
}
}
func TestState(t *testing.T) {
SetGlobalState("hello", "world")
state := GetGlobalState()
if res, ok := state["hello"]; ok {
if res != "world" {
t.Error("global state not set: hello->world")
}
} else {
result := GetGlobalStateKey("hello")
if result != "world" {
t.Error("global state not set: hello->world")
t.Fail()
}
ClearGlobalState()
@@ -85,7 +84,6 @@ func TestState(t *testing.T) {
}
} else {
err.Annotate(nil)
log.Println(err.EmitHumanReadable())
t.Error("foo2 should be set")
}
}