Skip to content

Commit a64f25c

Browse files
committed
完善bowl组件
1 parent 2b8a7d1 commit a64f25c

File tree

18 files changed

+380
-132
lines changed

18 files changed

+380
-132
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
@echo off
2+
setlocal
3+
4+
if "%~1"=="" (
5+
echo Please provide the export path as the first parameter.
6+
exit /b 1
7+
)
8+
9+
set exportDir=%~1
10+
11+
if not exist "%exportDir%" (
12+
mkdir "%exportDir%"
13+
)
14+
15+
set outputFile=%exportDir%\driverInfo.txt
16+
17+
:: 导出驱动信息
18+
driverquery /v /fo table > "%outputFile%"
19+
echo %outputFile% Export successfully.
20+
21+
:: 导出系统信息
22+
set systemInfoFile=%exportDir%\systeminfo.txt
23+
systeminfo > "%systemInfoFile%"
24+
echo %systemInfoFile% Export successfully.
25+
26+
:: 获取当前日期
27+
for /f "tokens=1-4 delims=/- " %%i in ('date /t') do (
28+
set yyyy=%%i
29+
set mm=%%j
30+
set dd=%%k
31+
)
32+
33+
:: 设置日志文件名
34+
set logFile=%exportDir%\systemlog.evtx
35+
36+
:: 导出系统日志
37+
wevtutil epl System "%logFile%" /q:"*[System[TimeCreated[timediff(@SystemTime) <= 86400000]]]"
38+
echo %logFile% Export successfully.
39+
40+
endlocal

src/c#/GeneralUpdate.Bowl/Bowl.cs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
2+
using System.IO;
23
using System.Runtime.InteropServices;
4+
using System.Text.Json;
35
using GeneralUpdate.Bowl.Strategys;
6+
using GeneralUpdate.Common.Shared.Object;
47

58
namespace GeneralUpdate.Bowl;
69

@@ -9,43 +12,49 @@ namespace GeneralUpdate.Bowl;
912
/// </summary>
1013
public sealed class Bowl
1114
{
12-
private IStrategy _strategy;
15+
private static IStrategy? _strategy;
1316

14-
public Bowl(MonitorParameter parameter = null)
15-
{
16-
CreateStrategy();
17-
_strategy!.SetParameter(parameter);
18-
}
17+
private Bowl() { }
1918

20-
private Bowl CreateStrategy()
19+
private static void CreateStrategy()
2120
{
2221
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
2322
{
2423
_strategy = new WindowStrategy();
2524
}
26-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
25+
/*else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
2726
{
2827
_strategy = new LinuxStrategy();
29-
}
28+
}*/
3029

3130
if (_strategy == null)
3231
throw new PlatformNotSupportedException("Unsupported operating system");
33-
34-
return this;
3532
}
36-
37-
public Bowl SetParameter(MonitorParameter parameter)
33+
34+
public static void Launch(MonitorParameter? monitorParameter = null)
3835
{
39-
if(parameter.Verify())
40-
throw new ArgumentException("Parameter contains illegal values");
41-
42-
_strategy.SetParameter(parameter);
43-
return this;
36+
monitorParameter ??= CreateParameter();
37+
CreateStrategy();
38+
_strategy?.SetParameter(monitorParameter);
39+
_strategy?.Launch();
4440
}
4541

46-
public Bowl Launch()
42+
private static MonitorParameter CreateParameter()
4743
{
48-
_strategy.Launch();
49-
return this;
44+
var json = Environment.GetEnvironmentVariable("ProcessInfo", EnvironmentVariableTarget.User);
45+
if(string.IsNullOrWhiteSpace(json))
46+
throw new ArgumentNullException("ProcessInfo environment variable not set !");
47+
48+
var processInfo = JsonSerializer.Deserialize<ProcessInfo>(json);
49+
return new MonitorParameter
50+
{
51+
ProcessNameOrId = processInfo.AppName,
52+
DumpFileName = $"{processInfo.LastVersion}_fail.dmp",
53+
FailFileName = $"{processInfo.LastVersion}_fail.json",
54+
TargetPath = processInfo.InstallPath,
55+
FailDirectory = Path.Combine(processInfo.InstallPath, "fail", processInfo.LastVersion),
56+
BackupDirectory = Path.Combine(processInfo.InstallPath, processInfo.LastVersion),
57+
ExtendedField = processInfo.LastVersion
58+
};
5059
}
5160
}

src/c#/GeneralUpdate.Bowl/GeneralUpdate.Bowl.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@
2525
<None Update="Applications\Linux\procdump_3.3.0_amd64.deb">
2626
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2727
</None>
28+
<None Update="Applications\Windows\export.bat">
29+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
30+
</None>
31+
</ItemGroup>
32+
33+
<ItemGroup>
34+
<PackageReference Include="System.Text.Json" Version="9.0.0" />
35+
</ItemGroup>
36+
37+
<ItemGroup>
38+
<ProjectReference Include="..\GeneralUpdate.Common\GeneralUpdate.Common.csproj" />
2839
</ItemGroup>
2940

3041
</Project>
Lines changed: 18 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.IO;
5+
using GeneralUpdate.Common.FileBasic;
46

57
namespace GeneralUpdate.Bowl.Strategys;
68

