aboutsummaryrefslogtreecommitdiffstats
path: root/src/rlx_log.erl
diff options
context:
space:
mode:
authorTristan Sloughter <[email protected]>2013-09-14 13:15:47 -0500
committerTristan Sloughter <[email protected]>2013-09-16 11:12:22 -0500
commitd72e2cda09df280e1e690d9233790ebbb9e58812 (patch)
tree4ab69650fb2d57a4c7c063914f4a2b5a02319e3e /src/rlx_log.erl
parent8606f9cb6d5f71611643780235bc0e57a757a759 (diff)
downloadrelx-d72e2cda09df280e1e690d9233790ebbb9e58812.tar.gz
relx-d72e2cda09df280e1e690d9233790ebbb9e58812.tar.bz2
relx-d72e2cda09df280e1e690d9233790ebbb9e58812.zip
improved user output look
Diffstat (limited to 'src/rlx_log.erl')
-rw-r--r--src/rlx_log.erl34
1 files changed, 27 insertions, 7 deletions
diff --git a/src/rlx_log.erl b/src/rlx_log.erl
index 901ae7c..6122e9b 100644
--- a/src/rlx_log.erl
+++ b/src/rlx_log.erl
@@ -59,6 +59,17 @@
-type log_fun() :: fun(() -> iolist()).
+-type color() :: 31..36.
+
+-define(RED, 31).
+-define(GREEN, 32).
+-define(YELLOW, 33).
+-define(BLUE, 34).
+-define(MAGENTA, 35).
+-define(CYAN, 36).
+
+-define(PREFIX, "===> ").
+
%%============================================================================
%% API
%%============================================================================
@@ -83,7 +94,7 @@ new(AtomLogLevel)
-spec debug(t(), string() | log_fun()) -> ok.
debug(LogState, Fun)
when erlang:is_function(Fun) ->
- log(LogState, ?RLX_DEBUG, Fun);
+ log(LogState, ?RLX_DEBUG, fun() -> colorize(?CYAN, false, Fun()) end);
debug(LogState, String) ->
debug(LogState, "~s~n", [String]).
@@ -91,14 +102,14 @@ debug(LogState, String) ->
%% and argements @see io:format/2
-spec debug(t(), string(), [any()]) -> ok.
debug(LogState, FormatString, Args) ->
- log(LogState, ?RLX_DEBUG, FormatString, Args).
+ log(LogState, ?RLX_DEBUG, colorize(?CYAN, false, FormatString), Args).
%% @doc log at the info level given the current log state with a string or
%% function that returns a string
-spec info(t(), string() | log_fun()) -> ok.
info(LogState, Fun)
when erlang:is_function(Fun) ->
- log(LogState, ?RLX_INFO, Fun);
+ log(LogState, ?RLX_INFO, fun() -> colorize(?GREEN, false, Fun()) end);
info(LogState, String) ->
info(LogState, "~s~n", [String]).
@@ -106,14 +117,14 @@ info(LogState, String) ->
%% and argements @see io:format/2
-spec info(t(), string(), [any()]) -> ok.
info(LogState, FormatString, Args) ->
- log(LogState, ?RLX_INFO, FormatString, Args).
+ log(LogState, ?RLX_INFO, colorize(?GREEN, false, FormatString), Args).
%% @doc log at the error level given the current log state with a string or
%% format string that returns a function
-spec error(t(), string() | log_fun()) -> ok.
error(LogState, Fun)
when erlang:is_function(Fun) ->
- log(LogState, ?RLX_ERROR, Fun);
+ log(LogState, ?RLX_ERROR, fun() -> colorize(?RED, false, Fun()) end);
error(LogState, String) ->
error(LogState, "~s~n", [String]).
@@ -121,7 +132,7 @@ error(LogState, String) ->
%% and argements @see io:format/2
-spec error(t(), string(), [any()]) -> ok.
error(LogState, FormatString, Args) ->
- log(LogState, ?RLX_ERROR, FormatString, Args).
+ log(LogState, ?RLX_ERROR, colorize(?GREEN, false, FormatString), Args).
%% @doc Execute the fun passed in if log level is as expected.
-spec log(t(), int_log_level(), log_fun()) -> ok.
@@ -131,7 +142,6 @@ log({?MODULE, DetailLogLevel}, LogLevel, Fun)
log(_, _, _) ->
ok.
-
%% @doc when the module log level is less then or equal to the log level for the
%% call then write the log info out. When its not then ignore the call.
-spec log(t(), int_log_level(), string(), [any()]) -> ok.
@@ -172,6 +182,16 @@ format(Log) ->
erlang:atom_to_list(atom_log_level(Log)),
<<")">>].
+-spec colorize(color(), boolean(), string()) -> string().
+colorize(Color, false, Msg) when is_integer(Color) ->
+ colorize_(Color, 0, Msg);
+colorize(Color, true, Msg) when is_integer(Color) ->
+ colorize_(Color, 1, Msg).
+
+-spec colorize_(color(), integer(), string()) -> string().
+colorize_(Color, Bold, Msg) when is_integer(Color), is_integer(Bold)->
+ lists:flatten(io_lib:format("\033[~B;~Bm~s~s\033[0m", [Bold, Color, ?PREFIX, Msg])).
+
%%%===================================================================
%%% Test Functions
%%%===================================================================