From 49ec5a7cdbb97940c776f5b51ddbe08cdff6dc4b Mon Sep 17 00:00:00 2001 From: Fred Dushin Date: Tue, 31 Oct 2023 21:27:41 -0400 Subject: [PATCH] All users to use logging macros without instantiating the logger manager. This change set allows users to use logging macros and functions in code, but for those calls not to fail with an error if the logger manager has not been instantiated. Users must still instantiate the logger manager (or it is instantiated automatically as part of the kernel application, TBD) in order for logs to pass the initial allow phase, but all logs will be silently dropped until the EAVMLib logger manager is instaniated. Signed-off-by: Fred Dushin --- libs/eavmlib/src/logger_manager.erl | 12 +++++++++++- tests/libs/estdlib/test_logger.erl | 6 +++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/libs/eavmlib/src/logger_manager.erl b/libs/eavmlib/src/logger_manager.erl index 3a91fbc8f..87374979c 100644 --- a/libs/eavmlib/src/logger_manager.erl +++ b/libs/eavmlib/src/logger_manager.erl @@ -80,7 +80,17 @@ get_id() -> %% @hidden allow(Level, Module) -> - gen_server:call(?MODULE, {allow, Level, Module}). + %% if the logger manager has not been instantiated, then + %% "fail" silently (i.e., log nothing). The logger manager + %% either needs to be instantiated manually, or it is be done + %% so automatically in OTP-style deployment when the kernel + %% is started by init. + case erlang:whereis(?MODULE) of + undefined -> + false; + _ -> + gen_server:call(?MODULE, {allow, Level, Module}) + end. %% %% gen_server callbacks diff --git a/tests/libs/estdlib/test_logger.erl b/tests/libs/estdlib/test_logger.erl index 2eb68b3f4..27ccff173 100644 --- a/tests/libs/estdlib/test_logger.erl +++ b/tests/libs/estdlib/test_logger.erl @@ -46,7 +46,11 @@ test_logger() -> ok. test_default_logger() -> - ?ASSERT_FAILURE(?LOG_NOTICE("Tried to log before starting log_manager!")), + %% silent "failure" (nothing will be logged) if the + %% logger manager has not been instantiated. In OTP-style + %% deployment, the logger manager is initialized in + %% the kernel application. + ok = ?LOG_NOTICE("Tried to log before starting log_manager!"), {ok, _Pid} = logger_manager:start_link(#{}),