Skip to content

Commit edf0a5f

Browse files
committed
Improvements in profiler host processors (#3114)
***NO_CI*** (cherry picked from commit 4d74775)
1 parent 17ace79 commit edf0a5f

File tree

3 files changed

+66
-11
lines changed

3 files changed

+66
-11
lines changed

targets/netcore/nanoFramework.nanoCLR.Host/NanoClrHostBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ public nanoCLRHost Build()
143143
s_nanoClrHost.WireProtocolPort = _wireProtocolPort;
144144
s_nanoClrHost.ConfigureSteps.AddRange(_configureSteps);
145145
s_nanoClrHost.PreInitConfigureSteps.AddRange(_preInitConfigureSteps);
146+
s_nanoClrHost.CleanupSteps.AddRange(_cleanupSteps);
146147

147148
s_nanoClrHost.nanoCLRSettings = new nanoCLRSettings
148149
{

targets/netcore/nanoFramework.nanoCLR.Host/Profiler/ProfilerLogFileProcessor.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,28 @@ namespace nanoFramework.nanoCLR.Host.Profiler
1313
internal class ProfilerLogFileProcessor : IDisposable
1414
{
1515
private readonly StreamWriter _profilerDumpWriter;
16+
private readonly string _logFilePath;
17+
private bool _disposedValue;
1618

1719
public ProfilerLogFileProcessor()
1820
{
1921
// create output file for profiler binary dump
2022
string _profileBinaryFileName = $"{Guid.NewGuid()}_{DateTime.Now.ToString("s").Replace(":", "_")}.log";
2123

22-
string logFilePath = Path.Combine(Path.GetTempPath(), _profileBinaryFileName);
24+
_logFilePath = Path.Combine(Path.GetTempPath(), _profileBinaryFileName);
2325

2426
// inform user about the binary file
25-
Console.WriteLine($"Profiler log file: {logFilePath}");
27+
Console.WriteLine($"{Environment.NewLine}Profiler log file: {_logFilePath}{Environment.NewLine}");
2628

2729
try
2830
{
2931
_profilerDumpWriter = new StreamWriter(
30-
logFilePath,
32+
_logFilePath,
3133
new FileStreamOptions() { Mode = FileMode.Create, Access = FileAccess.ReadWrite, Options = FileOptions.WriteThrough, Share = FileShare.ReadWrite });
3234
}
3335
catch (IOException ex)
3436
{
35-
throw new IOException($"Failed to create profiler message log file at {logFilePath}", ex);
37+
throw new IOException($"Failed to create profiler message log file at {_logFilePath}", ex);
3638
}
3739
}
3840

@@ -44,6 +46,30 @@ internal void MessageHandler(byte[] data, int length)
4446

4547
}
4648

47-
public void Dispose() => ((IDisposable)_profilerDumpWriter).Dispose();
49+
#region Dispose implementation
50+
51+
protected virtual void Dispose(bool disposing)
52+
{
53+
if (!_disposedValue)
54+
{
55+
if (disposing)
56+
{
57+
// output the file path so user can find it (usefull in case of long running)
58+
Console.WriteLine($"{Environment.NewLine}Profiler log file: {_logFilePath}{Environment.NewLine}");
59+
60+
_profilerDumpWriter.Dispose();
61+
}
62+
63+
_disposedValue = true;
64+
}
65+
}
66+
67+
public void Dispose()
68+
{
69+
Dispose(disposing: true);
70+
GC.SuppressFinalize(this);
71+
}
72+
73+
#endregion
4874
}
4975
}

targets/netcore/nanoFramework.nanoCLR.Host/Profiler/ProfilerMessageProcessor.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,61 @@ namespace nanoFramework.nanoCLR.Host.Profiler
1414
internal class ProfilerMessageProcessor : IDisposable
1515
{
1616
private readonly StreamWriter _profileLogWriter;
17+
private readonly string _logFilePath;
18+
private bool _disposedValue;
1719

1820
public ProfilerMessageProcessor()
1921
{
2022
// create output file for profiler messages
21-
string _profileLogFileName = $"{Guid.NewGuid()}_{DateTime.Now.ToString("s").Replace(":", "_")}.log";
23+
string _profileLogFileName = $"{Guid.NewGuid()}_{DateTime.Now.ToString("s").Replace(":", "_")}.txt";
2224

23-
string logFilePath = Path.Combine(Path.GetTempPath(), _profileLogFileName);
25+
_logFilePath = Path.Combine(Path.GetTempPath(), _profileLogFileName);
2426

2527
// inform user about the log file
26-
Console.WriteLine($"Profiler output file: {logFilePath}");
28+
Console.WriteLine($"{Environment.NewLine}Profiler output file: {_logFilePath}{Environment.NewLine}");
2729

2830
try
2931
{
3032
_profileLogWriter = new StreamWriter(
31-
logFilePath,
33+
_logFilePath,
3234
new FileStreamOptions() { Mode = FileMode.Create, Access = FileAccess.ReadWrite, Options = FileOptions.WriteThrough, Share = FileShare.ReadWrite });
3335
}
3436
catch (IOException ex)
3537
{
36-
throw new IOException($"Failed to create profiler log file at {logFilePath}", ex);
38+
throw new IOException($"Failed to create profiler log file at {_logFilePath}", ex);
3739
}
3840
}
3941
internal void MessageHandler(string message)
4042
{
4143
_profileLogWriter.WriteLine(message);
44+
// Ensure message is written immediately
45+
_profileLogWriter.Flush();
4246
}
4347

44-
public void Dispose() => ((IDisposable)_profileLogWriter).Dispose();
48+
#region Dispose implementation
49+
50+
protected virtual void Dispose(bool disposing)
51+
{
52+
if (!_disposedValue)
53+
{
54+
if (disposing)
55+
{
56+
// output the file path so user can find it (usefull in case of long running)
57+
Console.WriteLine($"{Environment.NewLine}Profiler output file: {_logFilePath}{Environment.NewLine}");
58+
59+
_profileLogWriter.Dispose();
60+
}
61+
62+
_disposedValue = true;
63+
}
64+
}
65+
66+
public void Dispose()
67+
{
68+
Dispose(disposing: true);
69+
GC.SuppressFinalize(this);
70+
}
71+
72+
#endregion
4573
}
4674
}

0 commit comments

Comments
 (0)