我试图在c ++ dll中使用pantheios日志框架。我已成功构建了dll,它通过我的测试应用程序(C ++ MFC应用程序)执行。
我使用隐式链接包含以下内容:
#include <pantheios/implicit_link/core.h>
#include <pantheios/implicit_link/fe.simple.h>
#include <pantheios/implicit_link/be.console.h>
我的DllMain通过以下调用初始化pantheios:
extern "C" const char PANTHEIOS_FE_PROCESS_IDENTITY[] = "FinishingLineController";
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
int panres = pantheios::pantheios_init();
if(panres < 0)
{
fprintf(stderr, "Failed to initialise the Pantheios libraries: %s\n",
pantheios::pantheios_getInitErrorString(panres));
return FALSE;
}
}
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
pantheios::pantheios_uninit();
break;
}
return TRUE;
}
当我执行以下代码时,我得到了一个
Microsoft C++ exception: stlsoft::winstl_project::windows_exception at memory location 0x0013da84
pantheios::log_DEBUG("Test logging");
我尝试使用显式链接而没有任何结果。
答案 0 :(得分:3)
AFAICT,您的代码看起来完整无误,但很难知道您是如何使用它的,以及从何处,以及何时获得异常。我想你应该提供更多信息。
我想说的一件事:我想你可能想要使用Pantheios的内部日志“bailout”功能 - 日志的日志。那么,你的
fprintf(stderr, "Failed to initialise the Pantheios libraries: %s\n",
pantheios::pantheios_getInitErrorString(panres));
会更好地写成
pantheios::util::onBailOut(pantheios::emergency,
"Failed to initialise the Pantheios libraries",
PANTHEIOS_FE_PROCESS_IDENTITY,
pantheios::getInitErrorString(panres));
这样就会记录您的日志初始化失败。
答案 1 :(得分:2)
一些进一步的测试显示,如果我将其链接到控制台应用程序而不是Windows应用程序,则来自dll的日志记录有效。如果我将后端更改为“文件”而不是“控制台”,则Windows应用程序会正确记录到文件中。所以问题似乎是Windows应用程序没有“控制台”。
解决方案是将标准输出/输入管道重定向到新控制台。这必须在win32应用程序中完成,因为默认情况下不会创建控制台。
void RedirectIOToConsole()
{
int hConHandle;
long lStdHandle;
CONSOLE_SCREEN_BUFFER_INFO coninfo;
FILE *fp;
// allocate a console for this app
AllocConsole();
// set the screen buffer to be big enough to let us scroll text
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
coninfo.dwSize.Y = MAX_CONSOLE_LINES;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
// redirect unbuffered STDOUT to the console
lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );
// redirect unbuffered STDIN to the console
lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "r" );
*stdin = *fp;
setvbuf( stdin, NULL, _IONBF, 0 );
// redirect unbuffered STDERR to the console
lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stderr = *fp;
setvbuf( stderr, NULL, _IONBF, 0 );
// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
// point to console as well
std::ios::sync_with_stdio();
}
然后从DllMain调用此函数。
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
RedirectIOToConsole();
int panres = pantheios::pantheios_init();
if(panres < 0)
{
fprintf(stderr, "Failed to initialise the Pantheios libraries: %s\n",
pantheios::pantheios_getInitErrorString(panres));
return FALSE;
}
// Set the file name for all back-ends.
//pantheios_be_file_setFilePath("output.log");
}
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
pantheios::pantheios_uninit();
break;
}
return TRUE;
}