Ejemplo n.º 1
0
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
}
Ejemplo n.º 2
0
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
}
Ejemplo n.º 3
0
// 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)
}
Ejemplo n.º 4
0
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
}