Skip to content

Commit

Permalink
Logging Refactor + Event IDs (#57)
Browse files Browse the repository at this point in the history
* Logging Refactor + Event IDs

* x1000

* Fixes

* Some misses
  • Loading branch information
Cyberboss authored and AnturK committed Jun 19, 2017
1 parent 030fe93 commit 09309cc
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 56 deletions.
10 changes: 5 additions & 5 deletions TGServerService/Byond.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void UpdateToVersionImpl(object param)
{
SendMessage("BYOND: Update download failed. Does the specified version exist?");
lastError = String.Format("Download of BYOND version {0}.{1} failed! Does it exist?", vi.major, vi.minor);
TGServerService.WriteLog(String.Format("Failed to update BYOND to version {0}.{1}!", vi.major, vi.minor), EventLogEntryType.Warning);
TGServerService.WriteWarning(String.Format("Failed to update BYOND to version {0}.{1}!", vi.major, vi.minor), TGServerService.EventID.BYONDUpdateFail);
lock (ByondLock)
{
updateStat = TGByondStatus.Idle;
Expand Down Expand Up @@ -217,7 +217,7 @@ public void UpdateToVersionImpl(object param)
RequestRestart();
lastError = "Update staged. Awaiting server restart...";
SendMessage(String.Format("BYOND: Staging complete. Awaiting server restart...", vi.major, vi.minor));
TGServerService.WriteLog(String.Format("BYOND update {0}.{1} staged", vi.major, vi.minor));
TGServerService.WriteInfo(String.Format("BYOND update {0}.{1} staged", vi.major, vi.minor), TGServerService.EventID.BYONDUpdateStaged);
break;
}
}
Expand All @@ -227,7 +227,7 @@ public void UpdateToVersionImpl(object param)
}
catch (Exception e)
{
TGServerService.WriteLog("Revision staging errror: " + e.ToString(), EventLogEntryType.Error);
TGServerService.WriteError("Revision staging errror: " + e.ToString(), TGServerService.EventID.BYONDUpdateFail);
lock (ByondLock)
{
updateStat = TGByondStatus.Idle;
Expand Down Expand Up @@ -274,14 +274,14 @@ public bool ApplyStagedUpdate()
Program.DeleteDirectory(StagingDirectory);
lastError = null;
SendMessage("BYOND: Update completed!");
TGServerService.WriteLog(String.Format("BYOND update {0} completed!", GetVersion(TGByondVersion.Installed)));
TGServerService.WriteInfo(String.Format("BYOND update {0} completed!", GetVersion(TGByondVersion.Installed)), TGServerService.EventID.BYONDUpdateComplete);
return true;
}
catch (Exception e)
{
lastError = e.ToString();
SendMessage("BYOND: Update failed!");
TGServerService.WriteLog("BYOND update failed!");
TGServerService.WriteError("BYOND update failed!", TGServerService.EventID.BYONDUpdateFail);
return false;
}
finally
Expand Down
10 changes: 5 additions & 5 deletions TGServerService/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ public void InitChat(TGChatSetupInfo info = null)
ChatProvider = new TGIRCChatProvider(info);
break;
default:
TGServerService.WriteLog(String.Format("Invalid chat provider: {0}", info.Provider), EventLogEntryType.Error);
TGServerService.WriteError(String.Format("Invalid chat provider: {0}", info.Provider), TGServerService.EventID.InvalidChatProvider);
break;
}
}
catch (Exception e)
{
TGServerService.WriteLog(String.Format("Failed to start chat provider {0}! Error: {1}", info.Provider, e.ToString()), EventLogEntryType.Error);
TGServerService.WriteError(String.Format("Failed to start chat provider {0}! Error: {1}", info.Provider, e.ToString()), TGServerService.EventID.ChatProviderStartFail);
return;
}
ChatProvider.OnChatMessage += ChatProvider_OnChatMessage;
if (Properties.Settings.Default.ChatEnabled)
{
var res = ChatProvider.Connect();
if (res != null)
TGServerService.WriteLog(String.Format("Unable to connect to chat! Provider {0}, Error: {1}", ChatProvider.GetType().ToString(), res));
TGServerService.WriteWarning(String.Format("Unable to connect to chat! Provider {0}, Error: {1}", ChatProvider.GetType().ToString(), res), TGServerService.EventID.ChatConnectFail);
}
}

