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

Win11 create log failed #2510

Closed
L-Super opened this issue Oct 13, 2022 · 10 comments
Closed

Win11 create log failed #2510

L-Super opened this issue Oct 13, 2022 · 10 comments

Comments

@L-Super
Copy link

L-Super commented Oct 13, 2022

The utf-8 code used by my qt program code.When I use spdlog, it failed to create a log file under the Win11 Chinese user name.

demo code:

logTest::logTest(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
	QString standPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
	standPath.append("/logtest.log");

	auto str = "请截图此窗口\n" + standPath;

	spdlog::info("log widget {}",standPath.toUtf8());
	
	QMessageBox::information(this, "请截图此窗口", str);
	
	auto getLogger = spdlog::get("file_logger");

	std::vector<spdlog::sink_ptr> sinks;
	sinks.push_back(std::make_shared<spdlog::sinks::stderr_color_sink_mt>());

	sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(standPath.toStdString(), 1048576 * 5, 3));
	auto mLogger =
		std::make_shared<spdlog::logger>("log", std::begin(sinks), std::end(sinks));
	spdlog::register_logger(mLogger);

	try
	{
		mLogger->error("test log path");
		mLogger->flush();
	}
	catch (const spdlog::spdlog_ex& ex)
	{
		auto s = ex.what();
		QMessageBox::information(this, "请截图此窗口", s);

	}

}

V%3%EWCJODZ)5@6@~91Y(G7
W6II5Y$2TNU$CS8_9`Y_U5A

The QMessageBox display path is normal, but the spdlog displays garbled code.

@tt4g
Copy link
Contributor

tt4g commented Oct 13, 2022

There is no specification in the C++ standard specification that the character encoding of string literals in source code is UTF-8. It is platform dependent.
If it is garbled, that is the developer's responsibility.

If you are building with MSVC, please make sure that the characters in the source code are interpreted as UTF-8 by specifying the /utf-8 option.
See: https://learn.microsoft.com/en-us/cpp/build/reference/utf-8-set-source-and-executable-character-sets-to-utf-8?view=msvc-170

@L-Super
Copy link
Author

L-Super commented Oct 13, 2022

My source code is UTF-8,and /utf-8 option added.
When I change some code
like this picture, It's normal.
image

But when the path is C:/Users/神/AppData/Roaming/logTest/logtest.log, it's create log file failed.So I think , when the Windows user called Chinese name, log will create a failure.
FL)D)E8B@}%O0O3AB7K)J_S

@tt4g
Copy link
Contributor

tt4g commented Oct 13, 2022

Since the Windows directory separator is \, not /, the log file will never be created correctly.

@L-Super
Copy link
Author

L-Super commented Oct 13, 2022

I don't think that's the problem.
They have the same effect

神\\AppData\\log.log = 神/AppData/log.log

@tt4g
Copy link
Contributor

tt4g commented Oct 13, 2022

So do executing user have write permission to C:/神/AppData/log.log?
In any case, catch the exception that is causing the abort and check the error message from std::exception::what().

@L-Super
Copy link
Author

L-Super commented Oct 13, 2022

catch Failed opening file C:/Users/刘彦辉/AppData/Roaming/TanggulaClient/VisionVera/logs/vvlog.log for writing: illegal byte sequence
image

I am normal on Win10, but Win11 has this problem.

@tt4g
Copy link
Contributor

tt4g commented Oct 13, 2022

As per the error message, the file path string you are passing to spdlog contains a byte sequence that is considered invalid in the character encoding expected by the OS.

The Qt documentation says that QString::toStdString() returns a UTF-8 string.

The character encoding that the Windows API expects for char * is determined by the current locale setting (I don't know of a character encoding that corresponds to a Chinese locale).
Generate a char * from QString with the character encoding corresponding to the current locale and use it as the spdlog file path.

@artemyv
Copy link

artemyv commented Oct 13, 2022

What happens if you define following setting
///////////////////////////////////////////////////////////////////////////////
// Uncomment to enable usage of wchar_t for file names on Windows.
//
// #define SPDLOG_WCHAR_FILENAMES
///////////////////////////////////////////////////////////////////////////////
and pass path as UTF-16 (const wchar_t*)?

@tt4g
Copy link
Contributor

tt4g commented Oct 13, 2022

If SPDLOG_WCHAR_FILENAMES is defined, It will accept filenames of type const wchar_t *.
So you can pass QString::toStdWString() to spdlog::sinks::rotating_file_sink_mt.

@L-Super
Copy link
Author

L-Super commented Oct 14, 2022

Thank you very much. It is confirmed that it is a UTF-8 and GBK coding problem. I used the QString().toLocal8Bit().data() to solve 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