Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
U
utils
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
library
utils
Commits
74eefd75
Verified
Commit
74eefd75
authored
Jan 17, 2019
by
Cui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add detect caller name tools
parent
87bfadfd
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
0 deletions
+69
-0
caller.go
caller.go
+39
-0
caller_test.go
caller_test.go
+30
-0
No files found.
caller.go
0 → 100644
View file @
74eefd75
/*
* @Author: cuiweiqiang
* @Date: 2019-01-17 10:17
*/
package
utils
import
(
"runtime"
)
func
GetCurrentFunctionName
()
string
{
return
getFrame
(
1
)
.
Function
}
func
GetCallerFunctionName
()
string
{
return
getFrame
(
2
)
.
Function
}
func
getFrame
(
skipFrames
int
)
runtime
.
Frame
{
targetFrameIndex
:=
skipFrames
+
2
programCounters
:=
make
([]
uintptr
,
targetFrameIndex
+
2
)
n
:=
runtime
.
Callers
(
0
,
programCounters
)
frame
:=
runtime
.
Frame
{
Function
:
"unknown"
}
if
n
>
0
{
frames
:=
runtime
.
CallersFrames
(
programCounters
[
:
n
])
for
more
,
frameIndex
:=
true
,
0
;
more
&&
frameIndex
<=
targetFrameIndex
;
frameIndex
++
{
var
frameCandidate
runtime
.
Frame
frameCandidate
,
more
=
frames
.
Next
()
if
frameIndex
==
targetFrameIndex
{
frame
=
frameCandidate
}
}
}
return
frame
}
caller_test.go
0 → 100644
View file @
74eefd75
/*
* @Author: cuiweiqiang
* @Date: 2019-01-17 10:30
*/
package
utils
import
(
"testing"
)
func
TestGetCurrentFunctionName
(
t
*
testing
.
T
)
{
funcName
:=
GetCurrentFunctionName
()
if
funcName
==
"gitlab.qingclass.cn/cuiweiqiang/utils.TestGetCurrentFunctionName"
{
t
.
Log
(
"success"
)
}
else
{
t
.
Failed
()
}
}
func
TestGetCallerFunctionName
(
t
*
testing
.
T
)
{
callerName
:=
GetCallerFunctionName
()
if
callerName
==
"testing.tRunner"
{
t
.
Log
(
"success"
)
}
else
{
t
.
Failed
()
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment