1 module gogga; 2 3 import std.conv : to; 4 import std.stdio : write, stdout; 5 import core.sync.mutex : Mutex; 6 7 public enum DebugType 8 { 9 INFO, 10 WARNING, 11 ERROR 12 } 13 14 private __gshared Mutex writeMutex; 15 16 /* Initialize the module (only once, regardless of threads) */ 17 shared static this() 18 { 19 /* Initialize the mutex */ 20 writeMutex = new Mutex(); 21 } 22 23 void gprint(messageT)(messageT message, DebugType debugType = DebugType.INFO) 24 { 25 /* TODO: Remove mutex, oneshot write */ 26 27 /* Lock output */ 28 writeMutex.lock(); 29 30 /* If INFO, set green */ 31 if(debugType == DebugType.INFO) 32 { 33 stdout.rawWrite(cast(byte[])[27, '[','3','2','m']); 34 } 35 /* If WARNING, set warning */ 36 else if(debugType == DebugType.WARNING) 37 { 38 stdout.rawWrite(cast(byte[])[27, '[','3','5','m']); /* TODO: FInd yllow */ 39 } 40 /* If ERROR, set error */ 41 else if(debugType == DebugType.ERROR) 42 { 43 stdout.rawWrite(cast(byte[])[27, '[','3','1','m']); 44 } 45 46 /* Write the message type */ 47 write("["~to!(string)(debugType)~"] "); 48 49 /* Switch back color */ 50 stdout.rawWrite(cast(byte[])[27, '[', '3', '9', 'm']); 51 52 /* Print the message */ 53 write(message); 54 55 /* Unlock output */ 56 writeMutex.unlock(); 57 } 58 59 void gprintln(messageT)(messageT message, DebugType debugType = DebugType.INFO) 60 { 61 /* Generate the string to print */ 62 string printStr = to!(string)(message)~"\n"; 63 64 /* Call `gprint` */ 65 gprint(printStr, debugType); 66 }