Skip to content

Commit

Permalink
Improve palabre output
Browse files Browse the repository at this point in the history
  • Loading branch information
ghivert committed Dec 15, 2024
1 parent 162e3f8 commit a4236bd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 42 deletions.
17 changes: 16 additions & 1 deletion apps/backend/src/backend.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import backend/workers
import envoy
import gleam/erlang/process
import gleam/function
import gleam/http
import gleam/int
import gleam/option.{Some}
import gleam/otp/supervisor
Expand All @@ -29,7 +30,7 @@ fn configure_logger() {
palabre.options()
|> palabre.json(False)
|> palabre.level(log_level)
|> palabre.color(True)
|> palabre.color(False)
|> palabre.configure
}

Expand All @@ -45,11 +46,25 @@ fn start_http_server(ctx) {
router.handle_request(_, ctx)
|> wisp_mist.handler(secret_key_base)
|> mist.new
|> mist.after_start(palabre_mist_start)
|> mist.bind("0.0.0.0")
|> mist.port(port)
|> mist.start_http_server
}

fn palabre_mist_start(
port: Int,
scheme: http.Scheme,
_ip_address: mist.IpAddress,
) -> Nil {
palabre.info()
|> palabre.int("port", port)
|> palabre.string("host", "0.0.0.0")
|> palabre.string("scheme", http.scheme_to_string(scheme))
|> palabre.message("Server started, listening")
|> palabre.dump
}

