plog icon indicating copy to clipboard operation
plog copied to clipboard

plog inside a shared library and an executable

Open TChristophe opened this issue 7 years ago • 12 comments

Using plog in both a shared library and an executable that links to this shared library does not work.

The log functions called from inside the library are working but any log function called from the executable code does not work (no output).

TChristophe avatar Nov 27 '17 16:11 TChristophe

What OS and compiler do you use? Could you post some code?

SergiusTheBest avatar Nov 27 '17 17:11 SergiusTheBest

Hi,

I have the same behavior on both Windows with Visual Studio 2015 and on Linux with GCC.

I don't do anything special. I initialize the Log system in the main file and use standard log macros LOG_ERROR, LOG_DEBUG... in both the application code and the library code.

On Mon, Nov 27, 2017 at 12:56 PM, Sergey Podobry [email protected] wrote:

What OS and compiler do you use? Could you post some code?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/SergiusTheBest/plog/issues/74#issuecomment-347268102, or mute the thread https://github.com/notifications/unsubscribe-auth/ANhx_dUvDpBlkjI70sy36m2YdNtliDBBks5s6vfpgaJpZM4Qr4xB .

-- Christophe Tornieri Developer Félix & Paul Studios +1.514.331.7001 www.felixandpaul.com

TChristophe avatar Nov 27 '17 18:11 TChristophe

Here is what I do in the main file:

plog::init(i_max_severity, i_file_name, i_max_file_size, i_max_files);

I just want to use the default log instance.

Thanks for your help.

On Mon, Nov 27, 2017 at 1:06 PM, Christophe Tornieri < [email protected]> wrote:

Hi,

I have the same behavior on both Windows with Visual Studio 2015 and on Linux with GCC.

I don't do anything special. I initialize the Log system in the main file and use standard log macros LOG_ERROR, LOG_DEBUG... in both the application code and the library code.

On Mon, Nov 27, 2017 at 12:56 PM, Sergey Podobry <[email protected]

wrote:

What OS and compiler do you use? Could you post some code?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/SergiusTheBest/plog/issues/74#issuecomment-347268102, or mute the thread https://github.com/notifications/unsubscribe-auth/ANhx_dUvDpBlkjI70sy36m2YdNtliDBBks5s6vfpgaJpZM4Qr4xB .

-- Christophe Tornieri Developer Félix & Paul Studios +1.514.331.7001 www.felixandpaul.com

-- Christophe Tornieri Developer Félix & Paul Studios +1.514.331.7001 www.felixandpaul.com

TChristophe avatar Nov 27 '17 18:11 TChristophe

Hi, In our project we made a special static function in shared library to initialize plog and called it on application start before any calls to that library.

In shared library:

shared_lib_log.h
#include <plog/Log.h>
namespace shared_lib_log
{
    void initializePlog(plog::Severity severity, plog::IAppender* appender);
}

shared_lib_log.cpp
#include <shared_lib_log.h>
namespace shared_lib_log
{
    void initializePlog(plog::Severity severity, plog::IAppender* appender)
    {
        plog::init(severity, appender); // Initialize the shared library logger.
    }
}

Then in main executable:

#include <plog/Appenders/ConsoleAppender.h>
#include <shared_lib_log.h>

int main()
{
...
    plog::ConsoleAppender<plog::TxtFormatter> consoleAppender;
    plog::init(plog::debug, &consoleAppender);
    shared_lib_log::initializePlog(plog::debug, plog::get());
...
}

variar avatar Nov 29 '17 19:11 variar

@TChristophe Are you using the same log file name for the both executable and shared library? If yes it can be the issue.

SergiusTheBest avatar Nov 30 '17 20:11 SergiusTheBest

Thanks I will try that.

On Wed, Nov 29, 2017 at 2:46 PM, Anton Filimonov [email protected] wrote:

Hi, In our project we made a special static function in shared library to initialize plog and called it on application start before any calls to that library.

In shared library:

shared_lib_log.h #include <plog/Log.h> namespace shared_lib_log { void initializePlog(plog::Severity severity, plog::IAppender* appender); }

shared_lib_log.cpp #include <shared_lib_log.h> namespace shared_lib_log { void initializePlog(plog::Severity severity, plog::IAppender* appender) { plog::init(severity, appender); // Initialize the shared library logger. } }

