1 /** 2 * Copyright © DiamondMVC 2018 3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE) 4 * Author: Jacob Jensen (bausshf) 5 */ 6 module diamond.core.debugging; 7 8 version (Diamond_Debug) 9 { 10 /// Boolean determining whether application is being debugged or not. 11 static const bool debugging = true; 12 } 13 else 14 { 15 /// Boolean determining whether application is being debugged or not. 16 static const bool debugging = false; 17 } 18 19 static if (debugging) 20 { 21 public import diamond.core.io; 22 23 /// A stack logger wrapper. 24 class StackLogger 25 { 26 private: 27 /// The call stack of the logger. 28 string[] _callStack; 29 30 /// Creates a new stack logger. 31 this() {} 32 33 public: 34 /// Gets the current callstack. 35 @property const(string[]) callStack() { return _callStack; } 36 37 /// Logs the current call. 38 void log(in string file = __FILE__, in size_t line = __LINE__, in string mod = __MODULE__, in string func = __PRETTY_FUNCTION__) 39 { 40 import std..string : format; 41 42 _callStack ~= format("file: '%s' line: '%s' module: '%s' function: '%s'", file, line, mod, func); 43 } 44 45 /// Clears the call stack. 46 void clear() 47 { 48 _callStack = null; 49 } 50 51 /// Prints the call stack. 52 void printCallStack() 53 { 54 print(_callStack); 55 } 56 } 57 58 /// The thread-local stack logger. 59 private StackLogger _stackLogger; 60 61 /// Gets the stack logger for the current thread. 62 @property StackLogger stackLogger() 63 { 64 if (_stackLogger) 65 { 66 return _stackLogger; 67 } 68 69 _stackLogger = new StackLogger; 70 71 return _stackLogger; 72 } 73 }