5

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.

Ravisha
  • 3,261
  • 9
  • 39
  • 66
  • If I understand correctly, let us say your code calls a function `foo()`, then inside this function you want to print out the filename of this function ? Is that right ? – Vishaal Shankar Jun 13 '18 at 05:17
  • It would be helpful if you can show a small example of what you want to achieve. – Vishaal Shankar Jun 13 '18 at 05:19
  • 3
    Are you looking for this?: [Predefined macros](http://en.cppreference.com/w/cpp/preprocessor/replace#Predefined_macros) i.e. `__FILE__` and `__LINE__`. Since C++11, there is a (compiler-defined) `__func__` variable in every function: [`__func__`](https://en.cppreference.com/w/c/language/function_definition#func) – Scheff's Cat Jun 13 '18 at 06:40
  • @Scheff thanks for your answer. but it is more to do with the QT creator that i am looking for – Ravisha Jun 28 '18 at 05:00
  • I don't know if this is possible, it would be too tightly coupled with Creator, and a lot of people don't use Creator. – dtech Jul 13 '18 at 18:02
  • I don't think there's nothing more special than `__FILE__` in qt [qDebug not showing `__FILE__`,`__LINE__`](https://stackoverflow.com/q/24012108/995714), [`__FILE__`, `__LINE__` and `__FUNCTION__` for QML files?](https://stackoverflow.com/q/41409273/995714), [`__FILE__`, `__LINE__`, and `__FUNCTION__` usage in C++](https://stackoverflow.com/q/597078/995714) – phuclv Jul 20 '18 at 04:19

2 Answers2

3

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
Felix
  • 6,885
  • 1
  • 29
  • 54
  • Thanks a ton for the response. Alas our project is in 4.8 QT. Any clue if we could set this with some API in this version – Ravisha Jul 22 '18 at 16:46
  • No Idea - I guess you will have to search the doc for that. (You should also consider updating - Qt4 is not maintained anymore!) – Felix Jul 22 '18 at 18:52
1

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.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • Hi @dtech. Thanks for your answer. Can you please elaborate may be an example would help me better understand the solution. – Ravisha Jul 16 '18 at 06:02
  • Well, it will allow you to do what the OP asks about - `print the some console logs with current file name and current method/tag name`. See the answer edit: – dtech Jul 16 '18 at 08:10