广汽安道拓Acura项目MES后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.3 KiB

3 years ago
  1. package logger
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "time"
  12. )
  13. // 获取今天的日期
  14. func getToday() string {
  15. now := time.Now()
  16. return now.Format("2006-01-02")
  17. }
  18. // 获取当前时间
  19. func getCurrentTime() string {
  20. now := time.Now()
  21. hour := now.Hour()
  22. minute := now.Minute()
  23. second := now.Second()
  24. return fmt.Sprintf("%02d:%02d:%02d", hour, minute, second)
  25. }
  26. // 获取调用代码的位置信息
  27. func getCaller(skip int) string {
  28. pc, file, line, ok := runtime.Caller(skip)
  29. if !ok {
  30. return ""
  31. }
  32. fileName := path.Base(file)
  33. funcName := runtime.FuncForPC(pc).Name()
  34. return fmt.Sprintf("file:%v;function:%v;line:%d", fileName, funcName, line)
  35. }
  36. // 获取程序进程id
  37. func getPid() int {
  38. return os.Getpid()
  39. }
  40. //windows环境下获取绝对路径
  41. func GetCurrentPath(dir string) (string, error) {
  42. file, err := exec.LookPath(os.Args[0])
  43. if err != nil {
  44. return "", err
  45. }
  46. path, err := filepath.Abs(file)
  47. if err != nil {
  48. return "", err
  49. }
  50. i := strings.LastIndex(path, "/")
  51. if i < 0 {
  52. i = strings.LastIndex(path, "\\")
  53. }
  54. if i < 0 {
  55. return "", errors.New(`error: Can't find "/" or "\".`)
  56. }
  57. pathdir := string(path[0 : i+1])
  58. if len(dir) > 0 {
  59. dir = strings.Replace(dir, "/", "\\", -1)
  60. return filepath.Join(pathdir, dir), nil
  61. }
  62. return string(path[0 : i+1]), nil
  63. }