// ------------------------------------------------------------------------ // Project active2 // Active Thing (activething.com) git.activething.com/go // // File name stack_frame.go // Created by DEV // Modified 20/03/2024 // // Copyright 2024 activething.com // ------------------------------------------------------------------------ // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ------------------------------------------------------------------------ package stack import ( "bufio" "bytes" "os" "runtime" "strings" ) type ( Frame struct { LineNumber int // Number line File string // File FuncName FuncName string // Function name Package string // Package name Counter uintptr files map[string][]byte } ) func NewFrame(counter uintptr) *Frame { if counter == 0 { return nil } sf := &Frame{ Counter: counter, } fn := runtime.FuncForPC(counter) sf.Package, sf.FuncName = packageAndName(fn) sf.File, sf.LineNumber = fn.FileLine(counter-1) return sf } func (f Frame) SourceLine() (string, error) { if f.LineNumber <= 0 { return stackUnknownInfo, nil } file, err := os.Open(f.File) if err != nil { return "", err } scanner := bufio.NewScanner(file) currentLine := 1 for scanner.Scan() { if currentLine == f.LineNumber { return string(bytes.Trim(scanner.Bytes(), " \t")), nil } currentLine++ } _ = file.Close() if err := scanner.Err(); err != nil { return "", err } return stackUnknownInfo, nil } func packageAndName (fn *runtime.Func) (string,string) { nm := fn.Name() pg := "" if ls := strings.LastIndex(nm,"/"); ls >= 0 { pg = nm[:ls] + "/" nm = nm[ls+1:] } if pd := strings.Index(nm,"."); pd >= 0 { pg += nm[:pd] nm = nm[pd+1:] } return pg,strings.Replace(nm,"ยท", ".",-1) }