0

我刚刚从离开公司的人那里继承了一个图书馆。它是用 C++ 编写的,并且在整个代码中都使用了 g_print()。我知道库正在运行,但我看不到任何调试输出。我需要做些什么才能让它出现吗?g_print() 是否仅适用于调试版本?还有其他建议吗?

4

1 回答 1

1

我最终使用了 g_log(如上面David Schwartz所建议的那样),然后按照C - gtk Logging overriding 中的指定覆盖它:

void log_handler(const gchar *log_domain,
                 GLogLevelFlags log_level,
                 const gchar *message,
                 gpointer user_data)
{
    FILE *logfile = fopen ("/tmp/debug.log", "a");
    if (logfile == NULL)
    {
        /*  Fall  back  to  console  output  if  unable  to  open  file  */
        printf ("Rerouted to console: %s", message);
        return;
    }

    fprintf (logfile, "%s", message);
    fclose (logfile);
}

uint handlerid = g_log_set_handler(NULL,
    G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
    log_handler,
    NULL);

if (!g_main_loop_is_running())
{
    g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "g_main_loop_is_running() returned 0\n");
}

if (handlerid != 0)
{
    g_log_remove_handler(NULL, handlerid);
    handlerid = 0;
}
于 2012-12-06T01:17:41.443 回答