fn start_periodic_workers(ctx) {
use children <- supervisor.start()
use children <- function.tap(children)
Expand Down
28 changes: 8 additions & 20 deletions apps/backend/src/backend_ffi.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,14 @@ extract_tar(Binary, BaseName, Version, Slug) ->
ContentDest = <<PackagePath/binary, "/contents">>,
Content = <<PackagePath/binary, "/contents.tar.gz">>,
case erl_tar:extract({binary, Binary}, [{cwd, PackagePath}]) of
{error, _} ->
{error, nil};
{error, _} -> {error, nil};
_ ->
Url =
<<"https://hexdocs.pm/",
BaseName/binary,
"/",
Version/binary,
"/package-interface.json">>,
Url = <<"https://hexdocs.pm/", BaseName/binary, "/", Version/binary, "/package-interface.json">>,
erl_tar:extract(Content, [{cwd, ContentDest}, compressed]),
{PackageInterface, Result} =
case httpc:request(Url) of
{ok, {{_, 200, _}, _, HttpBody}} ->
save_file(ContentDest, HttpBody);
{ok, {200, HttpBody}} ->
save_file(ContentDest, HttpBody);
{ok, {{_, 200, _}, _, HttpBody}} -> save_file(ContentDest, HttpBody);
{ok, {200, HttpBody}} -> save_file(ContentDest, HttpBody);
{error, _} ->
BuildCmd = <<"cd ", ContentDest/binary, " && gleam docs build">>,
Res = os:cmd(binary_to_list(BuildCmd)),
Expand All @@ -54,18 +46,14 @@ remove_tar(Slug) ->

is_match(Version, Requirement) ->
case verl:is_match(Version, Requirement) of
{error, _} ->
{error, nil};
Bool ->
{ok, Bool}
{error, _} -> {error, nil};
Bool -> {ok, Bool}
end.

get_home() ->
case init:get_argument(home) of
{ok, Content} ->
{ok, unicode:characters_to_binary(Content)};
error ->
{error, nil}
{ok, Content} -> {ok, unicode:characters_to_binary(Content)};
error -> {error, nil}
end.

set_level(Level) ->
Expand Down
12 changes: 9 additions & 3 deletions packages/palabre/src/palabre.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gleam/float
import gleam/function
import gleam/http
import gleam/int
import gleam/list
Expand Down Expand Up @@ -113,7 +114,13 @@ pub fn float(log: Log, key: String, value: Float) {
}

pub fn dump(log_: Log) -> Nil {
let text = option.unwrap(log_.message, "")
let text =
log_.message
|> case utils.is_color() {
True -> option.map(_, fn(m) { "\u{1b}[1m" <> m <> "\u{1b}[0m" })
False -> function.identity
}
|> option.unwrap("")
case log_.level {
level.Emergency -> log(log_.level, log_.fields, text)
level.Alert -> log(log_.level, log_.fields, text)
Expand All @@ -130,8 +137,7 @@ pub fn dump(log_: Log) -> Nil {
fn log(level: level.Level, message: a, text: String) -> Nil

fn init(level: level.Level) {
[#("when", [utils.iso8601()]), #("id", [utils.uuid()])]
|> Log(level:, fields: _, message: None)
Log(level:, fields: [], message: None)
}

fn append_field(
Expand Down
48 changes: 30 additions & 18 deletions packages/palabre/src/palabre_ffi.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,42 @@ format(#{level := Level, msg := Msg, meta := _Meta}, #{json := Json, color := Co

format_level(Level, #{color := Color}) ->
case Level of
emergency when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31memergency\x1b[0m";
alert when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31malert\x1b[0m";
critical when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31mcritical\x1b[0m";
error when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31merror\x1b[0m";
warning when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;33mwarning\x1b[0m";
notice when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;32mnotice\x1b[0m";
emergency when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31memrg\x1b[0m";
alert when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31malrt\x1b[0m";
critical when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31mcrit\x1b[0m";
error when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31meror\x1b[0m";
warning when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;33mwarn\x1b[0m";
notice when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;32mntce\x1b[0m";
info when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;34minfo\x1b[0m";
debug when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;36mdebug\x1b[0m";
emergency -> "level=emergency";
alert -> "level=alert";
critical -> "level=critical";
error -> "level=error";
warning -> "level=warning";
notice -> "level=notice";
debug when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;36mdebg\x1b[0m";
emergency -> "level=emrg";
alert -> "level=alrt";
critical -> "level=crit";
error -> "level=eror";
warning -> "level=warn";
notice -> "level=ntce";
info -> "level=info";
debug -> "level=debug"
debug -> "level=debg"
end.

format_msg(Report0, #{json := Json}) ->
case Report0 of
{string, Msg} -> json_wrap([$\s, Msg]);
{string, Msg} ->
case is_json() of
true -> maps:put("id", uuid(), maps:put("when", format_iso8601(), json_wrap([$\s, Msg])));
false ->
Defaults = [{<<"when"/utf8>>, [format_iso8601()]}, {<<"id"/utf8>>, [uuid()]}],
Converted = palabre@internals@converter:format_fields(Defaults),
case is_color() of
false -> json_wrap([$\s, Converted, $\s, Msg]);
true -> json_wrap([$\s, Converted, $\s, "\x1b[1m", Msg, "\x1b[0m"])
end
end;
{report, [{palabre, Fields, Text}]} ->
Fields1 = [{<<"when"/utf8>>, [format_iso8601()]}, {<<"id"/utf8>>, [uuid()]} | Fields],
case Json of
false -> [$\s, palabre@internals@converter:format_fields(Fields), $\s, Text];
true -> palabre@internals@converter:format_json(Fields, Text)
false -> [$\s, palabre@internals@converter:format_fields(Fields1), $\s, Text];
true -> palabre@internals@converter:format_json(Fields1, Text)
end;
{report, Report1} when is_map(Report1) -> json_wrap(format_kv(maps:to_list(Report1)));
{report, Report1} when is_list(Report1) -> json_wrap(format_kv(Report1));
Expand Down Expand Up @@ -114,4 +125,5 @@ is_json() ->
persistent_term:get(json, false).

is_color() ->
persistent_term:get(color, false).
not (persistent_term:get(json, false))
andalso persistent_term:get(color, false).

0 comments on commit a4236bd

Please sign in to comment.