Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[All Plugins]Added the ability to capture log data #88

Merged
merged 19 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions Application/Models/ApplicationInfoCollector/AppStatusBundles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ public class AppBasicInfo

public string Platform { get; set; } = string.Empty;

public string LogFilepath { get; set; } = string.Empty;

public void Set(string inName, string inValue)
{
if(inName == GetVarName(nameof(ClientID)) && int.TryParse(inValue, out int outClientID))
if (inName == GetVarName(nameof(ClientID)) && int.TryParse(inValue, out int outClientID))
ClientID = outClientID;
else if(inName == GetVarName(nameof(Pid)))
else if (inName == GetVarName(nameof(Pid)))
Pid = inValue;
else if (inName == GetVarName(nameof(AppName)))
AppName = inValue;
Expand All @@ -37,16 +39,19 @@ public void Set(string inName, string inValue)
Args = inValue;
else if (inName == GetVarName(nameof(Platform)))
Platform = inValue;
else if (inName == GetVarName(nameof(LogFilepath)))
LogFilepath = inValue;
}

public Dictionary<string, string> ToDict()
{
return new Dictionary<string, string> {
{ GetVarName(nameof(Pid)), Pid },
{ GetVarName(nameof(AppName)), AppName },
{ GetVarName(nameof(Version)), Version },
{ GetVarName(nameof(Args)), Args },
{ GetVarName(nameof(Platform)), Platform },
{ GetVarName(nameof(Pid)), Pid },
{ GetVarName(nameof(AppName)), AppName },
{ GetVarName(nameof(Version)), Version },
{ GetVarName(nameof(Args)), Args },
{ GetVarName(nameof(Platform)), Platform },
{ GetVarName(nameof(LogFilepath)), LogFilepath },
};
}

Expand Down
5 changes: 2 additions & 3 deletions Application/Models/IssueAssetCollector/AssetData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ public AssetData(string inFilename)
FullName = inFilename;
Name = Path.GetFileName(FullName);

var ext = Path.GetExtension(FullName);
if (_preview_support_exts.Contains(ext))
Extension = Path.GetExtension(FullName);
if (_preview_support_exts.Contains(Extension))
{
Extension = ext;
Task.Factory.StartNew(() => { PictureRawData = ImageProcessor.Instnace.Load(FullName); });
}
}
Expand Down
49 changes: 46 additions & 3 deletions Application/Models/IssueAssetCollector/FilenameGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,64 @@
using System;
using BocchiTracker.ApplicationInfoCollector.Handlers;
using BocchiTracker.ApplicationInfoCollector;
using BocchiTracker.ModelEvent;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using BocchiTracker.ServiceClientData;
using System.Runtime.CompilerServices;

