I am trying to print caller function, line number and file name without throwing an error for normal debugging purpose in QML. I can print caller function name as follows
console.log("Caller Function Name"+arguments.callee.caller.name);
I am trying to print caller function, line number and file name without throwing an error for normal debugging purpose in QML. I can print caller function name as follows
console.log("Caller Function Name"+arguments.callee.caller.name);
You can override
qInstallMessageHandler
default function and provide your custom function which also prints line number / caller. You can find an example in the linked documentation. Another partial example:If
logFile
is open, logging data is wrote to that in a critical section delimited by aQMutex
otherwise it is simply output to the standard output of each platform.Whatever is the handler you define, it can be combined with categorized logging (available since Qt 5.2) to easily setup a custom logging facility tailored on your needs. You just need to define your logging categories, as described in this blog post, and call
qCDebug
,qCInfo()
,qCWarning()
and so on. Depending on the active categories (set via the static functionsetFilterRules()
ofQLoggingCategory
) different logging info can be printed or skipped.That's especially interesting now that Qt 5.8 is available. Since this release, you can use categories also in QML, i.e. you can call
console
functions and pass along a category, e.g.Also, categories declaration can be done fully in QML via the ad hoc type
LoggingCategory
.ADDENDUM (Qt < 5.0)
The proposed solution works in a Qt 5.0+ environment with categories fully usable with Qt 5.3+ and QML categories available in Qt 5.8+; in a Qt 4.x environment you should override
qInstallMsgHandler
but you do not have aQMessageLogContext
. That means you should manage file/line info outside the handler, e.g. you have to useQ_FUNC_INFO
or rely on__FILE__
and__LINE__
in C++ (note that the latters have been removed in latest 5.x releases as e.g. discussed here).