Skip to content

Commit

Permalink
Merge pull request #914 from fadushin/loggger_manager-fix
Browse files Browse the repository at this point in the history
Allow use of 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.

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
  • Loading branch information
bettio committed Nov 2, 2023
2 parents cadcb67 + 49ec5a7 commit 493d719
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 11 additions & 1 deletion libs/eavmlib/src/logger_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion tests/libs/estdlib/test_logger.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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(#{}),

Expand Down

0 comments on commit 493d719

Please sign in to comment.