Skip to content

Commit

Permalink
Renamed package runtime to diag (Azure#15278)
Browse files Browse the repository at this point in the history
* Renamed package runtime to diag

Since it deals with diagnostics and we want to use the name runtime for
a package in azcore.
Deleted FrameError as it's not very useful.
Added Caller() for times when a full stack trace isn't needed.

* Remove log.Listener
  • Loading branch information
jhendrixMSFT authored and vindicatesociety committed Sep 18, 2021
1 parent a38afee commit dea8545
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 145 deletions.
22 changes: 19 additions & 3 deletions sdk/internal/runtime/runtime.go → sdk/internal/diag/diag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,38 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package runtime
package diag

import (
"fmt"
"runtime"
"strings"
)

// Caller returns the file and line number of a frame on the caller's stack.
// If the funtion fails an empty string is returned.
// skipFrames - the number of frames to skip when determining the caller.
// Passing a value of 0 will return the immediate caller of this function.
func Caller(skipFrames int) string {
if pc, file, line, ok := runtime.Caller(skipFrames + 1); ok {
// the skipFrames + 1 is to skip ourselves
frame := runtime.FuncForPC(pc)
return fmt.Sprintf("%s()\n\t%s:%d", frame.Name(), file, line)
}
return ""
}

// StackTrace returns a formatted stack trace string.
// If the funtion fails an empty string is returned.
// skipFrames - the number of stack frames to skip before composing the trace string.
// totalFrames - the maximum number of stack frames to include in the trace string.
func StackTrace(skipFrames, totalFrames int) string {
sb := strings.Builder{}
pcCallers := make([]uintptr, totalFrames)
runtime.Callers(skipFrames, pcCallers)
if frames := runtime.Callers(skipFrames, pcCallers); frames == 0 {
return ""
}
frames := runtime.CallersFrames(pcCallers)
sb := strings.Builder{}
for {
frame, more := frames.Next()
sb.WriteString(frame.Function)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,36 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package runtime
package diag

import (
"regexp"
"strings"
"testing"
)

func TestCallerBasic(t *testing.T) {
c := Caller(0)
matched, err := regexp.MatchString(`/diag_test.go:\d+$`, c)
if err != nil {
t.Fatal(err)
}
if !matched {
t.Fatalf("got %s", c)
}
}

func TestCallerSkipFrame(t *testing.T) {
c := Caller(1)
matched, err := regexp.MatchString(`/testing.go:\d+$`, c)
if err != nil {
t.Fatal(err)
}
if !matched {
t.Fatalf("got %s", c)
}
}

func TestStackTraceBasic(t *testing.T) {
trace := StackTrace(0, 1)
trace = strings.TrimSpace(trace)
Expand All @@ -24,7 +47,7 @@ func TestStackTraceSkipFrame(t *testing.T) {
trace := StackTrace(1, 1)
trace = strings.TrimSpace(trace)
parts := strings.Split(trace, "\n")
const topFrame = "runtime.StackTrace()"
const topFrame = "diag.StackTrace()"
if strings.LastIndex(parts[0], topFrame) == -1 {
t.Fatalf("%s didn't end with %s", parts[0], topFrame)
}
Expand Down
15 changes: 5 additions & 10 deletions sdk/internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,10 @@ const (
LongRunningOperation Classification = "LongRunningOperation"
)

// Listener is the funciton signature invoked when writing log entries.
// A Listener is required to perform its own synchronization if it's expected to be called
// from multiple Go routines
type Listener func(Classification, string)

// logger controls which classifications to log and writing to the underlying log.
type logger struct {
cls []Classification
lst Listener
lst func(Classification, string)
}

// SetClassifications is used to control which classifications are written to
Expand All @@ -48,8 +43,8 @@ func SetClassifications(cls ...Classification) {
log.cls = cls
}

// SetListener will set the Logger to write to the specified Listener.
func SetListener(lst Listener) {
// SetListener will set the Logger to write to the specified listener.
func SetListener(lst func(Classification, string)) {
log.lst = lst
}

Expand All @@ -74,7 +69,7 @@ func Should(cls Classification) bool {
return false
}

// Write invokes the underlying Listener with the specified classification and message.
// Write invokes the underlying listener with the specified classification and message.
// If the classification shouldn't be logged or there is no listener then Write does nothing.
func Write(cls Classification, message string) {
if !Should(cls) {
Expand All @@ -83,7 +78,7 @@ func Write(cls Classification, message string) {
log.lst(cls, message)
}

// Writef invokes the underlying Listener with the specified classification and formatted message.
// Writef invokes the underlying listener with the specified classification and formatted message.
// If the classification shouldn't be logged or there is no listener then Writef does nothing.
func Writef(cls Classification, format string, a ...interface{}) {
if !Should(cls) {
Expand Down
45 changes: 0 additions & 45 deletions sdk/internal/runtime/frame_error.go

This file was deleted.

42 changes: 0 additions & 42 deletions sdk/internal/runtime/frame_error_test.go

This file was deleted.

43 changes: 0 additions & 43 deletions sdk/internal/runtime/response_error.go

This file was deleted.

0 comments on commit dea8545

Please sign in to comment.