PageRenderTime 24ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/_Settings/main.go

https://gitlab.com/re-pe/phpApps-Go
Go | 119 lines | 95 code | 21 blank | 3 comment | 18 complexity | c682139949cbfc60433f501ed4be13ef MD5 | raw file
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/eiannone/keyboard"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. )
  10. const (
  11. fRunningFile = "_Settings.exe"
  12. fDir = "_tmp/log/"
  13. fExt = ".log"
  14. )
  15. var flags struct {
  16. Debug bool
  17. Verbose bool
  18. }
  19. func NewLogFile(runFileName string) (logFile *os.File, err error) {
  20. err = os.MkdirAll(fDir, os.ModeDir)
  21. logFileName := filepath.Base(runFileName)
  22. logFileName = strings.Replace(logFileName, filepath.Ext(logFileName), "", -1)
  23. logFileName = fDir + logFileName + fExt
  24. logFile, err = os.Create(logFileName)
  25. if err != nil { panic(err) }
  26. return
  27. }
  28. func ExitLn(args ...interface{}) {
  29. fmt.Println(args...)
  30. log.Fatalln(args...)
  31. }
  32. func CheckFormat(args []interface{}) (format string, hasFormat bool) {
  33. if len(args) < 1 { return }
  34. switch arg := args[0].(type){
  35. case string:
  36. format = arg
  37. hasFormat = true
  38. }
  39. if !hasFormat { return }
  40. if len(format) < 1 || strings.Index(format, "?:") != 0 {
  41. hasFormat = false
  42. return
  43. }
  44. format = strings.Replace(format, "?:", "", 1)
  45. return
  46. }
  47. func Print(args ...interface{}) {
  48. if format, hasFormat := CheckFormat(args); hasFormat {
  49. fmt.Printf(format, args[1:]...)
  50. } else {
  51. fmt.Print(args...)
  52. }
  53. }
  54. func Log(args ...interface{}) {
  55. if format, hasFormat := CheckFormat(args); hasFormat {
  56. log.Printf(format, args[1:]...)
  57. } else {
  58. log.Print(args...)
  59. }
  60. }
  61. func Out(args ...interface{}) {
  62. Log(args...)
  63. Print(args...)
  64. }
  65. func Debug(args ...interface{}) {
  66. if flags.Debug {
  67. Out(args...)
  68. }
  69. }
  70. func main() {
  71. args := os.Args
  72. for _, arg := range args {
  73. switch arg {
  74. case "--debug" :
  75. flags.Debug = true
  76. case "--verbose" :
  77. flags.Verbose = true
  78. }
  79. }
  80. // logfailo sukūrimas
  81. logFile, err := NewLogFile(fRunningFile)
  82. defer logFile.Close()
  83. log.SetOutput(logFile)
  84. Debug("\nargs: ", args, "\n\n")
  85. Debug("flags.Debug: ", flags.Debug, "\n\n")
  86. Debug("flags.Verbose: ", flags.Verbose, "\n\n")
  87. // logfailo pildymo pradžia
  88. Debug(fRunningFile, " started\n")
  89. Out("\n")
  90. // darbo pradžia
  91. var confKeeper ConfKeeper
  92. err = confKeeper.Run()
  93. if err != nil {
  94. Out(err.Error(), "\n\n")
  95. }
  96. Out("Press any key to exit...\n")
  97. keyboard.GetSingleKey()
  98. }