summaryrefslogtreecommitdiff
path: root/src/rabbit_log.erl
diff options
context:
space:
mode:
authorSimon MacMullen <simon@rabbitmq.com>2014-08-14 12:07:37 +0100
committerSimon MacMullen <simon@rabbitmq.com>2014-08-14 12:07:37 +0100
commit4a3968ead4cf5dcbca4e1a5a8d68b3e0d8ba428a (patch)
tree4223701ec7d200f71d112ddd35742e5633adb8c4 /src/rabbit_log.erl
parentf8e3cc7d56b34409fd1570a71db45e8cb1a75b82 (diff)
downloadrabbitmq-server-4a3968ead4cf5dcbca4e1a5a8d68b3e0d8ba428a.tar.gz
Further simplify logging. Get rabbit_log to figure out whether the group leader switcheroo is needed, rather than leaving it up to the caller. We now have one logging API. Woot!bug26345
Diffstat (limited to 'src/rabbit_log.erl')
-rw-r--r--src/rabbit_log.erl30
1 files changed, 25 insertions, 5 deletions
diff --git a/src/rabbit_log.erl b/src/rabbit_log.erl
index 2ca5260c..e05ef05a 100644
--- a/src/rabbit_log.erl
+++ b/src/rabbit_log.erl
@@ -17,6 +17,7 @@
-module(rabbit_log).
-export([log/3, log/4, info/1, info/2, warning/1, warning/2, error/1, error/2]).
+-export([with_local_io/1]).
%%----------------------------------------------------------------------------
@@ -37,6 +38,8 @@
-spec(error/1 :: (string()) -> 'ok').
-spec(error/2 :: (string(), [any()]) -> 'ok').
+-spec(with_local_io/1 :: (fun (() -> A)) -> A).
+
-endif.
%%----------------------------------------------------------------------------
@@ -46,11 +49,12 @@ log(Category, Level, Fmt) -> log(Category, Level, Fmt, []).
log(Category, Level, Fmt, Args) when is_list(Args) ->
case level(Level) =< catlevel(Category) of
false -> ok;
- true -> (case Level of
- info -> fun error_logger:info_msg/2;
- warning -> fun error_logger:warning_msg/2;
- error -> fun error_logger:error_msg/2
- end)(Fmt, Args)
+ true -> F = case Level of
+ info -> fun error_logger:info_msg/2;
+ warning -> fun error_logger:warning_msg/2;
+ error -> fun error_logger:error_msg/2
+ end,
+ with_local_io(fun () -> F(Fmt, Args) end)
end.
info(Fmt) -> log(default, info, Fmt).
@@ -75,3 +79,19 @@ level(info) -> 3;
level(warning) -> 2;
level(error) -> 1;
level(none) -> 0.
+
+%% Execute Fun using the IO system of the local node (i.e. the node on
+%% which the code is executing). Since this is invoked for every log
+%% message, we try to avoid unnecessarily churning group_leader/1.
+with_local_io(Fun) ->
+ GL = group_leader(),
+ Node = node(),
+ case node(GL) of
+ Node -> Fun();
+ _ -> group_leader(whereis(user), self()),
+ try
+ Fun()
+ after
+ group_leader(GL, self())
+ end
+ end.