Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add godot error messages #10

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ singleton:

Read the code for details about the API, it's extensively documented.

Error Codes
-----------

All logging levels can also optionally include an [error code](https://docs.godotengine.org/en/stable/classes/class_@globalscope.html?#enum-globalscope-error), which will be mapped to the corresponding error message as of Godot 3.2.
```
Logger.error('failed to rotate the albatross', 'main', ERR_INVALID_DATA)
```

## Output format configuration

The `output_format` property can be customized using format fields from the
Expand All @@ -31,10 +39,11 @@ relevant content.

**Log format fields**:

* `{LVL}` = Level of the log
* `{MOD}` = Module that does the logging
* `{MSG}` = Message from the user
* `{TIME}` = Timestamp when the logging occurred
* `{LVL}` = Level of the log
* `{MOD}` = Module that does the logging
* `{MSG}` = Message from the user
* `{TIME}` = Timestamp when the logging occurred
* `{ERR_MSG}` = Godot error message corrsesponding to provided error code

The timestamp format can be configured for each module using the `time_format`
property, with the placeholders described below.
Expand All @@ -53,7 +62,7 @@ property, with the placeholders described below.
```gdscript
var msg = "Error occurred!"

Logger.output_format = "[{TIME}] [{LVL}] [{MOD}] {MSG}"
Logger.output_format = "[{TIME}] [{LVL}] [{MOD}]{ERR_MSG} {MSG}"
Logger.time_format = "YYYY.MM.DD hh:mm:ss"
Logger.error(msg)

Expand Down
91 changes: 76 additions & 15 deletions logger.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,58 @@

extends Node # Needed to work as a singleton

const error_messages = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine for the purpose of this plugin, but in the long run I think it might be worth having this kind of stringification of error codes directly in the engine itself, as many users would benefit from an easy translation of error numbers to human readable strings.

OK: "OK!",
FAILED: "Generic error.",
ERR_UNAVAILABLE: "Unavailable error.",
ERR_UNCONFIGURED: "Unconfigured error.",
ERR_UNAUTHORIZED: "Unauthorized error.",
ERR_PARAMETER_RANGE_ERROR: "Parameter range error.",
ERR_OUT_OF_MEMORY: "Out of memory (OOM) error.",
ERR_FILE_NOT_FOUND: "File: Not found error.",
ERR_FILE_BAD_DRIVE: "File: Bad drive error.",
ERR_FILE_BAD_PATH: "File: Bad path error.",
ERR_FILE_NO_PERMISSION: "File: No permission error.",
ERR_FILE_ALREADY_IN_USE: "File: Already in use error.",
ERR_FILE_CANT_OPEN: "File: Can't open error.",
ERR_FILE_CANT_WRITE: "File: Can't write error.",
ERR_FILE_CANT_READ: "File: Can't read error.",
ERR_FILE_UNRECOGNIZED: "File: Unrecognized error.",
ERR_FILE_CORRUPT: "File: Corrupt error.",
ERR_FILE_MISSING_DEPENDENCIES: "File: Missing dependencies error.",
ERR_FILE_EOF: "File: End of file (EOF) error.",
ERR_CANT_OPEN: "Can't open error.",
ERR_CANT_CREATE: "Can't create error.",
ERR_QUERY_FAILED: "Query failed error.",
ERR_ALREADY_IN_USE: "Already in use error.",
ERR_LOCKED: "Locked error.",
ERR_TIMEOUT: "Timeout error.",
ERR_CANT_CONNECT: "Can't connect error.",
ERR_CANT_RESOLVE: "Can't resolve error.",
ERR_CONNECTION_ERROR: "Connection error.",
ERR_CANT_ACQUIRE_RESOURCE: "Can't acquire resource error.",
ERR_CANT_FORK: "Can't fork process error.",
ERR_INVALID_DATA: "Invalid data error.",
ERR_INVALID_PARAMETER: "Invalid parameter error.",
ERR_ALREADY_EXISTS: "Already exists error.",
ERR_DOES_NOT_EXIST: "Does not exist error.",
ERR_DATABASE_CANT_READ: "Database: Read error.",
ERR_DATABASE_CANT_WRITE: "Database: Write error.",
ERR_COMPILATION_FAILED: "Compilation failed error.",
ERR_METHOD_NOT_FOUND: "Method not found error.",
ERR_LINK_FAILED: "Linking failed error.",
ERR_SCRIPT_FAILED: "Script failed error.",
ERR_CYCLIC_LINK: "Cycling link (import cycle) error.",
ERR_INVALID_DECLARATION: "Invalid declaration error.",
ERR_DUPLICATE_SYMBOL: "Duplicate symbol error.",
ERR_PARSE_ERROR: "Parse error.",
ERR_BUSY: "Busy error.",
ERR_SKIP: "Skip error.",
ERR_HELP: "Help error.",
ERR_BUG: "Bug error.",
ERR_PRINTER_ON_FIRE: "Printer on fire error.",
}

##================##
## Inner classes ##
##================##
Expand Down Expand Up @@ -221,6 +273,7 @@ const FORMAT_IDS = {
"level": "{LVL}",
"module": "{MOD}",
"message": "{MSG}",
Samuel-Lewis marked this conversation as resolved.
Show resolved Hide resolved
"error_message": "{ERR_MSG}",
"time": "{TIME}"
}

Expand All @@ -244,7 +297,8 @@ var default_logfile_path = "user://%s.log" % ProjectSettings.get_setting("applic
var default_configfile_path = "user://%s.cfg" % PLUGIN_NAME

# e.g. "[INFO] [main] The young alpaca started growing a goatie."
var output_format = "[{TIME}] [{LVL}] [{MOD}] {MSG}"
var output_format = "[{TIME}] [{LVL}] [{MOD}]{ERR_MSG} {MSG}"

# Example with all supported placeholders: "YYYY.MM.DD hh.mm.ss"
# would output e.g.: "2020.10.09 12:10:47".
var time_format = "hh:mm:ss"
Expand All @@ -270,14 +324,14 @@ var modules = {}
## Functions ##
##=============##

func put(level, message, module = default_module_name):
func put(level, message, module = default_module_name, error_code = -1):
"""Log a message in the given module with the given logging level."""
var module_ref = get_module(module)
var output_strategy = module_ref.get_output_strategy(level)
if output_strategy == STRATEGY_MUTE or module_ref.get_output_level() > level:
return # Out of scope

var output = format(output_format, level, module, message)
var output = format(output_format, level, module, message, error_code)

if output_strategy & STRATEGY_PRINT:
print(output)
Expand All @@ -296,25 +350,25 @@ func put(level, message, module = default_module_name):
# Helper functions for each level
# -------------------------------

func verbose(message, module = default_module_name):
func verbose(message, module = default_module_name, error_code = -1):
"""Log a message in the given module with level VERBOSE."""
put(VERBOSE, message, module)
put(VERBOSE, message, module, error_code)

func debug(message, module = default_module_name):
func debug(message, module = default_module_name, error_code = -1):
"""Log a message in the given module with level DEBUG."""
put(DEBUG, message, module)
put(DEBUG, message, module, error_code)

func info(message, module = default_module_name):
func info(message, module = default_module_name, error_code = -1):
"""Log a message in the given module with level INFO."""
put(INFO, message, module)
put(INFO, message, module, error_code)

func warn(message, module = default_module_name):
func warn(message, module = default_module_name, error_code = -1):
"""Log a message in the given module with level WARN."""
put(WARN, message, module)
put(WARN, message, module, error_code)

func error(message, module = default_module_name):
func error(message, module = default_module_name, error_code = -1):
"""Log a message in the given module with level ERROR."""
put(ERROR, message, module)
put(ERROR, message, module, error_code)

# Module management
# -----------------
Expand Down Expand Up @@ -463,12 +517,19 @@ func get_formatted_datetime():
result = result.replacen("ss", "%02d" % [datetime.second])
return result

func format(template, level, module, message):
func format(template, level, module, message, error_code = -1):
var output = template
output = output.replace(FORMAT_IDS.level, LEVELS[level])
output = output.replace(FORMAT_IDS.module, module)
output = output.replace(FORMAT_IDS.message, message)
output = output.replace(FORMAT_IDS.time, get_formatted_datetime())
output = output.replace(FORMAT_IDS.time, get_formatted_datetime())

# Error message substitution
var error_message = error_messages.get(error_code)
akien-mga marked this conversation as resolved.
Show resolved Hide resolved
if error_message != null:
output = output.replace(FORMAT_IDS.error_message, " " + error_message)
else:
output = output.replace(FORMAT_IDS.error_message, "")
return output

func set_output_format(new_format):
Expand Down