Skip to content

Commit

Permalink
chore: formats logger.gd
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Ruben committed May 26, 2024
1 parent 147d6a9 commit d0f6042
Showing 1 changed file with 32 additions and 116 deletions.
148 changes: 32 additions & 116 deletions logger.gd
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,14 @@ class Logfile:
if not dir:
var err = DirAccess.get_open_error()
if err:
print(
(
"[ERROR] [logger] Could not create the '%s' directory; exited with error %d."
% [base_dir, err]
)
)
print("[ERROR] [logger] Could not create the '%s' directory; exited with error %d." % [base_dir, err])
return false
else:
# TODO: Move directory creation to the function that will actually *write*
dir.make_dir_recursive(base_dir)
var err2 = DirAccess.get_open_error()
if err2:
print(
(
"[ERROR] [logger] Could not create the '%s' directory; exited with error %d."
% [base_dir, err2]
)
)
print("[ERROR] [logger] Could not create the '%s' directory; exited with error %d." % [base_dir, err2])
return false

print("[INFO] [logger] Successfully created the '%s' directory." % base_dir)
Expand Down Expand Up @@ -157,12 +147,7 @@ class Logfile:

if result == null:
var err = FileAccess.get_open_error()
print(
(
"[ERROR] [logger] Could not open the '%s' log file; exited with error %d."
% [path, err]
)
)
print("[ERROR] [logger] Could not open the '%s' log file; exited with error %d." % [path, err])
return null
else:
return result
Expand Down Expand Up @@ -197,12 +182,7 @@ class Module:
on their respective strategies, while levels lower than the given one
will be discarded."""
if not level in range(0, LEVELS.size()):
print(
(
"[ERROR] [%s] The level must be comprised between 0 and %d."
% [PLUGIN_NAME, LEVELS.size() - 1]
)
)
print("[ERROR] [%s] The level must be comprised between 0 and %d." % [PLUGIN_NAME, LEVELS.size() - 1])
return
output_level = level

Expand All @@ -212,12 +192,7 @@ class Module:
func set_common_output_strategy(output_strategy_mask):
"""Set the common output strategy mask for all levels of the module."""
if not output_strategy_mask in range(0, MAX_STRATEGY + 1):
print(
(
"[ERROR] [%s] The output strategy mask must be comprised between 0 and %d."
% [PLUGIN_NAME, MAX_STRATEGY]
)
)
print("[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." % [PLUGIN_NAME, MAX_STRATEGY])
return
for i in range(0, LEVELS.size()):
output_strategies[i] = output_strategy_mask
Expand All @@ -226,24 +201,14 @@ class Module:
"""Set the output strategy for the given level or (by default) all
levels of the module."""
if not output_strategy_mask in range(0, MAX_STRATEGY + 1):
print(
(
"[ERROR] [%s] The output strategy mask must be comprised between 0 and %d."
% [PLUGIN_NAME, MAX_STRATEGY]
)
)
print("[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." % [PLUGIN_NAME, MAX_STRATEGY])
return
if level == -1: # Set for all levels
for i in range(0, LEVELS.size()):
output_strategies[i] = output_strategy_mask
else:
if not level in range(0, LEVELS.size()):
print(
(
"[ERROR] [%s] The level must be comprised between 0 and %d."
% [PLUGIN_NAME, LEVELS.size() - 1]
)
)
print("[ERROR] [%s] The level must be comprised between 0 and %d." % [PLUGIN_NAME, LEVELS.size() - 1])
return
output_strategies[level] = output_strategy_mask

Expand Down Expand Up @@ -463,10 +428,7 @@ func add_module(
default ones.
Returns a reference to the instanced module."""
if modules.has(name):
info(
"The module '%s' already exists; discarding the call to add it anew." % name,
PLUGIN_NAME
)
info("The module '%s' already exists; discarding the call to add it anew." % name, PLUGIN_NAME)
else:
if logfile == null:
logfile = get_external_sink(default_logfile_path)
Expand All @@ -477,17 +439,10 @@ func add_module(
func get_module(module = default_module_name):
"""Retrieve the given module if it exists; if not, it will be created."""
if not modules.has(module):
info(
(
"The requested module '%s' does not exist. It will be created with default values."
% module
),
PLUGIN_NAME
)
info("The requested module '%s' does not exist. It will be created with default values." % module, PLUGIN_NAME)
add_module(module)
return modules[module]


func get_modules():
"""Retrieve the dictionary containing all modules."""
return modules
Expand Down Expand Up @@ -529,13 +484,7 @@ func add_logfile(logfile_path = default_logfile_path):
"""Add a new logfile that can then be attached to one or more modules.
Returns a reference to the instanced logfile."""
if external_sinks.has(logfile_path):
info(
(
"A logfile pointing to '%s' already exists; discarding the call to add it anew."
% logfile_path
),
PLUGIN_NAME
)
info("A logfile pointing to '%s' already exists; discarding the call to add it anew." % logfile_path, PLUGIN_NAME)
else:
external_sinks[logfile_path] = Logfile.new(logfile_path)
return external_sinks[logfile_path]
Expand Down Expand Up @@ -578,35 +527,18 @@ func set_default_output_strategy(output_strategy_mask, level = -1):
"""Set the default output strategy mask of the given level or (by
default) all levels for all modules without a custom strategy."""
if not output_strategy_mask in range(0, MAX_STRATEGY + 1):
error(
"The output strategy mask must be comprised between 0 and %d." % MAX_STRATEGY,
PLUGIN_NAME
)
error("The output strategy mask must be comprised between 0 and %d." % MAX_STRATEGY, PLUGIN_NAME)
return
if level == -1: # Set for all levels
for i in range(0, LEVELS.size()):
default_output_strategies[i] = output_strategy_mask
info(
(
"The default output strategy mask was set to '%d' for all levels."
% [output_strategy_mask]
),
PLUGIN_NAME
)
info("The default output strategy mask was set to '%d' for all levels." % [output_strategy_mask], PLUGIN_NAME)
else:
if not level in range(0, LEVELS.size()):
error(
"The level must be comprised between 0 and %d." % (LEVELS.size() - 1), PLUGIN_NAME
)
error("The level must be comprised between 0 and %d." % (LEVELS.size() - 1), PLUGIN_NAME)
return
default_output_strategies[level] = output_strategy_mask
info(
(
"The default output strategy mask was set to '%d' for the '%s' level."
% [output_strategy_mask, LEVELS[level]]
),
PLUGIN_NAME
)
info("The default output strategy mask was set to '%d' for the '%s' level." % [output_strategy_mask, LEVELS[level]], PLUGIN_NAME)


func get_default_output_strategy(level):
Expand Down Expand Up @@ -661,7 +593,8 @@ func get_formatted_datetime():
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)
if(module!=null):
output = output.replace(FORMAT_IDS.module, module)
output = output.replace(FORMAT_IDS.message, str(message))
output = output.replace(FORMAT_IDS.time, get_formatted_datetime())

