-
Notifications
You must be signed in to change notification settings - Fork 0
Change logging API from push to pull #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e1ceffa
Change logging API from push to pull
sylane d35c4f7
Fix doc grammatical error
sylane fe1d2c0
Upgrade to grisp_cryptoauth 2.4.1
ziopio 8328bf4
Update jarl version and add flags in grisp.ini
ziopio 1bd3ae3
Prevent crash if grisp_connect log handler is not setup properly
sylane bbe9b19
Update log configuration in README
sylane 94bbb0a
Upgrade jarl
ziopio 03f811d
Cleanup changelog
ziopio 3af007c
Fix yet another grammar error in the doc
sylane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
%% @doc Utility module to handle logs. | ||
%% @end | ||
-module(grisp_connect_log). | ||
|
||
-include_lib("kernel/include/logger.hrl"). | ||
|
||
% API functions | ||
-export([get/1]). | ||
-export([sync/1]). | ||
|
||
%--- Macros -------------------------------------------------------------------- | ||
|
||
% FixMe: | ||
% Sending over ~30_000 bytes over WS breaks rtems I/O driver. | ||
% We want avoid to return chunks that are bigger then that. | ||
-define(MAX_CHUNK_BYTES, 30_000). | ||
|
||
%--- Types --------------------------------------------------------------------- | ||
|
||
-type get_options() :: #{ | ||
max_batch_size => non_neg_integer(), | ||
max_byte_size => non_neg_integer() | ||
}. | ||
|
||
-type sync_options() :: #{ | ||
seq := non_neg_integer(), | ||
dropped => non_neg_integer() | ||
}. | ||
|
||
|
||
%--- API Functions ------------------------------------------------------------- | ||
|
||
-spec get(Opts :: get_options()) -> | ||
#{dropped := non_neg_integer(), events := list()}. | ||
get(Opts) -> | ||
{ok, DefaultSize} = application:get_env(grisp_connect, logs_batch_size), | ||
BatchSize = maps:get(max_batch_size, Opts, DefaultSize), | ||
ByteSize = min(maps:get(max_byte_size, Opts, ?MAX_CHUNK_BYTES), ?MAX_CHUNK_BYTES), | ||
{Events, Dropped} = grisp_connect_logger_bin:chunk(BatchSize, ByteSize), | ||
#{events => [[Seq, jsonify(E)] || {Seq, E} <- Events], | ||
dropped => Dropped}. | ||
|
||
-spec sync(Opts :: sync_options()) -> ok. | ||
sync(#{seq := Seq, dropped := Dropped}) -> | ||
grisp_connect_logger_bin:sync(Seq, Dropped). | ||
|
||
|
||
%--- Internal Functions -------------------------------------------------------- | ||
|
||
jsonify(Event) -> | ||
jsonify_meta(jsonify_msg(binary_to_term(base64:decode(Event)))). | ||
|
||
jsonify_msg(#{msg := {string, String}} = Event) -> | ||
maps:put(msg, unicode:characters_to_binary(String), Event); | ||
jsonify_msg(#{msg := {report, Report}} = Event) -> | ||
case is_json_compatible(Report) of | ||
true -> | ||
maps:put(msg, Report, Event); | ||
false -> | ||
String = unicode:characters_to_binary( | ||
io_lib:format("[JSON incompatible term]~n~tp", [Report]) | ||
), | ||
maps:put(msg, String, Event) | ||
end; | ||
jsonify_msg(#{msg := {FormatString, Term}} = Event) -> | ||
%FIXME: scan format and ensure unicode encoding | ||
String = unicode:characters_to_binary(io_lib:format(FormatString, Term)), | ||
maps:put(msg, String, Event). | ||
|
||
jsonify_meta(#{meta := Meta} = Event) -> | ||
MFA = case maps:is_key(mfa, Meta) of | ||
true -> | ||
{M, F, A} = maps:get(mfa, Meta), | ||
[M, F, A]; | ||
false -> | ||
null | ||
end, | ||
File = case maps:is_key(file, Meta) of | ||
true -> unicode:characters_to_binary(maps:get(file, Meta)); | ||
false -> null | ||
end, | ||
Default = #{mfa => MFA, file => File}, | ||
Optional = maps:without(maps:keys(Default), Meta), | ||
FilterFun = fun(Key, Value) -> jsx:is_term(#{Key => Value}) end, | ||
maps:put(meta, maps:merge(maps:filter(FilterFun, Optional), Default), Event). | ||
|
||
is_json_compatible(Term) -> | ||
try jsx:is_term(Term) | ||
catch error:_ -> | ||
false | ||
end. |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.