简体   繁体   English

Pantheios无法使用MFC应用程序登录到一个文件,包括(一个exe +两个DLL)

[英]Pantheios not logging into one file with MFC app comprise (one exe + two DLLs)

I have got an MFC app comprised of one exe and two DLLs. 我有一个包含一个exe和两个DLL的MFC应用程序。 The exe calls functions from those two DLLs. 该exe从这两个DLL调用函数。 Now I am trying to add some logging by using the Pantheios logging lib. 现在,我尝试使用Pantheios日志记录库添加一些日志记录。

What I want to achieve: 我要实现的目标:
The exe and the two DLLs are all logging to the same log file in hard drive. 该exe和两个DLL都记录到硬盘驱动器中的同一日志文件中。

Here is what I have done: 这是我所做的:

1) Using implicit linking in main exe program: 1)在主exe程序中使用隐式链接:


#include "stdafx.h"

#include <pantheios/implicit_link/core.h>
#include <pantheios/implicit_link/fe.simple.h>
#include <pantheios/implicit_link/be.file.h>


#define USER_SPECIFIED_LEVEL
#ifndef USER_SPECIFIED_LEVEL
  #include <pantheios/implicit_link/fe.simple.h>
#endif

#ifdef USER_SPECIFIED_LEVEL
PANTHEIOS_CALL(int) pantheios_fe_init(void*   reserved,void**  ptoken)
{
    *ptoken = NULL;
    return 0;
}

PANTHEIOS_CALL(void) pantheios_fe_uninit(void* token)
{}

PANTHEIOS_CALL(PAN_CHAR_T const*)  pantheios_fe_getProcessIdentity  (void *  token)
{
    return PANTHEIOS_LITERAL_STRING(MY_PROGRAM_ID);
}

PANTHEIOS_CALL(int) pantheios_fe_isSeverityLogged(void* token
                                                  , int   severity
                                                  , int   backEndId)
{
    //SEV_CRITICAL=2 < SEV_ERROR=3 < SEV_WARNING=4 < SEV_INFORMATIONAL=6
    if(severity <= pantheios::SEV_INFORMATIONAL)
        return 1;//allow output for anything above information lvl
    return 0;
}

#endif

In the main exe program where I need to add logging I use (after looked this link in SO talking about PANTHEIOS_BE_FILE_F_SHARE_ON_WINDOWS ): 在我需要添加日志的主exe程序中(在SO中谈论PANTHEIOS_BE_FILE_F_SHARE_ON_WINDOWS 链接后):

pantheios_be_file_setFilePath(PSTR("C:\\TestLog.log"),**PANTHEIOS_BE_FILE_F_SHARE_ON_WINDOWS**, 0, PANTHEIOS_BEID_ALL);
        pantheios::log_NOTICE(PSTR("process id: ["), pantheios::processId, PSTR("]"));
    pantheios::log_NOTICE(PSTR("thread id: ["), pantheios::threadId, PSTR("]"));
    pantheios::log_INFORMATIONAL(PSTR("testing log messagse"));

This works fine, and I can get logging in c:\\TestLog.log file as expected. 这可以正常工作,并且我可以按预期方式登录c:\\ TestLog.log文件。 However, I cannot get logging at all in those two DLLs, it simply gives erros for each logging call: 但是,我根本无法在这两个DLL中进行日志记录,它只是为每个日志记录调用提供了错误:

    pantheios::log_INFORMATIONAL(PSTR("testing message"));

saying the token is emtpy, so I Googled and found a solution: 说令牌是空的,所以我用Google搜索并找到了解决方案:

2) I need to call: 2)我需要致电:

pantheios::init();

for each DLL initialization inside their DllMain function. 对于其DllMain函数中的每个DLL初始化。 This way, there is no more "empty token" error when I try to log in the DLL, but still there's nothing logged by the DLLs in the log file (again, the main exe program is fine). 这样,当我尝试登录DLL时,没有更多的“空令牌”错误,但是在日志文件中DLL仍然没有任何记录(再次,主exe程序很好)。

3) I tweaked stuff a bit and I have to change the logging file names in the DLL loggings so they all log to different files: 3)我做了一些调整,我必须更改DLL日志中的日志文件名,以便它们都记录到不同的文件中:

DLL #1: DLL#1:

pantheios_be_file_setFilePath(PSTR("C:\\TestLogDll1.log"),PANTHEIOS_BE_FILE_F_SHARE_ON_WINDOWS, 0, PANTHEIOS_BEID_ALL);

DLL #2: DLL#2:

pantheios_be_file_setFilePath(PSTR("C:\\TestLogDll2.log"),PANTHEIOS_BE_FILE_F_SHARE_ON_WINDOWS, 0, PANTHEIOS_BEID_ALL);

This way I have 3 logging files all working fine: 这样,我有3个日志文件都正常工作:

  • TestLog.log for the main program 主程序的TestLog.log
  • TestLogDll1.log for DLL #1 DLL#1的TestLogDll1.log
  • TestLogDll2.log for DLL #2 DLL#2的TestLogDll2.log

But still I just cannot get both DLLs logging to the same file as the main exe does (TestLog.log). 但是我仍然无法像主exe一样将两个DLL都记录到同一文件(TestLog.log)。

To share entity or object between exe and dll, you may need to declare __declspec(dllexport) in the exe which exports them, and __declspec(dllimport) in the dll which import them. 若要在exe和dll之间共享实体或对象,您可能需要在导出它们的exe中声明__declspec(dllexport),并在导入它们的dll中声明__declspec(dllimport)。

For reference: 以供参考:
1) http://sourceforge.net/projects/pantheios/forums/forum/647484/topic/1639420/index/page/1 1) http://sourceforge.net/projects/pantheios/forums/forum/647484/topic/1639420/index/page/1
2) Use Pantheios logging framework from a dll 2) 从dll使用Pantheios日志框架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM