Repository navigation
Expand file tree
/
Copy pathCrashDiagnostics.cs
More file actions
281 lines (249 loc) · 11.9 KB
/
Copy pathCrashDiagnostics.cs
File metadata and controls
281 lines (249 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WinRT.Interop;
using Windows.ApplicationModel.Core;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using Windows.System;
using Windows.UI.Popups;
namespace NativeScriptWindowsDemo
{
internal static class CrashDiagnostics
{
private static bool _installed;
public static void InstallGlobalHandlers()
{
if (_installed) return;
_installed = true;
AppDomain.CurrentDomain.UnhandledException += (_, args) =>
{
var ex = args.ExceptionObject as Exception;
WriteExceptionReport("AppDomain.UnhandledException", ex, "IsTerminating=" + args.IsTerminating);
};
TaskScheduler.UnobservedTaskException += (_, args) =>
{
// Always mark the exception as observed to prevent the finalizer
// from rethrowing it on the finalizer thread.
try { args.SetObserved(); } catch { }
// Log the exception but never allow logging failures to propagate
// (this handler runs on the finalizer thread in some cases).
try
{
WriteExceptionReport("TaskScheduler.UnobservedTaskException", args.Exception, null);
}
catch (Exception logEx)
{
try { WriteToTraceLog("[CrashDiagnostics] Failed to write unobserved exception: " + logEx); } catch { }
}
};
}
public static void WriteExceptionReport(string source, Exception ex, string details)
{
var sb = new StringBuilder();
sb.AppendLine("============================================================");
sb.AppendLine("Timestamp: " + DateTimeOffset.UtcNow.ToString("o"));
sb.AppendLine("Source: " + source);
if (!string.IsNullOrWhiteSpace(details)) sb.AppendLine("Details: " + details);
if (ex != null) { sb.AppendLine("Exception:"); sb.AppendLine(ex.ToString()); }
else sb.AppendLine("Exception: <null>");
sb.AppendLine("Managed stack snapshot:");
sb.AppendLine(Environment.StackTrace);
sb.AppendLine();
AppendToLog(sb.ToString());
}
public static void WriteMessage(string source, string message)
{
var sb = new StringBuilder();
sb.AppendLine("============================================================");
sb.AppendLine("Timestamp: " + DateTimeOffset.UtcNow.ToString("o"));
sb.AppendLine("Source: " + source);
sb.AppendLine("Message: " + message);
sb.AppendLine();
AppendToLog(sb.ToString());
}
public static string BuildErrorReport(Exception ex, string jsError = null, string extraDetails = null)
{
var sb = new StringBuilder();
if (!string.IsNullOrWhiteSpace(jsError))
{
sb.AppendLine("── JavaScript Error ──────────────────────────────────────");
sb.AppendLine(jsError.Trim());
sb.AppendLine();
}
if (ex != null)
{
sb.AppendLine("── Native Exception ──────────────────────────────────────");
sb.AppendLine(ex.GetType().Name + ": " + ex.Message);
if (ex.StackTrace != null) sb.AppendLine(ex.StackTrace);
var inner = ex.InnerException;
while (inner != null)
{
sb.AppendLine("Caused by: " + inner.GetType().Name + ": " + inner.Message);
if (inner.StackTrace != null) sb.AppendLine(inner.StackTrace);
inner = inner.InnerException;
}
sb.AppendLine();
}
// Include panic log from previous or current run
try
{
var panicLog = Path.Combine(ApplicationData.Current.LocalFolder.Path, "nativescript-panic.log");
if (File.Exists(panicLog))
{
var content = File.ReadAllText(panicLog, Encoding.UTF8).Trim();
if (!string.IsNullOrEmpty(content))
{
sb.AppendLine("── Rust Panic Log ────────────────────────────────────────");
sb.AppendLine(content);
sb.AppendLine();
}
}
}
catch { }
if (!string.IsNullOrWhiteSpace(extraDetails))
{
sb.AppendLine("── Additional Info ───────────────────────────────────────");
sb.AppendLine(extraDetails);
sb.AppendLine();
}
sb.AppendLine("──────────────────────────────────────────────────────────");
sb.AppendLine("Timestamp: " + DateTimeOffset.UtcNow.ToString("o"));
return sb.ToString();
}
public static string CrashLogPath()
{
try { return Path.Combine(ApplicationData.Current.LocalFolder.Path, "nativescript-crash.log"); }
catch { return null; }
}
/// Parses the first "path:line:col" source location from a JS stack trace.
/// Returns null if none found. Handles Windows drive-letter paths (C:\...).
public static (string file, int line, int col)? TryExtractSourceLocation(string errorReport)
{
if (string.IsNullOrEmpty(errorReport)) return null;
// Match the contents of the first (path:line:col) group in the stack trace.
var m = Regex.Match(errorReport, @"\(([^)]+)\)");
if (!m.Success) return null;
var inner = m.Groups[1].Value; // e.g. "C:\app\bundle.js:42:13"
// Parse col from the right (last :digits).
var lastColon = inner.LastIndexOf(':');
if (lastColon < 0 || !int.TryParse(inner.Substring(lastColon + 1), out int col)) return null;
inner = inner.Substring(0, lastColon);
// Parse line from the right (second-to-last :digits).
lastColon = inner.LastIndexOf(':');
if (lastColon < 0 || !int.TryParse(inner.Substring(lastColon + 1), out int line)) return null;
var file = inner.Substring(0, lastColon);
return string.IsNullOrEmpty(file) ? null : (file, line, col);
}
public static async Task ShowCrashDialogAsync(string heading, string errorReport)
{
try
{
// Write full details to log so developer can access them even if the
// dialog summary is truncated.
var logPath = CrashLogPath();
if (logPath != null)
File.WriteAllText(logPath, errorReport, Encoding.UTF8);
// Truncate for the dialog (MessageDialog has practical limits).
const int MaxLen = 800;
var summary = errorReport.Length > MaxLen
? errorReport.Substring(0, MaxLen) + "\n\n[truncated — see log for full details]"
: errorReport;
if (logPath != null)
summary += "\n\nFull log: " + logPath;
var dialog = new MessageDialog(summary, "NativeScript Runtime Error");
try
{
var window = App.CurrentWindow;
if (window != null)
{
InitializeWithWindow.Initialize(dialog, WindowNative.GetWindowHandle(window));
}
}
catch { }
// MessageDialog supports at most 3 commands. When a source location is
// available, swap "Copy Details" for "Open in VS Code" so the user can
// choose between jumping to the error or restarting. Full details are
// always written to the log file shown in the dialog text.
var srcLocation = TryExtractSourceLocation(errorReport);
if (srcLocation.HasValue)
{
var (srcFile, srcLine, srcCol) = srcLocation.Value;
dialog.Commands.Add(new UICommand("Open Source File", async _ =>
{
try
{
var normalizedPath = srcFile.Replace('\\', '/');
var uriStr = $"file:///{normalizedPath}";
if (Uri.TryCreate(uriStr, UriKind.Absolute, out var fileUri))
await Launcher.LaunchUriAsync(fileUri,
new LauncherOptions { DisplayApplicationPicker = true });
}
catch { }
}));
}
else
{
dialog.Commands.Add(new UICommand("Copy Details", _ =>
{
try
{
var dp = new DataPackage();
dp.SetText(errorReport);
Clipboard.SetContent(dp);
}
catch { }
}));
}
dialog.Commands.Add(new UICommand("Restart App", async _ =>
{
try { await CoreApplication.RequestRestartAsync("crash-restart"); }
catch { }
}));
dialog.Commands.Add(new UICommand("Dismiss"));
dialog.DefaultCommandIndex = 0;
dialog.CancelCommandIndex = 2;
await dialog.ShowAsync();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("[NativeScript] Failed to show crash dialog: " + ex.Message);
}
}
/// <summary>
/// Appends <paramref name="message"/> to the runtime trace file in the Win32 temp path.
/// Prefer `console.log`; fall back to `ns_trace.log` for CLI/legacy compatibility.
/// In a packaged Windows app, GetTempPath() resolves inside the app container temp area,
/// matching Rust's std::env::temp_dir() so the CLI still sees the same log stream.
/// </summary>
public static void WriteToTraceLog(string message)
{
try
{
// System.IO.Path.GetTempPath() calls Win32 GetTempPathW() which for a packaged
// desktop app resolves into the container temp folder, matching Rust.
var temp = System.IO.Path.GetTempPath();
var consolePath = Path.Combine(temp, "console.log");
File.AppendAllText(consolePath, message, Encoding.UTF8);
// Maintain legacy compatibility: also append to ns_trace.log if it already exists
var legacyPath = Path.Combine(temp, "ns_trace.log");
if (File.Exists(legacyPath))
{
File.AppendAllText(legacyPath, message, Encoding.UTF8);
}
}
catch { }
}
private static void AppendToLog(string content)
{
try
{
var logPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "nativescript-crash.log");
File.AppendAllText(logPath, content, Encoding.UTF8);
}
catch { }
}
}
}