exit.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2025 BackendServerTemplate Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package exitutils
  5. import (
  6. "fmt"
  7. "github.com/SongZihuan/BackendServerTemplate/src/logger"
  8. "log"
  9. )
  10. const (
  11. exitCodeMin = 0
  12. exitCodeMax = 255
  13. exitCodeErrorLogMustBeReady = 254
  14. )
  15. type ExitCode int
  16. func (e ExitCode) Error() string {
  17. return fmt.Sprintf("Exit with code %d", e)
  18. }
  19. func getExitCode(defaultExitCode int, exitCode ...int) (ec ExitCode) {
  20. if len(exitCode) == 1 {
  21. ec = ExitCode(exitCode[0])
  22. } else {
  23. ec = ExitCode(defaultExitCode)
  24. }
  25. if ec < exitCodeMin {
  26. ec = -ec
  27. }
  28. if ec > exitCodeMax {
  29. ec = exitCodeMax
  30. }
  31. return ec
  32. }
  33. func InitFailedErrorForWin32ConsoleModule(reason string, exitCode ...int) ExitCode {
  34. if reason == "" {
  35. reason = "no reason"
  36. }
  37. ec := getExitCode(1, exitCode...)
  38. log.Printf("The module `Win32 Console` init failed (reason: `%s`) .", reason)
  39. log.Printf("Now we should exit with code %d.", ec)
  40. return ec
  41. }
  42. func InitFailedErrorForLoggerModule(reason string, exitCode ...int) ExitCode {
  43. if reason == "" {
  44. reason = "no reason"
  45. }
  46. ec := getExitCode(1, exitCode...)
  47. log.Printf("The module `Logger` init failed (reason: `%s`) .", reason)
  48. log.Printf("Now we should exit with code %d.", ec)
  49. return ec
  50. }
  51. func InitFailedError(module string, reason string, exitCode ...int) ExitCode {
  52. if !logger.IsReady() {
  53. return exitCodeErrorLogMustBeReady
  54. }
  55. if reason == "" {
  56. reason = "no reason"
  57. }
  58. ec := getExitCode(1, exitCode...)
  59. logger.Errorf("The module `%s` init failed (reason: `%s`) .", module, reason)
  60. logger.Errorf("Now we should exit with code %d.", ec)
  61. return ec
  62. }
  63. func RunErrorQuite(exitCode ...int) ExitCode {
  64. if !logger.IsReady() {
  65. return exitCodeErrorLogMustBeReady
  66. }
  67. return getExitCode(1, exitCode...)
  68. }
  69. func RunError(reason string, exitCode ...int) ExitCode {
  70. if !logger.IsReady() {
  71. return exitCodeErrorLogMustBeReady
  72. }
  73. if reason == "" {
  74. reason = "no reason"
  75. }
  76. ec := getExitCode(1, exitCode...)
  77. logger.Errorf("Run error (reason: `%s`) .", reason)
  78. logger.Errorf("Now we should exit with code %d.", ec)
  79. return ec
  80. }
  81. func SuccessExit(reason string, exitCode ...int) ExitCode {
  82. if !logger.IsReady() {
  83. return exitCodeErrorLogMustBeReady
  84. }
  85. if reason == "" {
  86. reason = "no reason"
  87. }
  88. ec := getExitCode(0, exitCode...)
  89. logger.Warnf("Now we should exit with code %d (reason: %s) .", ec, reason)
  90. return ec
  91. }
  92. func SuccessExitQuite(exitCode ...int) ExitCode {
  93. if !logger.IsReady() {
  94. return exitCodeErrorLogMustBeReady
  95. }
  96. ec := getExitCode(0, exitCode...)
  97. return ec
  98. }