libstrophe库中的xmpp_debug

时间:2016-12-09 05:30:14

标签: debugging xmpp

如何在libstrophe库中启用xmpp_debug? 似乎没有单独的变量来启用和禁用调试选项,但即使在函数入口处添加xmpp_debug,我也看不到跟踪

1 个答案:

答案 0 :(得分:0)

简而言之,您的任务需要适当的记录器:

int main()
{
    xmpp_log_t *log;
    xmpp_ctx_t *ctx;

    ...
    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
    ctx = xmpp_ctx_new(NULL, log);
    ...
}

使用此记录器,每个调试(和更高级别)消息都将打印到stderr。

此外,libstrophe允许自己创建记录器:

static void my_logger(void                  *userdata,
                      const xmpp_log_level_t level,
                      const char * const     area,
                      const char * const     msg)
{
    static const char *levels[] = {
        [XMPP_LEVEL_DEBUG] = "DEBUG",
        [XMPP_LEVEL_INFO]  = "INFO",
        [XMPP_LEVEL_WARN]  = "WARN",
        [XMPP_LEVEL_ERROR] = "ERROR",
    };

    (void)userdata;
    /* This example simply prints the message to stdout. */
    printf("[%s] %s: %s\n", levels[level], area, msg);
}

int main()
{
    xmpp_log_t log = { .handler = &my_logger };
    xmpp_ctx_t *ctx;

    ...
    ctx = xmpp_ctx_new(NULL, &log);
    ...
}