Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
[dirty] add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
yretenai committed Mar 15, 2018
1 parent ebf7e26 commit 79f8a57
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Warden.Tests/Warden.Tests.csproj
Expand Up @@ -69,6 +69,9 @@
<Name>Warden</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
23 changes: 23 additions & 0 deletions Warden/Core/DummyWardenLogger.cs
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Warden.Core
{
public class DummyWardenLogger : IWardenLogger
{
public void Debug(string message)
{
}

public void Error(string message)
{
}

public void Info(string message)
{
}
}
}
17 changes: 17 additions & 0 deletions Warden/Core/IWardenLogger.cs
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Warden.Core
{
public interface IWardenLogger
{
void Error(string message);

void Debug(string message);

void Info(string message);
}
}
13 changes: 12 additions & 1 deletion Warden/Core/WardenManager.cs
Expand Up @@ -44,6 +44,8 @@ public static class WardenManager
private static ManagementEventWatcher _processStartEvent;
private static ManagementEventWatcher _processStopEvent;

public static IWardenLogger Logger = new DummyWardenLogger();

public static ConcurrentDictionary<Guid, WardenProcess> ManagedProcesses = new ConcurrentDictionary<Guid, WardenProcess>();


Expand Down Expand Up @@ -81,6 +83,7 @@ public static void Initialize(WardenOptions options)
_processStartEvent.Start();
_processStopEvent.Start();
Initialized = true;
Logger?.Debug("Initialized");
}
catch (Exception ex)
{
Expand All @@ -102,6 +105,7 @@ public static void Flush(int processId)
{
var key = ManagedProcesses.FirstOrDefault(x => x.Value.Id == processId).Key;
ManagedProcesses.TryRemove(key, out _);
Logger?.Debug($"Flushed PID {processId}");
}
catch
{
Expand All @@ -118,6 +122,7 @@ private static void ProcessStopped(object sender, EventArrivedEventArgs e)
{
var processName = e.NewEvent.Properties["ProcessName"].Value.ToString();
var processId = int.Parse(e.NewEvent.Properties["ProcessID"].Value.ToString());
Logger?.Debug($"Detected {processName}:{processId} stopped");
HandleStoppedProcess(processId);
}

Expand All @@ -132,10 +137,15 @@ private static void HandleStoppedProcess(int processId)
if (managed.Id == processId)
{
managed.UpdateState(ProcessState.Dead);
Logger?.Debug($"Sent dead event to {managed.Id}");
return;
}
var child = FindChildById(processId, managed.Children);
child?.UpdateState(ProcessState.Dead);
if (child != null)
{
Logger?.Debug($"Sent dead event to child {child?.Id}");
}
});
}

Expand Down Expand Up @@ -190,6 +200,7 @@ private static void PreProcessing(string processName, int processId)
ManagedProcesses[kvp.Key].Path = ProcessUtils.GetProcessPath(processId);
ManagedProcesses[kvp.Key].Arguments = ProcessUtils.GetCommandLine(processId);
ManagedProcesses[kvp.Key]?.FoundCallback?.Invoke(true);
Logger?.Info($"Updated managed process for {newProcesWithoutExt}:{processId}");
};
});
}
Expand All @@ -205,6 +216,7 @@ private static void ProcessStarted(object sender, EventArrivedEventArgs e)
var processName = e.NewEvent.Properties["ProcessName"].Value.ToString();
var processId = int.Parse(e.NewEvent.Properties["ProcessID"].Value.ToString());
var processParent = int.Parse(e.NewEvent.Properties["ParentProcessID"].Value.ToString());
Logger?.Debug($"Detected {processName}:{processId} started");
PreProcessing(processName, processId);
HandleNewProcess(processName, processId, processParent);
}
Expand Down Expand Up @@ -239,7 +251,6 @@ private static void HandleNewProcess(string processName, int processId, int proc
var child = FindChildById(processParent, managed.Children);
if (child == null)
{
return;
}
var grandChild = CreateProcessFromId(processName, child.Id, processId, child.Filters);
Expand Down
2 changes: 2 additions & 0 deletions Warden/Warden.csproj
Expand Up @@ -46,6 +46,7 @@
<Reference Include="System.Security.Principal.Windows" />
</ItemGroup>
<ItemGroup>
<Compile Include="Core\DummyWardenLogger.cs" />
<Compile Include="Core\Exceptions\WardenExceptionStrings.cs" />
<Compile Include="Core\Exceptions\WardenException.cs" />
<Compile Include="Core\Exceptions\WardenManageException.cs" />
Expand All @@ -61,6 +62,7 @@
<Compile Include="Core\Utils\StringUtils.cs" />
<Compile Include="Core\Utils\TaskKill.cs" />
<Compile Include="Core\WardenImpersonator.cs" />
<Compile Include="Core\IWardenLogger.cs" />
<Compile Include="Core\WardenManager.cs" />
<Compile Include="Core\WardenProcess.cs" />
<Compile Include="Core\WardenEnums.cs" />
Expand Down

0 comments on commit 79f8a57

Please sign in to comment.