Skip to content

Commit

Permalink
added config file support
Browse files Browse the repository at this point in the history
  • Loading branch information
hedin-hiervard committed Jul 28, 2019
1 parent e6a7ed7 commit cabedea
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions logger.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
extends Node # Needed to work as a singleton

signal logged
signal module_enabled_changed

var _config_file = null
var _config_file_path = null

##================##
## Inner classes ##
##================##
Expand Down Expand Up @@ -644,3 +648,70 @@ func _exit_tree():
continue
logfile.flush_buffer()
processed_logfiles.append(logfile)

func _save_config_file():
_config_file.save(_config_file_path)

func _load_config_file():
_config_file = ConfigFile.new()
_config_file.load(_config_file_path)
set_output_level(_config_file.get_value("settings", "output_level", INFO))

func set_output_level(val: int) -> void:
set_default_output_level(val)

for mname in get_modules():
get_module(mname).output_level = val

info("logging level set to %s" % LEVELS[val])
_config_file.set_value("settings", "output_level", val)
_save_config_file()

func _set_module_enabled_internal(mname: String, enable: bool) -> void:
var module = get_module(mname)

if enable:
module.set_output_strategy(STRATEGY_FILE | STRATEGY_PRINT)
else:
module.set_output_strategy(STRATEGY_MUTE)

func start(config_file_path, log_file_path, mlist: Array = []):
assert(_config_file_path == null)
assert(config_file_path != null)

_config_file_path = config_file_path
_load_config_file()

set_default_logfile_path(log_file_path)
set_default_output_strategy(STRATEGY_PRINT | STRATEGY_FILE)

# in case we already logged something, reset the output level of the modules
for m in get_modules():
get_module(m).output_level = default_output_level

var mnames = []
if _config_file.has_section("modules"):
mnames = _config_file.get_section_keys("modules")

for m in mlist:
mnames.append(m)

for mname in mnames:
if !get_modules().has(mname):
add_module(mname)
_set_module_enabled_internal(mname, is_module_enabled(mname))
get_module(mname).output_level = default_output_level

var stripped_name = log_file_path.replace("user:/", OS.get_user_data_dir())
info("logging to %s" % stripped_name)
_save_config_file()

func is_module_enabled(mname: String) -> bool:
var default = mname.find("debug") == -1
return _config_file.get_value("modules", mname, default)

func set_module_enabled(mname: String, enable: bool) -> void:
_config_file.set_value("modules", mname, enable)
_set_module_enabled_internal(mname, enable)
emit_signal("module_enabled_changed", mname, enable)
_save_config_file()

0 comments on commit cabedea

Please sign in to comment.