Added a logging facility (MoonshotLogger, in moonshot-logger.vala).
[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 #if USE_LOG4VALA
39
40 /** Logger class that wraps the Log4Vala logger */
41 public class MoonshotLogger : Object {
42         static bool logger_is_initialized = false;
43
44         private Log4Vala.Logger logger;
45
46         public MoonshotLogger(string name) {
47                 if (!logger_is_initialized) {
48                         //!! TODO: Don't hard-code the pathname.
49                         Log4Vala.init("/home/dbreslau/log4vala.conf");
50                         logger_is_initialized = true;
51                 }
52
53                 logger = Log4Vala.Logger.get_logger(name);
54         }
55
56         /**
57          * Log a trace message.
58          * @param message log message
59          * @param e optional Error to be logged
60          */
61         public void trace(string message, Error? e = null) {
62                 logger.trace(message, e);
63         }
64
65
66         /**
67          * Log a debug message.
68          * @param message log message
69          * @param e optional Error to be logged
70          */
71         public void debug(string message, Error? e = null) {
72                 logger.debug(message, e);
73         }
74
75
76         /**
77          * Log an info message.
78          * @param e optional Error to be logged
79          */
80         public void info(string message, Error? e = null) {
81                 logger.info(message, e);
82         }
83
84         /**
85          * Log a warning message.
86          * @param message log message
87          * @param e optional Error to be logged
88          */
89         public void warn(string message, Error? e = null) {
90                 logger.warn(message, e);
91         }
92
93         /**
94          * Log an error message.
95          * @param message log message
96          * @param e optional Error to be logged
97          */
98         public void error(string message, Error? e = null) {
99                 logger.error(message, e);
100         }
101
102         /**
103          * Log a fatal message.
104          * @param message log message
105          * @param e optional Error to be logged
106          */
107         public void fatal(string message, Error? e = null) {
108                 logger.fatal(message, e);
109         }
110
111 }
112
113
114 #else
115
116 /** Logger that currently does nothing, but may eventually write to stdout or a file */
117 public class MoonshotLogger : Object {
118
119         internal MoonshotLogger(string name) {
120         }
121
122         /**
123          * Log a trace message.
124          * @param message log message
125          * @param e optional Error to be logged
126          */
127         public void trace(string message, Error? e = null) {
128         }
129
130
131         /**
132          * Log a debug message.
133          * @param message log message
134          * @param e optional Error to be logged
135          */
136         public void debug(string message, Error? e = null) {
137         }
138
139
140         /**
141          * Log an info message.
142          * @param e optional Error to be logged
143          */
144         public void info(string message, Error? e = null) {
145         }
146
147         /**
148          * Log a warning message.
149          * @param message log message
150          * @param e optional Error to be logged
151          */
152         public void warn(string message, Error? e = null) {
153         }
154
155         /**
156          * Log an error message.
157          * @param message log message
158          * @param e optional Error to be logged
159          */
160         public void error(string message, Error? e = null) {
161         }
162
163         /**
164          * Log a fatal message.
165          * @param message log message
166          * @param e optional Error to be logged
167          */
168         public void fatal(string message, Error? e = null) {
169         }
170 }
171
172 #endif
173