Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

754 Zeilen
37 KiB

vor 14 Jahren
vor 14 Jahren
vor 14 Jahren
vor 14 Jahren
vor 14 Jahren
vor 14 Jahren
vor 14 Jahren
vor 14 Jahren
vor 14 Jahren
vor 14 Jahren
vor 13 Jahren
vor 13 Jahren
vor 13 Jahren
vor 13 Jahren
vor 14 Jahren
vor 14 Jahren
vor 14 Jahren
vor 14 Jahren
vor 14 Jahren
vor 14 Jahren
vor 14 Jahren
vor 13 Jahren
vor 13 Jahren
vor 13 Jahren
vor 13 Jahren
  1. %% Copyright (c) 2011 Basho Technologies, Inc. All Rights Reserved.
  2. %%
  3. %% This file is provided to you under the Apache License,
  4. %% Version 2.0 (the "License"); you may not use this file
  5. %% except in compliance with the License. You may obtain
  6. %% a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing,
  11. %% software distributed under the License is distributed on an
  12. %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. %% KIND, either express or implied. See the License for the
  14. %% specific language governing permissions and limitations
  15. %% under the License.
  16. -module(lager_test_backend).
  17. -include("lager.hrl").
  18. -behaviour(gen_event).
  19. -export([init/1, handle_call/2, handle_event/2, handle_info/2, terminate/2,
  20. code_change/3]).
  21. -record(state, {level, buffer, ignored}).
  22. -compile([{parse_transform, lager_transform}]).
  23. -ifdef(TEST).
  24. -include_lib("eunit/include/eunit.hrl").
  25. -export([pop/0, count/0, count_ignored/0, flush/0]).
  26. -endif.
  27. init(Level) ->
  28. {ok, #state{level=lager_util:level_to_num(Level), buffer=[], ignored=[]}}.
  29. handle_call(count, #state{buffer=Buffer} = State) ->
  30. {ok, length(Buffer), State};
  31. handle_call(count_ignored, #state{ignored=Ignored} = State) ->
  32. {ok, length(Ignored), State};
  33. handle_call(flush, State) ->
  34. {ok, ok, State#state{buffer=[], ignored=[]}};
  35. handle_call(pop, #state{buffer=Buffer} = State) ->
  36. case Buffer of
  37. [] ->
  38. {ok, undefined, State};
  39. [H|T] ->
  40. {ok, H, State#state{buffer=T}}
  41. end;
  42. handle_call(get_loglevel, #state{level=Level} = State) ->
  43. {ok, Level, State};
  44. handle_call({set_loglevel, Level}, State) ->
  45. {ok, ok, State#state{level=lager_util:level_to_num(Level)}};
  46. handle_call(_Request, State) ->
  47. {ok, ok, State}.
  48. handle_event({log, [?MODULE], Level, Time, Message}, #state{buffer=Buffer} = State) ->
  49. {ok, State#state{buffer=Buffer ++ [{Level, Time, Message}]}};
  50. handle_event({log, Level, Time, Message}, #state{level=LogLevel,
  51. buffer=Buffer} = State) when Level =< LogLevel ->
  52. {ok, State#state{buffer=Buffer ++ [{Level, Time, Message}]}};
  53. handle_event({log, _Level, _Time, _Message}, #state{ignored=Ignored} = State) ->
  54. {ok, State#state{ignored=Ignored ++ [ignored]}};
  55. handle_event(_Event, State) ->
  56. {ok, State}.
  57. handle_info(_Info, State) ->
  58. {ok, State}.
  59. terminate(_Reason, _State) ->
  60. ok.
  61. code_change(_OldVsn, State, _Extra) ->
  62. {ok, State}.
  63. -ifdef(TEST).
  64. pop() ->
  65. gen_event:call(lager_event, ?MODULE, pop).
  66. count() ->
  67. gen_event:call(lager_event, ?MODULE, count).
  68. count_ignored() ->
  69. gen_event:call(lager_event, ?MODULE, count_ignored).
  70. flush() ->
  71. gen_event:call(lager_event, ?MODULE, flush).
  72. has_line_numbers() ->
  73. %% are we R15 or greater
  74. Rel = erlang:system_info(otp_release),
  75. {match, [Major]} = re:run(Rel, "^R(\\d+)[A|B](|0(\\d))$", [{capture, [1], list}]),
  76. list_to_integer(Major) >= 15.
  77. not_running_test() ->
  78. ?assertEqual({error, lager_not_running}, lager:log(info, self(), "not running")).
  79. lager_test_() ->
  80. {foreach,
  81. fun setup/0,
  82. fun cleanup/1,
  83. [
  84. {"observe that there is nothing up my sleeve",
  85. fun() ->
  86. ?assertEqual(undefined, pop()),
  87. ?assertEqual(0, count())
  88. end
  89. },
  90. {"logging works",
  91. fun() ->
  92. lager:warning("test message"),
  93. ?assertEqual(1, count()),
  94. {Level, _Time, Message} = pop(),
  95. ?assertMatch(Level, lager_util:level_to_num(warning)),
  96. [LevelStr, _LocStr, MsgStr] = re:split(Message, " ", [{return, list}, {parts, 3}]),
  97. ?assertEqual("[warning]", LevelStr),
  98. ?assertEqual("test message", MsgStr),
  99. ok
  100. end
  101. },
  102. {"logging with arguments works",
  103. fun() ->
  104. lager:warning("test message ~p", [self()]),
  105. ?assertEqual(1, count()),
  106. {Level, _Time, Message} = pop(),
  107. ?assertMatch(Level, lager_util:level_to_num(warning)),
  108. [LevelStr, _LocStr, MsgStr] = re:split(Message, " ", [{return, list}, {parts, 3}]),
  109. ?assertEqual("[warning]", LevelStr),
  110. ?assertEqual(lists:flatten(io_lib:format("test message ~p", [self()])), MsgStr),
  111. ok
  112. end
  113. },
  114. {"logging works from inside a begin/end block",
  115. fun() ->
  116. ?assertEqual(0, count()),
  117. begin
  118. lager:warning("test message 2")
  119. end,
  120. ?assertEqual(1, count()),
  121. ok
  122. end
  123. },
  124. {"logging works from inside a list comprehension",
  125. fun() ->
  126. ?assertEqual(0, count()),
  127. [lager:warning("test message") || _N <- lists:seq(1, 10)],
  128. ?assertEqual(10, count()),
  129. ok
  130. end
  131. },
  132. {"logging works from a begin/end block inside a list comprehension",
  133. fun() ->
  134. ?assertEqual(0, count()),
  135. [ begin lager:warning("test message") end || _N <- lists:seq(1, 10)],
  136. ?assertEqual(10, count()),
  137. ok
  138. end
  139. },
  140. {"logging works from a nested list comprehension",
  141. fun() ->
  142. ?assertEqual(0, count()),
  143. [ [lager:warning("test message") || _N <- lists:seq(1, 10)] ||
  144. _I <- lists:seq(1, 10)],
  145. ?assertEqual(100, count()),
  146. ok
  147. end
  148. },
  149. {"log messages below the threshold are ignored",
  150. fun() ->
  151. ?assertEqual(0, count()),
  152. lager:debug("this message will be ignored"),
  153. ?assertEqual(0, count()),
  154. ?assertEqual(0, count_ignored()),
  155. lager_mochiglobal:put(loglevel, {?DEBUG, []}),
  156. lager:debug("this message should be ignored"),
  157. ?assertEqual(0, count()),
  158. ?assertEqual(1, count_ignored()),
  159. lager:set_loglevel(?MODULE, debug),
  160. lager:debug("this message should be logged"),
  161. ?assertEqual(1, count()),
  162. ?assertEqual(1, count_ignored()),
  163. ?assertEqual(debug, lager:get_loglevel(?MODULE)),
  164. ok
  165. end
  166. },
  167. {"tracing works",
  168. fun() ->
  169. lager_mochiglobal:put(loglevel, {?ERROR, []}),
  170. ok = lager:info("hello world"),
  171. ?assertEqual(0, count()),
  172. lager_mochiglobal:put(loglevel, {?ERROR, [{[{module,
  173. ?MODULE}], ?DEBUG, ?MODULE}]}),
  174. ok = lager:info("hello world"),
  175. ?assertEqual(1, count()),
  176. ok
  177. end
  178. },
  179. {"tracing works with custom attributes",
  180. fun() ->
  181. lager_mochiglobal:put(loglevel, {?ERROR, []}),
  182. lager:info([{requestid, 6}], "hello world"),
  183. ?assertEqual(0, count()),
  184. lager_mochiglobal:put(loglevel, {?ERROR,
  185. [{[{requestid, 6}], ?DEBUG, ?MODULE}]}),
  186. lager:info([{requestid, 6}, {foo, bar}], "hello world"),
  187. ?assertEqual(1, count()),
  188. lager_mochiglobal:put(loglevel, {?ERROR,
  189. [{[{requestid, '*'}], ?DEBUG, ?MODULE}]}),
  190. lager:info([{requestid, 6}], "hello world"),
  191. ?assertEqual(2, count()),
  192. ok
  193. end
  194. },
  195. {"tracing honors loglevel",
  196. fun() ->
  197. lager_mochiglobal:put(loglevel, {?ERROR, [{[{module,
  198. ?MODULE}], ?NOTICE, ?MODULE}]}),
  199. ok = lager:info("hello world"),
  200. ?assertEqual(0, count()),
  201. ok = lager:notice("hello world"),
  202. ?assertEqual(1, count()),
  203. ok
  204. end
  205. }
  206. ]
  207. }.
  208. setup() ->
  209. error_logger:tty(false),
  210. application:load(lager),
  211. application:set_env(lager, handlers, [{?MODULE, info}]),
  212. application:set_env(lager, error_logger_redirect, false),
  213. application:start(compiler),
  214. application:start(syntax_tools),
  215. application:start(lager),
  216. gen_event:call(lager_event, ?MODULE, flush).
  217. cleanup(_) ->
  218. application:stop(lager),
  219. error_logger:tty(true).
  220. crash(Type) ->
  221. spawn(fun() -> gen_server:call(crash, Type) end),
  222. timer:sleep(100),
  223. _ = gen_event:which_handlers(error_logger),
  224. ok.
  225. test_body(Expected, Actual, File) ->
  226. case has_line_numbers() of
  227. true ->
  228. FileLine = string:substr(Actual, length(Expected)+1),
  229. Body = string:substr(Actual, 1, length(Expected)),
  230. ?assertEqual(Expected, Body),
  231. ?assertEqual(File, string:substr(FileLine, 3, length(File)));
  232. false ->
  233. ?assertEqual(Expected, Actual)
  234. end.
  235. error_logger_redirect_crash_test_() ->
  236. {foreach,
  237. fun() ->
  238. error_logger:tty(false),
  239. application:load(lager),
  240. application:set_env(lager, error_logger_redirect, true),
  241. application:set_env(lager, handlers, [{?MODULE, error}]),
  242. application:start(compiler),
  243. application:start(syntax_tools),
  244. application:start(lager),
  245. crash:start()
  246. end,
  247. fun(_) ->
  248. application:stop(lager),
  249. case whereis(crash) of
  250. undefined -> ok;
  251. Pid -> exit(Pid, kill)
  252. end,
  253. error_logger:tty(true)
  254. end,
  255. [
  256. {"again, there is nothing up my sleeve",
  257. fun() ->
  258. ?assertEqual(undefined, pop()),
  259. ?assertEqual(0, count())
  260. end
  261. },
  262. {"bad return value",
  263. fun() ->
  264. Pid = whereis(crash),
  265. crash(bad_return),
  266. {_, _, Msg} = pop(),
  267. Expected = lists:flatten(io_lib:format("[error] ~w gen_server crash terminated with reason: bad return value: bleh", [Pid])),
  268. ?assertEqual(Expected, lists:flatten(Msg))
  269. %test_body(Expected, lists:flatten(msg), "test/crash.erl")
  270. end
  271. },
  272. {"bad return value with string",
  273. fun() ->
  274. Pid = whereis(crash),
  275. crash(bad_return_string),
  276. {_, _, Msg} = pop(),
  277. Expected = lists:flatten(io_lib:format("[error] ~w gen_server crash terminated with reason: bad return value: {tuple,{tuple,\"string\"}}", [Pid])),
  278. ?assertEqual(Expected, lists:flatten(Msg))
  279. end
  280. },
  281. {"case clause",
  282. fun() ->
  283. Pid = whereis(crash),
  284. crash(case_clause),
  285. {_, _, Msg} = pop(),
  286. Expected = lists:flatten(io_lib:format("[error] ~w gen_server crash terminated with reason: no case clause matching {} in crash:handle_call/3", [Pid])),
  287. test_body(Expected, lists:flatten(Msg), "test/crash.erl")
  288. end
  289. },
  290. {"case clause string",
  291. fun() ->
  292. Pid = whereis(crash),
  293. crash(case_clause_string),
  294. {_, _, Msg} = pop(),
  295. Expected = lists:flatten(io_lib:format("[error] ~w gen_server crash terminated with reason: no case clause matching \"crash\" in crash:handle_call/3", [Pid])),
  296. test_body(Expected, lists:flatten(Msg), "test/crash.erl")
  297. end
  298. },
  299. {"function clause",
  300. fun() ->
  301. Pid = whereis(crash),
  302. crash(function_clause),
  303. {_, _, Msg} = pop(),
  304. Expected = lists:flatten(io_lib:format("[error] ~w gen_server crash terminated with reason: no function clause matching crash:function({})", [Pid])),
  305. test_body(Expected, lists:flatten(Msg), "test/crash.erl")
  306. end
  307. },
  308. {"if clause",
  309. fun() ->
  310. Pid = whereis(crash),
  311. crash(if_clause),
  312. {_, _, Msg} = pop(),
  313. Expected = lists:flatten(io_lib:format("[error] ~w gen_server crash terminated with reason: no true branch found while evaluating if expression in crash:handle_call/3", [Pid])),
  314. test_body(Expected, lists:flatten(Msg), "test/crash.erl")
  315. end
  316. },
  317. {"try clause",
  318. fun() ->
  319. Pid = whereis(crash),
  320. crash(try_clause),
  321. {_, _, Msg} = pop(),
  322. Expected = lists:flatten(io_lib:format("[error] ~w gen_server crash terminated with reason: no try clause matching [] in crash:handle_call/3", [Pid])),
  323. test_body(Expected, lists:flatten(Msg), "test/crash.erl")
  324. end
  325. },
  326. {"undefined function",
  327. fun() ->
  328. Pid = whereis(crash),
  329. crash(undef),
  330. {_, _, Msg} = pop(),
  331. Expected = lists:flatten(io_lib:format("[error] ~w gen_server crash terminated with reason: call to undefined function crash:booger/0 from crash:handle_call/3", [Pid])),
  332. test_body(Expected, lists:flatten(Msg), "test/crash.erl")
  333. end
  334. },
  335. {"bad math",
  336. fun() ->
  337. Pid = whereis(crash),
  338. crash(badarith),
  339. {_, _, Msg} = pop(),
  340. Expected = lists:flatten(io_lib:format("[error] ~w gen_server crash terminated with reason: bad arithmetic expression in crash:handle_call/3", [Pid])),
  341. test_body(Expected, lists:flatten(Msg), "test/crash.erl")
  342. end
  343. },
  344. {"bad match",
  345. fun() ->
  346. Pid = whereis(crash),
  347. crash(badmatch),
  348. {_, _, Msg} = pop(),
  349. Expected = lists:flatten(io_lib:format("[error] ~w gen_server crash terminated with reason: no match of right hand value {} in crash:handle_call/3", [Pid])),
  350. test_body(Expected, lists:flatten(Msg), "test/crash.erl")
  351. end
  352. },
  353. {"bad arity",
  354. fun() ->
  355. Pid = whereis(crash),
  356. crash(badarity),
  357. {_, _, Msg} = pop(),
  358. Expected = lists:flatten(io_lib:format("[error] ~w gen_server crash terminated with reason: fun called with wrong arity of 1 instead of 3 in crash:handle_call/3", [Pid])),
  359. test_body(Expected, lists:flatten(Msg), "test/crash.erl")
  360. end
  361. },
  362. {"bad arg1",
  363. fun() ->
  364. Pid = whereis(crash),
  365. crash(badarg1),
  366. {_, _, Msg} = pop(),
  367. Expected = lists:flatten(io_lib:format("[error] ~w gen_server crash terminated with reason: bad argument in crash:handle_call/3", [Pid])),
  368. test_body(Expected, lists:flatten(Msg), "test/crash.erl")
  369. end
  370. },
  371. {"bad arg2",
  372. fun() ->
  373. Pid = whereis(crash),
  374. crash(badarg2),
  375. {_, _, Msg} = pop(),
  376. Expected = lists:flatten(io_lib:format("[error] ~w gen_server crash terminated with reason: bad argument in call to erlang:iolist_to_binary([\"foo\",bar]) in crash:handle_call/3", [Pid])),
  377. test_body(Expected, lists:flatten(Msg), "test/crash.erl")
  378. end
  379. },
  380. {"noproc",
  381. fun() ->
  382. Pid = whereis(crash),
  383. crash(noproc),
  384. {_, _, Msg} = pop(),
  385. Expected = lists:flatten(io_lib:format("[error] ~w gen_server crash terminated with reason: no such process or port in call to gen_event:call(foo, bar, baz)", [Pid])),
  386. ?assertEqual(Expected, lists:flatten(Msg))
  387. end
  388. },
  389. {"badfun",
  390. fun() ->
  391. Pid = whereis(crash),
  392. crash(badfun),
  393. {_, _, Msg} = pop(),
  394. Expected = lists:flatten(io_lib:format("[error] ~w gen_server crash terminated with reason: bad function booger in crash:handle_call/3", [Pid])),
  395. test_body(Expected, lists:flatten(Msg), "test/crash.erl")
  396. end
  397. }
  398. ]
  399. }.
  400. error_logger_redirect_test_() ->
  401. {foreach,
  402. fun() ->
  403. error_logger:tty(false),
  404. application:load(lager),
  405. application:set_env(lager, error_logger_redirect, true),
  406. application:set_env(lager, handlers, [{?MODULE, info}]),
  407. application:start(lager),
  408. lager:log(error, self(), "flush flush"),
  409. timer:sleep(100),
  410. gen_event:call(lager_event, ?MODULE, flush)
  411. end,
  412. fun(_) ->
  413. application:stop(lager),
  414. error_logger:tty(true)
  415. end,
  416. [
  417. {"error reports are printed",
  418. fun() ->
  419. sync_error_logger:error_report([{this, is}, a, {silly, format}]),
  420. _ = gen_event:which_handlers(error_logger),
  421. {_, _, Msg} = pop(),
  422. Expected = lists:flatten(io_lib:format("[error] ~w this: is, a, silly: format", [self()])),
  423. ?assertEqual(Expected, lists:flatten(Msg))
  424. end
  425. },
  426. {"string error reports are printed",
  427. fun() ->
  428. sync_error_logger:error_report("this is less silly"),
  429. _ = gen_event:which_handlers(error_logger),
  430. {_, _, Msg} = pop(),
  431. Expected = lists:flatten(io_lib:format("[error] ~w this is less silly", [self()])),
  432. ?assertEqual(Expected, lists:flatten(Msg))
  433. end
  434. },
  435. {"error messages are printed",
  436. fun() ->
  437. sync_error_logger:error_msg("doom, doom has come upon you all"),
  438. _ = gen_event:which_handlers(error_logger),
  439. {_, _, Msg} = pop(),
  440. Expected = lists:flatten(io_lib:format("[error] ~w doom, doom has come upon you all", [self()])),
  441. ?assertEqual(Expected, lists:flatten(Msg))
  442. end
  443. },
  444. {"error messages are truncated at 4096 characters",
  445. fun() ->
  446. sync_error_logger:error_msg("doom, doom has come upon you all ~p", [string:copies("doom", 10000)]),
  447. _ = gen_event:which_handlers(error_logger),
  448. {_, _, Msg} = pop(),
  449. ?assert(length(lists:flatten(Msg)) < 5100)
  450. end
  451. },
  452. {"info reports are printed",
  453. fun() ->
  454. sync_error_logger:info_report([{this, is}, a, {silly, format}]),
  455. _ = gen_event:which_handlers(error_logger),
  456. {_, _, Msg} = pop(),
  457. Expected = lists:flatten(io_lib:format("[info] ~w this: is, a, silly: format", [self()])),
  458. ?assertEqual(Expected, lists:flatten(Msg))
  459. end
  460. },
  461. {"info reports are truncated at 4096 characters",
  462. fun() ->
  463. sync_error_logger:info_report([[{this, is}, a, {silly, format}] || _ <- lists:seq(0, 600)]),
  464. _ = gen_event:which_handlers(error_logger),
  465. {_, _, Msg} = pop(),
  466. ?assert(length(lists:flatten(Msg)) < 5000)
  467. end
  468. },
  469. {"single term info reports are printed",
  470. fun() ->
  471. sync_error_logger:info_report({foolish, bees}),
  472. _ = gen_event:which_handlers(error_logger),
  473. {_, _, Msg} = pop(),
  474. Expected = lists:flatten(io_lib:format("[info] ~w {foolish,bees}", [self()])),
  475. ?assertEqual(Expected, lists:flatten(Msg))
  476. end
  477. },
  478. {"single term error reports are printed",
  479. fun() ->
  480. sync_error_logger:error_report({foolish, bees}),
  481. _ = gen_event:which_handlers(error_logger),
  482. {_, _, Msg} = pop(),
  483. Expected = lists:flatten(io_lib:format("[error] ~w {foolish,bees}", [self()])),
  484. ?assertEqual(Expected, lists:flatten(Msg))
  485. end
  486. },
  487. {"string info reports are printed",
  488. fun() ->
  489. sync_error_logger:info_report("this is less silly"),
  490. _ = gen_event:which_handlers(error_logger),
  491. {_, _, Msg} = pop(),
  492. Expected = lists:flatten(io_lib:format("[info] ~w this is less silly", [self()])),
  493. ?assertEqual(Expected, lists:flatten(Msg))
  494. end
  495. },
  496. {"string info reports are truncated at 4096 characters",
  497. fun() ->
  498. sync_error_logger:info_report(string:copies("this is less silly", 1000)),
  499. _ = gen_event:which_handlers(error_logger),
  500. {_, _, Msg} = pop(),
  501. ?assert(length(lists:flatten(Msg)) < 5100)
  502. end
  503. },
  504. {"strings in a mixed report are printed as strings",
  505. fun() ->
  506. sync_error_logger:info_report(["this is less silly", {than, "this"}]),
  507. _ = gen_event:which_handlers(error_logger),
  508. {_, _, Msg} = pop(),
  509. Expected = lists:flatten(io_lib:format("[info] ~w \"this is less silly\", than: \"this\"", [self()])),
  510. ?assertEqual(Expected, lists:flatten(Msg))
  511. end
  512. },
  513. {"info messages are printed",
  514. fun() ->
  515. sync_error_logger:info_msg("doom, doom has come upon you all"),
  516. _ = gen_event:which_handlers(error_logger),
  517. {_, _, Msg} = pop(),
  518. Expected = lists:flatten(io_lib:format("[info] ~w doom, doom has come upon you all", [self()])),
  519. ?assertEqual(Expected, lists:flatten(Msg))
  520. end
  521. },
  522. {"info messages are truncated at 4096 characters",
  523. fun() ->
  524. sync_error_logger:info_msg("doom, doom has come upon you all ~p", [string:copies("doom", 10000)]),
  525. _ = gen_event:which_handlers(error_logger),
  526. {_, _, Msg} = pop(),
  527. ?assert(length(lists:flatten(Msg)) < 5100)
  528. end
  529. },
  530. {"warning messages are printed at the correct level",
  531. fun() ->
  532. sync_error_logger:warning_msg("doom, doom has come upon you all"),
  533. Map = error_logger:warning_map(),
  534. _ = gen_event:which_handlers(error_logger),
  535. {_, _, Msg} = pop(),
  536. Expected = lists:flatten(io_lib:format("[~w] ~w doom, doom has come upon you all", [Map, self()])),
  537. ?assertEqual(Expected, lists:flatten(Msg))
  538. end
  539. },
  540. {"warning reports are printed at the correct level",
  541. fun() ->
  542. sync_error_logger:warning_report([{i, like}, pie]),
  543. Map = error_logger:warning_map(),
  544. _ = gen_event:which_handlers(error_logger),
  545. {_, _, Msg} = pop(),
  546. Expected = lists:flatten(io_lib:format("[~w] ~w i: like, pie", [Map, self()])),
  547. ?assertEqual(Expected, lists:flatten(Msg))
  548. end
  549. },
  550. {"single term warning reports are printed at the correct level",
  551. fun() ->
  552. sync_error_logger:warning_report({foolish, bees}),
  553. Map = error_logger:warning_map(),
  554. _ = gen_event:which_handlers(error_logger),
  555. {_, _, Msg} = pop(),
  556. Expected = lists:flatten(io_lib:format("[~w] ~w {foolish,bees}", [Map, self()])),
  557. ?assertEqual(Expected, lists:flatten(Msg))
  558. end
  559. },
  560. {"application stop reports",
  561. fun() ->
  562. sync_error_logger:info_report([{application, foo}, {exited, quittin_time}, {type, lazy}]),
  563. _ = gen_event:which_handlers(error_logger),
  564. {_, _, Msg} = pop(),
  565. Expected = lists:flatten(io_lib:format("[info] ~w Application foo exited with reason: quittin_time", [self()])),
  566. ?assertEqual(Expected, lists:flatten(Msg))
  567. end
  568. },
  569. {"supervisor reports",
  570. fun() ->
  571. sync_error_logger:error_report(supervisor_report, [{errorContext, france}, {offender, [{name, mini_steve}, {mfargs, {a, b, [c]}}, {pid, bleh}]}, {reason, fired}, {supervisor, {local, steve}}]),
  572. _ = gen_event:which_handlers(error_logger),
  573. {_, _, Msg} = pop(),
  574. Expected = lists:flatten(io_lib:format("[error] ~w Supervisor steve had child mini_steve started with a:b(c) at bleh exit with reason fired in context france", [self()])),
  575. ?assertEqual(Expected, lists:flatten(Msg))
  576. end
  577. },
  578. {"supervisor reports with real error",
  579. fun() ->
  580. sync_error_logger:error_report(supervisor_report, [{errorContext, france}, {offender, [{name, mini_steve}, {mfargs, {a, b, [c]}}, {pid, bleh}]}, {reason, {function_clause,[{crash,handle_info,[foo]}]}}, {supervisor, {local, steve}}]),
  581. _ = gen_event:which_handlers(error_logger),
  582. {_, _, Msg} = pop(),
  583. Expected = lists:flatten(io_lib:format("[error] ~w Supervisor steve had child mini_steve started with a:b(c) at bleh exit with reason no function clause matching crash:handle_info(foo) in context france", [self()])),
  584. ?assertEqual(Expected, lists:flatten(Msg))
  585. end
  586. },
  587. {"supervisor_bridge reports",
  588. fun() ->
  589. sync_error_logger:error_report(supervisor_report, [{errorContext, france}, {offender, [{mod, mini_steve}, {pid, bleh}]}, {reason, fired}, {supervisor, {local, steve}}]),
  590. _ = gen_event:which_handlers(error_logger),
  591. {_, _, Msg} = pop(),
  592. Expected = lists:flatten(io_lib:format("[error] ~w Supervisor steve had child at module mini_steve at bleh exit with reason fired in context france", [self()])),
  593. ?assertEqual(Expected, lists:flatten(Msg))
  594. end
  595. },
  596. {"application progress report",
  597. fun() ->
  598. sync_error_logger:info_report(progress, [{application, foo}, {started_at, node()}]),
  599. _ = gen_event:which_handlers(error_logger),
  600. {_, _, Msg} = pop(),
  601. Expected = lists:flatten(io_lib:format("[info] ~w Application foo started on node ~w", [self(), node()])),
  602. ?assertEqual(Expected, lists:flatten(Msg))
  603. end
  604. },
  605. {"supervisor progress report",
  606. fun() ->
  607. lager:set_loglevel(?MODULE, debug),
  608. sync_error_logger:info_report(progress, [{supervisor, {local, foo}}, {started, [{mfargs, {foo, bar, 1}}, {pid, baz}]}]),
  609. _ = gen_event:which_handlers(error_logger),
  610. {_, _, Msg} = pop(),
  611. Expected = lists:flatten(io_lib:format("[debug] ~w Supervisor foo started foo:bar/1 at pid baz", [self()])),
  612. ?assertEqual(Expected, lists:flatten(Msg))
  613. end
  614. },
  615. {"crash report for emfile",
  616. fun() ->
  617. sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, []}, {error_info, {error, {emfile, [{stack, trace, 1}]}, []}}], []]),
  618. _ = gen_event:which_handlers(error_logger),
  619. {_, _, Msg} = pop(),
  620. Expected = lists:flatten(io_lib:format("[error] ~w CRASH REPORT Process ~w with 0 neighbours crashed with reason: maximum number of file descriptors exhausted, check ulimit -n", [self(), self()])),
  621. ?assertEqual(Expected, lists:flatten(Msg))
  622. end
  623. },
  624. {"crash report for system process limit",
  625. fun() ->
  626. sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, []}, {error_info, {error, {system_limit, [{erlang, spawn, 1}]}, []}}], []]),
  627. _ = gen_event:which_handlers(error_logger),
  628. {_, _, Msg} = pop(),
  629. Expected = lists:flatten(io_lib:format("[error] ~w CRASH REPORT Process ~w with 0 neighbours crashed with reason: system limit: maximum number of processes exceeded", [self(), self()])),
  630. ?assertEqual(Expected, lists:flatten(Msg))
  631. end
  632. },
  633. {"crash report for system process limit2",
  634. fun() ->
  635. sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, []}, {error_info, {error, {system_limit, [{erlang, spawn_opt, 1}]}, []}}], []]),
  636. _ = gen_event:which_handlers(error_logger),
  637. {_, _, Msg} = pop(),
  638. Expected = lists:flatten(io_lib:format("[error] ~w CRASH REPORT Process ~w with 0 neighbours crashed with reason: system limit: maximum number of processes exceeded", [self(), self()])),
  639. ?assertEqual(Expected, lists:flatten(Msg))
  640. end
  641. },
  642. {"crash report for system port limit",
  643. fun() ->
  644. sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, []}, {error_info, {error, {system_limit, [{erlang, open_port, 1}]}, []}}], []]),
  645. _ = gen_event:which_handlers(error_logger),
  646. {_, _, Msg} = pop(),
  647. Expected = lists:flatten(io_lib:format("[error] ~w CRASH REPORT Process ~w with 0 neighbours crashed with reason: system limit: maximum number of ports exceeded", [self(), self()])),
  648. ?assertEqual(Expected, lists:flatten(Msg))
  649. end
  650. },
  651. {"crash report for system port limit",
  652. fun() ->
  653. sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, []}, {error_info, {error, {system_limit, [{erlang, list_to_atom, 1}]}, []}}], []]),
  654. _ = gen_event:which_handlers(error_logger),
  655. {_, _, Msg} = pop(),
  656. Expected = lists:flatten(io_lib:format("[error] ~w CRASH REPORT Process ~w with 0 neighbours crashed with reason: system limit: tried to create an atom larger than 255, or maximum atom count exceeded", [self(), self()])),
  657. ?assertEqual(Expected, lists:flatten(Msg))
  658. end
  659. },
  660. {"crash report for system ets table limit",
  661. fun() ->
  662. sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, test}, {error_info, {error, {system_limit,[{ets,new,[segment_offsets,[ordered_set,public]]},{mi_segment,open_write,1},{mi_buffer_converter,handle_cast,2},{gen_server,handle_msg,5},{proc_lib,init_p_do_apply,3}]}, []}}], []]),
  663. _ = gen_event:which_handlers(error_logger),
  664. {_, _, Msg} = pop(),
  665. Expected = lists:flatten(io_lib:format("[error] ~w CRASH REPORT Process ~w with 0 neighbours crashed with reason: system limit: maximum number of ETS tables exceeded", [self(), test])),
  666. ?assertEqual(Expected, lists:flatten(Msg))
  667. end
  668. },
  669. {"crash report for unknown system limit should be truncated at 500 characters",
  670. fun() ->
  671. sync_error_logger:error_report(crash_report, [[{pid, self()}, {error_info, {error, {system_limit,[{wtf,boom,[string:copies("aaaa", 4096)]}]}, []}}], []]),
  672. _ = gen_event:which_handlers(error_logger),
  673. {_, _, Msg} = pop(),
  674. ?assert(length(lists:flatten(Msg)) > 600),
  675. ?assert(length(lists:flatten(Msg)) < 650)
  676. end
  677. },
  678. {"crash reports for 'special processes' should be handled right",
  679. fun() ->
  680. {ok, Pid} = special_process:start(),
  681. unlink(Pid),
  682. Pid ! function_clause,
  683. timer:sleep(500),
  684. _ = gen_event:which_handlers(error_logger),
  685. {_, _, Msg} = pop(),
  686. Expected = lists:flatten(io_lib:format("[error] ~p CRASH REPORT Process ~p with 0 neighbours crashed with reason: no function clause matching special_process:foo(bar)",
  687. [Pid, Pid])),
  688. test_body(Expected, lists:flatten(Msg), "test/special_process.erl")
  689. end
  690. },
  691. {"messages should not be generated if they don't satisfy the threshold",
  692. fun() ->
  693. lager:set_loglevel(?MODULE, error),
  694. sync_error_logger:info_report([hello, world]),
  695. _ = gen_event:which_handlers(error_logger),
  696. ?assertEqual(0, count()),
  697. ?assertEqual(0, count_ignored()),
  698. lager:set_loglevel(?MODULE, info),
  699. sync_error_logger:info_report([hello, world]),
  700. _ = gen_event:which_handlers(error_logger),
  701. ?assertEqual(1, count()),
  702. ?assertEqual(0, count_ignored()),
  703. lager:set_loglevel(?MODULE, error),
  704. lager_mochiglobal:put(loglevel, {?DEBUG, []}),
  705. sync_error_logger:info_report([hello, world]),
  706. _ = gen_event:which_handlers(error_logger),
  707. ?assertEqual(1, count()),
  708. ?assertEqual(1, count_ignored())
  709. end
  710. }
  711. ]
  712. }.
  713. safe_format_test() ->
  714. ?assertEqual("foo bar", lists:flatten(lager:safe_format("~p ~p", [foo, bar], 1024))),
  715. ?assertEqual("FORMAT ERROR: \"~p ~p ~p\" [foo,bar]", lists:flatten(lager:safe_format("~p ~p ~p", [foo, bar], 1024))),
  716. ok.
  717. -endif.