Question

I am new to Vala programming and have experiences with Java and .NET yet I haven't been able to find anything useful on how to log with Vala. Is there any useful logging facility like log4j or log4net or what's the suggested way to log in Vala with scaling into several logging levels like error, warn, debug and trace? And what about defensive programming like assertions and contracts? Are there any or what's the suggested way to do defensive programming and to get most useful logs with stack traces and precise root cause? Thanks for an advice.

Was it helpful?

Solution

Logging

Vala has some fairly robust logging facilities built in. You can use debug(), message(), warning(), error(), and critical() as shortcuts for the slightly more complex log() function. All are included in the base (automatically included) GLib namespace.

If you want to redirect log output or send different types of messages to different destinations, everything you need is in the GLib.Log namespace. You might want to read the glib docs on logging for what's going on behind the scenes there.

Defensive Programming

Vala also includes support for assertions and contracts. The short version: assert() and assert_not_reached() for assertions, and requires() and ensures() in method signatures for contracts. See the tutorial section on Assertions and Contract Programming for more info.

Error Handling

Vala's error handling is a little odd: similar-looking to exceptions, but not quite the same. The tutorial section on Error Handling covers the basics pretty well, and again it might be helpful to read the glib docs on errors to get a more advanced understanding of what's going on behind the scenes. As far as I know, there is no way to get a stack trace out of Vala errors -- just a type and a message.

Best Practices

In terms of best practices, I think Vala is similar enough to Java or C# that the practices you already know can be applied in a general sense. I suggest playing around with these features to get a feel for how the specifics look in Vala. Good luck!

OTHER TIPS

As an addition to @chazomaticus great answer, you can get stacktrace from Vala by unwiding the call stack.

It's just that this is not baked into the Vala language.

I happen to have written such a library: ivy

ivy

I would like to add 2 things to @chazomaticus answer:

  1. Why I'm not able to see any debug/log outputs?

    You have to set the enviroment variable G_MESSAGES_DEBUG to all. Which is also mentioned by the documentation, but not further explained. E.g. for debug:

    public static int main (string[] args) {
        // Use "G_MESSAGES_DEBUG=all ./test" to print debug messages!
    
        // Output: ``** (process:<PID>): DEBUG: <FILENAME>:<LINE>: my 10. debug message``
        debug ("my %d. %s", 10, "debug message");
        return 0;
    }
    
    valac --pkg glib-2.0 GLib.debug.vala
    
    G_MESSAGES_DEBUG=all ./GLib.debug
    
    ./GLib.debug
    

    If you want to further restrict the output, you can use error, warning, critical, message, info,debug and help instead of all.

  2. Where can I find the documentation for every logging function?

Further reading:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top