Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging with Vala

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.

like image 537
Martin Macak Avatar asked May 01 '12 16:05

Martin Macak


2 Answers

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!

like image 186
chazomaticus Avatar answered Nov 14 '22 08:11

chazomaticus


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

like image 4
Name is carl Avatar answered Nov 14 '22 10:11

Name is carl