namespace BocchiTracker.IssueAssetCollector
{
public interface IFilenameGenerator
{
string Generate();
string Generate(AppStatusBundle inAppStatusBundle);
}

public class TimestampedFilenameGenerator : IFilenameGenerator
{
public string Generate()
public string Generate(AppStatusBundle inAppStatusBundle)
{
return DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss");
}
}

public class RunningAppFilenameGenerator : IFilenameGenerator
{
public string Generate(AppStatusBundle inAppStatusBundle)
{
var info = inAppStatusBundle;
if (info == null)
return string.Empty;

return $"{info.AppBasicInfo.AppName}_{info.AppBasicInfo.ClientID}";
}
}

public interface IFilenameGeneratorFactory
{
IFilenameGenerator GetFilenameGenerator(Type inType);
}

public class FilenameGeneratorFactory : IFilenameGeneratorFactory
{
private static Dictionary<Type, IFilenameGenerator> _services = new Dictionary<Type, IFilenameGenerator>();

public FilenameGeneratorFactory(AppStatusBundles inAppStatusBundles)
{
if (!_services.ContainsKey(typeof(TimestampedFilenameGenerator)))
_services.Add(typeof(TimestampedFilenameGenerator), new TimestampedFilenameGenerator());

if (!_services.ContainsKey(typeof(RunningAppFilenameGenerator)))
_services.Add(typeof(RunningAppFilenameGenerator), new RunningAppFilenameGenerator());
}

public IFilenameGenerator GetFilenameGenerator(Type inType)
{
return _services[inType];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public CoredumpHandler(IFilenameGenerator inFilenameGenerator)
_filenameGenerator = inFilenameGenerator;
}

public virtual void Handle(int inClientID, int inPID, string inOutput) { }
public virtual void Handle(AppStatusBundle inAppStatusBundle, int inPID, string inOutput) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public WindowsCoredumpHandler(IFilenameGenerator inFilenameGenerator, string? in
_procDumpApp = inProcDumpPath;
}

public override void Handle(int inClientID, int inPID, string inOutput)
public override void Handle(AppStatusBundle inAppStatusBundle, int inPID, string inOutput)
{
if(!File.Exists(_procDumpApp))
{
Expand All @@ -30,7 +30,7 @@ public override void Handle(int inClientID, int inPID, string inOutput)

using (Process proc = new Process())
{
string output = Path.Combine(inOutput, _filenameGenerator.Generate() + ".dmp");
string output = Path.Combine(inOutput, _filenameGenerator.Generate(inAppStatusBundle) + ".dmp");
proc.StartInfo = new ProcessStartInfo
{
FileName = _procDumpApp,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using BocchiTracker.Config.Configs;
using BocchiTracker.ApplicationInfoCollector;
using BocchiTracker.Config.Configs;
using BocchiTracker.IssueAssetCollector.Handlers.Coredump;
using BocchiTracker.IssueAssetCollector.Handlers.Log;
using BocchiTracker.IssueAssetCollector.Handlers.Screenshot;
using BocchiTracker.ModelEvent;
using BocchiTracker.ProcessLinkQuery.Queries;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -19,14 +24,16 @@ public class CreateActionHandler : ICreateActionHandler
{
private Dictionary<Type, IHandle> _cacheHandles = new Dictionary<Type, IHandle>();
private readonly IEventAggregator _eventAggregator;
private readonly IFilenameGenerator _filenameGenerator;
private readonly IFilenameGeneratorFactory _filenameGeneratorFactory;
private readonly ProjectConfig _projectConfig;
private readonly AppStatusBundles _appStatusBundles;

public CreateActionHandler(IEventAggregator inEventAggregator, IFilenameGenerator inFilenameGen, ProjectConfig inConfig)
public CreateActionHandler(IEventAggregator inEventAggregator, IFilenameGeneratorFactory inFilenameGenFac, AppStatusBundles inAppStatusBundles, ProjectConfig inConfig)
{
_eventAggregator = inEventAggregator;
_filenameGenerator = inFilenameGen;
_filenameGeneratorFactory = inFilenameGenFac;
_projectConfig = inConfig;
_appStatusBundles = inAppStatusBundles;
}

public IHandle Create(Type inType)
Expand All @@ -36,23 +43,34 @@ public IHandle Create(Type inType)
#if WINDOWS
if (inType == typeof(LocalScreenshotHandler))
{
var handler = new LocalScreenshotHandler(new Utils.Win32.WindowsClientCapture(), new Utils.Win32.GetWindowHandleFromPid(), _filenameGenerator);
var handler = new LocalScreenshotHandler(new Utils.Win32.WindowsClientCapture(), new Utils.Win32.GetWindowHandleFromPid(), _filenameGeneratorFactory.GetFilenameGenerator(typeof(TimestampedFilenameGenerator)));
_cacheHandles.Add(inType, handler);
}
else
#endif
if(inType == typeof(RemoteScreenshotHandler))
{
var handler = new RemoteScreenshotHandler(_eventAggregator, _filenameGenerator);
var handler = new RemoteScreenshotHandler(_eventAggregator, _filenameGeneratorFactory.GetFilenameGenerator(typeof(TimestampedFilenameGenerator)));
_cacheHandles.Add(inType, handler);
}
#if WINDOWS
else if (inType == typeof(WindowsCoredumpHandler))
{
var handler = new WindowsCoredumpHandler(_filenameGenerator, _projectConfig.ExternalToolsPath?.ProcDumpPath);
var handler = new WindowsCoredumpHandler(_filenameGeneratorFactory.GetFilenameGenerator(typeof(TimestampedFilenameGenerator)), _projectConfig.ExternalToolsPath?.ProcDumpPath);
_cacheHandles.Add(inType, handler);
}
#endif
if(inType == typeof(LogRemoteCaptureHandler))
{
var handler = new LogRemoteCaptureHandler(_eventAggregator, _filenameGeneratorFactory.GetFilenameGenerator(typeof(RunningAppFilenameGenerator)));
_cacheHandles.Add(inType, handler);
}
else if (inType == typeof(LogFileCaptureHandler))
{
var handler = new LogFileCaptureHandler(_eventAggregator, _filenameGeneratorFactory.GetFilenameGenerator(typeof(RunningAppFilenameGenerator)));
_cacheHandles.Add(inType, handler);
}

return _cacheHandles[inType];
}
}
Expand Down
2 changes: 1 addition & 1 deletion Application/Models/IssueAssetCollector/Handlers/IHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ namespace BocchiTracker.IssueAssetCollector.Handlers
{
public interface IHandle
{
void Handle(int inClientID, int inPID, string inOutput);
void Handle(AppStatusBundle inAppStatusBundle, int inPID, string inOutput);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using BocchiTracker.ApplicationInfoCollector;
using BocchiTracker.ModelEvent;
using BocchiTracker.ProcessLinkQuery.Queries;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BocchiTracker.IssueAssetCollector.Handlers.Log
{
public class LogCopyEvent
{
private string _source_filename { get; set; } = string.Empty;
private string _output_filename { get; set; } = string.Empty;

public LogCopyEvent(IEventAggregator inEventAggregator, string inLog, string inOutput)
{
_source_filename = inLog;
_output_filename = inOutput;

inEventAggregator
.GetEvent<IssueSubmitPreEvent>()
.Subscribe(OnCopy, ThreadOption.BackgroundThread);

//!< First copy to add to attachment list
OnCopy();
}

private void OnCopy()
{
try
{
File.Copy(_source_filename, _output_filename, true);
}
catch (Exception ex)
{
Console.WriteLine($"Error during log copy: {ex.Message}");
}
}
}

public class LogFileCaptureHandler : IHandle
{
private IEventAggregator _event;
private RunningAppFilenameGenerator? _filename_generator = null;
private Dictionary<int, LogCopyEvent> _save_process_map = new Dictionary<int, LogCopyEvent>();

public LogFileCaptureHandler(IEventAggregator inEventAggregator, IFilenameGenerator inFilenameGenerator)
{
_event = inEventAggregator;
_filename_generator = inFilenameGenerator as RunningAppFilenameGenerator;

_event = inEventAggregator;
_event
.GetEvent<AppDisconnectEvent>()
.Subscribe((AppDisconnectEventParameter inParameter) =>
{
if (_save_process_map.ContainsKey(inParameter.ClientID))
_save_process_map.Remove(inParameter.ClientID);
}, ThreadOption.BackgroundThread);
}

public void Handle(AppStatusBundle inAppStatusBundle, int inPID, string inOutput)
{
if (_filename_generator == null) { return; }

if(!File.Exists(inAppStatusBundle.AppBasicInfo.LogFilepath)) { return; }

if (!_save_process_map.ContainsKey(inAppStatusBundle.AppBasicInfo.ClientID))
{
_save_process_map.Add(
inAppStatusBundle.AppBasicInfo.ClientID,
new LogCopyEvent(_event, inAppStatusBundle.AppBasicInfo.LogFilepath, Path.Combine(inOutput, _filename_generator.Generate(inAppStatusBundle) + ".txt"))
);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using BocchiTracker.ApplicationInfoCollector;
using BocchiTracker.IssueAssetCollector.Handlers.Screenshot;
using BocchiTracker.ModelEvent;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BocchiTracker.IssueAssetCollector.Handlers.Log
{
public class LogAppendProcess
{
private string _output_filename { get; set; } = string.Empty;

private IEventAggregator _event;

public LogAppendProcess(IEventAggregator inEventAggregator, string inOutput)
{
_output_filename = inOutput;
_event = inEventAggregator;
_event
.GetEvent<ReceiveLogDataEvent>()
.Subscribe(Handle, ThreadOption.PublisherThread);
}

public void Handle(ReceiveLogDataEventParameter inEvent)
{
File.AppendAllText(_output_filename, inEvent.Log);
}
}

public class LogRemoteCaptureHandler : IHandle
{
private IEventAggregator _event;
private RunningAppFilenameGenerator? _filename_generator = null;
private Dictionary<int, LogAppendProcess> _save_process_map = new Dictionary<int, LogAppendProcess>();

public LogRemoteCaptureHandler(IEventAggregator inEventAggregator, IFilenameGenerator inFilenameGenerator)
{
_filename_generator = inFilenameGenerator as RunningAppFilenameGenerator;

_event = inEventAggregator;
_event
.GetEvent<AppDisconnectEvent>()
.Subscribe((AppDisconnectEventParameter inParameter) =>
{
if (_save_process_map.ContainsKey(inParameter.ClientID))
_save_process_map.Remove(inParameter.ClientID);
}, ThreadOption.BackgroundThread);
}

public void Handle(AppStatusBundle inAppStatusBundle, int inPID, string inOutput)
{
if(_filename_generator == null) { return; }

if(!_save_process_map.ContainsKey(inAppStatusBundle.AppBasicInfo.ClientID))
{
_save_process_map.Add(
inAppStatusBundle.AppBasicInfo.ClientID,
new LogAppendProcess(_event, Path.Combine(inOutput, _filename_generator.Generate(inAppStatusBundle) + ".txt"))
);
}
}
}
}
Loading
Loading