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
193 	 *
194 	 * Params:
195 	 *   segments = the arbitrary argumnets (alias sequence)
196 	 *   __FILE_FULL_PATH__ = compile time usage file
197 	 *   __FILE__ = compile time usage file (relative)
198 	 *   __LINE__ = compile time usage line number
199 	 *   __MODULE__ = compile time usage module
200 	 *   __FUNCTION__ = compile time usage function
201 	 *   __PRETTY_FUNCTION__ = compile time usage function (pretty)
202 	 */
203 	public final void debug_(TextType...)(TextType segments,
204 									string c1 = __FILE_FULL_PATH__,
205 									string c2 = __FILE__, ulong c3 = __LINE__,
206 									string c4 = __MODULE__, string c5 = __FUNCTION__,
207 									string c6 = __PRETTY_FUNCTION__)
208 	{
209 		/* Only debug if debugging is enabled */
210 		if(debugEnabled)
211 		{
212 			/* Use the context `GoggaContext` */
213 			GoggaContext defaultContext = new GoggaContext();
214 
215 			/* Build up the line information */
216 			CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
217 
218 			/* Set the line information in the context */
219 			defaultContext.setLineInfo(compilationInfo);
220 
221 			/* Set the level to DEBUG */
222 			defaultContext.setLevel(Level.DEBUG);
223 
224 			/**
225 			* Grab at compile-time all arguments and generate runtime code to add them to `components`
226 			*/		
227 			string[] components = flatten(segments);
228 
229 			/* Join all `components` into a single string */
230 			string messageOut = join(components, multiArgJoiner);
231 
232 			/* Call the log */
233 			logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6);
234 		}
235 	}
236 
237 	/* You can also call using `dbg` */
238 	public alias dbg = debug_;
239 
240 	/** 
241 	 * Enables debug prints
242 	 */
243     public void enableDebug()
244     {
245         this.debugEnabled = true;
246 	}
247 
248 	/** 
249 	 * Disables debug prints
250 	 */
251     public void disableDebug()
252     {
253         this.debugEnabled = false;
254     }
255 }
256 
257 version(unittest)
258 {
259 	import std.stdio : writeln;
260 }
261 
262 unittest
263 {
264     GoggaLogger gLogger = new GoggaLogger();
265 
266 	// Test the normal modes
267     gLogger.info("This is an info message");
268     gLogger.warn("This is a warning message");
269     gLogger.error("This is an error message");
270 
271 	// We shouldn't see anything as debug is off
272 	gLogger.dbg("This is a debug which is hidden", 1);
273 
274 	// Now enable debugging and you should see it
275 	gLogger.enableDebug();
276 	gLogger.dbg("This is a VISIBLE debug", true);
277 
278     // Make space between unit tests
279 	writeln();
280 }
281 
282 unittest
283 {
284     GoggaLogger gLogger = new GoggaLogger();
285 	gLogger.mode(GoggaMode.TwoKTwenty3);
286 
287     // Test the normal modes
288     gLogger.info("This is an info message");
289     gLogger.warn("This is a warning message");
290     gLogger.error("This is an error message");
291 
292 	// We shouldn't see anything as debug is off
293 	gLogger.dbg("This is a debug which is hidden", 1);
294 
295 	// Now enable debugging and you should see it
296 	gLogger.enableDebug();
297 	gLogger.dbg("This is a VISIBLE debug", true);
298 
299     // Make space between unit tests
300 	writeln();
301 }
302 
303 unittest
304 {
305     GoggaLogger gLogger = new GoggaLogger();
306 	gLogger.mode(GoggaMode.SIMPLE);
307 
308     // Test the normal modes
309     gLogger.info("This is an info message");
310     gLogger.warn("This is a warning message");
311     gLogger.error("This is an error message");
312 
313 	// We shouldn't see anything as debug is off
314 	gLogger.dbg("This is a debug which is hidden", 1);
315 
316 	// Now enable debugging and you should see it
317 	gLogger.enableDebug();
318 	gLogger.dbg("This is a VISIBLE debug", true);
319 
320     // Make space between unit tests
321 	writeln();
322 }
323 
324 unittest
325 {
326     GoggaLogger gLogger = new GoggaLogger();
327 	gLogger.mode(GoggaMode.RUSTACEAN);
328 
329     // Test the normal modes
330     gLogger.info("This is an info message");
331     gLogger.warn("This is a warning message");
332     gLogger.error("This is an error message");
333 
334 	// We shouldn't see anything as debug is off
335 	gLogger.dbg("This is a debug which is hidden", 1);
336 
337 	// Now enable debugging and you should see it
338 	gLogger.enableDebug();
339 	gLogger.dbg("This is a VISIBLE debug", true);
340 
341     // Make space between unit tests
342 	writeln();
343 }