Home > Erlang探索 > 从Megaco学如何写诊断代码

从Megaco学如何写诊断代码

April 1st, 2010

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: 从Megaco学如何写诊断代码

Megaco/H.248 is a protocol for control of elements in a physically decomposed multimedia gateway, enabling separation of call control from media conversion. A Media Gateway Controller (MGC) controls one or more Media Gateways (MG).

Megaco有非常清晰的日志和诊断供我们参考, 精细到几行代码一个诊断信息, 非常好的风格. 有了这些信息对系统的运行完全了如指掌.

有3种方式:
1. 大量的调试日志, 在DEBUG方式下, 会有大量的系统允许期间的轨迹和消息

%%%----------------------------------------------------------------------                                                                                                                         %%% Debug                                                                                                                                                                                                      
%%%----------------------------------------------------------------------  
-ifdef(megaco_debug).                                                                               
-define(d(F,A), io:format("~w: " ++ F ++ "~n",[?MODULE|A])). 
-else.
-define(d(F,A), ok).
-endif.

用起来很方便, 摘抄一段如下

?d("encode(~p) -> entry with"
"~n   PackageItem: ~p"
"~n   SubItem:     ~p", [Scope, PackageItem, SubItem]),

2. 异常日志, 用于记录程序允许中间产生的各种各样的异常, 便于事后调查.

%%%----------------------------------------------------------------------                                                                                                                         %%% Error/warning/info message macro(s)                                                                                                                                                                        
%%%----------------------------------------------------------------------                                                                                                                         -define(megaco_info(F, A),                                                                                                                                                                                     
        (catch error_logger:info_msg("[ ~w : ~w : ~p ] ~n" ++ F ++ "~n", [?APPLICATION, ?MODULE, self()|A]))).
                                                                                                                                                                                                               
-define(megaco_warning(F, A),                                                                                                                                                                                  
        (catch error_logger:warning_msg("[ ~w : ~w : ~p ] ~n" ++ F ++ "~n", [?APPLICATION, ?MODULE, self()|A]))).
                                                                                                                                                                                                               
-define(megaco_error(F, A),                                                                                                                                                                                    
        (catch error_logger:error_msg("[ ~w : ~w : ~p ] ~n" ++ F ++ "~n",[?APPLICATION, ?MODULE, self()|A]))). 

使用, 摘抄一段如下

warning_msg(F, A) ->                                                                                
   ?megaco_warning("Transaction sender: " ++ F, A). 
error_msg(F, A) ->                                       
   ?megaco_error("Transaction sender: " ++ F, A).

3. Event Trace机制
先进的trace能够让系统的消息交互,运行轨迹以可视的方式体现在用户面前.

%%%----------------------------------------------------------------------                                                                                                                         
%%% Event Trace                                                                                                                                                                                                
%%%----------------------------------------------------------------------                                                                                                                         -ifdef(megaco_trace_io).
-define(report(Level, C, Label, Contents),
        io:format("*** [~s] ~p ~p *** "
                  "~n   ~p[~p] " ++ Label ++
                  "~n   ~p"
                  "~n   ~p"
                  "~n",
                  [megaco:format_timestamp(now()),
                   self(), Level, ?MODULE, ?LINE, C, Contents])).
-else.
-define(report(Level, C, Label, Contents),
        megaco:report_event(Level, ?APPLICATION, Label,
                            [{line, ?MODULE, ?LINE}, C | Contents])).
-endif.

-define(report_important(C, Label, Contents), ?report(20, C, Label, Contents)).
-define(report_verbose(  C, Label, Contents), ?report(40, C, Label, Contents)).
-define(report_debug(    C, Label, Contents), ?report(60, C, Label, Contents)).
-define(report_trace(    C, Label, Contents), ?report(80, C, Label, Contents)).

使用, 摘抄一段如下

?report_trace(ReceiveHandle, "callback: syntax error", [ErrorDesc, Error]), 

实际效果图(点击放大):

总结: ET很强大, 而且是专门为Megaco开发的, 目的就是能够可视化看到Megaco系统组件见的消息流程交互.

Post Footer automatically generated by wp-posturl plugin for wordpress.

Categories: Erlang探索 Tags: , ,
  1. litaocheng
    April 1st, 2010 at 14:07 | #1

    真好哈哈。。至今没有用过ET。准备中。。。

  2. April 1st, 2010 at 14:52 | #2

    ET是专门为Megaco开发的 用来显示消息包的来回交互 这不是我们梦寐以求的东西吗

  3. zdx3578
    April 14th, 2010 at 09:47 | #3

    不错,向牛人学习

Comments are closed.