Vala - Time and Log
This article describes two things about Vala programming. First, how to get current time and output it as a formatted timesamp. Second, how to design custom log.
What Time Is It?
Vala provides two way to get time (it represents the number of seconds elapsed since 00:00:00 on January 1, 1970, UTC). They are TimeVal and time_t. time_t represents time by seconds. TimeVal represents more precise time, with seconds and microseconds.
time_t does not provide any methods to format as string. We should use Time to do this.
The following is an example to get current time and to output it as a formatted timestamp.
rock@desktop:~$ valac what_time_is_it.vala rock@desktop:~$ ./what_time_is_it It is 2010-08-24T08:10:18.484744Z. It is 2010-08-24 16:10:18. It is 2010-08-24 16:10:18. It is 2010-08-24 16:10:18.
Custom Your Log
When we design a background daemon, we usually need to log some informations to file. The simple way is just redirect stdout (or stderr) to our log file, then we could simply log our informations via printf or puts.
The following is a custom log example. It also adds a timestamp (see above) to log messages.
rock@rock-desktop:~$ ./time_log rock@rock-desktop:~$ cat /tmp/test.log [2010-08-24 16:26:01] error message about world. [2010-08-24 16:26:01] i is 10, j is jack.
How about GLib.log
By default way, GLib.log(), GLib.message(), GLib.error(), GLib.warning(), and etc, will directly output message to /dev/stderr. We could not use FileStream to redirect those. You may invoke GLib.Log.set_default_handler() or GLib.Log.set_handler() to change handler.
rock@rock-desktop:~$ ./time_log2 rock@rock-desktop:~$ cat /tmp/test2.log [2010-08-24 16:48:27] time_log2.vala:11: hello