Let's have a look at this little application:
#include <QString>
#include <QDebug>
int main(int argc, char *argv[]) {
const auto test_string =
QString{"Some string \n \x01 \u0002 with some \r special chars"};
qDebug() << test_string;
qDebug(qPrintable(test_string));
}
It gives the following output:
"Some string \n \u0001 \u0002 with some \r special chars"
Some string
special chars
Press <RETURN> to close this window...
This demonstrates how the qDebug << operator comes with some functionality that converts all the special characters of a QString to some readable string, which can easily be put in a string declaration in C++.
I would like to use this functionality to feed strings into a custom logging framework. Is there a possibility to use the same conversion function directly?
Effectively, this would mean to convert test_string to a QString instance that gives the same output on both the above qDebug statements.
QDebug::operator<<(const QString&)
and ended up in a function templatestatic inline putEscapedString()
. Btw. I found thatQDebug
has a constructor where aQString
may be passed. So, you might instanceQDebug
with aQString
to use this as formatter. To me, this seems less annoying as just copying the source code fromputEscapedString()
to make your own formatter but the latter might be an option too. – Scheff's Cat