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

fix: QString cannot directly print non-English (e.g. Chinese) #852

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1117,11 +1117,11 @@ Easylogging++ has complete logging support for Qt core library. When enabled, th

Following Qt classes and containers are supported by Easylogging++ v9.0+

| * | * | * | * | * | * |
|---------------|---------------------------|--------------------|--------------------|--------------------|--------------------|
| `QString` | `QByteArray` | `QLatin` | `QList` | `QVector` | `QQueue` |
| `QSet` | `QPair` | `QMap` | `QMultiMap` | `QHash` | `QMultiHash` |
| `QLinkedList` | `QStack` | `QChar` | `q[u]int[64]` | | |
| * | * | * | * | * | * |
| ------------- | ------------ | -------- | ------------- | --------- | ------------ |
| `QString` | `QByteArray` | `QLatin` | `QList` | `QVector` | `QQueue` |
| `QSet` | `QPair` | `QMap` | `QMultiMap` | `QHash` | `QMultiHash` |
| `QLinkedList` | `QStack` | `QChar` | `q[u]int[64]` | `QRect` | |

Similar to STL logging, Qt containers are also limit to log 100 entries per log, you can change this behaviour by changing base::consts::kMaxLogPerContainer from header but this is not recommended as this was done for performance purposes.

Expand Down
17 changes: 16 additions & 1 deletion src/easylogging++.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ ELPP_INTERNAL_DEBUGGING_OUT_INFO << ELPP_INTERNAL_DEBUGGING_MSG(internalInfoStre
#if defined(ELPP_QT_LOGGING)
// For logging Qt based classes & templates
# include <QString>
# include <QRect>
# include <QByteArray>
# include <QVector>
# include <QList>
Expand Down Expand Up @@ -2987,10 +2988,24 @@ return writeIterator(template_inst.begin(), template_inst.end(), template_inst.s
# if defined(ELPP_UNICODE)
m_logger->stream() << msg.toStdWString();
# else
m_logger->stream() << msg.toStdString();
m_logger->stream() << msg.toLocal8Bit().toStdString();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By converting to native encoding (ANSI), non-English strings can be displayed normally.

# endif // defined(ELPP_UNICODE)
return *this;
}
inline MessageBuilder& operator<<(const QRect& rect) {
const int& left = rect.left();
const int& top = rect.top();
const int& width = rect.width();
const int& height = rect.height();
# if defined(ELPP_UNICODE)
#define int2wstr(name) QString::number(name).toStdWString()
m_logger->stream() << L"QRect(" << int2wstr(left) << L"," << int2wstr(top) << L" " << int2wstr(width) << L"x" << int2wstr(height) << L")";
# else
#define int2str(name) QString::number(name).toLocal8Bit().toStdString()
m_logger->stream() << "QRect(" << int2str(left) << "," << int2str(top) << " " << int2str(width) << "x" << int2str(height) << ")";
# endif
return *this;
}
inline MessageBuilder& operator<<(const QByteArray& msg) {
return operator << (QString(msg));
}
Expand Down