mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-13 05:31:08 +08:00
feat: impl. new protol design & new head
This commit is contained in:
37
internal/file/file.go
Normal file
37
internal/file/file.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IsExist 文件/路径存在
|
||||
func IsExist(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil || os.IsExist(err)
|
||||
}
|
||||
|
||||
// IsNotExist 文件/路径不存在
|
||||
func IsNotExist(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err != nil && os.IsNotExist(err)
|
||||
}
|
||||
|
||||
// FolderName 本文件所在最下级文件夹名
|
||||
func FolderName() string {
|
||||
_, file, _, ok := runtime.Caller(1)
|
||||
if !ok {
|
||||
return "<unk>"
|
||||
}
|
||||
i := strings.LastIndex(file, "/")
|
||||
if i <= 0 {
|
||||
return file
|
||||
}
|
||||
file = file[:i]
|
||||
i = strings.LastIndex(file, "/")
|
||||
if i <= 0 || i+1 >= len(file) {
|
||||
return file
|
||||
}
|
||||
return file[i+1:]
|
||||
}
|
||||
52
internal/file/log.go
Normal file
52
internal/file/log.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Header() string {
|
||||
file, fn := fileFuncName(2)
|
||||
sb := strings.Builder{}
|
||||
sb.WriteString("[")
|
||||
sb.WriteString(file)
|
||||
sb.WriteString("] ")
|
||||
sb.WriteString(fn)
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func fileFuncName(skip int) (string, string) {
|
||||
pc, file, _, ok := runtime.Caller(skip)
|
||||
if !ok {
|
||||
return "unknown", "unknown"
|
||||
}
|
||||
fn := runtime.FuncForPC(pc).Name()
|
||||
i := strings.LastIndex(fn, "/")
|
||||
fn = fn[i+1:]
|
||||
i = strings.LastIndex(file, "/")
|
||||
if i < 0 {
|
||||
i = strings.LastIndex(file, "\\")
|
||||
if i < 0 {
|
||||
return file, fn
|
||||
}
|
||||
}
|
||||
nm := file[i+1:]
|
||||
if len(nm) == 0 {
|
||||
return file, fn
|
||||
}
|
||||
i = strings.LastIndex(nm, ".")
|
||||
if i <= 0 {
|
||||
return nm, fn
|
||||
}
|
||||
return nm[:i], fn
|
||||
}
|
||||
|
||||
func ToLimitHexString(data []byte, bound int) string {
|
||||
endl := "..."
|
||||
if len(data) < bound {
|
||||
bound = len(data)
|
||||
endl = "."
|
||||
}
|
||||
return hex.EncodeToString(data[:bound]) + endl
|
||||
}
|
||||
Reference in New Issue
Block a user