~ubuntu-branches/ubuntu/saucy/monodevelop/saucy-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Core/MonoDevelop.Core.LogReporting/CrashMonitor.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields, 1840cc1
  • Date: 2012-02-05 10:49:36 UTC
  • mfrom: (10.2.12)
  • Revision ID: package-import@ubuntu.com-20120205104936-f3dutq6lnseokb6d
Tags: 2.8.6.3+dfsg-1
[1840cc1] Imported Upstream version 2.8.6.3+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// CrashMonitor.cs
 
3
//  
 
4
// Author:
 
5
//       Alan McGovern <alan@xamarin.com>
 
6
// 
 
7
// Copyright 2011, Xamarin Inc.
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.IO;
 
29
using System.Threading;
 
30
 
 
31
namespace MonoDevelop.Core.LogReporting
 
32
{
 
33
        public abstract class CrashMonitor : ICrashMonitor
 
34
        {
 
35
                public static ICrashMonitor Create (int pid)
 
36
                {
 
37
                        // FIXME: Add in proper detection for mac/windows/linux as appropriate
 
38
                        return new MacCrashMonitor (pid);
 
39
                }
 
40
                
 
41
                public event EventHandler ApplicationExited;
 
42
                public event EventHandler<CrashEventArgs> CrashDetected;
 
43
                
 
44
                public int Pid {
 
45
                        get; private set;
 
46
                }
 
47
                
 
48
                FileSystemWatcher Watcher {
 
49
                        get; set;
 
50
                }
 
51
                
 
52
                protected CrashMonitor (int pid, string path)
 
53
                        : this (pid, path, "")
 
54
                {
 
55
                        
 
56
                }
 
57
                
 
58
                protected CrashMonitor (int pid, string path, string filter)
 
59
                {
 
60
                        Pid = pid;
 
61
                        Watcher = new FileSystemWatcher (path, filter);
 
62
                        Watcher.Created += (o, e) => {
 
63
                                OnCrashDetected (new CrashEventArgs (e.FullPath));
 
64
                        };
 
65
                        
 
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) {
 
73
                                        Thread.Sleep (1000);
 
74
                                        info.Refresh ();
 
75
                                }
 
76
                                
 
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.
 
80
                                Thread.Sleep (5000);
 
81
                                OnApplicationExited ();
 
82
                        });
 
83
                }
 
84
                
 
85
                protected virtual void OnApplicationExited ()
 
86
                {
 
87
                        if (ApplicationExited != null)
 
88
                                ApplicationExited (this, EventArgs.Empty);
 
89
                }
 
90
                
 
91
                protected virtual void OnCrashDetected (CrashEventArgs e)
 
92
                {
 
93
                        if (CrashDetected != null)
 
94
                                CrashDetected (this, e);
 
95
                }
 
96
 
 
97
                public void Start () {
 
98
                        Watcher.EnableRaisingEvents = true;
 
99
                }
 
100
 
 
101
                public void Stop () {
 
102
                        Watcher.EnableRaisingEvents = false;
 
103
                }
 
104
        }
 
105
}
 
 
b'\\ No newline at end of file'