mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-06-26 18:18:24 +00:00
165
vendor/github.com/tsaikd/KDGoLib/LICENSE
generated
vendored
165
vendor/github.com/tsaikd/KDGoLib/LICENSE
generated
vendored
@@ -1,165 +0,0 @@
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
|
||||
This version of the GNU Lesser General Public License incorporates
|
||||
the terms and conditions of version 3 of the GNU General Public
|
||||
License, supplemented by the additional permissions listed below.
|
||||
|
||||
0. Additional Definitions.
|
||||
|
||||
As used herein, "this License" refers to version 3 of the GNU Lesser
|
||||
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
||||
General Public License.
|
||||
|
||||
"The Library" refers to a covered work governed by this License,
|
||||
other than an Application or a Combined Work as defined below.
|
||||
|
||||
An "Application" is any work that makes use of an interface provided
|
||||
by the Library, but which is not otherwise based on the Library.
|
||||
Defining a subclass of a class defined by the Library is deemed a mode
|
||||
of using an interface provided by the Library.
|
||||
|
||||
A "Combined Work" is a work produced by combining or linking an
|
||||
Application with the Library. The particular version of the Library
|
||||
with which the Combined Work was made is also called the "Linked
|
||||
Version".
|
||||
|
||||
The "Minimal Corresponding Source" for a Combined Work means the
|
||||
Corresponding Source for the Combined Work, excluding any source code
|
||||
for portions of the Combined Work that, considered in isolation, are
|
||||
based on the Application, and not on the Linked Version.
|
||||
|
||||
The "Corresponding Application Code" for a Combined Work means the
|
||||
object code and/or source code for the Application, including any data
|
||||
and utility programs needed for reproducing the Combined Work from the
|
||||
Application, but excluding the System Libraries of the Combined Work.
|
||||
|
||||
1. Exception to Section 3 of the GNU GPL.
|
||||
|
||||
You may convey a covered work under sections 3 and 4 of this License
|
||||
without being bound by section 3 of the GNU GPL.
|
||||
|
||||
2. Conveying Modified Versions.
|
||||
|
||||
If you modify a copy of the Library, and, in your modifications, a
|
||||
facility refers to a function or data to be supplied by an Application
|
||||
that uses the facility (other than as an argument passed when the
|
||||
facility is invoked), then you may convey a copy of the modified
|
||||
version:
|
||||
|
||||
a) under this License, provided that you make a good faith effort to
|
||||
ensure that, in the event an Application does not supply the
|
||||
function or data, the facility still operates, and performs
|
||||
whatever part of its purpose remains meaningful, or
|
||||
|
||||
b) under the GNU GPL, with none of the additional permissions of
|
||||
this License applicable to that copy.
|
||||
|
||||
3. Object Code Incorporating Material from Library Header Files.
|
||||
|
||||
The object code form of an Application may incorporate material from
|
||||
a header file that is part of the Library. You may convey such object
|
||||
code under terms of your choice, provided that, if the incorporated
|
||||
material is not limited to numerical parameters, data structure
|
||||
layouts and accessors, or small macros, inline functions and templates
|
||||
(ten or fewer lines in length), you do both of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the object code that the
|
||||
Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the object code with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
4. Combined Works.
|
||||
|
||||
You may convey a Combined Work under terms of your choice that,
|
||||
taken together, effectively do not restrict modification of the
|
||||
portions of the Library contained in the Combined Work and reverse
|
||||
engineering for debugging such modifications, if you also do each of
|
||||
the following:
|
||||
|
||||
a) Give prominent notice with each copy of the Combined Work that
|
||||
the Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
c) For a Combined Work that displays copyright notices during
|
||||
execution, include the copyright notice for the Library among
|
||||
these notices, as well as a reference directing the user to the
|
||||
copies of the GNU GPL and this license document.
|
||||
|
||||
d) Do one of the following:
|
||||
|
||||
0) Convey the Minimal Corresponding Source under the terms of this
|
||||
License, and the Corresponding Application Code in a form
|
||||
suitable for, and under terms that permit, the user to
|
||||
recombine or relink the Application with a modified version of
|
||||
the Linked Version to produce a modified Combined Work, in the
|
||||
manner specified by section 6 of the GNU GPL for conveying
|
||||
Corresponding Source.
|
||||
|
||||
1) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (a) uses at run time
|
||||
a copy of the Library already present on the user's computer
|
||||
system, and (b) will operate properly with a modified version
|
||||
of the Library that is interface-compatible with the Linked
|
||||
Version.
|
||||
|
||||
e) Provide Installation Information, but only if you would otherwise
|
||||
be required to provide such information under section 6 of the
|
||||
GNU GPL, and only to the extent that such information is
|
||||
necessary to install and execute a modified version of the
|
||||
Combined Work produced by recombining or relinking the
|
||||
Application with a modified version of the Linked Version. (If
|
||||
you use option 4d0, the Installation Information must accompany
|
||||
the Minimal Corresponding Source and Corresponding Application
|
||||
Code. If you use option 4d1, you must provide the Installation
|
||||
Information in the manner specified by section 6 of the GNU GPL
|
||||
for conveying Corresponding Source.)
|
||||
|
||||
5. Combined Libraries.
|
||||
|
||||
You may place library facilities that are a work based on the
|
||||
Library side by side in a single library together with other library
|
||||
facilities that are not Applications and are not covered by this
|
||||
License, and convey such a combined library under terms of your
|
||||
choice, if you do both of the following:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work based
|
||||
on the Library, uncombined with any other library facilities,
|
||||
conveyed under the terms of this License.
|
||||
|
||||
b) Give prominent notice with the combined library that part of it
|
||||
is a work based on the Library, and explaining where to find the
|
||||
accompanying uncombined form of the same work.
|
||||
|
||||
6. Revised Versions of the GNU Lesser General Public License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions
|
||||
of the GNU Lesser General Public License from time to time. Such new
|
||||
versions will be similar in spirit to the present version, but may
|
||||
differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Library as you received it specifies that a certain numbered version
|
||||
of the GNU Lesser General Public License "or any later version"
|
||||
applies to it, you have the option of following the terms and
|
||||
conditions either of that published version or of any later version
|
||||
published by the Free Software Foundation. If the Library as you
|
||||
received it does not specify a version number of the GNU Lesser
|
||||
General Public License, you may choose any version of the GNU Lesser
|
||||
General Public License ever published by the Free Software Foundation.
|
||||
|
||||
If the Library as you received it specifies that a proxy can decide
|
||||
whether future versions of the GNU Lesser General Public License shall
|
||||
apply, that proxy's public statement of acceptance of any version is
|
||||
permanent authorization for you to choose that version for the
|
||||
Library.
|
||||
88
vendor/github.com/tsaikd/KDGoLib/errutil/ConsoleFormatter.go
generated
vendored
88
vendor/github.com/tsaikd/KDGoLib/errutil/ConsoleFormatter.go
generated
vendored
@@ -1,88 +0,0 @@
|
||||
package errutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"time"
|
||||
)
|
||||
|
||||
// NewConsoleFormatter create JSONErrorFormatter instance
|
||||
func NewConsoleFormatter(seperator string) *ConsoleFormatter {
|
||||
return &ConsoleFormatter{
|
||||
Seperator: seperator,
|
||||
}
|
||||
}
|
||||
|
||||
// ConsoleFormatter used to format error object in console readable
|
||||
type ConsoleFormatter struct {
|
||||
// seperator between errors, e.g. "; " will output "err1; err2; err3"
|
||||
Seperator string
|
||||
// output timestamp for prefix, e.g. "2006-01-02 15:04:05 "
|
||||
TimeFormat string
|
||||
// show error position with long filename
|
||||
LongFile bool
|
||||
// show error position with short filename
|
||||
ShortFile bool
|
||||
// show error position with line number, work with LongFile or ShortFile
|
||||
Line bool
|
||||
// replace package name for securify
|
||||
ReplacePackages map[string]string
|
||||
}
|
||||
|
||||
// Format error object
|
||||
func (t *ConsoleFormatter) Format(errin error) (errtext string, err error) {
|
||||
return t.FormatSkip(errin, 1)
|
||||
}
|
||||
|
||||
// FormatSkip trace error line and format object
|
||||
func (t *ConsoleFormatter) FormatSkip(errin error, skip int) (errtext string, err error) {
|
||||
errobj := castErrorObject(nil, skip+1, errin)
|
||||
if errobj == nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
buffer := &bytes.Buffer{}
|
||||
|
||||
if t.TimeFormat != "" {
|
||||
if _, errio := buffer.WriteString(time.Now().Format(t.TimeFormat)); errio != nil {
|
||||
return buffer.String(), errio
|
||||
}
|
||||
}
|
||||
|
||||
if t.LongFile || t.ShortFile {
|
||||
if _, errio := WriteCallInfo(buffer, errobj, t.LongFile, t.Line, t.ReplacePackages); errio != nil {
|
||||
return buffer.String(), errio
|
||||
}
|
||||
if _, errio := buffer.WriteString(" "); errio != nil {
|
||||
return buffer.String(), errio
|
||||
}
|
||||
}
|
||||
|
||||
if t.Seperator == "" {
|
||||
if _, errio := buffer.WriteString(getErrorText(errin)); errio != nil {
|
||||
return buffer.String(), errio
|
||||
}
|
||||
return buffer.String(), nil
|
||||
}
|
||||
|
||||
firstError := true
|
||||
if walkerr := WalkErrors(errobj, func(errloop ErrorObject) (stop bool, walkerr error) {
|
||||
if !firstError {
|
||||
if _, errio := buffer.WriteString(t.Seperator); errio != nil {
|
||||
return true, errio
|
||||
}
|
||||
}
|
||||
firstError = false
|
||||
|
||||
if _, errio := buffer.WriteString(getErrorText(errloop)); errio != nil {
|
||||
return true, errio
|
||||
}
|
||||
return false, nil
|
||||
}); walkerr != nil {
|
||||
return buffer.String(), walkerr
|
||||
}
|
||||
|
||||
return buffer.String(), nil
|
||||
}
|
||||
|
||||
var _ ErrorFormatter = &ConsoleFormatter{}
|
||||
var _ TraceFormatter = &ConsoleFormatter{}
|
||||
113
vendor/github.com/tsaikd/KDGoLib/errutil/ErrorFactory.go
generated
vendored
113
vendor/github.com/tsaikd/KDGoLib/errutil/ErrorFactory.go
generated
vendored
@@ -1,113 +0,0 @@
|
||||
package errutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// ErrorFactory is used for create or check ErrorObject
|
||||
type ErrorFactory interface {
|
||||
Error() string
|
||||
Name() string
|
||||
|
||||
New(err error, params ...interface{}) ErrorObject
|
||||
Match(err error) bool
|
||||
In(err error) bool
|
||||
}
|
||||
|
||||
type errorFactory struct {
|
||||
errtext string
|
||||
name string
|
||||
}
|
||||
|
||||
var namedFactories = map[string]ErrorFactory{}
|
||||
|
||||
// AllNamedFactories return all named factories
|
||||
func AllNamedFactories() map[string]ErrorFactory {
|
||||
return namedFactories
|
||||
}
|
||||
|
||||
// AllSortedNamedFactories return all sorted named factories
|
||||
// NOTE: this is slow for sorting in runtime
|
||||
func AllSortedNamedFactories() []ErrorFactory {
|
||||
sorter := newSorter(namedFactories)
|
||||
sort.Sort(sorter)
|
||||
return sorter.data
|
||||
}
|
||||
|
||||
// NewFactory return new ErrorFactory instance
|
||||
func NewFactory(errtext string) ErrorFactory {
|
||||
callinfo, _ := RuntimeCaller(1)
|
||||
return NewNamedFactory(callinfo.PackageName()+"->"+errtext, errtext)
|
||||
}
|
||||
|
||||
// NewNamedFactory return new ErrorFactory instance with factory name, panic if name duplicated
|
||||
func NewNamedFactory(name string, errtext string) ErrorFactory {
|
||||
if _, ok := namedFactories[name]; ok {
|
||||
panic(fmt.Errorf("error factory name duplicated: %q", name))
|
||||
}
|
||||
factory := &errorFactory{
|
||||
errtext: errtext,
|
||||
name: name,
|
||||
}
|
||||
namedFactories[name] = factory
|
||||
return factory
|
||||
}
|
||||
|
||||
// FactoryOf return factory of error, return nil if not factory found
|
||||
func FactoryOf(err error) ErrorFactory {
|
||||
errobj := castErrorObject(nil, 1, err)
|
||||
if errobj == nil {
|
||||
return nil
|
||||
}
|
||||
return errobj.Factory()
|
||||
}
|
||||
|
||||
func (t errorFactory) Error() string {
|
||||
return t.errtext
|
||||
}
|
||||
|
||||
func (t errorFactory) Name() string {
|
||||
return t.name
|
||||
}
|
||||
|
||||
func (t *errorFactory) New(parent error, params ...interface{}) ErrorObject {
|
||||
errobj := castErrorObject(t, 1, fmt.Errorf(t.errtext, params...))
|
||||
errobj.SetParent(castErrorObject(nil, 1, parent))
|
||||
return errobj
|
||||
}
|
||||
|
||||
func (t *errorFactory) Match(err error) bool {
|
||||
if t == nil || err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
errcomp := castErrorObject(nil, 1, err)
|
||||
if errcomp == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return errcomp.Factory() == t
|
||||
}
|
||||
|
||||
func (t *errorFactory) In(err error) bool {
|
||||
if t == nil || err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
exist := false
|
||||
|
||||
if errtmp := WalkErrors(castErrorObject(nil, 1, err), func(errcomp ErrorObject) (stop bool, walkerr error) {
|
||||
if errcomp.Factory() == t {
|
||||
exist = true
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}); errtmp != nil {
|
||||
panic(errtmp)
|
||||
}
|
||||
|
||||
return exist
|
||||
}
|
||||
|
||||
var _ ErrorFactory = (*errorFactory)(nil)
|
||||
12
vendor/github.com/tsaikd/KDGoLib/errutil/ErrorFormatter.go
generated
vendored
12
vendor/github.com/tsaikd/KDGoLib/errutil/ErrorFormatter.go
generated
vendored
@@ -1,12 +0,0 @@
|
||||
package errutil
|
||||
|
||||
// ErrorFormatter to format error
|
||||
type ErrorFormatter interface {
|
||||
Format(error) (string, error)
|
||||
}
|
||||
|
||||
// TraceFormatter to trace error occur line formatter
|
||||
type TraceFormatter interface {
|
||||
ErrorFormatter
|
||||
FormatSkip(errin error, skip int) (string, error)
|
||||
}
|
||||
57
vendor/github.com/tsaikd/KDGoLib/errutil/ErrorJSON.go
generated
vendored
57
vendor/github.com/tsaikd/KDGoLib/errutil/ErrorJSON.go
generated
vendored
@@ -1,57 +0,0 @@
|
||||
package errutil
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ErrorJSON is a helper struct for display error
|
||||
type ErrorJSON struct {
|
||||
ErrorPath string `json:"errorpath,omitempty"`
|
||||
ErrorMsg string `json:"error,omitempty"`
|
||||
ErrorMsgs []string `json:"errors,omitempty"`
|
||||
ErrorFactories map[string]bool `json:"errfac,omitempty"`
|
||||
}
|
||||
|
||||
// NewJSON create ErrorJSON
|
||||
func NewJSON(err error) (*ErrorJSON, error) {
|
||||
return newJSON(1, err)
|
||||
}
|
||||
|
||||
func newJSON(skip int, err error) (*ErrorJSON, error) {
|
||||
errobj := castErrorObject(nil, skip+1, err)
|
||||
if errobj == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
errors := []string{}
|
||||
facs := map[string]bool{}
|
||||
if err := WalkErrors(errobj, func(errcomp ErrorObject) (stop bool, walkerr error) {
|
||||
errors = append(errors, getErrorText(errcomp))
|
||||
factory := errcomp.Factory()
|
||||
if factory != nil {
|
||||
if !strings.Contains(factory.Name(), "->") {
|
||||
facs[factory.Name()] = true
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ErrorJSON{
|
||||
ErrorPath: errobj.PackageName() + "/" + errobj.FileName() + ":" + strconv.Itoa(errobj.Line()),
|
||||
ErrorMsg: getErrorText(errobj),
|
||||
ErrorMsgs: errors,
|
||||
ErrorFactories: facs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *ErrorJSON) Error() string {
|
||||
if t == nil {
|
||||
return ""
|
||||
}
|
||||
return t.ErrorMsg
|
||||
}
|
||||
|
||||
var _ error = (*ErrorJSON)(nil)
|
||||
133
vendor/github.com/tsaikd/KDGoLib/errutil/ErrorObject.go
generated
vendored
133
vendor/github.com/tsaikd/KDGoLib/errutil/ErrorObject.go
generated
vendored
@@ -1,133 +0,0 @@
|
||||
package errutil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/tsaikd/KDGoLib/runtimecaller"
|
||||
)
|
||||
|
||||
// errors
|
||||
var (
|
||||
ErrorWalkLoop = NewFactory("detect error component parents loop when walking")
|
||||
)
|
||||
|
||||
// New return a new ErrorObject object
|
||||
func New(text string, errs ...error) ErrorObject {
|
||||
if text != "" {
|
||||
errs = append([]error{errors.New(text)}, errs...)
|
||||
}
|
||||
return NewErrorsSkip(1, errs...)
|
||||
}
|
||||
|
||||
// NewErrors return ErrorObject that contains all input errors
|
||||
func NewErrors(errs ...error) ErrorObject {
|
||||
return NewErrorsSkip(1, errs...)
|
||||
}
|
||||
|
||||
// NewErrorsSkip return ErrorObject, skip function call
|
||||
func NewErrorsSkip(skip int, errs ...error) ErrorObject {
|
||||
var errcomp ErrorObject
|
||||
var errtmp ErrorObject
|
||||
for i, size := 0, len(errs); i < size; i++ {
|
||||
errtmp = castErrorObject(nil, skip+1, errs[i])
|
||||
if errtmp == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if errcomp == nil {
|
||||
errcomp = errtmp
|
||||
continue
|
||||
}
|
||||
|
||||
if err := AddParent(errcomp, errtmp); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
return errcomp
|
||||
}
|
||||
|
||||
// ErrorObject is a rich error interface
|
||||
type ErrorObject interface {
|
||||
Error() string
|
||||
Factory() ErrorFactory
|
||||
Parent() ErrorObject
|
||||
SetParent(parent ErrorObject) ErrorObject
|
||||
runtimecaller.CallInfo
|
||||
}
|
||||
|
||||
type errorObject struct {
|
||||
errtext string
|
||||
factory ErrorFactory
|
||||
parent ErrorObject
|
||||
runtimecaller.CallInfo
|
||||
}
|
||||
|
||||
func castErrorObject(factory ErrorFactory, skip int, err error) ErrorObject {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
switch err.(type) {
|
||||
case errorObject:
|
||||
res := err.(errorObject)
|
||||
return &res
|
||||
case *errorObject:
|
||||
return err.(*errorObject)
|
||||
case ErrorObject:
|
||||
return err.(ErrorObject)
|
||||
default:
|
||||
callinfo, _ := RuntimeCaller(skip + 1)
|
||||
return &errorObject{
|
||||
errtext: err.Error(),
|
||||
factory: factory,
|
||||
CallInfo: callinfo,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getErrorText(errin error) string {
|
||||
errobj, ok := errin.(*errorObject)
|
||||
if ok {
|
||||
return errobj.errtext
|
||||
}
|
||||
return errin.Error()
|
||||
}
|
||||
|
||||
func (t errorObject) Error() string {
|
||||
errtext, _ := errorObjectFormatter.Format(&t)
|
||||
return errtext
|
||||
}
|
||||
|
||||
func (t *errorObject) Factory() ErrorFactory {
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
return t.factory
|
||||
}
|
||||
|
||||
func (t *errorObject) Parent() ErrorObject {
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
return t.parent
|
||||
}
|
||||
|
||||
func (t *errorObject) SetParent(parent ErrorObject) ErrorObject {
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
if t == parent {
|
||||
return t
|
||||
}
|
||||
t.parent = parent
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *errorObject) MarshalJSON() ([]byte, error) {
|
||||
return MarshalJSON(t)
|
||||
}
|
||||
|
||||
var _ ErrorObject = (*errorObject)(nil)
|
||||
|
||||
var errorObjectFormatter = ErrorFormatter(&ConsoleFormatter{
|
||||
Seperator: "; ",
|
||||
})
|
||||
88
vendor/github.com/tsaikd/KDGoLib/errutil/ErrorObjectUtil.go
generated
vendored
88
vendor/github.com/tsaikd/KDGoLib/errutil/ErrorObjectUtil.go
generated
vendored
@@ -1,88 +0,0 @@
|
||||
package errutil
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// WalkFunc is a callback for WalkErrors
|
||||
type WalkFunc func(errcomp ErrorObject) (stop bool, err error)
|
||||
|
||||
// WalkErrors walk from base error through all parents
|
||||
// return ErrorWalkLoop if detected loop
|
||||
func WalkErrors(base ErrorObject, walkFunc WalkFunc) (err error) {
|
||||
if base == nil {
|
||||
return
|
||||
}
|
||||
|
||||
loopCheckMap := map[ErrorObject]bool{}
|
||||
for base != nil {
|
||||
if _, exist := loopCheckMap[base]; exist {
|
||||
return ErrorWalkLoop.New(nil)
|
||||
}
|
||||
loopCheckMap[base] = true
|
||||
|
||||
stop, walkerr := walkFunc(base)
|
||||
if walkerr != nil {
|
||||
return walkerr
|
||||
}
|
||||
if stop {
|
||||
return
|
||||
}
|
||||
|
||||
base = base.Parent()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Length count number of ErrorObject and all parents, return -1 if error
|
||||
func Length(base ErrorObject) int {
|
||||
length := 0
|
||||
if err := WalkErrors(base, func(errcomp ErrorObject) (stop bool, walkerr error) {
|
||||
length++
|
||||
return false, nil
|
||||
}); err != nil {
|
||||
return -1
|
||||
}
|
||||
return length
|
||||
}
|
||||
|
||||
// AddParent add parent to errobj
|
||||
func AddParent(errobj ErrorObject, parent ErrorObject) error {
|
||||
if errobj == nil || parent == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// set parent if not exist
|
||||
if errobj.Parent() == nil {
|
||||
errobj.SetParent(parent)
|
||||
return nil
|
||||
}
|
||||
|
||||
// find oldest parent to set
|
||||
base := errobj
|
||||
if err := WalkErrors(base.Parent(), func(errcomp ErrorObject) (stop bool, walkerr error) {
|
||||
// already in parent tree
|
||||
if errcomp == parent {
|
||||
base = nil
|
||||
return true, nil
|
||||
}
|
||||
base = errcomp
|
||||
return false, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if base != nil {
|
||||
base.SetParent(parent)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON marshal error to json
|
||||
func MarshalJSON(errobj error) ([]byte, error) {
|
||||
errjson, err := newJSON(1, errobj)
|
||||
if errjson == nil || err != nil {
|
||||
return []byte(""), err
|
||||
}
|
||||
return json.Marshal(errjson)
|
||||
}
|
||||
37
vendor/github.com/tsaikd/KDGoLib/errutil/JSONFormatter.go
generated
vendored
37
vendor/github.com/tsaikd/KDGoLib/errutil/JSONFormatter.go
generated
vendored
@@ -1,37 +0,0 @@
|
||||
package errutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// NewJSONFormatter create JSONFormatter instance
|
||||
func NewJSONFormatter() *JSONFormatter {
|
||||
return &JSONFormatter{}
|
||||
}
|
||||
|
||||
// JSONFormatter used to format error to json
|
||||
type JSONFormatter struct{}
|
||||
|
||||
// Format error to json
|
||||
func (t *JSONFormatter) Format(errin error) (errtext string, err error) {
|
||||
return t.FormatSkip(errin, 1)
|
||||
}
|
||||
|
||||
// FormatSkip trace error line and format to json
|
||||
func (t *JSONFormatter) FormatSkip(errin error, skip int) (errtext string, err error) {
|
||||
errjson, err := newJSON(skip+1, errin)
|
||||
if errjson == nil || err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
buffer := &bytes.Buffer{}
|
||||
if err = json.NewEncoder(buffer).Encode(errjson); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return buffer.String(), nil
|
||||
}
|
||||
|
||||
var _ ErrorFormatter = &JSONFormatter{}
|
||||
var _ TraceFormatter = &JSONFormatter{}
|
||||
88
vendor/github.com/tsaikd/KDGoLib/errutil/README.md
generated
vendored
88
vendor/github.com/tsaikd/KDGoLib/errutil/README.md
generated
vendored
@@ -1,88 +0,0 @@
|
||||
errutil
|
||||
=======
|
||||
|
||||
An error handling helper, providing more APIs than built-in package (errors, fmt), and compatible with go error interface
|
||||
|
||||
## Why use errutil instead of built-in errors and fmt
|
||||
|
||||
[](https://milkr.io/tsaikd/Go-lang-error-handling)
|
||||
|
||||
* https://milkr.io/tsaikd/Go-lang-error-handling
|
||||
|
||||
## Usage
|
||||
|
||||
* Import package from master branch
|
||||
|
||||
```
|
||||
import "github.com/tsaikd/KDGoLib/errutil"
|
||||
```
|
||||
|
||||
* Declare error factory
|
||||
|
||||
```
|
||||
var ErrorOccurWithReason = errutil.NewFactory("An error occur, reason: %q")
|
||||
```
|
||||
|
||||
* Return error with factory
|
||||
|
||||
```
|
||||
func doSomething() (err error) {
|
||||
// do something
|
||||
|
||||
// return error with factory,
|
||||
// first argument is parent error,
|
||||
// the others are used for factory
|
||||
return ErrorOccurWithReason.New(nil, "some reason here")
|
||||
}
|
||||
```
|
||||
|
||||
* Handle errors
|
||||
|
||||
```
|
||||
func errorHandlingForOneCase() {
|
||||
if err := doSomething(); err != nil {
|
||||
if ErrorOccurWithReason.In(err) {
|
||||
// handling expected error
|
||||
return
|
||||
}
|
||||
|
||||
// handling unexpected error
|
||||
return
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```
|
||||
func errorHandlingForMultipleCases() {
|
||||
if err := doSomething(); err != nil {
|
||||
switch errutil.FactoryOf(err) {
|
||||
case ErrorOccurWithReason:
|
||||
// handling expected error
|
||||
return
|
||||
default:
|
||||
// handling unexpected error
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Optional usage
|
||||
|
||||
* Import from v1 branch
|
||||
|
||||
```
|
||||
import "gopkg.in/tsaikd/KDGoLib.v1/errutil"
|
||||
```
|
||||
|
||||
* Use like built-in errors package
|
||||
* bad case because all errors should be exported for catching by other package
|
||||
|
||||
```
|
||||
func doSomething() (err error) {
|
||||
// do something
|
||||
|
||||
// return error with factory
|
||||
return errutil.New("An error occur")
|
||||
}
|
||||
```
|
||||
86
vendor/github.com/tsaikd/KDGoLib/errutil/RuntimeCaller.go
generated
vendored
86
vendor/github.com/tsaikd/KDGoLib/errutil/RuntimeCaller.go
generated
vendored
@@ -1,86 +0,0 @@
|
||||
package errutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/tsaikd/KDGoLib/runtimecaller"
|
||||
)
|
||||
|
||||
// DefaultRuntimeCallerFilter use for filter error stack info
|
||||
var DefaultRuntimeCallerFilter = []runtimecaller.Filter{}
|
||||
|
||||
func init() {
|
||||
DefaultRuntimeCallerFilter = append(
|
||||
runtimecaller.FilterCommons,
|
||||
RuntimeCallerFilterStopErrutilPackage,
|
||||
RuntimeCallerFilterStopSQLUtilPackage,
|
||||
)
|
||||
}
|
||||
|
||||
// AddRuntimeCallerFilter add filters to DefaultRuntimeCallerFilter for RuntimeCaller()
|
||||
func AddRuntimeCallerFilter(filters ...runtimecaller.Filter) {
|
||||
DefaultRuntimeCallerFilter = append(DefaultRuntimeCallerFilter, filters...)
|
||||
}
|
||||
|
||||
// RuntimeCallerFilterStopErrutilPackage filter CallInfo to stop after reach KDGoLib/errutil package
|
||||
func RuntimeCallerFilterStopErrutilPackage(callinfo runtimecaller.CallInfo) (valid bool, stop bool) {
|
||||
if callinfo.PackageName() == "github.com/tsaikd/KDGoLib/errutil" {
|
||||
return false, true
|
||||
}
|
||||
return true, false
|
||||
}
|
||||
|
||||
// RuntimeCallerFilterStopSQLUtilPackage filter CallInfo to stop after reach KDGoLib/sqlutil package
|
||||
func RuntimeCallerFilterStopSQLUtilPackage(callinfo runtimecaller.CallInfo) (valid bool, stop bool) {
|
||||
if callinfo.PackageName() == "github.com/tsaikd/KDGoLib/sqlutil" {
|
||||
return false, true
|
||||
}
|
||||
return true, false
|
||||
}
|
||||
|
||||
// RuntimeCaller wrap runtimecaller.GetByFilters() with DefaultRuntimeCallerFilter
|
||||
func RuntimeCaller(skip int, extraFilters ...runtimecaller.Filter) (callinfo runtimecaller.CallInfo, ok bool) {
|
||||
filters := append(DefaultRuntimeCallerFilter, extraFilters...)
|
||||
return runtimecaller.GetByFilters(skip+1, filters...)
|
||||
}
|
||||
|
||||
// WriteCallInfo write readable callinfo with options
|
||||
func WriteCallInfo(
|
||||
output io.Writer,
|
||||
callinfo runtimecaller.CallInfo,
|
||||
longFile bool,
|
||||
line bool,
|
||||
replacePackages map[string]string,
|
||||
) (n int, err error) {
|
||||
buffer := &bytes.Buffer{}
|
||||
if longFile {
|
||||
pkgname := replacePackage(replacePackages, callinfo.PackageName())
|
||||
if _, err = buffer.WriteString(pkgname + "/" + callinfo.FileName()); err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if _, err = buffer.WriteString(callinfo.FileName()); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if line {
|
||||
if _, err = buffer.WriteString(":" + strconv.Itoa(callinfo.Line())); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return output.Write([]byte(buffer.String()))
|
||||
}
|
||||
|
||||
func replacePackage(replacePackages map[string]string, pkgname string) (replaced string) {
|
||||
replaced = pkgname
|
||||
if replacePackages == nil {
|
||||
return
|
||||
}
|
||||
for src, tar := range replacePackages {
|
||||
replaced = strings.Replace(replaced, src, tar, -1)
|
||||
}
|
||||
return
|
||||
}
|
||||
127
vendor/github.com/tsaikd/KDGoLib/errutil/logger.go
generated
vendored
127
vendor/github.com/tsaikd/KDGoLib/errutil/logger.go
generated
vendored
@@ -1,127 +0,0 @@
|
||||
package errutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// LoggerType declare general log types
|
||||
type LoggerType interface {
|
||||
Debug(v ...interface{})
|
||||
Print(v ...interface{})
|
||||
Error(v ...interface{})
|
||||
Trace(errin error)
|
||||
TraceSkip(errin error, skip int)
|
||||
}
|
||||
|
||||
// Logger return default LoggerType instance
|
||||
func Logger() LoggerType {
|
||||
return defaultLogger
|
||||
}
|
||||
|
||||
// NewLogger create LoggerType instance
|
||||
func NewLogger(opt LoggerOptions) LoggerType {
|
||||
opt.check()
|
||||
return loggerImpl{
|
||||
opt: opt,
|
||||
}
|
||||
}
|
||||
|
||||
// LoggerOptions for Logger
|
||||
type LoggerOptions struct {
|
||||
DefaultOutput io.Writer
|
||||
ErrorOutput io.Writer
|
||||
HideFile bool
|
||||
ShortFile bool
|
||||
HideLine bool
|
||||
ReplacePackages map[string]string
|
||||
TraceFormatter TraceFormatter
|
||||
}
|
||||
|
||||
func (t *LoggerOptions) check() {
|
||||
if t.DefaultOutput == nil {
|
||||
t.DefaultOutput = os.Stdout
|
||||
}
|
||||
if t.ErrorOutput == nil {
|
||||
t.ErrorOutput = os.Stderr
|
||||
}
|
||||
if t.TraceFormatter == nil {
|
||||
t.TraceFormatter = &ConsoleFormatter{
|
||||
Seperator: "; ",
|
||||
TimeFormat: "2006-01-02 15:04:05 ",
|
||||
LongFile: true,
|
||||
Line: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var defaultLogger = NewLogger(LoggerOptions{})
|
||||
|
||||
// SetDefaultLogger set default LoggerType
|
||||
func SetDefaultLogger(logger LoggerType) {
|
||||
defaultLogger = logger
|
||||
}
|
||||
|
||||
type loggerImpl struct {
|
||||
opt LoggerOptions
|
||||
}
|
||||
|
||||
func (t loggerImpl) Debug(v ...interface{}) {
|
||||
t.log(t.opt.DefaultOutput, 1, v...)
|
||||
}
|
||||
|
||||
func (t loggerImpl) Print(v ...interface{}) {
|
||||
t.log(t.opt.DefaultOutput, 1, v...)
|
||||
}
|
||||
|
||||
func (t loggerImpl) Error(v ...interface{}) {
|
||||
t.log(t.opt.ErrorOutput, 1, v...)
|
||||
}
|
||||
|
||||
func (t loggerImpl) log(output io.Writer, skip int, v ...interface{}) {
|
||||
errtext := fmt.Sprint(v...)
|
||||
if errtext == "" {
|
||||
return
|
||||
}
|
||||
|
||||
opt := t.opt
|
||||
if !opt.HideFile {
|
||||
buffer := &bytes.Buffer{}
|
||||
callinfo, _ := RuntimeCaller(skip + 1)
|
||||
if _, err := WriteCallInfo(buffer, callinfo, !opt.ShortFile, !opt.HideLine, opt.ReplacePackages); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
errtext = buffer.String() + " " + errtext
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(errtext, "\n") {
|
||||
errtext += "\n"
|
||||
}
|
||||
if _, err := output.Write([]byte(errtext)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (t loggerImpl) Trace(errin error) {
|
||||
TraceSkip(errin, 1)
|
||||
}
|
||||
|
||||
func (t loggerImpl) TraceSkip(errin error, skip int) {
|
||||
var errtext string
|
||||
var errfmt error
|
||||
if errtext, errfmt = t.opt.TraceFormatter.FormatSkip(errin, skip+1); errfmt != nil {
|
||||
panic(errfmt)
|
||||
}
|
||||
if errtext == "" {
|
||||
return
|
||||
}
|
||||
if !strings.HasSuffix(errtext, "\n") {
|
||||
errtext += "\n"
|
||||
}
|
||||
if _, errfmt = t.opt.ErrorOutput.Write([]byte(errtext)); errfmt != nil {
|
||||
panic(errfmt)
|
||||
}
|
||||
}
|
||||
27
vendor/github.com/tsaikd/KDGoLib/errutil/sort.go
generated
vendored
27
vendor/github.com/tsaikd/KDGoLib/errutil/sort.go
generated
vendored
@@ -1,27 +0,0 @@
|
||||
package errutil
|
||||
|
||||
func newSorter(factoryMap map[string]ErrorFactory) *sorter {
|
||||
data := []ErrorFactory{}
|
||||
for _, factory := range factoryMap {
|
||||
data = append(data, factory)
|
||||
}
|
||||
return &sorter{
|
||||
data: data,
|
||||
}
|
||||
}
|
||||
|
||||
type sorter struct {
|
||||
data []ErrorFactory
|
||||
}
|
||||
|
||||
func (t sorter) Len() int {
|
||||
return len(t.data)
|
||||
}
|
||||
|
||||
func (t sorter) Swap(i int, j int) {
|
||||
t.data[i], t.data[j] = t.data[j], t.data[i]
|
||||
}
|
||||
|
||||
func (t sorter) Less(i int, j int) bool {
|
||||
return t.data[i].Name() < t.data[j].Name()
|
||||
}
|
||||
19
vendor/github.com/tsaikd/KDGoLib/errutil/trace.go
generated
vendored
19
vendor/github.com/tsaikd/KDGoLib/errutil/trace.go
generated
vendored
@@ -1,19 +0,0 @@
|
||||
package errutil
|
||||
|
||||
// Trace error stack, output to default ErrorFormatter, panic if output error
|
||||
func Trace(errin error) {
|
||||
TraceSkip(errin, 1)
|
||||
}
|
||||
|
||||
// TraceWrap trace errin and wrap with wraperr only if errin != nil
|
||||
func TraceWrap(errin error, wraperr error) {
|
||||
if errin != nil {
|
||||
errs := NewErrorsSkip(1, wraperr, errin)
|
||||
TraceSkip(errs, 1)
|
||||
}
|
||||
}
|
||||
|
||||
// TraceSkip error stack, output to default ErrorFormatter, skip n function calls, panic if output error
|
||||
func TraceSkip(errin error, skip int) {
|
||||
Logger().TraceSkip(errin, 1)
|
||||
}
|
||||
19
vendor/github.com/tsaikd/KDGoLib/errutil/util.go
generated
vendored
19
vendor/github.com/tsaikd/KDGoLib/errutil/util.go
generated
vendored
@@ -1,19 +0,0 @@
|
||||
package errutil
|
||||
|
||||
// ContainErrorFunc check error contain error by custom equalFunc
|
||||
func ContainErrorFunc(err error, equalFunc func(error) bool) bool {
|
||||
errobj := castErrorObject(nil, 1, err)
|
||||
contain := false
|
||||
|
||||
if walkerr := WalkErrors(errobj, func(errcomp ErrorObject) (stop bool, walkerr error) {
|
||||
if equalFunc(errcomp) {
|
||||
contain = true
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}); walkerr != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return contain
|
||||
}
|
||||
106
vendor/github.com/tsaikd/KDGoLib/logrusutil/ConsoleLogFormatter.go
generated
vendored
106
vendor/github.com/tsaikd/KDGoLib/logrusutil/ConsoleLogFormatter.go
generated
vendored
@@ -1,106 +0,0 @@
|
||||
package logrusutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/tsaikd/KDGoLib/errutil"
|
||||
"github.com/tsaikd/KDGoLib/runtimecaller"
|
||||
)
|
||||
|
||||
// flags
|
||||
const (
|
||||
Llongfile = 1 << iota
|
||||
Lshortfile
|
||||
Ltime
|
||||
Llevel
|
||||
LstdFlags = Ltime | Lshortfile | Llevel
|
||||
)
|
||||
|
||||
// ConsoleLogFormatter suitable formatter for console
|
||||
type ConsoleLogFormatter struct {
|
||||
TimestampFormat string
|
||||
Flag int
|
||||
CallerOffset int
|
||||
RuntimeCallerFilters []runtimecaller.Filter
|
||||
}
|
||||
|
||||
func addspace(text string, addspaceflag bool) (string, bool) {
|
||||
if addspaceflag {
|
||||
return " " + text, true
|
||||
}
|
||||
return text, true
|
||||
}
|
||||
|
||||
func filterLogrusRuntimeCaller(callinfo runtimecaller.CallInfo) (valid bool, stop bool) {
|
||||
return !strings.Contains(callinfo.PackageName(), "github.com/sirupsen/logrus"), false
|
||||
}
|
||||
|
||||
// Format output logrus entry
|
||||
func (t *ConsoleLogFormatter) Format(entry *logrus.Entry) (data []byte, err error) {
|
||||
buffer := bytes.Buffer{}
|
||||
addspaceflag := false
|
||||
|
||||
if t.Flag == 0 {
|
||||
t.Flag = LstdFlags
|
||||
}
|
||||
|
||||
if t.TimestampFormat == "" {
|
||||
t.TimestampFormat = time.RFC3339
|
||||
}
|
||||
|
||||
if t.Flag&Ltime != 0 {
|
||||
timetext := entry.Time.Format(t.TimestampFormat)
|
||||
timetext, addspaceflag = addspace(timetext, addspaceflag)
|
||||
if _, err = buffer.WriteString(timetext); err != nil {
|
||||
err = errutil.New("write timestamp to buffer failed", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if t.Flag&(Lshortfile|Llongfile) != 0 {
|
||||
var filelinetext string
|
||||
filters := append([]runtimecaller.Filter{filterLogrusRuntimeCaller}, t.RuntimeCallerFilters...)
|
||||
if callinfo, ok := errutil.RuntimeCaller(1+t.CallerOffset, filters...); ok {
|
||||
if t.Flag&Lshortfile != 0 {
|
||||
filelinetext = fmt.Sprintf("%s:%d", callinfo.FileName(), callinfo.Line())
|
||||
} else {
|
||||
filelinetext = fmt.Sprintf("%s/%s:%d", callinfo.PackageName(), callinfo.FileName(), callinfo.Line())
|
||||
}
|
||||
|
||||
filelinetext, addspaceflag = addspace(filelinetext, addspaceflag)
|
||||
}
|
||||
|
||||
if _, err = buffer.WriteString(filelinetext); err != nil {
|
||||
err = errutil.New("write fileline to buffer failed", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if t.Flag&Llevel != 0 {
|
||||
leveltext := fmt.Sprintf("[%s]", entry.Level.String())
|
||||
leveltext, addspaceflag = addspace(leveltext, addspaceflag)
|
||||
if _, err = buffer.WriteString(leveltext); err != nil {
|
||||
err = errutil.New("write level to buffer failed", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
message := entry.Message
|
||||
message, _ = addspace(message, addspaceflag)
|
||||
if _, err = buffer.WriteString(message); err != nil {
|
||||
err = errutil.New("write message to buffer failed", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = buffer.WriteByte('\n'); err != nil {
|
||||
err = errutil.New("write newline to buffer failed", err)
|
||||
return
|
||||
}
|
||||
|
||||
data = buffer.Bytes()
|
||||
return
|
||||
}
|
||||
17
vendor/github.com/tsaikd/KDGoLib/logrusutil/ConsoleLogger.go
generated
vendored
17
vendor/github.com/tsaikd/KDGoLib/logrusutil/ConsoleLogger.go
generated
vendored
@@ -1,17 +0,0 @@
|
||||
package logrusutil
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// NewConsoleLogger create new Logger with ConsoleLogFormatter
|
||||
func NewConsoleLogger() *logrus.Logger {
|
||||
return &logrus.Logger{
|
||||
Out: os.Stdout,
|
||||
Formatter: &ConsoleLogFormatter{},
|
||||
Hooks: make(logrus.LevelHooks),
|
||||
Level: logrus.InfoLevel,
|
||||
}
|
||||
}
|
||||
17
vendor/github.com/tsaikd/KDGoLib/logrusutil/stack.go
generated
vendored
17
vendor/github.com/tsaikd/KDGoLib/logrusutil/stack.go
generated
vendored
@@ -1,17 +0,0 @@
|
||||
package logrusutil
|
||||
|
||||
import "github.com/sirupsen/logrus"
|
||||
|
||||
// StackLogLevel temporary change log level and return recover function
|
||||
func StackLogLevel(logger *logrus.Logger, level logrus.Level) (recover func()) {
|
||||
if logger.Level == level {
|
||||
return func() {}
|
||||
}
|
||||
|
||||
originLevel := logger.Level
|
||||
logger.Level = level
|
||||
|
||||
return func() {
|
||||
logger.Level = originLevel
|
||||
}
|
||||
}
|
||||
75
vendor/github.com/tsaikd/KDGoLib/runtimecaller/callinfo.go
generated
vendored
75
vendor/github.com/tsaikd/KDGoLib/runtimecaller/callinfo.go
generated
vendored
@@ -1,75 +0,0 @@
|
||||
package runtimecaller
|
||||
|
||||
import "runtime"
|
||||
|
||||
// CallInfo contains runtime caller information
|
||||
type CallInfo interface {
|
||||
// builtin data
|
||||
PC() uintptr
|
||||
FilePath() string
|
||||
Line() int
|
||||
|
||||
// extra info after some process
|
||||
PCFunc() *runtime.Func
|
||||
PackageName() string
|
||||
FileDir() string
|
||||
FileName() string
|
||||
FuncName() string
|
||||
}
|
||||
|
||||
// CallInfoImpl implement CallInfo
|
||||
type CallInfoImpl struct {
|
||||
// builtin data
|
||||
pc uintptr
|
||||
filePath string
|
||||
line int
|
||||
|
||||
// extra info after some process
|
||||
pcFunc *runtime.Func
|
||||
packageName string
|
||||
fileDir string
|
||||
fileName string
|
||||
funcName string
|
||||
}
|
||||
|
||||
// PC return CallInfo data
|
||||
func (t CallInfoImpl) PC() uintptr {
|
||||
return t.pc
|
||||
}
|
||||
|
||||
// FilePath return CallInfo data
|
||||
func (t CallInfoImpl) FilePath() string {
|
||||
return t.filePath
|
||||
}
|
||||
|
||||
// Line return CallInfo data
|
||||
func (t CallInfoImpl) Line() int {
|
||||
return t.line
|
||||
}
|
||||
|
||||
// PCFunc return CallInfo data
|
||||
func (t CallInfoImpl) PCFunc() *runtime.Func {
|
||||
return t.pcFunc
|
||||
}
|
||||
|
||||
// PackageName return CallInfo data
|
||||
func (t CallInfoImpl) PackageName() string {
|
||||
return t.packageName
|
||||
}
|
||||
|
||||
// FileDir return CallInfo data
|
||||
func (t CallInfoImpl) FileDir() string {
|
||||
return t.fileDir
|
||||
}
|
||||
|
||||
// FileName return CallInfo data
|
||||
func (t CallInfoImpl) FileName() string {
|
||||
return t.fileName
|
||||
}
|
||||
|
||||
// FuncName return CallInfo data
|
||||
func (t CallInfoImpl) FuncName() string {
|
||||
return t.funcName
|
||||
}
|
||||
|
||||
var _ = CallInfo(CallInfoImpl{})
|
||||
26
vendor/github.com/tsaikd/KDGoLib/runtimecaller/filter.go
generated
vendored
26
vendor/github.com/tsaikd/KDGoLib/runtimecaller/filter.go
generated
vendored
@@ -1,26 +0,0 @@
|
||||
package runtimecaller
|
||||
|
||||
import "strings"
|
||||
|
||||
// Filter use to filter runtime.Caller result
|
||||
type Filter func(callinfo CallInfo) (valid bool, stop bool)
|
||||
|
||||
// FilterCommons contains all common filters
|
||||
var FilterCommons = []Filter{
|
||||
FilterOnlyGoSource,
|
||||
FilterStopRuntimeCallerPackage,
|
||||
}
|
||||
|
||||
// FilterOnlyGoSource filter CallInfo FileName end with ".go"
|
||||
func FilterOnlyGoSource(callinfo CallInfo) (valid bool, stop bool) {
|
||||
filename := strings.ToLower(callinfo.FileName())
|
||||
return strings.HasSuffix(filename, ".go"), false
|
||||
}
|
||||
|
||||
// FilterStopRuntimeCallerPackage filter CallInfo to stop after reach KDGoLib/runtimecaller package
|
||||
func FilterStopRuntimeCallerPackage(callinfo CallInfo) (valid bool, stop bool) {
|
||||
if callinfo.PackageName() == "github.com/tsaikd/KDGoLib/runtimecaller" {
|
||||
return false, true
|
||||
}
|
||||
return true, false
|
||||
}
|
||||
92
vendor/github.com/tsaikd/KDGoLib/runtimecaller/runtimeCaller.go
generated
vendored
92
vendor/github.com/tsaikd/KDGoLib/runtimecaller/runtimeCaller.go
generated
vendored
@@ -1,92 +0,0 @@
|
||||
package runtimecaller
|
||||
|
||||
import (
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetByFilters return CallInfo until all filters are valid
|
||||
func GetByFilters(skip int, filters ...Filter) (callinfo CallInfo, ok bool) {
|
||||
filters = append(FilterCommons, filters...)
|
||||
for {
|
||||
skip++
|
||||
|
||||
if callinfo, ok = retrieveCallInfo(skip); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
valid, stop := filterAll(callinfo, filters...)
|
||||
if valid {
|
||||
return callinfo, true
|
||||
}
|
||||
if stop {
|
||||
return callinfo, false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ListByFilters return all CallInfo stack for all filters are valid
|
||||
func ListByFilters(skip int, filters ...Filter) (callinfos []CallInfo) {
|
||||
filters = append(FilterCommons, filters...)
|
||||
for {
|
||||
var callinfo CallInfo
|
||||
var ok bool
|
||||
skip++
|
||||
|
||||
if callinfo, ok = retrieveCallInfo(skip); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
valid, stop := filterAll(callinfo, filters...)
|
||||
if valid {
|
||||
callinfos = append(callinfos, callinfo)
|
||||
}
|
||||
if stop {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// http://stackoverflow.com/questions/25262754/how-to-get-name-of-current-package-in-go
|
||||
func retrieveCallInfo(skip int) (result CallInfo, ok bool) {
|
||||
callinfo := CallInfoImpl{}
|
||||
|
||||
if callinfo.pc, callinfo.filePath, callinfo.line, ok = runtime.Caller(skip + 1); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
callinfo.fileDir, callinfo.fileName = path.Split(callinfo.filePath)
|
||||
callinfo.pcFunc = runtime.FuncForPC(callinfo.pc)
|
||||
|
||||
parts := strings.Split(callinfo.pcFunc.Name(), ".")
|
||||
pl := len(parts)
|
||||
if pl < 1 {
|
||||
return result, false
|
||||
}
|
||||
callinfo.funcName = parts[pl-1]
|
||||
|
||||
if pl >= 2 && parts[pl-2] != "" && parts[pl-2][0] == '(' {
|
||||
callinfo.funcName = parts[pl-2] + "." + callinfo.funcName
|
||||
callinfo.packageName = strings.Join(parts[0:pl-2], ".")
|
||||
} else {
|
||||
callinfo.packageName = strings.Join(parts[0:pl-1], ".")
|
||||
}
|
||||
|
||||
return callinfo, true
|
||||
}
|
||||
|
||||
func filterAll(callinfo CallInfo, filters ...Filter) (allvalid bool, onestop bool) {
|
||||
allvalid = true
|
||||
for _, filter := range filters {
|
||||
valid, stop := filter(callinfo)
|
||||
allvalid = allvalid && valid
|
||||
if stop {
|
||||
return allvalid, true
|
||||
}
|
||||
if !allvalid {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user