1 /** 2 * Context for logging 3 */ 4 5 module dlog.context; 6 7 import std.conv : to; 8 9 /** 10 * Debugging trace level 11 */ 12 public enum Level 13 { 14 /** 15 * Informative message 16 */ 17 INFO, 18 19 /** 20 * Warning message 21 */ 22 WARN, 23 24 /** 25 * Error message 26 */ 27 ERROR, 28 29 /** 30 * Debugging message 31 */ 32 DEBUG 33 } 34 35 /** 36 * Context that is passed in with the call to log 37 */ 38 public class Context 39 { 40 private CompilationInfo lineInfo; 41 private Level level; 42 43 /** 44 * Constructs a new Context 45 */ 46 this() 47 { 48 49 } 50 51 /** 52 * Set the line information 53 * 54 * Params: 55 * lineInfo = the CompilationInfo struct to use 56 */ 57 public final void setLineInfo(CompilationInfo lineInfo) 58 { 59 this.lineInfo = lineInfo; 60 } 61 62 /** 63 * Obtain the line information generated at compilation 64 * time 65 * 66 * Returns: the CompilationInfo struct 67 */ 68 public final CompilationInfo getLineInfo() 69 { 70 return lineInfo; 71 } 72 73 /** 74 * Returns the current tarce level 75 * 76 * Returns: the Level 77 */ 78 public final Level getLevel() 79 { 80 return level; 81 } 82 83 /** 84 * Set the trace level 85 * 86 * Params: 87 * level = the Level to use 88 */ 89 public final void setLevel(Level level) 90 { 91 this.level = level; 92 } 93 } 94 95 /** 96 * Information obtained during compilation time (if any) 97 */ 98 public struct CompilationInfo 99 { 100 /** 101 * compile time usage file 102 */ 103 public string fullFilePath; 104 105 /** 106 * compile time usage file (relative) 107 */ 108 public string file; 109 110 /** 111 * compile time usage line number 112 */ 113 public ulong line; 114 115 /** 116 * compile time usage module 117 */ 118 public string moduleName; 119 120 /** 121 * compile time usage function 122 */ 123 public string functionName; 124 125 /** 126 * compile time usage function (pretty) 127 */ 128 public string prettyFunctionName; 129 130 /** 131 * Constructs the compilation information with the provided 132 * parameters 133 * 134 * Params: 135 * __FILE_FULL_PATH__ = compile time usage file 136 * __FILE__ = compile time usage file (relative) 137 * __LINE__ = compile time usage line number 138 * __MODULE__ = compile time usage module 139 * __FUNCTION__ = compile time usage function 140 * __PRETTY_FUNCTION__ = compile time usage function (pretty) 141 */ 142 this(string fullFilePath, string file, ulong line, string moduleName, string functionName, string prettyFunctionName) 143 { 144 this.fullFilePath = fullFilePath; 145 this.file = file; 146 this.line = line; 147 this.moduleName = moduleName; 148 this.functionName = functionName; 149 this.prettyFunctionName = prettyFunctionName; 150 } 151 152 /** 153 * Flattens the known compile-time information into a string array 154 * 155 * Returns: a string[] 156 */ 157 public string[] toArray() 158 { 159 return [fullFilePath, file, to!(string)(line), moduleName, functionName, prettyFunctionName]; 160 } 161 }