diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-09-27 14:00:32 +0200 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-09-27 14:00:32 +0200 |
| commit | d8e466712a4c5af17c27953b38f1b4e7d914d5cf (patch) | |
| tree | 449149eaebdf8494a2bf8e3c7428b85814cd28b5 | |
| parent | a943641fdae051346355a3aaf1d8887da674c97c (diff) | |
Log: Add 'inherit' that sets the error level as the log level
| -rw-r--r-- | internal/log/log.go | 28 | ||||
| -rw-r--r-- | state.go | 8 | ||||
| -rw-r--r-- | types/error.go | 10 |
3 files changed, 41 insertions, 5 deletions
diff --git a/internal/log/log.go b/internal/log/log.go index c0e9c7d..7241164 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -18,10 +18,16 @@ type FileLogger struct { type LogLevel int8 const ( + // No level set, not allowed LOG_NOTSET LogLevel = iota + // Log info, this message is not an error LOG_INFO + // Log only to provide a warning, the app still functions LOG_WARNING + // Log to provide a generic error, the app still functions but some functionality might not work LOG_ERROR + // Log to provide a fatal error, the app cannot function correctly when such an error occurs + LOG_FATAL ) func (e LogLevel) String() string { @@ -34,6 +40,8 @@ func (e LogLevel) String() string { return "WARNING" case LOG_ERROR: return "ERROR" + case LOG_FATAL: + return "FATAL" default: return "UNKNOWN" } @@ -60,6 +68,22 @@ func (logger *FileLogger) Init(level LogLevel, name string, directory string) er return nil } +func (logger *FileLogger) Inherit(err error, msg string) { + level := types.GetErrorLevel(err) + + switch(level) { + case types.ERR_INFO: + logger.Info(msg) + case types.ERR_WARNING: + logger.Warning(msg) + case types.ERR_OTHER: + logger.Error(msg) + case types.ERR_FATAL: + logger.Fatal(msg) + } + +} + func (logger *FileLogger) Info(msg string) { logger.log(LOG_INFO, msg) } @@ -72,6 +96,10 @@ func (logger *FileLogger) Error(msg string) { logger.log(LOG_ERROR, msg) } +func (logger *FileLogger) Fatal(msg string) { + logger.log(LOG_FATAL, msg) +} + func (logger *FileLogger) Close() { logger.File.Close() } @@ -478,7 +478,8 @@ func (state *VPNState) GetConfigSecureInternet( config, configType, configErr := state.getConfig(server, forceTCP) if configErr != nil { - state.Logger.Error( + state.Logger.Inherit( + configErr, fmt.Sprintf( "Failed getting a secure internet configuration with error: %s", GetErrorTraceback(configErr), @@ -548,7 +549,7 @@ func (state *VPNState) GetConfigInstituteAccess(url string, forceTCP bool) (stri config, configType, configErr := state.getConfig(server, forceTCP) if configErr != nil { - state.Logger.Error( + state.Logger.Inherit(configErr, fmt.Sprintf( "Failed getting an institute access server configuration with error: %s", GetErrorTraceback(configErr), @@ -577,7 +578,8 @@ func (state *VPNState) GetConfigCustomServer(url string, forceTCP bool) (string, config, configType, configErr := state.getConfig(server, forceTCP) if configErr != nil { - state.Logger.Error( + state.Logger.Inherit( + configErr, fmt.Sprintf( "Failed getting a custom server with error: %s", GetErrorTraceback(configErr), diff --git a/types/error.go b/types/error.go index 607e6c6..c49fba2 100644 --- a/types/error.go +++ b/types/error.go @@ -8,11 +8,17 @@ import ( type ErrorLevel int8 const ( - // All other errors + // All other errors, default ERR_OTHER ErrorLevel = iota - // The error is just here as additional info + // The erorr is just here as additional info ERR_INFO + + // The error is just here as a warning + ERR_WARNING + + // The error is fatal, the app cannot function + ERR_FATAL ) type WrappedErrorMessage struct { |