Expand Down Expand Up @@ -135,7 +135,7 @@ void DisposeChat()
//Do stuff with words that were spoken to us
string ChatCommand(string command, string speaker, string channel, IList<string> parameters)
{
TGServerService.WriteLog(String.Format("Chat Command from {0}: {1} {2}", speaker, command, String.Join(" ", parameters)));
TGServerService.WriteInfo(String.Format("Chat Command from {0}: {1} {2}", speaker, command, String.Join(" ", parameters)), TGServerService.EventID.ChatCommand);
switch (command)
{
case "check":
Expand Down Expand Up @@ -256,7 +256,7 @@ public TGChatSetupInfo ProviderInfo()
case TGChatProvider.IRC:
return new TGIRCSetupInfo();
default:
TGServerService.WriteLog("Invalid chat provider: " + Config.ChatProvider.ToString());
TGServerService.WriteError("Invalid chat provider: " + Config.ChatProvider.ToString(), TGServerService.EventID.InvalidChatProvider);
return null;
}

Expand Down
10 changes: 7 additions & 3 deletions TGServerService/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ void CompileImpl()
{
var errorMsg = String.Format("Could not find {0}!", dmeName);
SendMessage("DM: " + errorMsg);
TGServerService.WriteError(errorMsg, TGServerService.EventID.DMCompileCrash);
lock (CompilerLock)
{
lastCompilerError = errorMsg;
Expand Down Expand Up @@ -438,7 +439,9 @@ void CompileImpl()
}
}
}
SendMessage(String.Format("DM: Compile complete!{0}", DaemonStatus() == TGDreamDaemonStatus.Offline ? "" : " Server will update next round."));
var msg = String.Format("Compile complete!{0}", DaemonStatus() == TGDreamDaemonStatus.Offline ? "" : " Server will update next round.");
SendMessage("DM: " + msg);
TGServerService.WriteInfo(msg, TGServerService.EventID.DMCompileSuccess);
lock (CompilerLock)
{
lastCompilerError = null;
Expand All @@ -448,7 +451,7 @@ void CompileImpl()
else
{
SendMessage("DM: Compile failed!"); //Also happens for warnings
TGServerService.WriteLog("Compile error: " + OutputList.ToString(), EventLogEntryType.Warning);
TGServerService.WriteWarning("Compile error: " + OutputList.ToString(), TGServerService.EventID.DMCompileError);
lock (CompilerLock)
{
lastCompilerError = "DM compile failure";
Expand All @@ -465,7 +468,7 @@ void CompileImpl()
catch (Exception e)
{
SendMessage("DM: Compiler thread crashed!");
TGServerService.WriteLog("Compile manager errror: " + e.ToString(), EventLogEntryType.Error);
TGServerService.WriteError("Compile manager errror: " + e.ToString(), TGServerService.EventID.DMCompileCrash);
lock (CompilerLock)
{
lastCompilerError = e.ToString();
Expand All @@ -482,6 +485,7 @@ void CompileImpl()
compilerCurrentStatus = TGCompilerStatus.Initialized;
compilationCancellationRequestation = false;
SendMessage("Compile cancelled!");
TGServerService.WriteInfo("Compilation cancelled", TGServerService.EventID.DMCompileCancel);
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions TGServerService/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,10 @@ public IList<JobSetting> Jobs(out string error)
//public api
public string MoveServer(string new_location)
{
var Config = Properties.Settings.Default;
try
{
var di1 = new DirectoryInfo(Environment.CurrentDirectory);
var di1 = new DirectoryInfo(Config.ServerDirectory);
var di2 = new DirectoryInfo(new_location);

var copy = di1.Root.FullName != di2.Root.FullName;
Expand Down Expand Up @@ -335,7 +336,6 @@ public string MoveServer(string new_location)
{
if (currentStatus != TGDreamDaemonStatus.Offline)
return "Watchdog running!";
var Config = Properties.Settings.Default;
lock (configLock)
{
CleanGameFolder();
Expand All @@ -350,9 +350,10 @@ public string MoveServer(string new_location)
{
Program.DeleteDirectory(Config.ServerDirectory);
}
catch
catch (Exception e)
{
error = "The move was successful, but the path " + Config.ServerDirectory + " was unable to be deleted fully!";
TGServerService.WriteWarning(String.Format("Server move from {0} to {1} partial success: {2}", Config.ServerDirectory, new_location, e.ToString()), TGServerService.EventID.ServerMovePartial);
}
}
else
Expand All @@ -363,14 +364,14 @@ public string MoveServer(string new_location)
Directory.Move(Config.ServerDirectory, new_location);
Environment.CurrentDirectory = new_location;
}
catch (Exception e)
catch
{
Environment.CurrentDirectory = Config.ServerDirectory;
return e.ToString();
throw;
}
}
TGServerService.WriteInfo(String.Format("Server moved from {0} to {1}", Config.ServerDirectory, new_location), TGServerService.EventID.ServerMoveComplete);
Config.ServerDirectory = new_location;
TGServerService.WriteLog("Server moved to: " + new_location);
return null;
}
}
Expand All @@ -396,6 +397,7 @@ public string MoveServer(string new_location)
}
catch (Exception e)
{
TGServerService.WriteError(String.Format("Server move from {0} to {1} failed: {2}", Config.ServerDirectory, new_location, e.ToString()), TGServerService.EventID.ServerMoveFailed);
return e.ToString();
}
}
Expand Down
6 changes: 3 additions & 3 deletions TGServerService/Discord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public string SendMessage(string msg, bool adminOnly = false)
}
foreach (var I in tasks)
I.Wait();
TGServerService.WriteLog(String.Format("Discord Send ({0}): {1}", adminOnly ? "Broadcast" : "Admin", msg));
TGServerService.WriteInfo(String.Format("Discord Send{0}: {1}", adminOnly ? " (ADMIN)" : "", msg), adminOnly ? TGServerService.EventID.ChatAdminBroadcast : TGServerService.EventID.ChatBroadcast);
return null;
}
}
Expand All @@ -148,7 +148,7 @@ public string SendMessageDirect(string message, string channelname)
foreach (var J in I.TextChannels)
if (J.Id == channel)
J.SendMessage(message).Wait();
TGServerService.WriteLog(String.Format("Discord Send ({0}): {1}", channel, message));
TGServerService.WriteInfo(String.Format("Discord Send ({0}): {1}", channelname, message), TGServerService.EventID.ChatSend);
return null;
}
}
Expand All @@ -170,7 +170,7 @@ void DisconnectAndDispose()
client.Disconnect().Wait();
}
catch (Exception e) {
TGServerService.WriteLog("Discord failed DnD: " + e.ToString());
TGServerService.WriteError("Discord failed DnD: " + e.ToString(), TGServerService.EventID.ChatDisconnectFail);
}
client.Dispose();
}
Expand Down
21 changes: 17 additions & 4 deletions TGServerService/DreamDaemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void InitDreamDaemon()
Proc = Process.GetProcessById(Properties.Settings.Default.ReattachPID);
if (Proc == null)
throw new Exception("GetProcessById returned null!");
TGServerService.WriteLog("Reattached to running DD process!");
TGServerService.WriteInfo("Reattached to running DD process!", TGServerService.EventID.DDReattachSuccess);
SendMessage("DD: Update complete. Watchdog reactivated...");

//start wd
Expand All @@ -59,7 +59,7 @@ void InitDreamDaemon()
}
catch (Exception e)
{
TGServerService.WriteLog(String.Format("Failed to reattach to DreamDaemon! PID: {0}. Exception: {1}", Properties.Settings.Default.ReattachPID, e.ToString()), EventLogEntryType.Error);
TGServerService.WriteError(String.Format("Failed to reattach to DreamDaemon! PID: {0}. Exception: {1}", Properties.Settings.Default.ReattachPID, e.ToString()), TGServerService.EventID.DDReattachFail);
}
finally
{
Expand Down Expand Up @@ -205,9 +205,15 @@ void Watchdog()
lock (restartLock)
{
if (!RestartInProgress)
{
SendMessage("DD: Server started, watchdog active...");
TGServerService.WriteInfo("Watchdog started", TGServerService.EventID.DDWatchdogStarted);
}
else
{
RestartInProgress = false;
TGServerService.WriteInfo("Watchdog started", TGServerService.EventID.DDWatchdogRestarted);
}
}
var retries = 0;
while (true)
Expand Down Expand Up @@ -242,7 +248,9 @@ void Watchdog()
else
{
retries = 0;
SendMessage("DD: DreamDaemon crashed or exited! Rebooting...");
var msg = "DD: DreamDaemon crashed! Rebooting...";
SendMessage(msg);
TGServerService.WriteWarning(msg, TGServerService.EventID.DDWatchdogRebootingServer);
}
}

