Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamicAppender can not work good #289

Open
FeJQ opened this issue May 13, 2024 · 2 comments
Open

DynamicAppender can not work good #289

FeJQ opened this issue May 13, 2024 · 2 comments
Assignees
Labels

Comments

@FeJQ
Copy link

FeJQ commented May 13, 2024

I want use MessageOnlyFormatter first, then switch to TxtFormatter, but following code is not working

static plog::DynamicAppender dynamicAppender;
static plog::RollingFileAppender<plog::MessageOnlyFormatter> startupAppender(logFileName.c_str(), logMaxSize, logMaxCount);
static plog::RollingFileAppender<plog::TxtFormatter> normalAppender(logFileName.c_str(), logMaxSize, logMaxCount);

dynamicAppender.addAppender(&startupAppender);

#ifdef QT_DEBUG
    plog::init(plog::debug, &dynamicAppender);
#else
    plog::init(plog::info, &dynamicAppender);
#endif

PLOG(plog::info) << "";
PLOG(plog::info) << "";
PLOG(plog::info) << "======================================";
PLOG(plog::info) << "Start time: " << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss").toStdString();
PLOG(plog::info) << "OS: " << QSysInfo::prettyProductName().toStdString();
PLOG(plog::info) << "Command: " << QCoreApplication::arguments().join(" ").toStdString();
PLOG(plog::info) << "Version: " << QCoreApplication::applicationVersion().toStdString();
PLOG(plog::info) << "======================================";
PLOG(plog::info) << "";

dynamicAppender.removeAppender(&startupAppender);
dynamicAppender.addAppender(&normalAppender);

PLOG(plog::info)<<"Normal mesage";
@SergiusTheBest
Copy link
Owner

I guess if you're on Windows you need to close a file handle in the startupAppender. Here is the sample code:

#include <plog/Log.h>
#include <plog/Initializers/RollingFileInitializer.h>
#include <plog/Appenders/DynamicAppender.h>
#include <plog/Formatters/MessageOnlyFormatter.h>

int main()
{
    const char logFileName[] = "mylog.log";
    const int logMaxSize = 10000000;
    const int logMaxCount = 3;

    static plog::DynamicAppender dynamicAppender;
    static plog::RollingFileAppender<plog::MessageOnlyFormatter> startupAppender(logFileName, logMaxSize, logMaxCount);
    static plog::RollingFileAppender<plog::TxtFormatter> normalAppender(logFileName, logMaxSize, logMaxCount);

    dynamicAppender.addAppender(&startupAppender);

    plog::init(plog::info, &dynamicAppender);

    PLOG(plog::info) << "";
    PLOG(plog::info) << "";
    PLOG(plog::info) << "======================================";
    PLOG(plog::info) << "Start time: ";
    PLOG(plog::info) << "OS: ";
    PLOG(plog::info) << "Command: ";
    PLOG(plog::info) << "Version: ";
    PLOG(plog::info) << "======================================";
    PLOG(plog::info) << "";

    dynamicAppender.removeAppender(&startupAppender);
    startupAppender.setFileName(""); // set a new file name to close a file handle for the original file name
    dynamicAppender.addAppender(&normalAppender);

    PLOG(plog::info)<< "Normal mesage";

    return 0;
}

@SergiusTheBest
Copy link
Owner

Note startupAppender.setFileName(""); part.

The resulting log file is:


======================================
Start time: 
OS: 
Command: 
Version: 
======================================

2024-05-13 19:34:12.022 INFO  [45812] [main@38] Normal mesage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants