Reputation: 4582
I have a function macro defined in c++ like below, the benefit here is that the identifier __PRETTY_FUNCTION__
, is automatically added to the final call thus original caller signature is captured, I need this also to be the case when I call my final function from QML without manually adding caller prety_type , I cant define a Q_INVOKABE called from QML but if the invoKable takes the rule of invoking the macro I would lost QML caller function signature , I know it should be possible because splodg library has this behavior of showing QML/JS function name .
#define my_debug(msg,category) \
myns::logger->log_debug(msg, category, __PRETTY_FUNCTION__)
in c++ ,
namespace myns{
class logger {
void log_debug(const char* msg , CATEGORY category , const char* caller_sign = ""){
...
...
}
}
}
Upvotes: 0
Views: 143
Reputation: 8277
There is no way to directly call your macro from QML. That is just not possible. But it sounds like that's not real your question. It sounds like you're more interested in just getting the line/function names in your debug output. You can do that easily with qInstallMessageHandler.
Start by defining your message handling function. Qt provides a context object that has all the line/function info you want.
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
const char *file = context.file ? context.file : "";
const char *function = context.function ? context.function : "";
switch (type) {
case QtDebugMsg:
fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
case QtInfoMsg:
fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
case QtWarningMsg:
fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
case QtCriticalMsg:
fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
case QtFatalMsg:
fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
}
}
Then tell Qt to use that function to handle your messages, like this:
int main(int argc, char **argv)
{
qInstallMessageHandler(myMessageOutput);
QGuiApplication app(argc, argv);
...
return app.exec();
}
Now when you use qDebug() or console.log(), it should call your output function and you should see the line numbers and function names.
Upvotes: 2