Expand Down Expand Up @@ -273,7 +281,7 @@ void Watchdog()
catch (Exception e)
{
SendMessage("DD: Watchdog thread crashed!");
TGServerService.WriteLog("Watch dog thread crashed! Exception: " + e.ToString(), EventLogEntryType.Error);
TGServerService.WriteError("Watch dog thread crashed: " + e.ToString(), TGServerService.EventID.DDWatchdogCrash);
}
finally
{
Expand All @@ -283,7 +291,12 @@ void Watchdog()
currentPort = 0;
AwaitingShutdown = ShutdownRequestPhase.None;
if (!RestartInProgress)
{
SendMessage("DD: Server stopped, watchdog exiting...");
TGServerService.WriteInfo("Watch dog exited", TGServerService.EventID.DDWatchdogExit);
}
else
TGServerService.WriteInfo("Watch dog restarting...", TGServerService.EventID.DDWatchdogRestart);
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions TGServerService/IRC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public string SendMessageDirect(string message, string channel)
channel = channel.Replace(PrivateMessageMarker, "");
irc.SendMessage(SendType.Message, channel, message);
}
TGServerService.WriteLog(String.Format("IRC Send ({0}): {1}", channel, message));
TGServerService.WriteInfo(String.Format("IRC Send ({0}): {1}", channel, message), TGServerService.EventID.ChatSend);
return null;
}
catch (Exception e)
Expand Down Expand Up @@ -196,8 +196,10 @@ public void Disconnect()
}
}
}
catch
{ }
catch (Exception e)
{
TGServerService.WriteError("IRC failed QnD: " + e.ToString(), TGServerService.EventID.ChatDisconnectFail);
}
}
//public api
public bool Connected()
Expand All @@ -223,7 +225,7 @@ public string SendMessage(string message, bool adminOnly)
foreach (var I in Config.ChatChannels)
irc.SendMessage(SendType.Message, I, message);
}
TGServerService.WriteLog(String.Format("IRC Send{0}: {1}", adminOnly ? " (ADMIN)" : "", message));
TGServerService.WriteInfo(String.Format("IRC Send{0}: {1}", adminOnly ? " (ADMIN)" : "", message), adminOnly ? TGServerService.EventID.ChatAdminBroadcast : TGServerService.EventID.ChatBroadcast);
return null;
}
catch (Exception e)
Expand Down
3 changes: 2 additions & 1 deletion TGServerService/InterfaceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace TGServerService
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
partial class TGStationServer : IDisposable, ITGSService, ITGServerUpdater
{

//call partial constructors/destructors from here
//called when the service is started
public TGStationServer()
Expand Down Expand Up @@ -86,7 +87,7 @@ public void VerifyConnection()
//public api
public void StopForUpdate()
{
TGServerService.WriteLog("Stopping for update!");
TGServerService.WriteInfo("Stopping for update!", TGServerService.EventID.UpdateRequest);
Properties.Settings.Default.ReattachToDD = true;
TGServerService.LocalStop();
}
Expand Down
12 changes: 6 additions & 6 deletions TGServerService/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void HandleCommand(string cmd)
SendMessage("RELAY: " + String.Join(" ", splits), true);
break;
case SRWorldReboot:
TGServerService.WriteLog("World Rebooted", EventLogEntryType.Information);
TGServerService.WriteInfo("World Rebooted", TGServerService.EventID.WorldReboot);
break;
}
}
Expand Down Expand Up @@ -130,12 +130,12 @@ string SendTopic(string topicdata, ushort port)
topicSender.Shutdown(SocketShutdown.Both);
}

TGServerService.WriteLog("Topic: \"" + topicdata + "\" Returned: " + returnedString);
TGServerService.WriteInfo("Topic: \"" + topicdata + "\" Returned: " + returnedString, TGServerService.EventID.TopicSent);
return returnedString;
}
catch
{
TGServerService.WriteLog("Failed to send topic: " + topicdata, EventLogEntryType.Error);
TGServerService.WriteWarning("Failed to send topic: " + topicdata, TGServerService.EventID.TopicFailed);
return "Topic delivery failed!";
}
}
Expand All @@ -155,7 +155,7 @@ void GenCommsKey()
serviceCommsKey += tmp;
} while (serviceCommsKey.Length < CommsKeyLen);
serviceCommsKey = serviceCommsKey.Substring(0, CommsKeyLen);
TGServerService.WriteLog("Service Comms Key set to: " + serviceCommsKey);
TGServerService.WriteInfo("Service Comms Key set to: " + serviceCommsKey, TGServerService.EventID.CommsKeySet);
}

//Start listening for nudges on the configured port
Expand Down Expand Up @@ -193,7 +193,7 @@ void NudgeHandler()
var np = InteropPort(out string error);
if (error != null)
{
TGServerService.WriteLog("Unable to start nudge handler! Error: " + error, EventLogEntryType.Warning);
TGServerService.WriteError("Unable to start nudge handler: " + error, TGServerService.EventID.NudgeStartFail);
return;
}

Expand Down Expand Up @@ -238,7 +238,7 @@ void NudgeHandler()
}
catch (Exception e)
{
TGServerService.WriteLog("Nudge handler thread crashed: " + e.ToString(), EventLogEntryType.Error);
TGServerService.WriteError("Nudge handler thread crashed: " + e.ToString(), TGServerService.EventID.NudgeCrash);
}
}
}
Expand Down
Loading

0 comments on commit 09309cc

Please sign in to comment.