1 module polyplex.utils.logging; 2 import polyplex.utils.strutils; 3 import std.conv; 4 import std.array; 5 import std.stdio; 6 import core.vararg; 7 8 public enum LogType { 9 Off = 0x00, 10 Info = 0x01, 11 Success = 0x02, 12 Warning = 0x04, 13 Error = 0x08, 14 Fatal = 0x10, 15 Recover = 0x20, 16 Debug = 0x40, 17 VerboseDebug = 0x80 18 } 19 20 public static LogType LogLevel = LogType.Info | LogType.Recover | LogType.Success | LogType.Warning | LogType.Error | LogType.Fatal; 21 public class Logger { 22 23 /** 24 Allows you to put a log of LogType type with an simple string message. 25 */ 26 public static void Log(string message, LogType type = LogType.Info) { 27 Log(message, type, null); 28 } 29 30 /** 31 Log a verbose debug message. <No extra parameters> 32 */ 33 public static void VerboseDebug (string message) { 34 Logger.VerboseDebug(message, null); 35 } 36 37 /** 38 Log a debug message. <No extra parameters> 39 */ 40 public static void Debug (string message) { 41 Logger.Debug(message, null); 42 } 43 44 /** 45 Log a success message. <No extra parameters> 46 */ 47 public static void Success (string message) { 48 Logger.Success(message, null); 49 } 50 51 /** 52 Log an info message. <No extra parameters> 53 */ 54 public static void Info (string message) { 55 Logger.Info(message, null); 56 } 57 58 /** 59 Log a warning message. <No extra parameters> 60 */ 61 public static void Warn (string message) { 62 Logger.Warn(message, null); 63 } 64 65 /** 66 Log a Error message. <No extra parameters> 67 */ 68 public static void Err (string message) { 69 Logger.Err(message, null); 70 } 71 72 /** 73 Log a Fatal Error message. <No extra parameters> 74 */ 75 public static void Fatal (string message) { 76 Logger.Fatal(message, null); 77 } 78 79 /** 80 Log an Error Recovery message. <No extra parameters> 81 */ 82 public static void Recover (string message) { 83 Logger.Recover(message, null); 84 } 85 86 /** 87 Log a verbose debug message. 88 */ 89 public static void VerboseDebug(T...) (string message, T args) { 90 Logger.Log(message, LogType.VerboseDebug, args); 91 } 92 93 /** 94 Log a debug message. 95 */ 96 public static void Debug(T...) (string message, T args) { 97 Logger.Log(message, LogType.Debug, args); 98 } 99 100 /** 101 Log an info message. 102 */ 103 public static void Info(T...) (string message, T args) { 104 Logger.Log(message, LogType.Info, args); 105 } 106 107 /** 108 Log a success message. 109 */ 110 public static void Success(T...) (string message, T args) { 111 Logger.Log(message, LogType.Success, args); 112 } 113 114 /** 115 Log a warning message. 116 */ 117 public static void Warn(T...) (string message, T args) { 118 Logger.Log(message, LogType.Warning, args); 119 } 120 121 /** 122 Log an Error message. 123 */ 124 public static void Err(T...) (string message, T args) { 125 Logger.Log(message, LogType.Error, args); 126 } 127 128 /** 129 Log a Fatal Error message. 130 */ 131 public static void Fatal(T...) (string message, T args) { 132 Logger.Log(message, LogType.Fatal, args); 133 } 134 135 /** 136 Log a Error Recovery message. 137 */ 138 public static void Recover(T...) (string message, T args) { 139 Logger.Log(message, LogType.Recover, args); 140 } 141 142 /* Raw impl */ 143 144 public static void Log(T...) (string message, LogType type, T args) { 145 bool color = false; 146 if ((LogLevel != LogType.Off && (LogLevel & type)) || (type == LogType.Fatal || type == LogType.Recover)) { 147 import colorize : fg, color, cwriteln; 148 string stxt = to!string(type); 149 if (type == LogType.VerboseDebug) stxt = stxt.color(fg.cyan); 150 if (type == LogType.Debug) stxt = stxt.color(fg.blue); 151 if (type == LogType.Info) stxt = stxt.color(fg.light_black); 152 if (type == LogType.Success) stxt = stxt.color(fg.light_green); 153 if (type == LogType.Warning) stxt = stxt.color(fg.yellow); 154 if (type == LogType.Error) stxt = stxt.color(fg.light_red); 155 if (type == LogType.Fatal) stxt = stxt.color(fg.red); 156 if (type == LogType.Recover) stxt = stxt.color(fg.light_blue); 157 string txt = "<".color(fg.light_black) ~ stxt ~ ">".color(fg.light_black); 158 159 cwriteln(txt, " ", Format(message, args)); 160 } 161 if (type == LogType.Fatal) throw new Exception(Format(message, args)); 162 163 } 164 165 }