Reputation: 3343
I am trying to create a code snippet which shall print some console logs with current file name and current method/tag name like the way we have systr
in eclipse.
Although I am able to add code snippet in Tools > Options > Text editor > Snippet, I could not find any tag to retrieve current file name.
Example File (TestMyUI.js):
function doSomething{
console.log("TestMyUI.doSomething()");
}
Here I like to generate console.log
using a template saved in snippets. So that for every place I type something like consLog
it must auto complete the log filled with current file and method name. This is similar to what we have systr
in eclipse IDE.
Upvotes: 5
Views: 1430
Reputation: 49289
If you want to get that info for QML files and integrate it into the output, you can use
qInstallMessageHandler(QtMessageHandler handler)
to install your own message handler function. Inside that function you will have a QMessageLogContext
reference, which will give you access to more information, such as file name, line number, function name and so on.
void msgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
static QTextStream ts(stdout);
static const char * err[] = {"", "WARNING: ", "ERROR!: ", "FATAL: ", ""};
QString m = QString(err[type]) + context.category + "/" + context.file + "@" + QString::number(context.line) + "-" + context.function + ": " + msg;
ts << msg << endl;
}
Now, you may not want to process all messages in that manner, and limit it to some specific pattern, which can easily be achieved by incorporating a particular "header" in the message string and checking whether a particular message contains that or not, and proceed accordingly. For example:
console.log("test") // regular msg
console.log("?test") // custom msg handling
Naturally, you can use an array of headers to specify custom handling, save logs to different files, send network messages or emails in custom formats or whatever. Once you utilize a custom handler you can do with the messages whatever you want.
Upvotes: 1
Reputation: 7146
Adding to the answer of @dtech:
Reporing the file, line and function is already an integral part of logging in Qt. All you have to do is use it correctly. There is no need for a code snippet to do this, as all this information is already collected by console.log
but not shown by default. All you have to do now is tell Qt to make it visible.
A way that is easier than creating a custom message handler is to simply register a message pattern (See qSetMessagePattern
). With this you can register a pattern that should be used to format anything logged via QDebug (including QML). A simple example:
int main(int argc, char **argv) {
qSetMessagePattern("%{file}:%{line} %{function} -> %{if-category}%{category}: %{endif}%{message}");
QGuiApplication app(argc, argv);
// ...
}
Now in your QML-file simply log anything, and your good to go. For example, the following code:
function doSomething() {
console.log("test");
}
Would output for this example (created with an actual test file):
qrc:/TestMyUI.js:11 doSomething -> qml: test
Upvotes: 3