2010-11-19 94 views
5

我試圖從pantheios登錄到一個文件,但無法設法使其工作。 消息在控制檯中正確顯示,但日誌文件未創建。 自從我看到thread以來,我試圖更改嚴重性級別,但沒有人可以使用。如何將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 */ 

有人看到我的問題來自哪裏嗎? 由於提前,

文森特

回答

0

至於我可以告訴這個代碼是相同的與庫給出的文件股票後端樣品,所以它應該工作。

你如何確定日誌文件沒有寫入?這些是相對路徑 - 嘗試使用絕對路徑來確保您正在尋找正確的位置。

如果一切都失敗了,可以通過代碼進行調試(在設置文件路徑之後),找出爲什麼沒有寫出來。

+1

文森特:你不要調用後端init函數:這是由Pantheios運行時完成的。鏈接到be.file時,無論是顯式還是隱式,都會自動調用init/uninit函數。對於大多數後端,您甚至不會調用後端功能。只有在be.file的情況下才可以設置日誌文件路徑。 – dcw 2010-11-20 11:03:45

4

我認爲你的困惑部分來自於這個例子做得太多:它顯示本地和遠程文件在一個。一個簡單的例子是:

// 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示例程序的問題是,它試圖illustraet如何使用本地,並在同一時間,遠程後端試圖說明如何使用be.file後端。

忘記所有不同的後端,並專注於be.file特定的東西。

HTH

+1

我試過你的例子,但結果是一樣的。我可以在控制檯中看到日誌,但從未創建日誌文件。當我調試時,我發現init從未被調用過。我成功地創建了日誌文件並寫入它,調用了'pantheios_be_file_init'和'pantheios_be_file_logEntry'函數。但就像你說的那樣,這些功能不應該被隱含地稱爲... – Vincent 2010-11-22 09:10:00

+0

相反,這個例子對我很有幫助。我不得不刪除我所有的代碼,直接使用這個例子來獲取日誌文件開始生成。對我來說,我不得不打電話給Init,即使我正在使用C++。這裏是我的帖子的鏈接,如果你好奇:http://stackoverflow.com/questions/20998306/pantheios-write-to-extenal-file/21022228#21022228 – Katianie 2014-01-09 16:56:52

2

我認爲這個問題是您鏈接命令,但我不太看給你發佈的代碼如何是可能的。

我遇到了同樣的問題,我意識到這是因爲我一次鏈接兩個後端:file和fprintf。更具體地說,這是因爲我在文件之前鏈接了fprintf 。當我先將訂單切換到鏈接文件時,它會創建並使用日誌文件,但當我註釋掉pantheios_be_file_setFilePath時,會將而不是輸出到stdout。因此,顯然首先鏈接的是唯一能夠工作的(查找多個後端)。

3

我得到了同樣的問題,爲今後的人來說,這個問題是爲了鏈接庫

Pantheios論壇: https://sourceforge.net/projects/pantheios/forums/forum/475314/topic/5313841/index/page/1

之前,我只是鏈接的pantheios.1.be.file.gcc44 pantheios.1.be.fprintf.gcc44

+0

+1這對我很好 - 我看到它在'/var/log/syslog'和'stderr'中,我需要寫入'/ var/log/myapp/error.log' - 我是否需要自己的後端(如果可能,我想避免)... – kfmfe04 2013-05-28 11:02:43

相關問題