How does Vala support the C language's __function__ __file__ __line__ macros?
Asked Answered
L

1

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 ?

Linkoski answered 17/3, 2016 at 9:6 Comment(0)
R
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.

Reames answered 17/3, 2016 at 9:40 Comment(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.Grays

© 2022 - 2024 — McMap. All rights reserved.