Always print GLib errors to the log as well as stderr
[moonshot-ui.git] / src / moonshot-logger.vala
1 /*
2  * Copyright (c) 2011-2014, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33
34 public MoonshotLogger get_logger(string name) {
35     return new MoonshotLogger(name);
36 }
37
38
39 #if USE_LOG4VALA
40
41 static void glib_default_log_handler(string? log_domain, LogLevelFlags log_level, string message)
42 {
43     Log4Vala.Logger logger = Log4Vala.Logger.get_logger(log_domain ?? "Glib");
44     stderr.printf(log_level.to_string() + " : " + message + "\n");
45     logger.error("Glib error level: " + log_level.to_string() + " : " + message);
46 }
47
48 /** Logger class that wraps the Log4Vala logger */
49 public class MoonshotLogger : Object {
50     static bool logger_is_initialized = false;
51
52     private Log4Vala.Logger logger;
53
54     public MoonshotLogger(string name) {
55         if (!logger_is_initialized) {
56             Log.set_default_handler(glib_default_log_handler);
57
58             //!! TODO: Don't hard-code the pathname.
59             Log4Vala.init("/home/dbreslau/log4vala.conf");
60             logger_is_initialized = true;
61         }
62
63         logger = Log4Vala.Logger.get_logger(name);
64     }
65
66     /**
67      * Log a trace message.
68      * @param message log message
69      * @param e optional Error to be logged
70      */
71     public void trace(string message, Error? e = null) {
72         logger.trace(message, e);
73     }
74
75
76     /**
77      * Log a debug message.
78      * @param message log message
79      * @param e optional Error to be logged
80      */
81     public void debug(string message, Error? e = null) {
82         logger.debug(message, e);
83     }
84
85
86     /**
87      * Log an info message.
88      * @param e optional Error to be logged
89      */
90     public void info(string message, Error? e = null) {
91         logger.info(message, e);
92     }
93
94     /**
95      * Log a warning message.
96      * @param message log message
97      * @param e optional Error to be logged
98      */
99     public void warn(string message, Error? e = null) {
100         logger.warn(message, e);
101     }
102
103     /**
104      * Log an error message.
105      * @param message log message
106      * @param e optional Error to be logged
107      */
108     public void error(string message, Error? e = null) {
109         logger.error(message, e);
110     }
111
112     /**
113      * Log a fatal message.
114      * @param message log message
115      * @param e optional Error to be logged
116      */
117     public void fatal(string message, Error? e = null) {
118         logger.fatal(message, e);
119     }
120 }
121
122
123 #else
124
125 /** Logger that currently does nothing, but may eventually write to stdout or a file if enabled */
126 public class MoonshotLogger : Object {
127
128     internal MoonshotLogger(string name) {
129     }
130
131     /**
132      * Log a trace message.
133      * @param message log message
134      * @param e optional Error to be logged
135      */
136     public void trace(string message, Error? e = null) {
137     }
138
139
140     /**
141      * Log a debug message.
142      * @param message log message
143      * @param e optional Error to be logged
144      */
145     public void debug(string message, Error? e = null) {
146     }
147
148
149     /**
150      * Log an info message.
151      * @param e optional Error to be logged
152      */
153     public void info(string message, Error? e = null) {
154     }
155
156     /**
157      * Log a warning message.
158      * @param message log message
159      * @param e optional Error to be logged
160      */
161     public void warn(string message, Error? e = null) {
162     }
163
164     /**
165      * Log an error message.
166      * @param message log message
167      * @param e optional Error to be logged
168      */
169     public void error(string message, Error? e = null) {
170     }
171
172     /**
173      * Log a fatal message.
174      * @param message log message
175      * @param e optional Error to be logged
176      */
177     public void fatal(string message, Error? e = null) {
178     }
179 }
180
181 #endif