@@ -85,24 +85,14 @@ class Logfile:
85
85
if not dir :
86
86
var err = DirAccess .get_open_error ()
87
87
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 ])
94
89
return false
95
90
else :
96
91
# TODO: Move directory creation to the function that will actually *write*
97
92
dir .make_dir_recursive (base_dir )
98
93
var err2 = DirAccess .get_open_error ()
99
94
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 ])
106
96
return false
107
97
108
98
print ("[INFO] [logger] Successfully created the '%s ' directory." % base_dir )
@@ -157,12 +147,7 @@ class Logfile:
157
147
158
148
if result == null :
159
149
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 ])
166
151
return null
167
152
else :
168
153
return result
@@ -197,12 +182,7 @@ class Module:
197
182
on their respective strategies, while levels lower than the given one
198
183
will be discarded."""
199
184
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 ])
206
186
return
207
187
output_level = level
208
188
@@ -212,12 +192,7 @@ class Module:
212
192
func set_common_output_strategy (output_strategy_mask ):
213
193
"""Set the common output strategy mask for all levels of the module."""
214
194
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 ])
221
196
return
222
197
for i in range (0 , LEVELS .size ()):
223
198
output_strategies [i ] = output_strategy_mask
@@ -226,24 +201,14 @@ class Module:
226
201
"""Set the output strategy for the given level or (by default) all
227
202
levels of the module."""
228
203
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 ])
235
205
return
236
206
if level == - 1 : # Set for all levels
237
207
for i in range (0 , LEVELS .size ()):
238
208
output_strategies [i ] = output_strategy_mask
239
209
else :
240
210
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 ])
247
212
return
248
213
output_strategies [level ] = output_strategy_mask
249
214
@@ -463,10 +428,7 @@ func add_module(
463
428
default ones.
464
429
Returns a reference to the instanced module."""
465
430
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 )
470
432
else :
471
433
if logfile == null :
472
434
logfile = get_external_sink (default_logfile_path )
@@ -477,17 +439,10 @@ func add_module(
477
439
func get_module (module = default_module_name ):
478
440
"""Retrieve the given module if it exists; if not, it will be created."""
479
441
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 )
487
443
add_module (module )
488
444
return modules [module ]
489
445
490
-
491
446
func get_modules ():
492
447
"""Retrieve the dictionary containing all modules."""
493
448
return modules
@@ -529,13 +484,7 @@ func add_logfile(logfile_path = default_logfile_path):
529
484
"""Add a new logfile that can then be attached to one or more modules.
530
485
Returns a reference to the instanced logfile."""
531
486
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 )
539
488
else :
540
489
external_sinks [logfile_path ] = Logfile .new (logfile_path )
541
490
return external_sinks [logfile_path ]
@@ -578,35 +527,18 @@ func set_default_output_strategy(output_strategy_mask, level = -1):
578
527
"""Set the default output strategy mask of the given level or (by
579
528
default) all levels for all modules without a custom strategy."""
580
529
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 )
585
531
return
586
532
if level == - 1 : # Set for all levels
587
533
for i in range (0 , LEVELS .size ()):
588
534
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 )
596
536
else :
597
537
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 )
601
539
return
602
540
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 )
610
542
611
543
612
544
func get_default_output_strategy (level ):
@@ -661,7 +593,8 @@ func get_formatted_datetime():
661
593
func format (template , level , module , message , error_code = - 1 ):
662
594
var output = template
663
595
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 )
665
598
output = output .replace (FORMAT_IDS .message , str (message ))
666
599
output = output .replace (FORMAT_IDS .time , get_formatted_datetime ())
667
600
@@ -682,10 +615,7 @@ func set_output_format(new_format):
682
615
"""
683
616
for key in FORMAT_IDS :
684
617
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 )
689
619
return
690
620
output_format = new_format
691
621
info ("Successfully changed the output format to '%s '." % output_format , PLUGIN_NAME )
@@ -704,13 +634,7 @@ func set_max_memory_size(new_size):
704
634
"""Set the maximum amount of messages to be remembered when
705
635
using the STRATEGY_MEMORY output strategy."""
706
636
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 )
714
638
return
715
639
716
640
var new_buffer = []
@@ -741,10 +665,7 @@ func set_max_memory_size(new_size):
741
665
memory_idx = new_idx
742
666
invalid_memory_cache = true
743
667
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 )
748
669
749
670
750
671
func get_max_memory_size ():
@@ -797,36 +718,31 @@ func save_config(configfile = default_configfile_path):
797
718
var config = ConfigFile .new ()
798
719
799
720
# 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 )
806
725
807
726
# External sink config
808
727
var external_sinks_arr = []
809
728
var sorted_keys = external_sinks .keys ()
810
729
sorted_keys .sort () # Sadly doesn't return the array, so we need to split it
811
730
for external_sink in sorted_keys :
812
731
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 )
814
733
815
734
# Modules config
816
735
var modules_arr = []
817
736
sorted_keys = modules .keys ()
818
737
sorted_keys .sort ()
819
738
for module in sorted_keys :
820
739
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 )
822
741
823
742
# Save and return the corresponding error code
824
743
var err = config .save (configfile )
825
744
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 )
830
746
return err
831
747
info ("Successfully saved the config to '%s '." % configfile , PLUGIN_NAME )
832
748
return OK
@@ -856,29 +772,29 @@ func load_config(configfile = default_configfile_path):
856
772
857
773
# Load default config
858
774
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
860
776
)
861
777
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
863
779
)
864
780
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
866
782
)
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 )
868
784
869
785
# Load external config and initialize them
870
786
flush_buffers ()
871
787
external_sinks = {}
872
788
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 , []):
874
790
var logfile = Logfile .new (logfile_cfg ["path" ], logfile_cfg ["queue_mode" ])
875
791
external_sinks [logfile_cfg ["path" ]] = logfile
876
792
877
793
# Load modules config and initialize them
878
794
modules = {}
879
795
add_module (PLUGIN_NAME )
880
796
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 , []):
882
798
var module = Module .new (
883
799
module_cfg ["name" ],
884
800
module_cfg ["output_level" ],
0 commit comments