anchor.diff:  	 ,monData			 %last system_monitor data
   	}).
   
  -rec_info(ld) ->
  -  record_info(fields,ld).
  -
   %% constants
   
   default_triggers() -> 
  
positive.code: %%%-------------------------------------------------------------------
  -module(watchdog).
  
  % API
  -export(
     [start/0,stop/0
      ,add_send_subscriber/4,add_log_subscriber/0,add_proc_subscriber/1
      ,clear_subscribers/0
      ,message/1]).
  
  % gen_serv callbacks
  -export(
     [handle_info/2
      ,init/1
      ,rec_info/1]).
  
  -include("log.hrl").
  
  
negative.code: %% api
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  start() -> 
    Init = fun(D) -> fun() -> init(D) end end,       % mmm.... haskell....
    {Pid,Ref} = erlang:spawn_monitor(Init(self())), 
    receive
      {Pid,ok} -> erlang:demonitor(Ref,[flush]),Pid;
      {'DOWN',Ref,_,Pid,OldPid} -> OldPid
    end.
  
  stop() -> 
    send_to_wd(stop).
  
  add_proc_subscriber(Pid) when is_pid(Pid) ->
    case is_process_alive(Pid) of
      true -> send_to_wd({add_subscriber,mk_send(Pid)});
      false-> {error,no_such_pid}
    end;
  add_proc_subscriber(Reg) when is_atom(Reg) ->
    send_to_wd({add_subscriber,mk_send(Reg)});
  add_proc_subscriber({Reg,Node}) when is_atom(Reg),is_atom(Node) ->
    send_to_wd({add_subscriber,mk_send({Reg,Node})}).
  
  %% E.g:  watchdog:add_send_subscriber(tcp,"localhost",56669,"I'm a Cookie").
  %% it's possible to add the same subscriber twice. this is a bug.
  add_send_subscriber(Proto,Host,Port,PassPhrase) ->
    case inet:gethostbyname(Host) of
      {ok,{hostent,Host,[],inet,4,_}} -> 
        send_to_wd({add_subscriber,mk_send(Proto,Host,Port,PassPhrase)});
      {ok,{hostent,G,[W],inet,4,_}} -> {error,{badhost,W,G}};
      {ok,R}                        -> {error,R};
      {error,R}                     -> {error,R}
    end.
  
  add_log_subscriber() ->
    try ?MODULE ! {add_subscriber,mk_log(group_leader())}, ok
    catch _:R -> {error,R}
    end.
  
  