Removed need for mutex

Added generator function
This commit is contained in:
Tristan B. Kildaire 2021-03-17 09:59:43 +02:00
parent 344df2bce9
commit 89df29f7a1
1 changed files with 17 additions and 24 deletions

View File

@ -2,7 +2,6 @@ module gogga;
import std.conv : to; import std.conv : to;
import std.stdio : write, stdout; import std.stdio : write, stdout;
import core.sync.mutex : Mutex;
public enum DebugType public enum DebugType
{ {
@ -11,49 +10,43 @@ public enum DebugType
ERROR ERROR
} }
private __gshared Mutex writeMutex; byte[] generateMessage(string message, DebugType debugType)
/* Initialize the module (only once, regardless of threads) */
shared static this()
{ {
/* Initialize the mutex */ /* The generated message */
writeMutex = new Mutex(); byte[] messageBytes;
}
void gprint(messageT)(messageT message, DebugType debugType = DebugType.INFO)
{
/* TODO: Remove mutex, oneshot write */
/* Lock output */
writeMutex.lock();
/* If INFO, set green */ /* If INFO, set green */
if(debugType == DebugType.INFO) if(debugType == DebugType.INFO)
{ {
stdout.rawWrite(cast(byte[])[27, '[','3','2','m']); messageBytes = cast(byte[])[27, '[','3','2','m'];
} }
/* If WARNING, set warning */ /* If WARNING, set warning */
else if(debugType == DebugType.WARNING) else if(debugType == DebugType.WARNING)
{ {
stdout.rawWrite(cast(byte[])[27, '[','3','5','m']); /* TODO: FInd yllow */ messageBytes = cast(byte[])[27, '[','3','5','m']; /* TODO: FInd yllow */
} }
/* If ERROR, set error */ /* If ERROR, set error */
else if(debugType == DebugType.ERROR) else if(debugType == DebugType.ERROR)
{ {
stdout.rawWrite(cast(byte[])[27, '[','3','1','m']); messageBytes = cast(byte[])[27, '[','3','1','m'];
} }
/* Write the message type */ /* Write the message type */
write("["~to!(string)(debugType)~"] "); messageBytes ~= "["~to!(string)(debugType)~"] ";
/* Switch back color */ /* Switch back color */
stdout.rawWrite(cast(byte[])[27, '[', '3', '9', 'm']); messageBytes ~= cast(byte[])[27, '[', '3', '9', 'm'];
return messageBytes;
}
void gprint(messageT)(messageT message, DebugType debugType = DebugType.INFO)
{
/* Generate the message */
byte[] messageBytes = generateMessage(message, debugType);
/* Print the message */ /* Print the message */
write(message); write(messageBytes);
/* Unlock output */
writeMutex.unlock();
} }
void gprintln(messageT)(messageT message, DebugType debugType = DebugType.INFO) void gprintln(messageT)(messageT message, DebugType debugType = DebugType.INFO)