2

I need add some log informations with souce file name, function name,

line number etc...

I have check the official docs, but not found...

so, how to do for it ?

Matrix
  • 152
  • 1
  • 5

1 Answers1

3

This is usually done via GLib logging.

For example try this Vala application:

int main (string[] args) {
    // info () is not shown by default, set G_MESSAGES_DEBUG=all in your shell to see them
    info ("Hello World");
    warning ("Hello World");
    //assert_true (false);
    // error () terminates the program
    error ("Hello World");
    return 0;
}

The output is:

$ G_MESSAGES_DEBUG=all src/glib_logging_test 
** INFO: glib_logging_test.vala:4: Hello World

** (process:10129): WARNING **: glib_logging_test.vala:5: Hello World

** (process:10129): ERROR **: glib_logging_test.vala:9: Hello World
Trace/Breakpoint ausgelöst

You can also set G_DEBUG in addition to G_MESSAGES_DEBUG, see running GLib applications.

You can install a custom handler with Log.set_handler () as well.

There is also Log.FILE, Log.LINE, Log.METHOD for the raw information equivalent to the C macros.

Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
  • 1
    If intermediary C code is generated (e.g. with `--ccode`), it's important to set the `--debug` flag so that the macros can resolve file, line and method properly. – arteymix Aug 16 '16 at 22:56