Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renamed package runtime to diag #15278

Merged
merged 2 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.