Packages

A terminal styling and logging framework based off the Golang Charm Lipgloss framework.

Current section

Files

Jump to
the_stars src log.erl
Raw

src/log.erl

-module(log).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new_logger/0, debug/2, info/2, fatal/2, warn/2, error/2]).
-export_type([logger/0]).
-opaque logger() :: {logger,
integer(),
boolean(),
styles:style(),
styles:style(),
styles:style(),
styles:style(),
styles:style()}.
-spec new_logger() -> logger().
new_logger() ->
Style = begin
_pipe = styles:new_style(),
styles:bold(_pipe, true)
end,
Debug_style = begin
_pipe@1 = Style,
_pipe@2 = styles:foreground(_pipe@1, {color24, <<"#FFFFFF"/utf8>>}),
styles:background(_pipe@2, {color24, <<"#414868"/utf8>>})
end,
Info_style = begin
_pipe@3 = Style,
_pipe@4 = styles:foreground(_pipe@3, {color24, <<"#FFFFFF"/utf8>>}),
styles:background(_pipe@4, {color24, <<"#485E30"/utf8>>})
end,
Warn_style = begin
_pipe@5 = Style,
_pipe@6 = styles:foreground(_pipe@5, {color24, <<"#000000"/utf8>>}),
styles:background(_pipe@6, {color24, <<"#FF9E64"/utf8>>})
end,
Error_style = begin
_pipe@7 = Style,
_pipe@8 = styles:foreground(_pipe@7, {color24, <<"#000000"/utf8>>}),
styles:background(_pipe@8, {color24, <<"#F7768E"/utf8>>})
end,
Fatal_style = begin
_pipe@9 = Style,
_pipe@10 = styles:foreground(_pipe@9, {color24, <<"#000000"/utf8>>}),
styles:background(_pipe@10, {color24, <<"#BB9AF7"/utf8>>})
end,
{logger,
0,
false,
Debug_style,
Info_style,
Warn_style,
Error_style,
Fatal_style}.
-spec pretty_datetime(logger()) -> binary().
pretty_datetime(Logger) ->
case erlang:element(3, Logger) of
true ->
Datetime = birl:now(),
Date = birl:get_day(Datetime),
Time = birl:get_time_of_day(Datetime),
Pad2 = fun(X) ->
gleam@string:pad_left(gleam@int:to_string(X), 2, <<"0"/utf8>>)
end,
Pad4 = fun(X@1) ->
gleam@string:pad_left(gleam@int:to_string(X@1), 4, <<"0"/utf8>>)
end,
<<<<<<<<<<<<<<<<<<<<<<"["/utf8,
(Pad2(
erlang:element(
4,
Date
)
))/binary>>/binary,
"-"/utf8>>/binary,
(Pad2(erlang:element(3, Date)))/binary>>/binary,
"-"/utf8>>/binary,
(Pad4(erlang:element(2, Date)))/binary>>/binary,
"] "/utf8>>/binary,
(Pad2(erlang:element(2, Time)))/binary>>/binary,
":"/utf8>>/binary,
(Pad2(erlang:element(3, Time)))/binary>>/binary,
":"/utf8>>/binary,
(Pad2(erlang:element(4, Time)))/binary>>;
false ->
<<""/utf8>>
end.
-spec debug(logger(), HJN) -> HJN.
debug(Logger, Value) ->
gleam@io:print_error(
<<<<<<(pretty_datetime(Logger))/binary, " "/utf8>>/binary,
(styles:render(erlang:element(4, Logger), <<" DEBUG: "/utf8>>))/binary>>/binary,
" "/utf8>>
),
gleam@io:debug(Value).
-spec info(logger(), binary()) -> binary().
info(Logger, Value) ->
case erlang:element(2, Logger) =< 1 of
true ->
gleam@io:println(
<<<<<<<<(pretty_datetime(Logger))/binary, " "/utf8>>/binary,
(styles:render(
erlang:element(5, Logger),
<<" INFO: "/utf8>>
))/binary>>/binary,
" "/utf8>>/binary,
Value/binary>>
);
_ ->
nil
end,
Value.
-spec fatal(logger(), binary()) -> binary().
fatal(Logger, Value) ->
case erlang:element(2, Logger) =< 1 of
true ->
gleam@io:println_error(
<<<<<<<<(pretty_datetime(Logger))/binary, " "/utf8>>/binary,
(styles:render(
erlang:element(8, Logger),
<<" FATAL: "/utf8>>
))/binary>>/binary,
" "/utf8>>/binary,
Value/binary>>
);
_ ->
nil
end,
Value.
-spec warn(logger(), binary()) -> binary().
warn(Logger, Value) ->
case erlang:element(2, Logger) =< 2 of
true ->
gleam@io:println_error(
<<<<<<<<(pretty_datetime(Logger))/binary, " "/utf8>>/binary,
(styles:render(
erlang:element(6, Logger),
<<" WARN: "/utf8>>
))/binary>>/binary,
" "/utf8>>/binary,
Value/binary>>
);
_ ->
nil
end,
Value.
-spec error(logger(), binary()) -> binary().
error(Logger, Value) ->
case erlang:element(2, Logger) =< 3 of
true ->
gleam@io:println_error(
<<<<<<<<(pretty_datetime(Logger))/binary, " "/utf8>>/binary,
(styles:render(
erlang:element(7, Logger),
<<" ERROR: "/utf8>>
))/binary>>/binary,
" "/utf8>>/binary,
Value/binary>>
);
_ ->
nil
end,
Value.