Merge pull request #789 from abperiasamy/trace-on-new

trace on New and add read locks
This commit is contained in:
Anand Babu (AB) Periasamy 2015-08-02 12:03:44 -07:00
commit 574f2aaafa
2 changed files with 20 additions and 7 deletions

View File

@ -60,7 +60,7 @@ type tracePoint struct {
// Error implements tracing error functionality. // Error implements tracing error functionality.
type Error struct { type Error struct {
lock sync.Mutex lock sync.RWMutex
e error e error
sysInfo map[string]string sysInfo map[string]string
tracePoints []tracePoint tracePoints []tracePoint
@ -71,7 +71,8 @@ type Error struct {
// trace the return path with Probe.Trace and finally handle reporting or quitting // trace the return path with Probe.Trace and finally handle reporting or quitting
// at the top level. // at the top level.
func New(e error) *Error { func New(e error) *Error {
return &Error{sync.Mutex{}, e, GetSysInfo(), []tracePoint{}} Err := Error{sync.RWMutex{}, e, GetSysInfo(), []tracePoint{}}
return Err.Trace()
} }
// Trace records the point at which it is invoked. Stack traces are important for // Trace records the point at which it is invoked. Stack traces are important for
@ -114,6 +115,9 @@ func (e *Error) Error() string {
// String returns error message. // String returns error message.
func (e *Error) String() string { func (e *Error) String() string {
e.lock.RLock()
defer e.lock.RUnlock()
trace := e.e.Error() + "\n" trace := e.e.Error() + "\n"
for i, tp := range e.tracePoints { for i, tp := range e.tracePoints {
if len(tp.Env) > 0 { if len(tp.Env) > 0 {
@ -135,6 +139,9 @@ func (e *Error) String() string {
// JSON returns JSON formated error trace. // JSON returns JSON formated error trace.
func (e *Error) JSON() string { func (e *Error) JSON() string {
e.lock.RLock()
defer e.lock.RUnlock()
anonError := struct { anonError := struct {
SysInfo map[string]string SysInfo map[string]string
TracePoints []tracePoint TracePoints []tracePoint
@ -153,5 +160,6 @@ func (e *Error) JSON() string {
// ToError returns original emnedded error. // ToError returns original emnedded error.
func (e *Error) ToError() error { func (e *Error) ToError() error {
// No need to lock. "e.e" is set once during New and never changed.
return e.e return e.e
} }

View File

@ -16,7 +16,6 @@
package probe package probe
import ( import (
"fmt"
"os" "os"
"testing" "testing"
) )
@ -33,8 +32,14 @@ func TestProbe(t *testing.T) {
if es == nil { if es == nil {
t.Fail() t.Fail()
} }
es.Trace()
newES := es.Trace()
if newES == nil {
t.Fail()
}
/*
fmt.Println(es) fmt.Println(es)
// fmt.Println(es.JSON()) fmt.Println(es.JSON())
// fmt.Println(es.ToError()) fmt.Println(es.ToError())
*/
} }