Просмотр исходного кода

Merge pull request #14072 from thalesmg/20241024-r58-printable-characters-unicode

feat: set printable characters range to unicode
Thales Macedo Garitezi 1 год назад
Родитель
Сommit
8cb0373bfc

+ 3 - 0
apps/emqx/etc/vm.args.cloud

@@ -128,3 +128,6 @@
 
 ## Disable os_mon's disksup by default
 -os_mon start_disksup false
+
+## Sets unicode as the printable character range when formatting binaries
++pc unicode

+ 1 - 1
apps/emqx_utils/src/emqx_utils.erl

@@ -569,7 +569,7 @@ tcp_keepalive_opts(OS, _Idle, _Interval, _Probes) ->
     {error, {unsupported_os, OS}}.
 
 format(Term) ->
-    unicode:characters_to_binary(io_lib:format("~0p", [Term])).
+    unicode:characters_to_binary(io_lib:format("~0tp", [Term])).
 
 format(Fmt, Args) ->
     unicode:characters_to_binary(io_lib:format(Fmt, Args)).

+ 27 - 0
apps/emqx_utils/test/emqx_utils_tests.erl

@@ -50,3 +50,30 @@ foldl_while_test_() ->
             )
         )
     ].
+
+to_binary_representation(Bin) when is_binary(Bin) ->
+    Bytes = binary_to_list(Bin),
+    iolist_to_binary([
+        [$<, $<],
+        lists:join($,, [
+            integer_to_binary(B)
+         || B <- Bytes
+        ]),
+        [$>, $>]
+    ]).
+
+readable_error_msg_test_() ->
+    [
+        {"binary in nested structure with non-latin1 characters",
+            ?_assert(begin
+                %% An unexpected error that could occur and be returned via HTTP API.
+                Text = <<"test中文"/utf8>>,
+                Error = {badmatch, #{description => Text}},
+                %% Output shouldn't contain the "exploded" bytes
+                Exploded = to_binary_representation(Text),
+                Formatted = emqx_utils:readable_error_msg(Error),
+                %% Precondition: `+pc unicode' must be set on the VM.
+                ?assertEqual(unicode, io:printable_range()),
+                nomatch =:= re:run(Formatted, Exploded, [{capture, all, binary}])
+            end)}
+    ].

+ 1 - 0
changes/ce/feat-14072.en.md

@@ -0,0 +1 @@
+Changed the virtual machine printable range to unicode.  This means that some binaries that previously had their byte representation printed in message will now be better formatted.  For example, a binary that was once formatted as `<<116,101,115,116,228,184,173,230,150,135>>` will now be formatted as `<<"test中文"/utf8>>`.

+ 1 - 1
scripts/ensure-rebar3.sh

@@ -10,7 +10,7 @@ case ${OTP_VSN} in
         VERSION="3.19.0-emqx-9"
         ;;
     26*)
-        VERSION="3.20.0-emqx-1"
+        VERSION="3.20.0-emqx-5"
         ;;
     *)
         echo "Unsupported Erlang/OTP version $OTP_VSN"