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