5
// Alan McGovern <alan@xamarin.com>
7
// Copyright 2011, Xamarin Inc.
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29
using System.Threading;
31
namespace MonoDevelop.Core.LogReporting
33
public abstract class CrashMonitor : ICrashMonitor
35
public static ICrashMonitor Create (int pid)
37
// FIXME: Add in proper detection for mac/windows/linux as appropriate
38
return new MacCrashMonitor (pid);
41
public event EventHandler ApplicationExited;
42
public event EventHandler<CrashEventArgs> CrashDetected;
48
FileSystemWatcher Watcher {
52
protected CrashMonitor (int pid, string path)
53
: this (pid, path, "")
58
protected CrashMonitor (int pid, string path, string filter)
61
Watcher = new FileSystemWatcher (path, filter);
62
Watcher.Created += (o, e) => {
63
OnCrashDetected (new CrashEventArgs (e.FullPath));
66
// Wait for the parent MD process to exit. This could be a crash
67
// or a graceful exit.
68
ThreadPool.QueueUserWorkItem (o => {
69
// Do a loop rather than calling WaitForExit or hooking into
70
// the Exited event as those do not work on MacOS on mono 2.10.3
71
var info = System.Diagnostics.Process.GetProcessById (Pid);
72
while (!info.HasExited) {
77
// If the application has crashed we want to wait a few seconds before
78
// raising this event so we allow time for the native crash reporter to
79
// write its log files.
81
OnApplicationExited ();
85
protected virtual void OnApplicationExited ()
87
if (ApplicationExited != null)
88
ApplicationExited (this, EventArgs.Empty);
91
protected virtual void OnCrashDetected (CrashEventArgs e)
93
if (CrashDetected != null)
94
CrashDetected (this, e);
97
public void Start () {
98
Watcher.EnableRaisingEvents = true;
101
public void Stop () {
102
Watcher.EnableRaisingEvents = false;
b'\\ No newline at end of file'