Add/Edit cards is now functionally complete (still needs aesthetic cleanup)
[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 // To use this, uncomment the line below that calls Log.set_default_handler. (It's often better
42 // to let Glib log messages be printed on stderr; but this way, they can be synchronized with
43 // our log messages in a single stream.)
44 static void glib_default_log_handler(string? log_domain, LogLevelFlags log_level, string message)
45 {
46     Log4Vala.Logger logger = Log4Vala.Logger.get_logger(log_domain ?? "Glib");
47     logger.error("Glib error level: " + log_level.to_string() + " : " + message);
48 }
49
50 /** Logger class that wraps the Log4Vala logger */
51 public class MoonshotLogger : Object {
52     static bool logger_is_initialized = false;
53
54     private Log4Vala.Logger logger;
55
56     public MoonshotLogger(string name) {
57         if (!logger_is_initialized) {
58             // Log.set_default_handler(glib_default_log_handler);
59
60             //!! TODO: Don't hard-code the pathname.
61             Log4Vala.init("/home/dbreslau/log4vala.conf");
62             logger_is_initialized = true;
63         }
64
65         logger = Log4Vala.Logger.get_logger(name);
66     }
67
68     /**
69      * Log a trace message.
70      * @param message log message
71      * @param e optional Error to be logged
72      */
73     public void trace(string message, Error? e = null) {
74         logger.trace(message, e);
75     }
76
77
78     /**
79      * Log a debug message.
80      * @param message log message
81      * @param e optional Error to be logged
82      */
83     public void debug(string message, Error? e = null) {
84         logger.debug(message, e);
85     }
86
87
88     /**
89      * Log an info message.
90      * @param e optional Error to be logged
91      */
92     public void info(string message, Error? e = null) {
93         logger.info(message, e);
94     }
95
96     /**
97      * Log a warning message.
98      * @param message log message
99      * @param e optional Error to be logged
100      */
101     public void warn(string message, Error? e = null) {
102         logger.warn(message, e);
103     }
104
105     /**
106      * Log an error message.
107      * @param message log message
108      * @param e optional Error to be logged
109      */
110     public void error(string message, Error? e = null) {
111         logger.error(message, e);
112     }
113
114     /**
115      * Log a fatal message.
116      * @param message log message
117      * @param e optional Error to be logged
118      */
119     public void fatal(string message, Error? e = null) {
120         logger.fatal(message, e);
121     }
122 }
123
124
125 #else
126
127 /** Logger that currently does nothing, but may eventually write to stdout or a file if enabled */
128 public class MoonshotLogger : Object {
129
130     internal MoonshotLogger(string name) {
131     }
132
133     /**
134      * Log a trace message.
135      * @param message log message
136      * @param e optional Error to be logged
137      */
138     public void trace(string message, Error? e = null) {
139     }
140
141
142     /**
143      * Log a debug message.
144      * @param message log message
145      * @param e optional Error to be logged
146      */
147     public void debug(string message, Error? e = null) {
148     }
149
150
151     /**
152      * Log an info message.
153      * @param e optional Error to be logged
154      */
155     public void info(string message, Error? e = null) {
156     }
157
158     /**
159      * Log a warning message.
160      * @param message log message
161      * @param e optional Error to be logged
162      */
163     public void warn(string message, Error? e = null) {
164     }
165
166     /**
167      * Log an error message.
168      * @param message log message
169      * @param e optional Error to be logged
170      */
171     public void error(string message, Error? e = null) {
172     }
173
174     /**
175      * Log a fatal message.
176      * @param message log message
177      * @param e optional Error to be logged
178      */
179     public void fatal(string message, Error? e = null) {
180     }
181 }
182
183 #endif