123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- // Copyright 2025 BackendServerTemplate Authors. All rights reserved.
- // Use of this source code is governed by a MIT-style
- // license that can be found in the LICENSE file.
- package exitutils
- import (
- "fmt"
- "github.com/SongZihuan/BackendServerTemplate/src/logger"
- "log"
- )
- const (
- exitCodeMin = 0
- exitCodeMax = 255
- exitCodeErrorLogMustBeReady = 254
- )
- type ExitCode int
- func (e ExitCode) Error() string {
- return fmt.Sprintf("Exit with code %d", e)
- }
- func getExitCode(defaultExitCode int, exitCode ...int) (ec ExitCode) {
- if len(exitCode) == 1 {
- ec = ExitCode(exitCode[0])
- } else {
- ec = ExitCode(defaultExitCode)
- }
- if ec < exitCodeMin {
- ec = -ec
- }
- if ec > exitCodeMax {
- ec = exitCodeMax
- }
- return ec
- }
- func InitFailedErrorForWin32ConsoleModule(reason string, exitCode ...int) ExitCode {
- if reason == "" {
- reason = "no reason"
- }
- ec := getExitCode(1, exitCode...)
- log.Printf("The module `Win32 Console` init failed (reason: `%s`) .", reason)
- log.Printf("Now we should exit with code %d.", ec)
- return ec
- }
- func InitFailedErrorForLoggerModule(reason string, exitCode ...int) ExitCode {
- if reason == "" {
- reason = "no reason"
- }
- ec := getExitCode(1, exitCode...)
- log.Printf("The module `Logger` init failed (reason: `%s`) .", reason)
- log.Printf("Now we should exit with code %d.", ec)
- return ec
- }
- func InitFailedError(module string, reason string, exitCode ...int) ExitCode {
- if !logger.IsReady() {
- return exitCodeErrorLogMustBeReady
- }
- if reason == "" {
- reason = "no reason"
- }
- ec := getExitCode(1, exitCode...)
- logger.Errorf("The module `%s` init failed (reason: `%s`) .", module, reason)
- logger.Errorf("Now we should exit with code %d.", ec)
- return ec
- }
- func RunErrorQuite(exitCode ...int) ExitCode {
- if !logger.IsReady() {
- return exitCodeErrorLogMustBeReady
- }
- return getExitCode(1, exitCode...)
- }
- func RunError(reason string, exitCode ...int) ExitCode {
- if !logger.IsReady() {
- return exitCodeErrorLogMustBeReady
- }
- if reason == "" {
- reason = "no reason"
- }
- ec := getExitCode(1, exitCode...)
- logger.Errorf("Run error (reason: `%s`) .", reason)
- logger.Errorf("Now we should exit with code %d.", ec)
- return ec
- }
- func SuccessExit(reason string, exitCode ...int) ExitCode {
- if !logger.IsReady() {
- return exitCodeErrorLogMustBeReady
- }
- if reason == "" {
- reason = "no reason"
- }
- ec := getExitCode(0, exitCode...)
- logger.Warnf("Now we should exit with code %d (reason: %s) .", ec, reason)
- return ec
- }
- func SuccessExitQuite(exitCode ...int) ExitCode {
- if !logger.IsReady() {
- return exitCodeErrorLogMustBeReady
- }
- ec := getExitCode(0, exitCode...)
- return ec
- }
|