//  Copyright(C) 2021. Huawei Technologies Co.,Ltd.  All rights reserved.

// Package hwlog provides the capability of processing Huawei log rules.
package hwlog

import (
"bytes"
"context"
"fmt"
"go.uber.org/zap"
"runtime"
"strings"
)

// printHelper helper function for log printing
func printHelper(f func(string, ...zap.Field), msg string, ctx ...context.Context) {
str := getCallerInfo(ctx...)
f(str + msg)
}

// getCallerInfo gets the caller's information
func getCallerInfo(ctx ...context.Context) string {
var deep = stackDeep
var userID interface{}
var traceID interface{}
for _, c := range ctx {
if c == nil {
deep++
continue
}
userID = c.Value(UserID)
traceID = c.Value(ReqID)
}
var funcName string
pc, codePath, codeLine, ok := runtime.Caller(deep)
if ok {
funcName = runtime.FuncForPC(pc).Name()
}
p := strings.Split(codePath, "/")
l := len(p)
if l == pathLen {
funcName = p[l-1]
} else if l > pathLen {
funcName = fmt.Sprintf("%s/%s", p[l-pathLen], p[l-1])
}
callerPath := fmt.Sprintf("%s:%d", funcName, codeLine)
goroutineID := getGoroutineID()
str := fmt.Sprintf("%-8s%s ", goroutineID, callerPath)
if userID != nil || traceID != nil {
str = fmt.Sprintf("%s{%v}-{%v} ", str, userID, traceID)
}
return str
}

// getCallerGoroutineID gets the goroutineID
func getGoroutineID() string {
b := make([]byte, bitsize, bitsize)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
return string(b)
}

最新文章

  1. Python debug
  2. SameSite Cookie,防止 CSRF 攻击
  3. 基于Token的WEB后台认证机制
  4. word第一讲(0723)
  5. canvas三角函数直线运动
  6. Thinking in java学习笔记之垃圾回收器如何工作
  7. iOS多线程GCD
  8. css3学习总结6--CSS3字体
  9. Eclipse 代码提示功能设置。
  10. 对C++进行优化了的android-ndk-r6-crystax-2
  11. (转).net程序员转战android第三篇---登录模块之静态登录
  12. Android学习总结——实现Home键功能
  13. OO第一单元作业小结
  14. Linux查看当前目录下所有子目录是否包含某个文件
  15. Spring的由来以及发展
  16. (转)SQLServer_十步优化SQL Server中的数据访问 三
  17. 自定义编写js格式化数字的函数
  18. gensim自然语言处理
  19. 时间同步ctss与ntp的关系【CTSSD Runs in Observer Mode Even Though No Time Sync Software is Running (Doc ID 1054006.1) 】
  20. JS 求一组数中所有数的和以及平均值

热门文章

  1. x64dbg 插件开发环境配置
  2. K8S Pod及其控制器
  3. Github-CLI
  4. MinIO Server配置指南
  5. Elasticsearch:如何调试集群状态 - 定位错误信息
  6. How to Create DLL(Dynamic link library)
  7. Maximum Entropy Population-Based Training for Zero-Shot Human-AI Coordination
  8. JavaScript基本语法(数组与JSON)
  9. 用昇腾AI护航“井下安全”
  10. 一篇文章带你了解轻量级Web服务器——Nginx简单入门