1 /** 
2  * Logging facilities
3  */
4 module gogga.core;
5 
6 import dlog;
7 import dlog.utilities : flatten;
8 import std.array : join;
9 
10 import gogga.transform;
11 import gogga.context;
12 
13 /** 
14  * The logging class which provides the logging print
15  * calls, controlling of style and whether to debug or
16  * not
17  */
18 public class GoggaLogger : Logger
19 {
20 	/** 
21 	 * The custom transformer
22 	 */
23     private GoggaTransform gTransform = new GoggaTransform();
24 
25 	/** 
26 	 * Whether debug prints are enabled or not
27 	 */
28     private bool debugEnabled = false;
29 
30 	/** 
31 	 * Constructs a new GoggaLOgger
32 	 */
33     this()
34     {
35         super(gTransform);
36     }
37     
38 	/** 
39 	 * Our underlying logging implementation
40 	 *
41 	 * Params:
42 	 *   text = the text to write
43 	 */
44     public override void logImpl(string text)
45     {
46         import std.stdio : write;
47         write(text);
48     }
49 
50 	/** 
51 	 * Set the style of print outs
52 	 *
53 	 * Params:
54 	 *   mode = the GoggaMode wanted
55 	 */
56 	public void mode(GoggaMode mode)
57 	{
58 		gTransform.setMode(mode);
59 	}
60 
61     /** 
62 	 * Logs using the default context an arbitrary amount of arguments
63 	 * specifically setting the context's level to ERROR
64 	 *
65 	 * Params:
66 	 *   segments = the arbitrary argumnets (alias sequence)
67 	 *   __FILE_FULL_PATH__ = compile time usage file
68 	 *   __FILE__ = compile time usage file (relative)
69 	 *   __LINE__ = compile time usage line number
70 	 *   __MODULE__ = compile time usage module
71 	 *   __FUNCTION__ = compile time usage function
72 	 *   __PRETTY_FUNCTION__ = compile time usage function (pretty)
73 	 */
74 	public final void error(TextType...)(TextType segments,
75 									string c1 = __FILE_FULL_PATH__,
76 									string c2 = __FILE__, ulong c3 = __LINE__,
77 									string c4 = __MODULE__, string c5 = __FUNCTION__,
78 									string c6 = __PRETTY_FUNCTION__)
79 	{
80 		/* Use the context `GoggaContext` */
81 		GoggaContext defaultContext = new GoggaContext();
82 
83 		/* Build up the line information */
84 		CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
85 
86 		/* Set the line information in the context */
87 		defaultContext.setLineInfo(compilationInfo);
88 
89 		/* Set the level to ERROR */
90 		defaultContext.setLevel(Level.ERROR);
91 
92 		/**
93 		 * Grab at compile-time all arguments and generate runtime code to add them to `components`
94 		 */		
95 		string[] components = flatten(segments);
96 
97 		/* Join all `components` into a single string */
98 		string messageOut = join(components, multiArgJoiner);
99 
100 		/* Call the log */
101 		logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6);
102 	}
103 
104 	/** 
105 	 * Logs using the default context an arbitrary amount of arguments
106 	 * specifically setting the context's level to INFO
107 	 *
108 	 * Params:
109 	 *   segments = the arbitrary argumnets (alias sequence)
110 	 *   __FILE_FULL_PATH__ = compile time usage file
111 	 *   __FILE__ = compile time usage file (relative)
112 	 *   __LINE__ = compile time usage line number
113 	 *   __MODULE__ = compile time usage module
114 	 *   __FUNCTION__ = compile time usage function
115 	 *   __PRETTY_FUNCTION__ = compile time usage function (pretty)
116 	 */
117 	public final void info(TextType...)(TextType segments,
118 									string c1 = __FILE_FULL_PATH__,
119 									string c2 = __FILE__, ulong c3 = __LINE__,
120 									string c4 = __MODULE__, string c5 = __FUNCTION__,
121 									string c6 = __PRETTY_FUNCTION__)
122 	{
123 		/* Use the context `GoggaContext` */
124 		GoggaContext defaultContext = new GoggaContext();
125 
126 		/* Build up the line information */
127 		CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
128 
129 		/* Set the line information in the context */
130 		defaultContext.setLineInfo(compilationInfo);
131 
132 		/* Set the level to INFO */
133 		defaultContext.setLevel(Level.INFO);
134 
135 		/**
136 		 * Grab at compile-time all arguments and generate runtime code to add them to `components`
137 		 */		
138 		string[] components = flatten(segments);
139 
140 		/* Join all `components` into a single string */
141 		string messageOut = join(components, multiArgJoiner);
142 
143 		/* Call the log */
144 		logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6);
145 	}
146 
147 	/** 
148 	 * Logs using the default context an arbitrary amount of arguments
149 	 * specifically setting the context's level to WARN
150 	 *
151 	 * Params:
152 	 *   segments = the arbitrary argumnets (alias sequence)
153 	 *   __FILE_FULL_PATH__ = compile time usage file
154 	 *   __FILE__ = compile time usage file (relative)
155 	 *   __LINE__ = compile time usage line number
156 	 *   __MODULE__ = compile time usage module
157 	 *   __FUNCTION__ = compile time usage function
158 	 *   __PRETTY_FUNCTION__ = compile time usage function (pretty)
159 	 */
160 	public final void warn(TextType...)(TextType segments,
161 									string c1 = __FILE_FULL_PATH__,
162 									string c2 = __FILE__, ulong c3 = __LINE__,
163 									string c4 = __MODULE__, string c5 = __FUNCTION__,
164 									string c6 = __PRETTY_FUNCTION__)
165 	{
166 		/* Use the context `GoggaContext` */
167 		GoggaContext defaultContext = new GoggaContext();
168 
169 		/* Build up the line information */
170 		CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
171 
172 		/* Set the line information in the context */
173 		defaultContext.setLineInfo(compilationInfo);
174 
175 		/* Set the level to WARN */
176 		defaultContext.setLevel(Level.WARN);
177 
178 		/**
179 		 * Grab at compile-time all arguments and generate runtime code to add them to `components`
180 		 */		
181 		string[] components = flatten(segments);
182 
183 		/* Join all `components` into a single string */
184 		string messageOut = join(components, multiArgJoiner);
185 
186 		/* Call the log */
187 		logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6);
188 	}
189 
190 	/** 
191 	 * Logs using the default context an arbitrary amount of arguments
192 	 * specifically setting the context's level to DEBUG and will
193 	 * only print if debugging is enabled
194 	 *
195 	 * Params:
196 	 *   segments = the arbitrary argumnets (alias sequence)
197 	 *   __FILE_FULL_PATH__ = compile time usage file
198 	 *   __FILE__ = compile time usage file (relative)
199 	 *   __LINE__ = compile time usage line number
200 	 *   __MODULE__ = compile time usage module
201 	 *   __FUNCTION__ = compile time usage function
202 	 *   __PRETTY_FUNCTION__ = compile time usage function (pretty)
203 	 */
204 	public final void debug_(TextType...)(TextType segments,
205 									string c1 = __FILE_FULL_PATH__,
206 									string c2 = __FILE__, ulong c3 = __LINE__,
207 									string c4 = __MODULE__, string c5 = __FUNCTION__,
208 									string c6 = __PRETTY_FUNCTION__)
209 	{
210 		/* Only debug if debugging is enabled */
211 		if(debugEnabled)
212 		{
213 			/* Use the context `GoggaContext` */
214 			GoggaContext defaultContext = new GoggaContext();
215 
216 			/* Build up the line information */
217 			CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
218 
219 			/* Set the line information in the context */
220 			defaultContext.setLineInfo(compilationInfo);
221 
222 			/* Set the level to DEBUG */
223 			defaultContext.setLevel(Level.DEBUG);
224 
225 			/**
226 			* Grab at compile-time all arguments and generate runtime code to add them to `components`
227 			*/		
228 			string[] components = flatten(segments);
229 
230 			/* Join all `components` into a single string */
231 			string messageOut = join(components, multiArgJoiner);
232 
233 			/* Call the log */
234 			logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6);
235 		}
236 	}
237 
238 	/** 
239 	 * Alias for debug_
240 	 */
241 	public alias dbg = debug_;
242 
243 	/** 
244 	 * Enables debug prints
245 	 */
246     public void enableDebug()
247     {
248         this.debugEnabled = true;
249 	}
250 
251 	/** 
252 	 * Disables debug prints
253 	 */
254     public void disableDebug()
255     {
256         this.debugEnabled = false;
257     }
258 }
259 
260 version(unittest)
261 {
262 	import std.stdio : writeln;
263 }
264 
265 unittest
266 {
267     GoggaLogger gLogger = new GoggaLogger();
268 
269 	// Test the normal modes
270     gLogger.info("This is an info message");
271     gLogger.warn("This is a warning message");
272     gLogger.error("This is an error message");
273 
274 	// We shouldn't see anything as debug is off
275 	gLogger.dbg("This is a debug which is hidden", 1);
276 
277 	// Now enable debugging and you should see it
278 	gLogger.enableDebug();
279 	gLogger.dbg("This is a VISIBLE debug", true);
280 
281     // Make space between unit tests
282 	writeln();
283 }
284 
285 unittest
286 {
287     GoggaLogger gLogger = new GoggaLogger();
288 	gLogger.mode(GoggaMode.TwoKTwenty3);
289 
290     // Test the normal modes
291     gLogger.info("This is an info message");
292     gLogger.warn("This is a warning message");
293     gLogger.error("This is an error message");
294 
295 	// We shouldn't see anything as debug is off
296 	gLogger.dbg("This is a debug which is hidden", 1);
297 
298 	// Now enable debugging and you should see it
299 	gLogger.enableDebug();
300 	gLogger.dbg("This is a VISIBLE debug", true);
301 
302     // Make space between unit tests
303 	writeln();
304 }
305 
306 unittest
307 {
308     GoggaLogger gLogger = new GoggaLogger();
309 	gLogger.mode(GoggaMode.SIMPLE);
310 
311     // Test the normal modes
312     gLogger.info("This is an info message");
313     gLogger.warn("This is a warning message");
314     gLogger.error("This is an error message");
315 
316 	// We shouldn't see anything as debug is off
317 	gLogger.dbg("This is a debug which is hidden", 1);
318 
319 	// Now enable debugging and you should see it
320 	gLogger.enableDebug();
321 	gLogger.dbg("This is a VISIBLE debug", true);
322 
323     // Make space between unit tests
324 	writeln();
325 }
326 
327 unittest
328 {
329     GoggaLogger gLogger = new GoggaLogger();
330 	gLogger.mode(GoggaMode.RUSTACEAN);
331 
332     // Test the normal modes
333     gLogger.info("This is an info message");
334     gLogger.warn("This is a warning message");
335     gLogger.error("This is an error message");
336 
337 	// We shouldn't see anything as debug is off
338 	gLogger.dbg("This is a debug which is hidden", 1);
339 
340 	// Now enable debugging and you should see it
341 	gLogger.enableDebug();
342 	gLogger.dbg("This is a VISIBLE debug", true);
343 
344     // Make space between unit tests
345 	writeln();
346 }