func NewRecord(s string, l LogLevel, m string, d map[string]interface{}) *Record { r := &Record{ Timestamp: RecordTimestamp(time.Now().UnixNano()) / 1000000000, Pid: pid, Source: s, Level: l, Message: m, Data: d, } if config.EnableLOC { var function *runtime.Func var file string var line int pc := make([]uintptr, 50) nptrs := runtime.Callers(2, pc) for i := 0; i < nptrs; i++ { function = runtime.FuncForPC(pc[i]) file, line = function.FileLine(pc[i]) if !strings.HasSuffix(file, "logger.go") { break } } r.File = file r.Line = line r.Method = function.Name() } return r }
func NewRecord(level LogLevel, message string, data map[string]string) *Record { record := new(Record) record.Timestamp = float64(time.Now().UnixNano()) / 1000000000 record.Message = message record.Level = level record.Data = data if config.EnableLOC { var function *runtime.Func var file string var line int pc := make([]uintptr, 50) nptrs := runtime.Callers(2, pc) for i := 0; i < nptrs; i++ { function = runtime.FuncForPC(pc[i]) file, line = function.FileLine(pc[i]) if !strings.HasSuffix(file, "logger.go") { break } } record.File = file record.Method = function.Name() record.Line = line } return record }
// UnstubDefaultTransport restores http.DefaultTransport func (mitm *MitmTransport) UnstubDefaultTransport() { mitm.mux.Lock() defer mitm.mux.Unlock() if mitm.stubbed { mitm.stubbed = false http.DefaultTransport = httpDefaultResponder } // is times miss match? if !mitm.paused { errlogs := []string{} for key, response := range mitm.stubs { for path, mocker := range response.Mocks() { if !mocker.IsTimesMatched() { key = strings.Replace(key, MockScheme, mocker.Scheme(), 1) expected, invoked := mocker.Times() errlogs = append(errlogs, " Error Trace: %s:%d\n Error: Expected "+key+path+" with "+fmt.Sprintf("%d", expected)+" times, but got "+fmt.Sprintf("%d", invoked)+" times\n\n") } } } if len(errlogs) > 0 { pcs := make([]uintptr, 10) max := runtime.Callers(2, pcs) var ( pcfunc *runtime.Func pcfile string pcline int ) for i := 0; i < max; i++ { pcfunc = runtime.FuncForPC(pcs[i] - 1) if strings.HasPrefix(pcfunc.Name(), "runtime.") { continue } pcfile, pcline = pcfunc.FileLine(pcs[i]) } // format errlogs for i, errlog := range errlogs { errlogs[i] = fmt.Sprintf(errlog, filepath.Base(pcfile), pcline) } fmt.Printf("--- FAIL: %s\n%s", pcfunc.Name(), strings.Join(errlogs, "\n")) mitm.testing.Fail() } } mitm.testing = nil mitm.stubs = make(map[string]*Responser) }
func newTestState(pc uintptr, f *runtime.Func) (*testState, error) { state := &testState{ pc: pc, fullName: f.Name(), } state.fileName, state.fileLine = f.FileLine(pc) splits := strings.Split(state.fullName, ".") state.name = splits[len(splits)-1] return state, nil }
func (this *Trace) Trace(level int) *Trace { var ok bool var pc uintptr var fn *runtime.Func var buf []byte var tmp []string var i int this.Record = r.NewRecord() if level == 0 { level = STEP_BACK } buf = make([]byte, 1<<16) pc, this.Record.FileNameLong, this.Record.FileLine, ok = runtime.Caller(level) if ok == true { fn = runtime.FuncForPC(pc) if fn != nil { this.Record.Function = fn.Name() } i = runtime.Stack(buf, true) this.Record.CallStack = string(buf[:i]) tmp = strings.Split(this.Record.Function, packageSeparator) if len(tmp) > 1 { this.Record.Package += strings.Join(tmp[:len(tmp)-1], packageSeparator) this.Record.Function = tmp[len(tmp)-1] } tmp = strings.SplitN(this.Record.Function, `.`, 2) if len(tmp) == 2 { if this.Record.Package != "" { this.Record.Package += packageSeparator } this.Record.Package += tmp[0] this.Record.Function = tmp[1] } // Filename short tmp = strings.Split(this.Record.FileNameLong, packageSeparator) if len(tmp) > 0 { this.Record.FileNameShort = tmp[len(tmp)-1] } // Module name tmp = strings.Split(this.Record.Package, packageSeparator) if len(tmp) > 0 { this.Record.Module = tmp[len(tmp)-1] } } return this }
func packageAndName(fn *runtime.Func) (string, string) { name := fn.Name() pkg := "" // we first remove the path prefix if there is one. if lastslash := strings.LastIndex(name, "/"); lastslash >= 0 { pkg += name[:lastslash] + "/" name = name[lastslash+1:] } if period := strings.Index(name, "."); period >= 0 { pkg += name[:period] name = name[period+1:] } return pkg, name }
/* "FuncName" or "Receiver.MethodName" */ func shortFuncName(f *runtime.Func) string { // f.Name() is like one of these: // - "github.com/palantir/shield/package.FuncName" // - "github.com/palantir/shield/package.Receiver.MethodName" // - "github.com/palantir/shield/package.(*PtrReceiver).MethodName" longName := f.Name() withoutPath := longName[strings.LastIndex(longName, "/")+1:] withoutPackage := withoutPath[strings.Index(withoutPath, ".")+1:] shortName := withoutPackage shortName = strings.Replace(shortName, "(", "", 1) shortName = strings.Replace(shortName, "*", "", 1) shortName = strings.Replace(shortName, ")", "", 1) return shortName }
func (t *trace) Trace(level int) *trace { var ok bool var pc uintptr var fn *runtime.Func var buf []byte var tmp []string var i int if level == 0 { level = traceStepBack } buf = make([]byte, 1<<16) pc, t.File, t.Line, ok = runtime.Caller(level) if ok == true { fn = runtime.FuncForPC(pc) if fn != nil { t.Function = fn.Name() } i = runtime.Stack(buf, true) t.Stack = string(buf[:i]) tmp = strings.Split(t.Function, packageSeparator) if len(tmp) > 1 { t.Package += strings.Join(tmp[:len(tmp)-1], packageSeparator) t.Function = tmp[len(tmp)-1] } tmp = strings.SplitN(t.Function, `.`, 2) if len(tmp) == 2 { if t.Package != "" { t.Package += packageSeparator } t.Package += tmp[0] t.Function = tmp[1] } } return t }
func packageAndName(fn *runtime.Func) (string, string) { name := fn.Name() pkg := "" // The name includes the path name to the package, which is unnecessary // since the file name is already included. Plus, it has center dots. // That is, we see // runtime/debug.*T·ptrmethod // and want // *T.ptrmethod // Since the package path might contains dots (e.g. code.google.com/...), // we first remove the path prefix if there is one. if lastslash := strings.LastIndex(name, "/"); lastslash >= 0 { pkg += name[:lastslash] + "/" name = name[lastslash+1:] } if period := strings.Index(name, "."); period >= 0 { pkg += name[:period] name = name[period+1:] } name = strings.Replace(name, "·", ".", -1) return pkg, name }
func isTestRunner(f *runtime.Func) bool { return f != nil && f.Name() == "testing.tRunner" }