Then in main executable:

#include <plog/Appenders/ConsoleAppender.h> #include <shared_lib_log.h>

int main() { ... plog::ConsoleAppenderplog::TxtFormatter consoleAppender; plog::init(plog::debug, &consoleAppender); shared_lib_log::initializePlog(plog::debug, plog::get()); ... }

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/SergiusTheBest/plog/issues/74#issuecomment-347974613, or mute the thread https://github.com/notifications/unsubscribe-auth/ANhx_ZRzv5o9S9fzmnYWTDxFD6B2-YMGks5s7bSTgaJpZM4Qr4xB .

-- Christophe Tornieri Developer Félix & Paul Studios +1.514.331.7001 www.felixandpaul.com

TChristophe avatar Nov 30 '17 21:11 TChristophe

Yes it is the same file.

On Thu, Nov 30, 2017 at 3:28 PM, Sergey Podobry [email protected] wrote:

@TChristophe https://github.com/tchristophe Are you using the same log file name for the both executable and shared library? If yes it can be the issue.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/SergiusTheBest/plog/issues/74#issuecomment-348311337, or mute the thread https://github.com/notifications/unsubscribe-auth/ANhx_cwg2f9EXc13zGx6kyZF4sfV_XgPks5s7w_wgaJpZM4Qr4xB .

-- Christophe Tornieri Developer Félix & Paul Studios +1.514.331.7001 www.felixandpaul.com

TChristophe avatar Nov 30 '17 21:11 TChristophe

Здравствуйте! У меня похожая проблема при использовании единого экземпляра логера. Имею 2 .dll и 1.exe, в котором происходит инициализация логера. В динамических библиотеках использую PLOG_EXPORT, в .exe - PLOG_IMPORT, как в примере plog-master\samples\Shared. В итоге, сообщения выводятся в исполняемом файле, и в первой залинкованой библиотеке а во второй-нет. ОС Windows с Visual Studio 2017.

avfrolkina avatar Nov 28 '21 19:11 avfrolkina

@avfrolkina Hi! There can't be multiple PLOG_EXPORT binaries. You need only one export and multiple imports. Often it's more convenient to use another approach shown in ChainedApp.

SergiusTheBest avatar Nov 29 '21 19:11 SergiusTheBest

I have the same issue: executable:

#include <sharedlib.h>
#include <plog/Log.h>
#include <plog/Initializers/RollingFileInitializer.h>
#include <plog/Appenders/ColorConsoleAppender.h>

int main() {

    static plog::RollingFileAppender<plog::CsvFormatter> fileAppender("Log.csv", 8000, 3);
    static plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender;
    plog::init(plog::debug, &fileAppender).addAppender(&consoleAppender);
    
    SetConfigurationFile("conf.xml");
}

shared lib.cpp:

#include <sharedlib.h>
#include "plog/Log.h"

void SetConfigurationFile(char* configFilePath)
{
    LOGD << "Managing configuration files";
}

shared lib.h:

#ifdef INTERFACE_EXPORT
#    define IGM_INTERFACE_EXPORT __declspec(dllexport)
#else
#    define IGM_INTERFACE_EXPORT __declspec(dllimport)
#endif

extern "C" IGM_INTERFACE_EXPORT void SetConfigurationFile(char* configFilePath);

The preprocessor definitions are PLOG_EXPORT for the executable and PLOG_IMPORT for the shared lib. If I debug, in the LOGD of the sharedLib, once it gets to GetInstance, m_instance inside Util.h is NULL

RaffaeleBerzoini avatar Oct 20 '23 14:10 RaffaeleBerzoini

Hi @RaffaeleBerzoini !

The problem is that Windows can't import and export from the same module simultaneously. You're trying to import plog and export SetConfigurationFile from a dll and vice versa from an exe. Also exporting from exe is not supported by some build tools (mingw).

I suggest not to export/import plog but to explicitly initialize it in a dll as done in this sample: https://github.com/SergiusTheBest/plog/tree/master/samples/Chained

SergiusTheBest avatar Oct 23 '23 13:10 SergiusTheBest

Thank you very much @SergiusTheBest for the fast response. That was something i did not know. Thank you again :)

RaffaeleBerzoini avatar Oct 23 '23 13:10 RaffaeleBerzoini