Pantheios写入外部文件

时间:2014-01-08 14:24:41

标签: c++ logging pantheios

我环顾四周,找不到如何做到这一点的答案。我正在尝试使用Pantheios进行日志记录,我想写入外部文件(否则最重要的是)。我正在关注提供的一个示例但它似乎没有将日志文件放在任何地方。这是代码:

编辑:还有pantheios_be_file_setFilePath返回-4(PANTHEIOS_INIT_RC_UNSPECIFIED_FAILURE)所以那.....非常有用

    #include "stdafx.h"
    #include <pantheios/pantheios.hpp> 
    #include <pantheios/implicit_link/core.h>
    #include <pantheios/implicit_link/fe.simple.h> 
    #include <pantheios/implicit_link/be.WindowsConsole.h>
    #include <pantheios/implicit_link/be.file.h>
    #include <pantheios/frontends/fe.simple.h>
    #include <pantheios/backends/bec.file.h>
    #include <pantheios/inserters/args.hpp>

    PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("LogTest");

    int _tmain(int argc, _TCHAR* argv[])
    {
        try
        {
            pantheios_be_file_setFilePath(PANTHEIOS_LITERAL_STRING("testlogforme.log"),  PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BEID_ALL);


            pantheios::log(pantheios::debug, "Entering main(", pantheios::args(argc,argv, pantheios::args::arg0FileOnly), ")");

            pantheios::log_DEBUG("debug yo");
            pantheios::log_INFORMATIONAL("informational fyi");
            pantheios::log_NOTICE("notice me!");
            pantheios::log_WARNING("warning!!");
            pantheios::log_ERROR("error omg");
            pantheios::log_CRITICAL("critical!!!");
            pantheios::log_ALERT("alert mang");
            pantheios::log_EMERGENCY("EMERGENCY!!!!!");

            pantheios_be_file_setFilePath(NULL, PANTHEIOS_BEID_ALL);

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

        return EXIT_FAILURE;
    }

也许我没有打电话给某个方法,也可能没有将它保存到一个好位置?

1 个答案:

答案 0 :(得分:0)

事实证明,Pantheios的一些例子是不正确的。即使您使用的是C ++,也需要调用pantheios_init()。这是我在删除所有代码并实现一个有效的示例后开始工作的示例。

    // 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>

    PANTHEIOS_EXTERN_C const char PANTHEIOS_FE_PROCESS_IDENTITY[] = "testLOL";

    int main()
    {
        if(pantheios::pantheios_init() < 0)
        {
            return 1;
        }

        pantheios::log_NOTICE("log-1"); // save until log file set
        pantheios_be_file_setFilePath("mylogfile.log"); // 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.log"); // sets log file; write "log-3" stmt
        pantheios::log_NOTICE("log-4"); // write "log-4" stmt

        //system("pause");
        return 0;
    } // closes "mylogfile2" during program closedown

我在堆栈溢出的不同帖子上找到了这个例子,但就像我说的那样,内置的例子不起作用。

相关问题