I am working on a logger framework for QT applications. I am not using QMessageLogger
directly because of understanding and learning purposes. There is one thing about one QMessageLogger
functionality that I would really like to have in my logger but I dont know how does it work. Lets take for example the qDebug
macro:
#define qDebug QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).debug
One can call this function in 2 ways: 1st way:
qDebug("abc = %u", abc);
2nd way:
qDebug() << "abc = " << abc;
I am looking at the library code, but I cannot quite understand how is it implemented that one can work with QMessageLogger
by using va_args
as well as some stream object.
How can I achieve such effect? I would really appreciate all help, would be grateful for an example.
Here is my print
method body. I need to achieve simmilar functionality with the "stream" way:
/*!
* \brief Adds the log line to the print queue.
* \param lvl: Log level of the line.
* \param text: Formatted input for va_list.
*/
void CBcLogger::print(MLL::ELogLevel lvl, const char* text, ...)
{
// check if logger initialized
if (!m_loggerStarted)
return;
// check if log level sufficient
if (lvl > m_setLogLvl)
return;
logLine_t logline;
logline.loglvl = lvl;
logline.datetime = QDateTime::currentDateTime();
va_list argptr;
va_start(argptr, text);
char* output = NULL;
if (vasprintf(&output, text, argptr))
{
logline.logstr = output;
delete output;
}
va_end(argptr);
emit addNewLogLine(logline);
}