Alow less colourful output
Basically this adds a new/3 with a third argument of intensity. The default (and by that the behaviour of new/1 and /2) remain unchanged so existing code isn't affected. If intenisty is set to `low` (instead of `high`) then only the prefix is coloured making the whole output less colourful and for some cases easyer to read.
This commit is contained in:
parent
49bc69e35a
commit
87c76aeb2a
4 changed files with 46 additions and 18 deletions
|
@ -1,7 +1,7 @@
|
||||||
%% -*- mode: Erlang; fill-column: 80; comment-column: 75; -*-
|
%% -*- mode: Erlang; fill-column: 80; comment-column: 75; -*-
|
||||||
|
|
||||||
%% Dependencies ================================================================
|
%% Dependencies ================================================================
|
||||||
{deps, []}.
|
{deps, [cf]}.
|
||||||
|
|
||||||
{erl_first_files, ["ec_dictionary", "ec_vsn"]}.
|
{erl_first_files, ["ec_dictionary", "ec_vsn"]}.
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
[].
|
[{<<"cf">>,{pkg,<<"cf">>,<<"0.1.2">>},0}].
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
|
|
||||||
-export([new/1,
|
-export([new/1,
|
||||||
new/2,
|
new/2,
|
||||||
|
new/3,
|
||||||
log/4,
|
log/4,
|
||||||
should/2,
|
should/2,
|
||||||
debug/2,
|
debug/2,
|
||||||
|
@ -40,17 +41,18 @@
|
||||||
|
|
||||||
-include("ec_cmd_log.hrl").
|
-include("ec_cmd_log.hrl").
|
||||||
|
|
||||||
-define(RED, 31).
|
-define(RED, $r).
|
||||||
-define(GREEN, 32).
|
-define(GREEN, $g).
|
||||||
-define(YELLOW, 33).
|
-define(YELLOW, $y).
|
||||||
-define(BLUE, 34).
|
-define(BLUE, $b).
|
||||||
-define(MAGENTA, 35).
|
-define(MAGENTA, $m).
|
||||||
-define(CYAN, 36).
|
-define(CYAN, $c).
|
||||||
|
|
||||||
-define(PREFIX, "===> ").
|
-define(PREFIX, "===> ").
|
||||||
|
|
||||||
-record(state_t, {log_level=0 :: int_log_level(),
|
-record(state_t, {log_level=0 :: int_log_level(),
|
||||||
caller=api :: caller(),
|
caller=api :: caller(),
|
||||||
|
intensity=low :: low | high,
|
||||||
term_cap=full :: full | dumb }).
|
term_cap=full :: full | dumb }).
|
||||||
|
|
||||||
%%============================================================================
|
%%============================================================================
|
||||||
|
@ -71,9 +73,11 @@
|
||||||
|
|
||||||
-type atom_log_level() :: error | warn | info | debug.
|
-type atom_log_level() :: error | warn | info | debug.
|
||||||
|
|
||||||
|
-type intensity() :: low | high.
|
||||||
|
|
||||||
-type log_fun() :: fun(() -> iolist()).
|
-type log_fun() :: fun(() -> iolist()).
|
||||||
|
|
||||||
-type color() :: 31..36.
|
-type color() :: char().
|
||||||
|
|
||||||
-opaque t() :: #state_t{}.
|
-opaque t() :: #state_t{}.
|
||||||
|
|
||||||
|
@ -86,9 +90,17 @@ new(LogLevel) ->
|
||||||
new(LogLevel, api).
|
new(LogLevel, api).
|
||||||
|
|
||||||
-spec new(log_level(), caller()) -> t().
|
-spec new(log_level(), caller()) -> t().
|
||||||
new(LogLevel, Caller) when LogLevel >= 0, LogLevel =< 3 ->
|
new(LogLevel, Caller) ->
|
||||||
#state_t{log_level=LogLevel, caller=Caller, term_cap=query_term_env()};
|
new(LogLevel, Caller, high).
|
||||||
new(AtomLogLevel, Caller)
|
|
||||||
|
|
||||||
|
-spec new(log_level(), caller(), intensity()) -> t().
|
||||||
|
new(LogLevel, Caller, Intensity) when (Intensity =:= low orelse
|
||||||
|
Intensity =:= high),
|
||||||
|
LogLevel >= 0, LogLevel =< 3 ->
|
||||||
|
#state_t{log_level=LogLevel, caller=Caller, term_cap=query_term_env(),
|
||||||
|
intensity=Intensity};
|
||||||
|
new(AtomLogLevel, Caller, Intensity)
|
||||||
when AtomLogLevel =:= error;
|
when AtomLogLevel =:= error;
|
||||||
AtomLogLevel =:= warn;
|
AtomLogLevel =:= warn;
|
||||||
AtomLogLevel =:= info;
|
AtomLogLevel =:= info;
|
||||||
|
@ -99,7 +111,8 @@ new(AtomLogLevel, Caller)
|
||||||
info -> 2;
|
info -> 2;
|
||||||
debug -> 3
|
debug -> 3
|
||||||
end,
|
end,
|
||||||
new(LogLevel, Caller).
|
new(LogLevel, Caller, Intensity).
|
||||||
|
|
||||||
|
|
||||||
%% @doc log at the debug level given the current log state with a string or
|
%% @doc log at the debug level given the current log state with a string or
|
||||||
%% function that returns a string
|
%% function that returns a string
|
||||||
|
@ -218,10 +231,25 @@ format(Log) ->
|
||||||
<<")">>].
|
<<")">>].
|
||||||
|
|
||||||
-spec colorize(t(), color(), boolean(), string()) -> string().
|
-spec colorize(t(), color(), boolean(), string()) -> string().
|
||||||
colorize(#state_t{caller=command_line, term_cap=full}, Color, false, Msg) when is_integer(Color) ->
|
|
||||||
lists:flatten(io_lib:format("\033[~B;~Bm~s~s\033[0m", [0, Color, ?PREFIX, Msg]));
|
-define(VALID_COLOR(C),
|
||||||
colorize(#state_t{caller=command_line, term_cap=dumb}, Color, _Bold, Msg) when is_integer(Color) ->
|
C =:= $r orelse C =:= $g orelse C =:= $y orelse
|
||||||
lists:flatten(io_lib:format("~s~s", [?PREFIX, Msg]));
|
C =:= $b orelse C =:= $m orelse C =:= $c orelse
|
||||||
|
C =:= $R orelse C =:= $G orelse C =:= $Y orelse
|
||||||
|
C =:= $B orelse C =:= $M orelse C =:= $C).
|
||||||
|
|
||||||
|
%% We're sneaky we can substract 32 to get the uppercase character
|
||||||
|
colorize(State, Color, true, Msg) when ?VALID_COLOR(Color) ->
|
||||||
|
colorize(State, Color - 32, fase, Msg);
|
||||||
|
colorize(#state_t{caller=command_line, term_cap=full, intensity = high},
|
||||||
|
Color, false, Msg) when ?VALID_COLOR(Color) ->
|
||||||
|
lists:flatten(cf:format("~!" ++ [Color] ++"~s~s", [?PREFIX, Msg]));
|
||||||
|
colorize(#state_t{caller=command_line, term_cap=full, intensity = low},
|
||||||
|
Color, false, Msg) when ?VALID_COLOR(Color) ->
|
||||||
|
lists:flatten(cf:format("~!" ++ [Color] ++"~s~!!~s", [?PREFIX, Msg]));
|
||||||
|
colorize(#state_t{caller=command_line, term_cap=dumb}, Color, _Bold, Msg)
|
||||||
|
when ?VALID_COLOR(Color) ->
|
||||||
|
lists:flatten(cf:format("~s~s", [?PREFIX, Msg]));
|
||||||
colorize(_LogState, _Color, _Bold, Msg) ->
|
colorize(_LogState, _Color, _Bold, Msg) ->
|
||||||
Msg.
|
Msg.
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{application,erlware_commons,
|
{application,erlware_commons,
|
||||||
[{description,"Additional standard library for Erlang"},
|
[{description,"Additional standard library for Erlang"},
|
||||||
{vsn,"0.16.0"},
|
{vsn,"0.16.1"},
|
||||||
{modules,[]},
|
{modules,[]},
|
||||||
{registered,[]},
|
{registered,[]},
|
||||||
{applications,[kernel,stdlib]},
|
{applications,[kernel,stdlib]},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue