| 1 |
|
%% Copyright (c) 2023 Peter Morgan <peter.james.morgan@gmail.com> |
| 2 |
|
%% |
| 3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
|
%% you may not use this file except in compliance with the License. |
| 5 |
|
%% You may obtain a copy of the License at |
| 6 |
|
%% |
| 7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
%% |
| 9 |
|
%% Unless required by applicable law or agreed to in writing, software |
| 10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
|
%% See the License for the specific language governing permissions and |
| 13 |
|
%% limitations under the License. |
| 14 |
|
|
| 15 |
|
%% @doc Pretty print function closures when using scran internal logging. |
| 16 |
|
|
| 17 |
|
-module(scran_debug). |
| 18 |
|
|
| 19 |
|
|
| 20 |
|
-export([pp/1]). |
| 21 |
|
-include_lib("kernel/include/logger.hrl"). |
| 22 |
|
|
| 23 |
|
|
| 24 |
|
%% @doc Pretty print a function closure. |
| 25 |
|
|
| 26 |
|
-spec pp(function()) -> io_lib:chars() | binary(). |
| 27 |
|
|
| 28 |
|
pp(Function) -> |
| 29 |
:-( |
{module, M} = erlang:fun_info(Function, module), |
| 30 |
:-( |
{name, Encoded} = erlang:fun_info(Function, name), |
| 31 |
:-( |
case string:split(atom_to_list(Encoded), "/") of |
| 32 |
|
[[$- | F], _] -> |
| 33 |
:-( |
{env, Env} = erlang:fun_info(Function, env), |
| 34 |
:-( |
case erlang:fun_info(Function, arity) of |
| 35 |
|
{arity, 0} -> |
| 36 |
:-( |
iolist_to_binary([atom_to_list(M), ":", F, "()"]); |
| 37 |
|
|
| 38 |
|
{arity, _} -> |
| 39 |
:-( |
iolist_to_binary( |
| 40 |
|
[atom_to_list(M), |
| 41 |
|
":", |
| 42 |
|
F, |
| 43 |
|
"(", |
| 44 |
|
lists:join(", ", mapping(Env)), |
| 45 |
|
")"]) |
| 46 |
|
end; |
| 47 |
|
|
| 48 |
|
[_] -> |
| 49 |
:-( |
io_lib:format("~p", [Function]) |
| 50 |
|
end. |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
mapping(Args) -> |
| 54 |
:-( |
?LOG_DEBUG(#{args => Args}), |
| 55 |
:-( |
lists:map( |
| 56 |
|
fun |
| 57 |
|
(Arg) when is_function(Arg) -> |
| 58 |
:-( |
pp(Arg); |
| 59 |
|
|
| 60 |
|
(Arg) when is_tuple(Arg), element(1, Arg) == re_pattern -> |
| 61 |
:-( |
"re_pattern"; |
| 62 |
|
|
| 63 |
|
(Arg) when is_list(Arg) -> |
| 64 |
:-( |
case io_lib:printable_list(Arg) of |
| 65 |
|
false -> |
| 66 |
:-( |
["[", lists:join(", ", ?FUNCTION_NAME(Arg)), "]"]; |
| 67 |
|
true -> |
| 68 |
:-( |
io_lib:format("~p", [Arg]) |
| 69 |
|
end; |
| 70 |
|
|
| 71 |
|
(Otherwise) -> |
| 72 |
:-( |
io_lib:format("~p", [Otherwise]) |
| 73 |
|
end, |
| 74 |
|
Args). |