|
|
- package logger
-
- import (
- "errors"
- "fmt"
- "os"
- "os/exec"
- "path"
- "path/filepath"
- "runtime"
- "strings"
- "time"
- )
-
- // 获取今天的日期
- func getToday() string {
- now := time.Now()
- return now.Format("2006-01-02")
- }
-
- // 获取当前时间
- func getCurrentTime() string {
- now := time.Now()
- hour := now.Hour()
- minute := now.Minute()
- second := now.Second()
- return fmt.Sprintf("%02d:%02d:%02d", hour, minute, second)
- }
-
- // 获取调用代码的位置信息
- func getCaller(skip int) string {
- pc, file, line, ok := runtime.Caller(skip)
- if !ok {
- return ""
- }
- fileName := path.Base(file)
- funcName := runtime.FuncForPC(pc).Name()
- return fmt.Sprintf("file:%v;function:%v;line:%d", fileName, funcName, line)
- }
-
- // 获取程序进程id
- func getPid() int {
- return os.Getpid()
- }
-
- //windows环境下获取绝对路径
- func GetCurrentPath(dir string) (string, error) {
- file, err := exec.LookPath(os.Args[0])
- if err != nil {
- return "", err
- }
- path, err := filepath.Abs(file)
- if err != nil {
- return "", err
- }
- i := strings.LastIndex(path, "/")
- if i < 0 {
- i = strings.LastIndex(path, "\\")
- }
- if i < 0 {
- return "", errors.New(`error: Can't find "/" or "\".`)
- }
- pathdir := string(path[0 : i+1])
- if len(dir) > 0 {
- dir = strings.Replace(dir, "/", "\\", -1)
- return filepath.Join(pathdir, dir), nil
- }
- return string(path[0 : i+1]), nil
- }
|