String formatting


#1

I’m writing log messages and have to do a lot like:
Logger::writeToLog (String( "This variable: ") + String (x));
That’s ugly, isn’t it?

My question, does there exist something like in python?
https://docs.python.org/2/library/stdtypes.html#string-formatting

Or how the Norwegians do like?
QString (“A string with %1 apples” ).arg (x);
http://doc.qt.io/qt-5/qstring.html#arg

String::formatted() is discouraged and I think there is good reason for that…

Any idea?


#2

Never understood why I so often see people over-using the String constructor as a wrapper around string literals… You could have just written

Logger::writeToLog ("This variable: " + String (x));

…but actually, you can do even better in a DBG statement if x is an int or double, e.g.

DBG ("This variable: " << x);

#3

Well, the XCode complained about that, but maybe I didn’t look close enough… I thought that the + operator for const char* + String was missing… but I’ll try again…
EDIT: the variable was not a String but a const char* string, that’s why xcode complained. so I took a bad example
Seems I got a litle confused, sorry

Thanks for the DBG, didn’t know that the << operator works there. Does DBG write to the currentLogger?

Would it be an idea to overload the stream operator for Logger too, like
Logger::log() << "Something " << x;