/lib/runtime_tools/src/ttb_autostart.erl

https://github.com/blackberry/Erlang-OTP · Erlang · 55 lines · 33 code · 10 blank · 12 comment · 0 complexity · 4aa5f372cfcd1a2c01366730a67b18e3 MD5 · raw file

  1. %%%-------------------------------------------------------------------
  2. %%% File : ttb_autostart.erl
  3. %%% Author : Bartłomiej Puzoń <bartlomiej.puzon@erlang-solutions.com>
  4. %%% Description : This supervisor is used to resume ttb tracing
  5. %%% Users are able to provide custom restart modules for *_config, as
  6. %%% file:write/read/delete may not be possible on diskless nodes.
  7. %%%
  8. %%% Created : 31 Jul 2010 by <bartlomiej.puzon@erlang-solutions.com>
  9. %%%-------------------------------------------------------------------
  10. -module(ttb_autostart).
  11. -behaviour(gen_server).
  12. %% API
  13. -export([start_link/0,
  14. read_config/0,
  15. write_config/1,
  16. delete_config/0]).
  17. %% gen_server callbacks
  18. -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
  19. terminate/2, code_change/3]).
  20. -define(DEF_AUTOSTART_MODULE, ?MODULE).
  21. -define(AUTOSTART_FILENAME, "ttb_autostart.bin").
  22. start_link() ->
  23. gen_server:start_link(?MODULE, no_args, []).
  24. delete_config() ->
  25. file:delete(?AUTOSTART_FILENAME).
  26. read_config() ->
  27. case file:read_file(?AUTOSTART_FILENAME) of
  28. {ok, Data} -> {ok, binary_to_term(Data)};
  29. Error -> Error
  30. end.
  31. write_config(Data) ->
  32. file:write_file(?AUTOSTART_FILENAME, term_to_binary(Data)).
  33. init(no_args) ->
  34. case application:get_env(runtime_tools, ttb_autostart_module) of
  35. {ok, _} -> ok;
  36. undefined -> application:set_env(runtime_tools, ttb_autostart_module, ?DEF_AUTOSTART_MODULE)
  37. end,
  38. observer_backend:ttb_resume_trace(),
  39. %%As the process is not needed any more, it will shut itself down
  40. {ok, no_args, 10000}.
  41. handle_call(_,_,_) -> {noreply, no_args}.
  42. handle_cast(_,_) -> {noreply, no_args}.
  43. handle_info(timeout,_) -> {stop, normal, no_args}.
  44. terminate(_,_) -> ok.
  45. code_change(_,_,_) -> {ok, no_args}.