4c4c7fc2add5a36a5bd6cb3086d5243311876c15
[moonshot-ui.git] / src / moonshot-logger.vala
1 /*
2  * Copyright (c) 2011-2016, 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 #if IPC_MSRPC
59             // Look for config file in the app's current directory.
60             string conf_file = "log4vala.conf";
61 #else
62             string conf_file = GLib.Environment.get_variable("MOONSHOT_UI_LOG_CONFIG");
63 #endif
64             Log4Vala.init(conf_file);
65             logger_is_initialized = true;
66         }
67
68         logger = Log4Vala.Logger.get_logger(name);
69     }
70
71     /**
72      * Log a trace message.
73      * @param message log message
74      * @param e optional Error to be logged
75      */
76     public void trace(string message, Error? e = null) {
77         logger.trace(message, e);
78     }
79
80
81     /**
82      * Log a debug message.
83      * @param message log message
84      * @param e optional Error to be logged
85      */
86     public void debug(string message, Error? e = null) {
87         logger.debug(message, e);
88     }
89
90
91     /**
92      * Log an info message.
93      * @param e optional Error to be logged
94      */
95     public void info(string message, Error? e = null) {
96         logger.info(message, e);
97     }
98
99     /**
100      * Log a warning message.
101      * @param message log message
102      * @param e optional Error to be logged
103      */
104     public void warn(string message, Error? e = null) {
105         logger.warn(message, e);
106     }
107
108     /**
109      * Log an error message.
110      * @param message log message
111      * @param e optional Error to be logged
112      */
113     public void error(string message, Error? e = null) {
114         logger.error(message, e);
115     }
116
117     /**
118      * Log a fatal message.
119      * @param message log message
120      * @param e optional Error to be logged
121      */
122     public void fatal(string message, Error? e = null) {
123         logger.fatal(message, e);
124     }
125 }
126
127
128 #else
129
130 /** Logger that currently does nothing, but may eventually write to stdout or a file if enabled */
131 public class MoonshotLogger : Object {
132
133     internal MoonshotLogger(string name) {
134     }
135
136     /**
137      * Log a trace message.
138      * @param message log message
139      * @param e optional Error to be logged
140      */
141     public void trace(string message, Error? e = null) {
142     }
143
144
145     /**
146      * Log a debug message.
147      * @param message log message
148      * @param e optional Error to be logged
149      */
150     public void debug(string message, Error? e = null) {
151     }
152
153
154     /**
155      * Log an info message.
156      * @param e optional Error to be logged
157      */
158     public void info(string message, Error? e = null) {
159     }
160
161     /**
162      * Log a warning message.
163      * @param message log message
164      * @param e optional Error to be logged
165      */
166     public void warn(string message, Error? e = null) {
167     }
168
169     /**
170      * Log an error message.
171      * @param message log message
172      * @param e optional Error to be logged
173      */
174     public void error(string message, Error? e = null) {
175     }
176
177     /**
178      * Log a fatal message.
179      * @param message log message
180      * @param e optional Error to be logged
181      */
182     public void fatal(string message, Error? e = null) {
183     }
184 }
185
186 #endif