1
0
mirror of https://github.com/deavmi/birchwood synced 2024-09-20 11:22:53 +02:00

Upgrade/logging (#44)

* Dub

- Switched to `gogga` for logging

* birchwood.logging

- Added logging facilities

* Client

- Use new logging facility

* Receiver

- Use new logging facility

* Messages

- Use new logging facility

* Messages

- Cleaned up

* Dub

- Bumped

* Dub

- Bumped

* Revert "Dub"

This reverts commit a7049d1d87.

* Reapply "Dub"

This reverts commit 44b9b19828.
This commit is contained in:
Tristan B. Velloza Kildaire 2024-05-12 18:08:40 +02:00 committed by GitHub
parent 23b5b3944b
commit b1e3f24fdd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 91 additions and 63 deletions

View File

@ -5,7 +5,7 @@
], ],
"copyright": "Copyright © 2023, Tristan B. Kildaire", "copyright": "Copyright © 2023, Tristan B. Kildaire",
"dependencies": { "dependencies": {
"dlog": ">=0.3.19", "gogga" : ">=3.1.1",
"eventy": ">=0.4.0" "eventy": ">=0.4.0"
}, },
"description": "A sane IRC framework for the D language", "description": "A sane IRC framework for the D language",

View File

@ -19,14 +19,7 @@ import birchwood.protocol.constants : ReplyType;
import birchwood.client.receiver : ReceiverThread; import birchwood.client.receiver : ReceiverThread;
import birchwood.client.sender : SenderThread; import birchwood.client.sender : SenderThread;
import birchwood.client.events; import birchwood.client.events;
import birchwood.logging;
import dlog;
package __gshared Logger logger;
__gshared static this()
{
logger = new DefaultLogger();
}
// TODO: Make abstract and for unit tests make a `DefaultClient` // TODO: Make abstract and for unit tests make a `DefaultClient`
// ... which logs outputs for the `onX()` handler functions // ... which logs outputs for the `onX()` handler functions
@ -136,7 +129,7 @@ public class Client : Thread
public void onChannelMessage(Message fullMessage, string channel, string msgBody) public void onChannelMessage(Message fullMessage, string channel, string msgBody)
{ {
/* Default implementation */ /* Default implementation */
logger.log("Channel("~channel~"): "~msgBody); DEBUG("Channel("~channel~"): "~msgBody);
} }
/** /**
@ -150,7 +143,7 @@ public class Client : Thread
public void onDirectMessage(Message fullMessage, string nickname, string msgBody) public void onDirectMessage(Message fullMessage, string nickname, string msgBody)
{ {
/* Default implementation */ /* Default implementation */
logger.log("DirectMessage("~nickname~"): "~msgBody); DEBUG("DirectMessage("~nickname~"): "~msgBody);
} }
/** /**
@ -162,7 +155,7 @@ public class Client : Thread
public void onGenericCommand(Message message) public void onGenericCommand(Message message)
{ {
/* Default implementation */ /* Default implementation */
logger.log("Generic("~message.getCommand()~", "~message.getFrom()~"): "~message.getParams()); DEBUG("Generic("~message.getCommand()~", "~message.getFrom()~"): "~message.getParams());
} }
// TODO: Hook certain ones default style with an implemenation // TODO: Hook certain ones default style with an implemenation
@ -179,29 +172,29 @@ public class Client : Thread
// ... state // ... state
/* Default implementation */ /* Default implementation */
logger.log("Response("~to!(string)(commandReply.getReplyType())~", "~commandReply.getFrom()~"): "~commandReply.toString()); DEBUG("Response("~to!(string)(commandReply.getReplyType())~", "~commandReply.getFrom()~"): "~commandReply.toString());
if(commandReply.getReplyType() == ReplyType.RPL_ISUPPORT) if(commandReply.getReplyType() == ReplyType.RPL_ISUPPORT)
{ {
// TODO: Testing code was here // TODO: Testing code was here
// logger.log(); // DEBUG();
// logger.log("<<<>>>"); // DEBUG("<<<>>>");
// logger.log("Take a look:\n\n"~commandReply.getParams()); // DEBUG("Take a look:\n\n"~commandReply.getParams());
// logger.log("And here is key-value pairs: ", commandReply.getKVPairs()); // DEBUG("And here is key-value pairs: ", commandReply.getKVPairs());
// logger.log("And here is array: ", commandReply.getPairs()); // DEBUG("And here is array: ", commandReply.getPairs());
// // TODO: DLog bug, this prints nothing // // TODO: DLog bug, this prints nothing
// logger.log("And here is trailing: ", commandReply.getTrailing()); // DEBUG("And here is trailing: ", commandReply.getTrailing());
// import std.stdio; // import std.stdio;
// writeln("Trailer: "~commandReply.getTrailing()); // writeln("Trailer: "~commandReply.getTrailing());
// writeln(cast(ubyte[])commandReply.getTrailing()); // writeln(cast(ubyte[])commandReply.getTrailing());
// logger.log("<<<>>>"); // DEBUG("<<<>>>");
// logger.log(); // DEBUG();
import std.stdio; import std.stdio;
writeln("Support stuff: ", commandReply.getKVPairs()); writeln("Support stuff: ", commandReply.getKVPairs());
@ -213,7 +206,7 @@ public class Client : Thread
/* Update the db */ /* Update the db */
string value = receivedKV[key]; string value = receivedKV[key];
connInfo.updateDB(key, value); connInfo.updateDB(key, value);
logger.log("Updated key in db '"~key~"' with value '"~value~"'"); DEBUG("Updated key in db '"~key~"' with value '"~value~"'");
} }
} }
@ -225,7 +218,7 @@ public class Client : Thread
public void onConnectionClosed() public void onConnectionClosed()
{ {
// TODO: Add log as default behaviour? // TODO: Add log as default behaviour?
logger.log("Connection was closed, not doing anything"); DEBUG("Connection was closed, not doing anything");
} }
/** /**
@ -773,7 +766,7 @@ public class Client : Thread
assert(ircEvent); //Should never fail, unless some BOZO regged multiple handles for 1 - wait idk does eventy do that even mmm assert(ircEvent); //Should never fail, unless some BOZO regged multiple handles for 1 - wait idk does eventy do that even mmm
// NOTE: Enable this when debugging // NOTE: Enable this when debugging
// logger.log("IRCEvent(message): "~ircEvent.getMessage().toString()); // DEBUG("IRCEvent(message): "~ircEvent.getMessage().toString());
/* TODO: We should use a switch statement, imagine how nice */ /* TODO: We should use a switch statement, imagine how nice */
Message ircMessage = ircEvent.getMessage(); Message ircMessage = ircEvent.getMessage();
@ -849,7 +842,7 @@ public class Client : Thread
// string messageToSend = "PONG "~pongEvent.getID(); // string messageToSend = "PONG "~pongEvent.getID();
Message pongMessage = new Message("", "PONG", pongEvent.getID()); Message pongMessage = new Message("", "PONG", pongEvent.getID());
client.sendMessage(pongMessage); client.sendMessage(pongMessage);
logger.log("Ponged back with "~pongEvent.getID()); DEBUG("Ponged back with "~pongEvent.getID());
} }
} }
engine.addSignalHandler(new PongSignal(this)); engine.addSignalHandler(new PongSignal(this));
@ -1034,7 +1027,7 @@ public class Client : Thread
{ {
/* Set the state of running to false */ /* Set the state of running to false */
running = false; running = false;
logger.log("disconnect() begin"); DEBUG("disconnect() begin");
/* Shutdown the socket */ /* Shutdown the socket */
@ -1048,7 +1041,7 @@ public class Client : Thread
*/ */
import std.socket : SocketShutdown; import std.socket : SocketShutdown;
socket.shutdown(SocketShutdown.BOTH); socket.shutdown(SocketShutdown.BOTH);
logger.log("disconnect() socket shutdown"); DEBUG("disconnect() socket shutdown");
} }
@ -1062,20 +1055,20 @@ public class Client : Thread
{ {
/* Stop the receive queue manager and wait for it to stop */ /* Stop the receive queue manager and wait for it to stop */
receiver.end(); receiver.end();
logger.log("doThreadCleanup() recvQueue manager stopped"); DEBUG("doThreadCleanup() recvQueue manager stopped");
receiver = null; receiver = null;
/* Stop the send queue manager and wait for it to stop */ /* Stop the send queue manager and wait for it to stop */
sender.end(); sender.end();
logger.log("doThreadCleanup() sendQueue manager stopped"); DEBUG("doThreadCleanup() sendQueue manager stopped");
sender = null; sender = null;
/* TODO: Stop eventy (FIXME: I don't know if this is implemented in Eventy yet, do this!) */ /* TODO: Stop eventy (FIXME: I don't know if this is implemented in Eventy yet, do this!) */
engine.shutdown(); engine.shutdown();
logger.log("doThreadCleanup() eventy stopped"); DEBUG("doThreadCleanup() eventy stopped");
engine = null; engine = null;
logger.log("doThreadCleanup() end"); DEBUG("doThreadCleanup() end");
} }
/** /**
@ -1088,8 +1081,8 @@ public class Client : Thread
private void processMessage(ubyte[] message) private void processMessage(ubyte[] message)
{ {
// import std.stdio; // import std.stdio;
// logger.log("Message length: "~to!(string)(message.length)); // DEBUG("Message length: "~to!(string)(message.length));
// logger.log("InterpAsString: "~cast(string)message); // DEBUG("InterpAsString: "~cast(string)message);
receiveQ(message); receiveQ(message);
} }

View File

@ -21,6 +21,7 @@ version(unittest)
{ {
import std.stdio : writeln; import std.stdio : writeln;
} }
import birchwood.logging;
/** /**
* Manages the receive queue and performs * Manages the receive queue and performs
@ -150,7 +151,7 @@ public final class ReceiverThread : Thread
*/ */
if(pingMessage !is null) if(pingMessage !is null)
{ {
logger.log("Found a ping: "~pingMessage.toString()); DEBUG("Found a ping: "~pingMessage.toString());
/* Extract the PING ID */ /* Extract the PING ID */
string pingID = pingMessage.getParams(); string pingID = pingMessage.getParams();

View File

@ -0,0 +1,56 @@
/**
* Internal logging facilities
*/
module birchwood.logging;
import gogga;
import gogga.extras;
import dlog.basic : Level, FileHandler;
import std.stdio : stdout;
/**
* Globally available logger
*/
package __gshared GoggaLogger logger;
/**
* Initializes a logger instance
* globally
*/
__gshared static this()
{
logger = new GoggaLogger();
GoggaMode mode;
// TODO: Add flag support
version(DBG_VERBOSE_LOGGING)
{
mode = GoggaMode.RUSTACEAN;
}
else
{
mode = GoggaMode.SIMPLE;
}
logger.mode(mode);
Level level = Level.DEBUG;
// TODO: Add flag support
// version(DBG_DEBUG_LOGGING)
// {
// level = Level.DEBUG;
// }
// else
// {
// level = Level.INFO;
// }
logger.setLevel(level);
logger.addHandler(new FileHandler(stdout));
}
// Bring in helper methods
mixin LoggingFuncs!(logger);

View File

@ -11,32 +11,10 @@ import birchwood.protocol.constants : ReplyType;
import birchwood.client.exceptions; import birchwood.client.exceptions;
import birchwood.config.conninfo : ChecksMode; import birchwood.config.conninfo : ChecksMode;
import birchwood.logging;
// TODO: Before release we should remove this import
import std.stdio : writeln;
/* TODO: We could move these all to `package.d` */ /* TODO: We could move these all to `package.d` */
/* Static is redundant as module is always static , gshared needed */
/* Apparebky works without gshared, that is kinda sus ngl */
package __gshared Logger logger;
/**
* source/birchwood/messages.d(10,8): Error: variable `birchwood.messages.logger` is a thread-local class and cannot have a static initializer. Use `static this()` to initialize instead.
*
* It is complaining that it wopuld static init per thread, static this() for module is required but that would
* do a module init per thread, so __gshared static this() is needed, we want one global init - a single logger
* variable and also class init
*/
__gshared static this()
{
logger = new DefaultLogger();
}
/**
* Encoding/decoding primitives
*/
/** /**
* Encodes the provided message into a CRLF * Encodes the provided message into a CRLF
* terminated byte array * terminated byte array
@ -140,8 +118,8 @@ public final class Message
} }
catch(ConvException e) catch(ConvException e)
{ {
logger.log("<<< Unsupported response code (Error below) >>>"); DEBUG("<<< Unsupported response code (Error below) >>>");
logger.log(e); DEBUG(e);
} }
} }
@ -286,7 +264,7 @@ public final class Message
{ {
from = message[1..firstSpace]; from = message[1..firstSpace];
// logger.log("from: "~from); // DEBUG("from: "~from);
/* TODO: Find next space (what follows `from` is `' ' { ' ' }`) */ /* TODO: Find next space (what follows `from` is `' ' { ' ' }`) */
ulong i = firstSpace; ulong i = firstSpace;
@ -306,7 +284,7 @@ public final class Message
/* Extract the command */ /* Extract the command */
command = rem[0..idx]; command = rem[0..idx];
// logger.log("command: "~command); // DEBUG("command: "~command);
/* Params are everything till the end */ /* Params are everything till the end */
i = idx; i = idx;
@ -318,12 +296,12 @@ public final class Message
} }
} }
params = rem[i..rem.length]; params = rem[i..rem.length];
// logger.log("params: "~params); // DEBUG("params: "~params);
} }
else else
{ {
//TODO: handle //TODO: handle
logger.log("Malformed message start after :"); DEBUG("Malformed message start after :");
assert(false); assert(false);
} }