79
public abstract class AbstractStrategy : IStrategy
810
{
911
protected MonitorParameter _parameter;
10-
11-
private readonly IReadOnlyList<string> _sensitiveCharacter = new List<string>
12-
{
13-
"Exit",
14-
"exit",
15-
"EXIT"
16-
};
12+
protected List<string> OutputList = new ();
13+
14+
public void SetParameter(MonitorParameter parameter) => _parameter = parameter;
1715

1816
public virtual void Launch()
1917
{
20-
Backup();
21-
Startup(_parameter.ProcessNameOrId, _parameter.InnerArguments);
18+
Startup(_parameter.InnerApp, _parameter.InnerArguments);
2219
}
2320

2421
private void Startup(string appName, string arguments)
2522
{
23+
if (Directory.Exists(_parameter.FailDirectory))
24+
{
25+
Directory.Delete(_parameter.FailDirectory, true);
26+
}
27+
Directory.CreateDirectory(_parameter.FailDirectory);
28+
2629
var startInfo = new ProcessStartInfo
2730
{
2831
FileName = appName,
2932
Arguments = arguments,
3033
RedirectStandardOutput = true,
34+
RedirectStandardError = true,
3135
UseShellExecute = false,
3236
CreateNoWindow = true
3337
};
@@ -36,71 +40,15 @@ private void Startup(string appName, string arguments)
3640
process.OutputDataReceived += OutputHandler;
3741
process.ErrorDataReceived += OutputHandler;
3842
process.Start();
39-
process.StandardOutput.ReadToEnd();
40-
process.WaitForExit();
43+
process.BeginOutputReadLine();
44+
process.BeginErrorReadLine();
45+
process.WaitForExit(1000 * 10);
4146
}
4247

4348
private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
4449
{
4550
var data = outLine.Data;
4651
if (!string.IsNullOrEmpty(data))
47-
{
48-
foreach (var sensitive in _sensitiveCharacter)
49-
{
50-
if (data.Contains(sensitive)){
51-
Restore();
52-
Process.Start(_parameter.ProcessNameOrId, _parameter.Arguments);
53-
break;
54-
}
55-
}
56-
}
52+
OutputList.Add(data);
5753
}
58-
59-
private void Backup()
60-
{
61-
var backupPath = _parameter.Target;
62-
var sourcePath = _parameter.Source;
63-
64-
if (Directory.Exists(backupPath))
65-
{
66-
Directory.Delete(backupPath, true);
67-
}
68-
69-
Directory.CreateDirectory(backupPath);
70-
71-
foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
72-
{
73-
Directory.CreateDirectory(dirPath.Replace(sourcePath, backupPath));
74-
}
75-
76-
foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
77-
{
78-
File.Copy(newPath, newPath.Replace(sourcePath, backupPath), true);
79-
}
80-
}
81-
82-
private void Restore()
83-
{
84-
var restorePath = _parameter.Target;
85-
var backupPath = _parameter.Source;
86-
87-
if (Directory.Exists(restorePath))
88-
{
89-
Directory.Delete(restorePath, true);
90-
}
91-
92-
Directory.CreateDirectory(restorePath);
93-
94-
foreach (string dirPath in Directory.GetDirectories(backupPath, "*", SearchOption.AllDirectories))
95-
{
96-
Directory.CreateDirectory(dirPath.Replace(backupPath, restorePath));
97-
}
98-
99-
foreach (string newPath in Directory.GetFiles(backupPath, "*.*", SearchOption.AllDirectories))
100-
{
101-
File.Copy(newPath, newPath.Replace(backupPath, restorePath), true);
102-
}
103-
}
104-
105-
public void SetParameter(MonitorParameter parameter) => _parameter = parameter;
10654
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
3+
namespace GeneralUpdate.Bowl.Strategys;
4+
5+
public class Crash
6+
{
7+
public MonitorParameter Parameter { get; set; }
8+
9+
public List<string> ProcdumpOutPutLines { get; set; }
10+
}

src/c#/GeneralUpdate.Bowl/Strategys/MonitorParameter.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@
22

33
public class MonitorParameter
44
{
5-
public string Target { get; set; }
6-
7-
public string Source { get; set; }
5+
public MonitorParameter() { }
86

9-
public string ProcessNameOrId { get; set; }
7+
public string TargetPath { get; set; }
108

11-
public string DumpPath { get; set; }
9+
public string FailDirectory { get; set; }
1210

11+
public string BackupDirectory { get; set; }
12+
13+
public string ProcessNameOrId { get; set; }
14+
1315
public string DumpFileName { get; set; }
1416

15-
public string Arguments { get; set; }
17+
public string FailFileName { get; set; }
1618

17-
internal string InnerArguments => $"-e -ma {ProcessNameOrId} {DumpPath}";
19+
internal string InnerArguments { get; set; }
1820

19-
internal string InnerAppName { get; set; }
20-
21-
public bool Verify()
22-
{
23-
return string.IsNullOrEmpty(Target) &&
24-
string.IsNullOrEmpty(Source) &&
25-
string.IsNullOrEmpty(ProcessNameOrId) &&
26-
string.IsNullOrEmpty(DumpPath) &&
27-
string.IsNullOrEmpty(DumpFileName) &&
28-
string.IsNullOrEmpty(Arguments);
29-
}
21+
internal string InnerApp { get; set; }
22+
23+
/// <summary>
24+
/// Upgrade: upgrade mode. This mode is primarily used in conjunction with GeneralUpdate for internal use. Please do not modify it arbitrarily when the default mode is activated.
25+
/// Normal: Normal mode,This mode can be used independently to monitor a single program. If the program crashes, it will export the crash information.
26+
/// </summary>
27+
public string WorkModel { get; set; } = "Upgrade";
28+
29+
public string ExtendedField { get; set; }
3030
}

0 commit comments

Comments
 (0)