Expand All @@ -682,10 +615,7 @@ func set_output_format(new_format):
"""
for key in FORMAT_IDS:
if new_format.find(FORMAT_IDS[key]) == -1:
error(
"Invalid output string format. It lacks the '%s' identifier." % FORMAT_IDS[key],
PLUGIN_NAME
)
error("Invalid output string format. It lacks the '%s' identifier." % FORMAT_IDS[key], PLUGIN_NAME)
return
output_format = new_format
info("Successfully changed the output format to '%s'." % output_format, PLUGIN_NAME)
Expand All @@ -704,13 +634,7 @@ func set_max_memory_size(new_size):
"""Set the maximum amount of messages to be remembered when
using the STRATEGY_MEMORY output strategy."""
if new_size <= 0:
error(
(
"The maximum amount of remembered messages must be a positive non-null integer. Received %d."
% new_size
),
PLUGIN_NAME
)
error("The maximum amount of remembered messages must be a positive non-null integer. Received %d." % new_size, PLUGIN_NAME)
return

var new_buffer = []
Expand Down Expand Up @@ -741,10 +665,7 @@ func set_max_memory_size(new_size):
memory_idx = new_idx
invalid_memory_cache = true
max_memory_size = new_size
info(
"Successfully set the maximum amount of remembered messages to %d." % max_memory_size,
PLUGIN_NAME
)
info("Successfully set the maximum amount of remembered messages to %d." % max_memory_size, PLUGIN_NAME)


func get_max_memory_size():
Expand Down Expand Up @@ -797,36 +718,31 @@ func save_config(configfile = default_configfile_path):
var config = ConfigFile.new()

# Store default config
config.set_value(PLUGIN_NAME, config_fields.default_output_level, default_output_level)
config.set_value(
PLUGIN_NAME, config_fields.default_output_strategies, default_output_strategies
)
config.set_value(PLUGIN_NAME, config_fields.default_logfile_path, default_logfile_path)
config.set_value(PLUGIN_NAME, config_fields.max_memory_size, max_memory_size)
config.set_value(PLUGIN_NAME, CONFIG_FIELDS.default_output_level, default_output_level)
config.set_value(PLUGIN_NAME, CONFIG_FIELDS.default_output_strategies, default_output_strategies)
config.set_value(PLUGIN_NAME, CONFIG_FIELDS.default_logfile_path, default_logfile_path)
config.set_value(PLUGIN_NAME, CONFIG_FIELDS.max_memory_size, max_memory_size)

# External sink config
var external_sinks_arr = []
var sorted_keys = external_sinks.keys()
sorted_keys.sort() # Sadly doesn't return the array, so we need to split it
for external_sink in sorted_keys:
external_sinks_arr.append(external_sinks[external_sink].get_config())
config.set_value(PLUGIN_NAME, config_fields.external_sinks, external_sinks_arr)
config.set_value(PLUGIN_NAME, CONFIG_FIELDS.external_sinks, external_sinks_arr)

# Modules config
var modules_arr = []
sorted_keys = modules.keys()
sorted_keys.sort()
for module in sorted_keys:
modules_arr.append(modules[module].get_config())
config.set_value(PLUGIN_NAME, config_fields.modules, modules_arr)
config.set_value(PLUGIN_NAME, CONFIG_FIELDS.modules, modules_arr)

# Save and return the corresponding error code
var err = config.save(configfile)
if err:
error(
"Could not save the config in '%s'; exited with error %d." % [configfile, err],
PLUGIN_NAME
)
error("Could not save the config in '%s'; exited with error %d." % [configfile, err], PLUGIN_NAME)
return err
info("Successfully saved the config to '%s'." % configfile, PLUGIN_NAME)
return OK
Expand Down Expand Up @@ -856,29 +772,29 @@ func load_config(configfile = default_configfile_path):

# Load default config
default_output_level = config.get_value(
PLUGIN_NAME, config_fields.default_output_level, default_output_level
PLUGIN_NAME, CONFIG_FIELDS.default_output_level, default_output_level
)
default_output_strategies = config.get_value(
PLUGIN_NAME, config_fields.default_output_strategies, default_output_strategies
PLUGIN_NAME, CONFIG_FIELDS.default_output_strategies, default_output_strategies
)
default_logfile_path = config.get_value(
PLUGIN_NAME, config_fields.default_logfile_path, default_logfile_path
PLUGIN_NAME, CONFIG_FIELDS.default_logfile_path, default_logfile_path
)
max_memory_size = config.get_value(PLUGIN_NAME, config_fields.max_memory_size, max_memory_size)
max_memory_size = config.get_value(PLUGIN_NAME, CONFIG_FIELDS.max_memory_size, max_memory_size)

# Load external config and initialize them
flush_buffers()
external_sinks = {}
add_logfile(default_logfile_path)
for logfile_cfg in config.get_value(PLUGIN_NAME, config_fields.external_sinks, []):
for logfile_cfg in config.get_value(PLUGIN_NAME, CONFIG_FIELDS.external_sinks, []):
var logfile = Logfile.new(logfile_cfg["path"], logfile_cfg["queue_mode"])
external_sinks[logfile_cfg["path"]] = logfile

# Load modules config and initialize them
modules = {}
add_module(PLUGIN_NAME)
add_module(default_module_name)
for module_cfg in config.get_value(PLUGIN_NAME, config_fields.modules, []):
for module_cfg in config.get_value(PLUGIN_NAME, CONFIG_FIELDS.modules, []):
var module = Module.new(
module_cfg["name"],
module_cfg["output_level"],
Expand Down

0 comments on commit d0f6042

Please sign in to comment.