如何使用Pantheios将日志记录到文件?

5
我尝试使用pantheios的示例将日志记录到文件中,但无法使其正常工作。 消息在控制台上正确显示,但日志文件未创建。 自从我看到了这个线程后,我尝试更改严重级别,但没有一个起作用。
以下是代码:
/* Pantheios Header Files */
#include <pantheios/pantheios.hpp>            // Pantheios C++ main header
#include <pantheios/inserters/args.hpp>       // for pantheios::args
#include <pantheios/inserters/exception.hpp>  // for pantheios::exception

#include <pantheios/backends/bec.file.h>      // be.file header

/* Standard C/C++ Header Files */
#include <exception>                          // for std::exception
#include <new>                                // for std::bad_alloc
#include <string>                             // for std::string
#include <stdlib.h>                           // for exit codes

/* ////////////////////////////////////////////////////////////////////// */

/* Define the stock front-end process identity, so that it links when using
* fe.N, fe.simple, etc. */
PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("example.cpp.file");

/* ////////////////////////////////////////////////////////////////////// */

#define PSTR(x)         PANTHEIOS_LITERAL_STRING(x)

/* ////////////////////////////////////////////////////////////////////// */

int main(int argc, char **argv)
{
    try
    {
#ifndef PANTHEIOS_USE_WIDE_STRINGS
        pantheios::log_DEBUG("main(", pantheios::args(argc, argv), ")");
#else /* ? !PANTHEIOS_USE_WIDE_STRINGS */
        STLSOFT_SUPPRESS_UNUSED(argc); STLSOFT_SUPPRESS_UNUSED(argv);
#endif /* !PANTHEIOS_USE_WIDE_STRINGS */

        pantheios::log_NOTICE(PSTR("stmt 1"));

        // Set the file name for the local back-end, truncating the
        // file's existing contents, if any.
        pantheios_be_file_setFilePath(PSTR("log.local"),     PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BEID_LOCAL);

        pantheios::log_NOTICE(PSTR("stmt 2"));

        // Set the file name for the remote back-end.
        pantheios_be_file_setFilePath(PSTR("log.remote"), PANTHEIOS_BEID_REMOTE);

        pantheios::log_NOTICE(PSTR("stmt 3"));

        // Set the file name for all back-ends.
        pantheios_be_file_setFilePath(PSTR("log.all"));

    pantheios::log_NOTICE(PSTR("stmt 4"));

    pantheios::log_DEBUG(PSTR("exiting main()"));

    system("pause");
    return EXIT_SUCCESS;
}
catch(std::bad_alloc&)
{
    pantheios::log(pantheios::alert, PSTR("out of memory"));
}
catch(std::exception& x)
{
    pantheios::log_CRITICAL(PSTR("Exception: "), pantheios::exception(x));
}
catch(...)
{
    pantheios::logputs(pantheios::emergency, PSTR("Unexpected unknown error"));
}

return EXIT_FAILURE;
}

/* ///////////////////////////// end of file //////////////////////////// */

我有一个用于隐式链接目的的"include_pantheios.cpp"文件。以下是它的内容:

/* Pantheios Header Files */
#include <pantheios/implicit_link/core.h>
#include <pantheios/implicit_link/fe.simple.h>
#include <platformstl/platformstl.h>
#include <pantheios/implicit_link/be.file.h>

#if (   defined(UNIX) || \
    defined(unix))&& \
    (   defined(_WIN32) || \
    defined(_WIN64))
# include <unixem/implicit_link.h>
#endif /* _WIN32 || _WIN64 */

有人能看出我的问题出在哪里吗? 提前感谢, Vincent

4个回答

3

+1 这对我非常有效 - 我现在可以在 /var/log/syslogstderr 中看到它,但我需要写入 /var/log/myapp/error.log - 我想知道是否需要自己的后端(如果可能的话,我想避免...) - kfmfe04

3

我认为你的困惑部分来自于示例做得过多:它将本地文件和远程文件都显示在一个页面上。一个更简单的示例可能是:

// Headers for main()
#include <pantheios/pantheios.hpp> 
#include <pantheios/backends/bec.file.h> 

// Headers for implicit linking
#include <pantheios/implicit_link/core.h>
#include <pantheios/implicit_link/fe.simple.h>
#include <pantheios/implicit_link/be.file.h>

int main() {

  pantheios::log_NOTICE("log-1"); // save until log file set
  pantheios_be_file_setFilePath("mylogfile"); // sets log file; write "log-1" stmt
  pantheios::log_NOTICE("log-2"); // write "log-2" stmt
  pantheios_be_file_setFilePath(NULL); // close "mylogfile"


  pantheios::log_NOTICE("log-3"); // save until log file set
  pantheios_be_file_setFilePath("mylogfile2"); // sets log file; write "log-3" stmt
  pantheios::log_NOTICE("log-4"); // write "log-4" stmt
} // closes "mylogfile2" during program closedown

原始代码存在问题,我认为它来自于Pantheios示例程序,它试图同时演示如何使用本地和远程后端以及如何使用be.file后端。

忘记所有不同的后端,专注于be.file特定的内容。

希望对你有所帮助。


1
我尝试了你的例子,但结果还是一样。我可以在控制台中看到日志,但日志文件从未被创建。当我进行调试时,我发现init从未被调用。我成功地创建了日志文件并通过调用pantheios_be_file_initpantheios_be_file_logEntry函数写入了它。但正如你所说,这些函数不应该被隐式调用... - Vincent
相比之下,这个例子对我有效。我不得不删除我所有的代码,确实只是使用这个例子才能开始生成日志文件。对我而言,即使我在使用C++,我仍然需要调用Init函数。如果您感兴趣,这是我的帖子链接:http://stackoverflow.com/questions/20998306/pantheios-write-to-extenal-file/21022228#21022228 - Katianie

2
我认为问题在于你链接的顺序,但是根据你发布的代码,我不太清楚如何做到这一点。
我遇到了同样的问题,后来发现是因为我同时连接了两个后端:文件和fprintf。更具体地说,是因为我先链接了fprintf,然后才链接了文件。当我改变链接顺序,先链接文件,那么它就会创建并使用日志文件,但是当我注释掉pantheios_be_file_setFilePath时,就不会输出到标准输出。所以显然,先链接的那个是唯一能正常工作的(请查阅多个后端)。

0
据我所知,这段代码与库中提供的股票后端示例文件完全相同,因此应该可以正常工作。
您是如何确定日志文件未被写入的?这些是相对路径-尝试使用绝对路径以确保您正在查找正确的位置。
如果一切都失败了,您可以在文件路径设置后通过代码进行调试,以找出为什么没有任何内容被写出。

1
文森特:你不需要调用后端的初始化函数:这是由Pantheios运行时完成的。当你链接be.file时,无论是显式还是隐式链接,都会自动调用init/uninit函数。对于大多数后端,你甚至不需要调用后端函数。只有在使用be.file时,你需要调用后端函数来设置日志文件路径。 - dcw

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接