Logging with Vala
Asked Answered
S

3

11

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.

Scuppernong answered 1/5, 2012 at 16:28 Comment(0)
C
27

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!

Candi answered 1/5, 2012 at 21:5 Comment(2)
Does Vala support custom messages for contract violations? Or is it going to give a cryptic default error message? I'm trying to find a good way to throw errors and the default "Error Handling"/"Contractual Programming" sections of the Vala tutorial don't seem to describe anything truly useful.Diazomethane
It looks like contracts are compiled into calls to g_return_if_fail(), which doesn't support any custom error message. And you're right that the error message it shows from the generated C code isn't the most helpful. If you want custom errors with your own messages to get thrown, Vala doesn't offer any syntactic sugar for you -- you'll just have to do it the old fashioned way with if and throw.Candi
C
4

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

Clothes answered 21/4, 2015 at 18:7 Comment(0)
H
3

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:

Hellenism answered 21/12, 2017 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.