From 87c76aeb2a9476f4341b715336046e57b03a3ffb Mon Sep 17 00:00:00 2001 From: "Heinz N. Gies" Date: Wed, 23 Sep 2015 20:41:15 +0200 Subject: [PATCH 1/2] 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. --- rebar.config | 2 +- rebar.lock | 2 +- src/ec_cmd_log.erl | 58 +++++++++++++++++++++++++++---------- src/erlware_commons.app.src | 2 +- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/rebar.config b/rebar.config index 7f67a43..c20e0f2 100644 --- a/rebar.config +++ b/rebar.config @@ -1,7 +1,7 @@ %% -*- mode: Erlang; fill-column: 80; comment-column: 75; -*- %% Dependencies ================================================================ -{deps, []}. +{deps, [cf]}. {erl_first_files, ["ec_dictionary", "ec_vsn"]}. diff --git a/rebar.lock b/rebar.lock index 57afcca..477758c 100644 --- a/rebar.lock +++ b/rebar.lock @@ -1 +1 @@ -[]. +[{<<"cf">>,{pkg,<<"cf">>,<<"0.1.2">>},0}]. diff --git a/src/ec_cmd_log.erl b/src/ec_cmd_log.erl index 66ee40c..3cd75ae 100644 --- a/src/ec_cmd_log.erl +++ b/src/ec_cmd_log.erl @@ -24,6 +24,7 @@ -export([new/1, new/2, + new/3, log/4, should/2, debug/2, @@ -40,17 +41,18 @@ -include("ec_cmd_log.hrl"). --define(RED, 31). --define(GREEN, 32). --define(YELLOW, 33). --define(BLUE, 34). --define(MAGENTA, 35). --define(CYAN, 36). +-define(RED, $r). +-define(GREEN, $g). +-define(YELLOW, $y). +-define(BLUE, $b). +-define(MAGENTA, $m). +-define(CYAN, $c). -define(PREFIX, "===> "). -record(state_t, {log_level=0 :: int_log_level(), caller=api :: caller(), + intensity=low :: low | high, term_cap=full :: full | dumb }). %%============================================================================ @@ -71,9 +73,11 @@ -type atom_log_level() :: error | warn | info | debug. +-type intensity() :: low | high. + -type log_fun() :: fun(() -> iolist()). --type color() :: 31..36. +-type color() :: char(). -opaque t() :: #state_t{}. @@ -86,9 +90,17 @@ new(LogLevel) -> new(LogLevel, api). -spec new(log_level(), caller()) -> t(). -new(LogLevel, Caller) when LogLevel >= 0, LogLevel =< 3 -> - #state_t{log_level=LogLevel, caller=Caller, term_cap=query_term_env()}; -new(AtomLogLevel, Caller) +new(LogLevel, Caller) -> + new(LogLevel, Caller, high). + + +-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; AtomLogLevel =:= warn; AtomLogLevel =:= info; @@ -99,7 +111,8 @@ new(AtomLogLevel, Caller) info -> 2; debug -> 3 end, - new(LogLevel, Caller). + new(LogLevel, Caller, Intensity). + %% @doc log at the debug level given the current log state with a string or %% function that returns a string @@ -218,10 +231,25 @@ format(Log) -> <<")">>]. -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])); -colorize(#state_t{caller=command_line, term_cap=dumb}, Color, _Bold, Msg) when is_integer(Color) -> - lists:flatten(io_lib:format("~s~s", [?PREFIX, Msg])); + +-define(VALID_COLOR(C), + C =:= $r orelse C =:= $g orelse C =:= $y orelse + 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) -> Msg. diff --git a/src/erlware_commons.app.src b/src/erlware_commons.app.src index c4ad6a7..899d930 100644 --- a/src/erlware_commons.app.src +++ b/src/erlware_commons.app.src @@ -1,6 +1,6 @@ {application,erlware_commons, [{description,"Additional standard library for Erlang"}, - {vsn,"0.16.0"}, + {vsn,"0.16.1"}, {modules,[]}, {registered,[]}, {applications,[kernel,stdlib]}, From 4aedc36937627557abd0f9deff88d38d74319ff5 Mon Sep 17 00:00:00 2001 From: "Heinz N. Gies" Date: Thu, 24 Sep 2015 19:58:24 +0200 Subject: [PATCH 2/2] Add rebar3 update to test run --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7d9265c..b06e34b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ otp_release: - R16B01 - R16B - R15B02 -script: "./rebar3 compile && ./rebar3 ct" +script: "./rebar3 update && ./rebar3 compile && ./rebar3 ct" branches: only: - master