Skip to content

Commit dd3359f

Browse files
author
Noah Ruben
committed
sq:
1 parent 147d6a9 commit dd3359f

File tree

1 file changed

+32
-116
lines changed

1 file changed

+32
-116
lines changed

logger.gd

+32-116
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,14 @@ class Logfile:
8585
if not dir:
8686
var err = DirAccess.get_open_error()
8787
if err:
88-
print(
89-
(
90-
"[ERROR] [logger] Could not create the '%s' directory; exited with error %d."
91-
% [base_dir, err]
92-
)
93-
)
88+
print("[ERROR] [logger] Could not create the '%s' directory; exited with error %d." % [base_dir, err])
9489
return false
9590
else:
9691
# TODO: Move directory creation to the function that will actually *write*
9792
dir.make_dir_recursive(base_dir)
9893
var err2 = DirAccess.get_open_error()
9994
if err2:
100-
print(
101-
(
102-
"[ERROR] [logger] Could not create the '%s' directory; exited with error %d."
103-
% [base_dir, err2]
104-
)
105-
)
95+
print("[ERROR] [logger] Could not create the '%s' directory; exited with error %d." % [base_dir, err2])
10696
return false
10797

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

158148
if result == null:
159149
var err = FileAccess.get_open_error()
160-
print(
161-
(
162-
"[ERROR] [logger] Could not open the '%s' log file; exited with error %d."
163-
% [path, err]
164-
)
165-
)
150+
print("[ERROR] [logger] Could not open the '%s' log file; exited with error %d." % [path, err])
166151
return null
167152
else:
168153
return result
@@ -197,12 +182,7 @@ class Module:
197182
on their respective strategies, while levels lower than the given one
198183
will be discarded."""
199184
if not level in range(0, LEVELS.size()):
200-
print(
201-
(
202-
"[ERROR] [%s] The level must be comprised between 0 and %d."
203-
% [PLUGIN_NAME, LEVELS.size() - 1]
204-
)
205-
)
185+
print("[ERROR] [%s] The level must be comprised between 0 and %d." % [PLUGIN_NAME, LEVELS.size() - 1])
206186
return
207187
output_level = level
208188

@@ -212,12 +192,7 @@ class Module:
212192
func set_common_output_strategy(output_strategy_mask):
213193
"""Set the common output strategy mask for all levels of the module."""
214194
if not output_strategy_mask in range(0, MAX_STRATEGY + 1):
215-
print(
216-
(
217-
"[ERROR] [%s] The output strategy mask must be comprised between 0 and %d."
218-
% [PLUGIN_NAME, MAX_STRATEGY]
219-
)
220-
)
195+
print("[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." % [PLUGIN_NAME, MAX_STRATEGY])
221196
return
222197
for i in range(0, LEVELS.size()):
223198
output_strategies[i] = output_strategy_mask
@@ -226,24 +201,14 @@ class Module:
226201
"""Set the output strategy for the given level or (by default) all
227202
levels of the module."""
228203
if not output_strategy_mask in range(0, MAX_STRATEGY + 1):
229-
print(
230-
(
231-
"[ERROR] [%s] The output strategy mask must be comprised between 0 and %d."
232-
% [PLUGIN_NAME, MAX_STRATEGY]
233-
)
234-
)
204+
print("[ERROR] [%s] The output strategy mask must be comprised between 0 and %d." % [PLUGIN_NAME, MAX_STRATEGY])
235205
return
236206
if level == -1: # Set for all levels
237207
for i in range(0, LEVELS.size()):
238208
output_strategies[i] = output_strategy_mask
239209
else:
240210
if not level in range(0, LEVELS.size()):
241-
print(
242-
(
243-
"[ERROR] [%s] The level must be comprised between 0 and %d."
244-
% [PLUGIN_NAME, LEVELS.size() - 1]
245-
)
246-
)
211+
print("[ERROR] [%s] The level must be comprised between 0 and %d." % [PLUGIN_NAME, LEVELS.size() - 1])
247212
return
248213
output_strategies[level] = output_strategy_mask
249214

@@ -463,10 +428,7 @@ func add_module(
463428
default ones.
464429
Returns a reference to the instanced module."""
465430
if modules.has(name):
466-
info(
467-
"The module '%s' already exists; discarding the call to add it anew." % name,
468-
PLUGIN_NAME
469-
)
431+
info("The module '%s' already exists; discarding the call to add it anew." % name, PLUGIN_NAME)
470432
else:
471433
if logfile == null:
472434
logfile = get_external_sink(default_logfile_path)
@@ -477,17 +439,10 @@ func add_module(
477439
func get_module(module = default_module_name):
478440
"""Retrieve the given module if it exists; if not, it will be created."""
479441
if not modules.has(module):
480-
info(
481-
(
482-
"The requested module '%s' does not exist. It will be created with default values."
483-
% module
484-
),
485-
PLUGIN_NAME
486-
)
442+
info("The requested module '%s' does not exist. It will be created with default values." % module, PLUGIN_NAME)
487443
add_module(module)
488444
return modules[module]
489445

490-
491446
func get_modules():
492447
"""Retrieve the dictionary containing all modules."""
493448
return modules
@@ -529,13 +484,7 @@ func add_logfile(logfile_path = default_logfile_path):
529484
"""Add a new logfile that can then be attached to one or more modules.
530485
Returns a reference to the instanced logfile."""
531486
if external_sinks.has(logfile_path):
532-
info(
533-
(
534-
"A logfile pointing to '%s' already exists; discarding the call to add it anew."
535-
% logfile_path
536-
),
537-
PLUGIN_NAME
538-
)
487+
info("A logfile pointing to '%s' already exists; discarding the call to add it anew." % logfile_path, PLUGIN_NAME)
539488
else:
540489
external_sinks[logfile_path] = Logfile.new(logfile_path)
541490
return external_sinks[logfile_path]
@@ -578,35 +527,18 @@ func set_default_output_strategy(output_strategy_mask, level = -1):
578527
"""Set the default output strategy mask of the given level or (by
579528
default) all levels for all modules without a custom strategy."""
580529
if not output_strategy_mask in range(0, MAX_STRATEGY + 1):
581-
error(
582-
"The output strategy mask must be comprised between 0 and %d." % MAX_STRATEGY,
583-
PLUGIN_NAME
584-
)
530+
error("The output strategy mask must be comprised between 0 and %d." % MAX_STRATEGY, PLUGIN_NAME)
585531
return
586532
if level == -1: # Set for all levels
587533
for i in range(0, LEVELS.size()):
588534
default_output_strategies[i] = output_strategy_mask
589-
info(
590-
(
591-
"The default output strategy mask was set to '%d' for all levels."
592-
% [output_strategy_mask]
593-
),
594-
PLUGIN_NAME
595-
)
535+
info("The default output strategy mask was set to '%d' for all levels." % [output_strategy_mask], PLUGIN_NAME)
596536
else:
597537
if not level in range(0, LEVELS.size()):
598-
error(
599-
"The level must be comprised between 0 and %d." % (LEVELS.size() - 1), PLUGIN_NAME
600-
)
538+
error("The level must be comprised between 0 and %d." % (LEVELS.size() - 1), PLUGIN_NAME)
601539
return
602540
default_output_strategies[level] = output_strategy_mask
603-
info(
604-
(
605-
"The default output strategy mask was set to '%d' for the '%s' level."
606-
% [output_strategy_mask, LEVELS[level]]
607-
),
608-
PLUGIN_NAME
609-
)
541+
info("The default output strategy mask was set to '%d' for the '%s' level." % [output_strategy_mask, LEVELS[level]], PLUGIN_NAME)
610542

611543

612544
func get_default_output_strategy(level):
@@ -661,7 +593,8 @@ func get_formatted_datetime():
661593
func format(template, level, module, message, error_code = -1):
662594
var output = template
663595
output = output.replace(FORMAT_IDS.level, LEVELS[level])
664-
output = output.replace(FORMAT_IDS.module, module)
596+
if(module!=null):
597+
output = output.replace(FORMAT_IDS.module, module)
665598
output = output.replace(FORMAT_IDS.message, str(message))
666599
output = output.replace(FORMAT_IDS.time, get_formatted_datetime())
667600

@@ -682,10 +615,7 @@ func set_output_format(new_format):
682615
"""
683616
for key in FORMAT_IDS:
684617
if new_format.find(FORMAT_IDS[key]) == -1:
685-
error(
686-
"Invalid output string format. It lacks the '%s' identifier." % FORMAT_IDS[key],
687-
PLUGIN_NAME
688-
)
618+
error("Invalid output string format. It lacks the '%s' identifier." % FORMAT_IDS[key], PLUGIN_NAME)
689619
return
690620
output_format = new_format
691621
info("Successfully changed the output format to '%s'." % output_format, PLUGIN_NAME)
@@ -704,13 +634,7 @@ func set_max_memory_size(new_size):
704634
"""Set the maximum amount of messages to be remembered when
705635
using the STRATEGY_MEMORY output strategy."""
706636
if new_size <= 0:
707-
error(
708-
(
709-
"The maximum amount of remembered messages must be a positive non-null integer. Received %d."
710-
% new_size
711-
),
712-
PLUGIN_NAME
713-
)
637+
error("The maximum amount of remembered messages must be a positive non-null integer. Received %d." % new_size, PLUGIN_NAME)
714638
return
715639

716640
var new_buffer = []
@@ -741,10 +665,7 @@ func set_max_memory_size(new_size):
741665
memory_idx = new_idx
742666
invalid_memory_cache = true
743667
max_memory_size = new_size
744-
info(
745-
"Successfully set the maximum amount of remembered messages to %d." % max_memory_size,
746-
PLUGIN_NAME
747-
)
668+
info("Successfully set the maximum amount of remembered messages to %d." % max_memory_size, PLUGIN_NAME)
748669

749670

750671
func get_max_memory_size():
@@ -797,36 +718,31 @@ func save_config(configfile = default_configfile_path):
797718
var config = ConfigFile.new()
798719

799720
# Store default config
800-
config.set_value(PLUGIN_NAME, config_fields.default_output_level, default_output_level)
801-
config.set_value(
802-
PLUGIN_NAME, config_fields.default_output_strategies, default_output_strategies
803-
)
804-
config.set_value(PLUGIN_NAME, config_fields.default_logfile_path, default_logfile_path)
805-
config.set_value(PLUGIN_NAME, config_fields.max_memory_size, max_memory_size)
721+
config.set_value(PLUGIN_NAME, CONFIG_FIELDS.default_output_level, default_output_level)
722+
config.set_value(PLUGIN_NAME, CONFIG_FIELDS.default_output_strategies, default_output_strategies)
723+
config.set_value(PLUGIN_NAME, CONFIG_FIELDS.default_logfile_path, default_logfile_path)
724+
config.set_value(PLUGIN_NAME, CONFIG_FIELDS.max_memory_size, max_memory_size)
806725

807726
# External sink config
808727
var external_sinks_arr = []
809728
var sorted_keys = external_sinks.keys()
810729
sorted_keys.sort() # Sadly doesn't return the array, so we need to split it
811730
for external_sink in sorted_keys:
812731
external_sinks_arr.append(external_sinks[external_sink].get_config())
813-
config.set_value(PLUGIN_NAME, config_fields.external_sinks, external_sinks_arr)
732+
config.set_value(PLUGIN_NAME, CONFIG_FIELDS.external_sinks, external_sinks_arr)
814733

815734
# Modules config
816735
var modules_arr = []
817736
sorted_keys = modules.keys()
818737
sorted_keys.sort()
819738
for module in sorted_keys:
820739
modules_arr.append(modules[module].get_config())
821-
config.set_value(PLUGIN_NAME, config_fields.modules, modules_arr)
740+
config.set_value(PLUGIN_NAME, CONFIG_FIELDS.modules, modules_arr)
822741

823742
# Save and return the corresponding error code
824743
var err = config.save(configfile)
825744
if err:
826-
error(
827-
"Could not save the config in '%s'; exited with error %d." % [configfile, err],
828-
PLUGIN_NAME
829-
)
745+
error("Could not save the config in '%s'; exited with error %d." % [configfile, err], PLUGIN_NAME)
830746
return err
831747
info("Successfully saved the config to '%s'." % configfile, PLUGIN_NAME)
832748
return OK
@@ -856,29 +772,29 @@ func load_config(configfile = default_configfile_path):
856772

857773
# Load default config
858774
default_output_level = config.get_value(
859-
PLUGIN_NAME, config_fields.default_output_level, default_output_level
775+
PLUGIN_NAME, CONFIG_FIELDS.default_output_level, default_output_level
860776
)
861777
default_output_strategies = config.get_value(
862-
PLUGIN_NAME, config_fields.default_output_strategies, default_output_strategies
778+
PLUGIN_NAME, CONFIG_FIELDS.default_output_strategies, default_output_strategies
863779
)
864780
default_logfile_path = config.get_value(
865-
PLUGIN_NAME, config_fields.default_logfile_path, default_logfile_path
781+
PLUGIN_NAME, CONFIG_FIELDS.default_logfile_path, default_logfile_path
866782
)
867-
max_memory_size = config.get_value(PLUGIN_NAME, config_fields.max_memory_size, max_memory_size)
783+
max_memory_size = config.get_value(PLUGIN_NAME, CONFIG_FIELDS.max_memory_size, max_memory_size)
868784

869785
# Load external config and initialize them
870786
flush_buffers()
871787
external_sinks = {}
872788
add_logfile(default_logfile_path)
873-
for logfile_cfg in config.get_value(PLUGIN_NAME, config_fields.external_sinks, []):
789+
for logfile_cfg in config.get_value(PLUGIN_NAME, CONFIG_FIELDS.external_sinks, []):
874790
var logfile = Logfile.new(logfile_cfg["path"], logfile_cfg["queue_mode"])
875791
external_sinks[logfile_cfg["path"]] = logfile
876792

877793
# Load modules config and initialize them
878794
modules = {}
879795
add_module(PLUGIN_NAME)
880796
add_module(default_module_name)
881-
for module_cfg in config.get_value(PLUGIN_NAME, config_fields.modules, []):
797+
for module_cfg in config.get_value(PLUGIN_NAME, CONFIG_FIELDS.modules, []):
882798
var module = Module.new(
883799
module_cfg["name"],
884800
module_cfg["output_level"],

0 commit comments

Comments
 (0)