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

How to set the absolute path of log files correctly(chinese or japanese) #2863

Open
toby20130333 opened this issue Aug 23, 2023 · 6 comments

Comments

@toby20130333
Copy link

Hi support:
this is a test code:
When the fileName 's path contains chinese or janpanse,the app crash,cannot open the fileName
I need help!
Thank you
`
QString fileName = QString("%1").arg(nLog);
{
auto stdout_sink = std::make_sharedspdlog::sinks::stdout_color_sink_mt();
stdout_sink->set_level(spdlog::level::off);

    auto rotating_sink
        = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(fileName.toStdString().c_str(),
                                                                 maxFileSize,
                                                                 maxFileCount);
    std::vector<spdlog::sink_ptr> sinks{stdout_sink, rotating_sink};
    auto logger = std::make_shared<spdlog::async_logger>("normal_logger",
                                                         sinks.begin(),
                                                         sinks.end(),
                                                         spdlog::thread_pool(),
                                                         spdlog::async_overflow_policy::block);
    logger->set_pattern("%v");

    spdlog::register_logger(logger);
}

`

@tt4g
Copy link
Contributor

tt4g commented Aug 23, 2023

Print error messages with try-catch.
Most questions about not being able to create log files are caused by permission errors.

try {

    // your code here.

} catch (const std::exception &ex) {
    std::cerr << ex.what() << std::endl;
}

@tt4g
Copy link
Contributor

tt4g commented Aug 23, 2023

Maybe duplicate #2510

@toby20130333
Copy link
Author

@tt4g
But the issue cannot sovled my crash
I fixed with SPDLOG_WCHAR_FILENAMES marco on windows

@tt4g
Copy link
Contributor

tt4g commented Aug 23, 2023

No source code or error messages are provided, so I have no idea what the cause is.

At least #2510 did not solve the problem with SPDLOG_WCHAR_FILENAMES macro, but with QString::toLocal8Bit().data().

@amanjha491
Copy link

QString fileName = QString("%1").arg(nLog);
{
auto stdout_sink = std::make_sharedspdlog::sinks::stdout_color_sink_mt();
stdout_sink->set_level(spdlog::level::off);

// Convert QString to UTF-8 encoded std::string
auto utf8FileName = fileName.toUtf8().constData();

auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
    utf8FileName, maxFileSize, maxFileCount);

std::vector<spdlog::sink_ptr> sinks{stdout_sink, rotating_sink};
auto logger = std::make_shared<spdlog::async_logger>(
    "normal_logger", sinks.begin(), sinks.end(),
    spdlog::thread_pool(), spdlog::async_overflow_policy::block);
logger->set_pattern("%v");

spdlog::register_logger(logger);

}

@toby20130333
Copy link
Author

@amanjha491 Thank you ,I will try it

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

No branches or pull requests

3 participants