erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 line
6.8 KiB

5 年之前
  1. %% 常用atom定为macro
  2. -define(TRUE, true).
  3. -define(FALSE, false).
  4. -define(BREAK, break).
  5. -define(BREAK(Value), {?BREAK, Value}).
  6. -define(CONTINUE, continue).
  7. -define(CONTINUE(Value), {?CONTINUE, Value}).
  8. -define(UNDEFINED, undefined).
  9. %% 三目元算符
  10. -define(ITE(Cond, Then, That), case Cond of true -> Then; _ -> That end).
  11. %%汉字unicode编码范围 0x4e00 - 0x9fa5
  12. -define(UNICODE_CHINESE_BEGIN, (4 * 16 * 16 * 16 + 14 * 16 * 16)).
  13. -define(UNICODE_CHINESE_END, (9 * 16 * 16 * 16 + 15 * 16 * 16 + 10 * 16 + 5)).
  14. -compile({parse_transform, module_strengthen}).
  15. -define(ifdo(__CONDS, __DO), ?func(case __CONDS of ?true -> __DO;_ -> false end)()).
  16. -define(ifdo_else(__CONDS, __DO, _ELSE), ?func(case __CONDS of ?true -> __DO;_ -> _ELSE end)()).
  17. -define(inline(__FUN, __ARGCOUNT), -compile({inline, [__FUN / __ARGCOUNT]})).
  18. -define(format(__FORMAT, __DATA), io_lib:format(__FORMAT, __DATA)).
  19. %% format_record(record名, record数据) -> [{#record.field, record_field_value}]
  20. -define(format_record(__RECORD_NAME, __RECORD_DATA),
  21. fun() ->
  22. __Fields = record_info(fields, __RECORD_NAME),
  23. [_ | __DATA] = tuple_to_list(__RECORD_DATA),
  24. {__RECORD_NAME, lists:zip(__Fields, __DATA)}
  25. end()).
  26. -define(block(__BLOCK), begin __BLOCK end).
  27. -define(CONFIG(__KEY), ?CONFIG(__KEY, undefined)).
  28. -define(CONFIG(__KEY, __DEF), (fun() ->
  29. case application:get_env(__KEY) of
  30. {ok, Val} -> Val;
  31. _ -> __DEF
  32. end
  33. end)()).
  34. -define(CONFIG(__KEY, __SUBKEY, __DEF), (fun() ->
  35. case application:get_env(__KEY) of
  36. {ok, Val} when is_list(Val) ->
  37. proplists:get_value(__SUBKEY, Val, __DEF);
  38. _ -> __DEF
  39. end
  40. end)()).
  41. -define(LOG_DEBUG(__DATA), ?LOG_DEBUG("~p", [__DATA])).
  42. -define(LOG_DEBUG(__FMT, __DATA), io:format("[DEBUG] S(~p) M(~p) L(~p)~n" ++ __FMT ++ "~n", [self(), ?MODULE, ?LINE] ++ __DATA)).
  43. -define(LOG_ERROR(__DATA), ?LOG_ERROR("~p", [__DATA])).
  44. -define(LOG_ERROR(__FMT, __DATA), io:format("[ERROR] S(~p) M(~p) L(~p)~n" ++ __FMT ++ "~n", [self(), ?MODULE, ?LINE] ++ __DATA)).
  45. -define(LOG_INFO(__DATA), ?LOG_INFO("~p", [__DATA])).
  46. -define(LOG_INFO(__FMT, __DATA), io:format("[INFO] S(~p) M(~p) L(~p)~n" ++ __FMT ++ "~n", [com_type:to_list(self()),
  47. com_type:to_list(?MODULE),
  48. com_type:to_list(?LINE)] ++ __DATA)).
  49. -define(LAGER_DEBUG(__DATA), ?LAGER_DEBUG("~p", [__DATA])).
  50. -define(LAGER_DEBUG(__FORMAT, __DATA), ?LARGER_LOG(debug, __FORMAT, __DATA)).
  51. -define(LAGER_INFO(__DATA), ?LARGER_LOG(info, "~p", [__DATA])).
  52. -define(LAGER_INFO(__FORMAT, __DATA), ?LARGER_LOG(info, __FORMAT, __DATA)).
  53. -define(LAGER_WARNING(__DATA), ?LARGER_LOG(warning, "~p", [__DATA])).
  54. -define(LAGER_WARNING(__FORMAT, __DATA), ?LARGER_LOG(warning, __FORMAT, __DATA)).
  55. -define(LAGER_ERROR(__DATA), ?LARGER_LOG(error, "~p", [__DATA])).
  56. -define(LAGER_ERROR(__FORMAT, __DATA), ?LARGER_LOG(error, __FORMAT, __DATA)).
  57. -define(LARGER_LOG(__LEVEL, __FORMAT, __DATA), lager:log(__LEVEL, self(), "M(~p)L(~p)S(~p) ~n" ++ __FORMAT, [?MODULE, ?LINE, self()] ++ __DATA)).
  58. -define(debug_kv(__K_V_LIST), lists:foldl(fun({K, V}, Str) -> ?format("~s ~p(~p)", [Str, K, V]) end, "", __K_V_LIST)).
  59. -define(return(__RETURN), throw(__RETURN)).
  60. -define(exit(__EXIT), {'EXIT', __EXIT}).
  61. -define(exit(__BASE, __REASON), ?exit({__BASE, __REASON})).
  62. -define(tmlm(__TAG, __MODULE, __LINE, __MSG), {__TAG, {__MODULE, __LINE}, __MSG}).
  63. -define(err(__ERR), ?err_match(?MODULE, ?LINE, __ERR)).
  64. -define(err_match(__ERR), ?err_match(_, _, __ERR)).
  65. -define(err_match(__MODULE, __LINE, __ERR), ?tmlm(err, __MODULE, __LINE, __ERR)).
  66. -define(ok(__OK), ?ok_match(?MODULE, ?LINE, __OK)).
  67. -define(ok_match(__OK), ?ok_match(_, _, __OK)).
  68. -define(ok_match(__MODULE, __LINE, __OK), ?tmlm(ok, __MODULE, __LINE, __OK)).
  69. -define(notknow(__NOTKNOW), ?notknow_match(?MODULE, ?LINE, __NOTKNOW)).
  70. -define(notknow_match(__NOTKNOW), ?notknow_match(_, _, __NOTKNOW)).
  71. -define(notknow_match(__MODULE, __LINE, __NOTKNOW), ?tmlm(notknow, __MODULE, __LINE, __NOTKNOW)).
  72. -define(pack_sendbinary(__Binary), {?MODULE, ?LINE, __Binary}).
  73. -define(noreply(__State), {noreply, __State}).
  74. -define(stop(__Reason, __NewState), {stop, __Reason, __NewState}).
  75. -define(self, self()).
  76. -define(msg(__MSG), {msg, ?MODULE, ?LINE, __MSG}).
  77. -define(send_msg(__MSG), ?send_msg(?self, __MSG)).
  78. -define(send_msg(__SENDER, __MSG), (__SENDER ! __MSG)).
  79. -define(func(__Fun), fun() -> __Fun end).
  80. -define(get_value(__KEY, __LIST), ?get_value(__KEY, __LIST, ?undefined)).
  81. -define(get_value(__KEY, __LIST, __DEFAULT), common:get_value(__KEY, __LIST, __DEFAULT)).
  82. %% base
  83. -define(amc(__MFA, __Options, Module, Line), fun() ->
  84. case catch __MFA of
  85. ?exit(Way, _) ->
  86. ErrMsg = ?get_value(Way, __Options, {Way, undefined}),
  87. Debug = ?get_value(debug, __Options, not_defined),
  88. ?LAGER_ERROR("M(~p)L(~p) D(~p) ErrMsg(~p) stacktrace(~p)", [Module, Line, Debug, ErrMsg, erlang:get_stacktrace()]),
  89. error({undef, __MFA});
  90. Ret -> Ret
  91. end
  92. end()).
  93. %% api
  94. -define(amc(__MFA), ?amc(__MFA, [], ?MODULE, ?LINE)).
  95. %% api
  96. -define(amc(__MFA, __Options), ?amc(__MFA, __Options, ?MODULE, ?LINE)).
  97. -define(catch_exp(__EXP), fun() -> catch __EXP end()).
  98. -define(SYSTE_PROCESS(__TRUE_OR_FLASE), erlang:process_flag(trap_exit, __TRUE_OR_FLASE)).
  99. -record(boot, {module :: atom(), type :: boot_type(), hasname = true :: boolean(), params :: any()}).
  100. -define(boot_type_supervisor, supervisor).
  101. -define(boot_type_worker, worker).
  102. -define(boot_type_simple_worker, simple_worker).
  103. -type boot_type() :: ?boot_type_simple_worker | ?boot_type_supervisor | ?boot_type_worker.
  104. -define(MATH_INT32_MAX, 4294967296).
  105. -define(cast_int(__Val), com_type:to_integer(__Val)).
  106. -define(cast_str(__Val), com_type:to_list(__Val)).
  107. -define(cast_erl(__Val), com_type:to_list(__Val)).
  108. -define(record_key_val_get(__RECORDNAME, __RECORDDATA, __KEY), (fun() ->
  109. case erlang:is_integer(__KEY) of
  110. true ->
  111. erlang:element(__KEY, __RECORDDATA);
  112. false ->
  113. Fields = record_info(fields, __RECORDNAME),
  114. case com_lists:position(__KEY, Fields) of
  115. {error, Error} ->
  116. {error, Error};
  117. Position ->
  118. erlang:element(Position + 1, __RECORDDATA)
  119. end
  120. end
  121. end)()).
  122. -define(record_key_val_set(__RECORDNAME, __RECORDDATA, __KEY, __VAL), (fun() ->
  123. case erlang:is_integer(__KEY) of
  124. true ->
  125. erlang:setelement(__KEY, __RECORDDATA, __VAL);
  126. false ->
  127. Fields = record_info(fields, __RECORDNAME),
  128. case com_lists:position(__KEY, Fields) of
  129. {error, Error} ->
  130. {error, Error};
  131. Position ->
  132. erlang:setelement(Position + 1, __RECORDDATA, __VAL)
  133. end
  134. end
  135. end)()).