]> git.saurik.com Git - wxWidgets.git/blob - utils/execmon/execmon.cpp
Deprecate old style wxPanel ctor taking separate coordinates.
[wxWidgets.git] / utils / execmon / execmon.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: execmon.cpp
3 // Purpose: A simple execution monitor to test if wx samples crash at startup or not
4 // Author: Francesco Montorsi
5 // Modified by:
6 // Created: 25/3/09
7 // RCS-ID: $Id$
8 // Copyright: (c) 2009 Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/app.h"
29 #include "wx/log.h"
30 #endif // WX_PRECOMP
31
32 #include "wx/cmdline.h"
33 #include "wx/vector.h"
34 #include "wx/process.h"
35 #include "wx/sstream.h"
36 #include "wx/utils.h"
37 #include "wx/filename.h"
38 #include "wx/app.h"
39 #include "wx/log.h"
40
41 // ============================================================================
42 // implementation
43 // ============================================================================
44
45 // ----------------------------------------------------------------------------
46 // utility classes
47 // ----------------------------------------------------------------------------
48
49 class MonitoredProcess : public wxProcess
50 {
51 public:
52 MonitoredProcess()
53 { Redirect(); m_crashed=false; m_exitCode=0; }
54
55 void OnTerminate(int WXUNUSED(pid), int status)
56 {
57 wxStringOutputStream out, err;
58 if (GetInputStream()) out.Write(*GetInputStream());
59 if (GetErrorStream()) err.Write(*GetErrorStream());
60
61 //wxPrintf("%s\n", stdout.GetString());
62 //wxPrintf("%s\n", stderr.GetString());
63
64 // when wx programs assert on wxGTK/wxMac, they put on stderr a message like:
65 // [Debug] date somefilename.pp(nnnn): assert "xxxxx" failed in yyyy
66 // but then the assert dialog pop-ups and thus the app doesn't exit
67 // FIXME: make assertion detection work also under other platforms
68 // see http://trac.wxwidgets.org/ticket/10697
69 m_crashed = out.GetString().Contains("assert") ||
70 err.GetString().Contains("assert");
71 m_exitCode = status;
72 }
73
74 void Kill()
75 {
76 wxProcess::Kill(GetPid());
77
78 // wxProcess::Kill doesn't trigger a call to OnTerminate() normally...
79 // but we still need to call it!
80 OnTerminate(0, -1);
81 }
82
83 bool Crashed() const
84 { return m_crashed; }
85
86 int GetExitCode() const
87 { return m_exitCode; }
88
89 private:
90 bool m_crashed;
91 int m_exitCode;
92 };
93
94 class MonitorData
95 {
96 public:
97 MonitorData(const wxString& cmd) : program(cmd) {}
98
99 wxString program;
100 MonitoredProcess process;
101 };
102
103 // ----------------------------------------------------------------------------
104 // the real main
105 // ----------------------------------------------------------------------------
106
107 bool TestExec(const wxVector<wxFileName>& programs, long timeout)
108 {
109 size_t i;
110 wxVector<MonitorData*> data;
111
112 // run all programs specified as command line parameters
113 wxArrayLong procID;
114 for (i=0; i<programs.size(); i++)
115 {
116 MonitorData *dt = new MonitorData(programs[i].GetFullPath());
117
118 long pid = wxExecute(programs[i].GetFullPath(), wxEXEC_ASYNC, &dt->process);
119 if (pid == 0)
120 {
121 wxLogError("could not run the program '%s'", programs[i].GetFullPath());
122 }
123 else
124 {
125 wxLogMessage("started program '%s' (pid %d)...",
126 programs[i].GetFullPath(), pid);
127 wxASSERT(dt->process.GetPid() == pid);
128
129 data.push_back(dt);
130 }
131 }
132
133 // sleep some moments
134 wxSleep(timeout);
135
136 // check if all processes are still running
137 bool allok = true;
138 for (i=0; i<data.size(); i++)
139 {
140 MonitoredProcess& proc = data[i]->process;
141 const wxString& prog = data[i]->program;
142
143 if (wxProcess::Exists(proc.GetPid()))
144 proc.Kill();
145 else
146 {
147 // this typically never happens, at least when running wx-programs
148 // built with debug builds of wx (see MonitoredProcess::OnTerminate;
149 // even if an asserts fail the app doesn't automatically close!):
150
151 wxLogMessage("program '%s' (pid %d) is NOT running anymore...",
152 prog, proc.GetPid());
153 allok = false;
154 }
155
156 if (data[i]->process.Crashed())
157 {
158 allok = false;
159 wxLogMessage("program '%s' (pid %d) crashed...",
160 prog, proc.GetPid());
161 }
162 else
163 wxLogMessage("program '%s' (pid %d) ended with exit code %d...",
164 prog, proc.GetPid(), proc.GetExitCode());
165 }
166
167 return allok;
168 }
169
170 // ----------------------------------------------------------------------------
171 // main
172 // ----------------------------------------------------------------------------
173
174 int main(int argc, char **argv)
175 {
176 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "execmon");
177
178 wxInitializer initializer;
179 if ( !initializer )
180 {
181 fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
182 return -1;
183 }
184
185 static const wxCmdLineEntryDesc cmdLineDesc[] =
186 {
187 { wxCMD_LINE_SWITCH, "h", "help",
188 "show this help message",
189 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
190 { wxCMD_LINE_OPTION, "t", "timeout",
191 "kills all processes still alive after 'num' seconds",
192 wxCMD_LINE_VAL_NUMBER, 0 },
193 { wxCMD_LINE_PARAM, "", "",
194 "program-to-run",
195 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
196
197 { wxCMD_LINE_NONE }
198 };
199
200 wxLog::DisableTimestamp();
201
202 wxCmdLineParser parser(cmdLineDesc, argc, argv);
203 switch ( parser.Parse() )
204 {
205 case -1:
206 // help was shown
207 break;
208
209 case 0:
210 {
211 // check arguments
212 wxVector<wxFileName> programs;
213 for (unsigned int i=0; i<parser.GetParamCount(); i++)
214 {
215 wxFileName fn(parser.GetParam(i));
216 if (!fn.IsAbsolute())
217 fn.MakeAbsolute();
218
219 programs.push_back(fn);
220 }
221
222 long timeout;
223 if (!parser.Found("t", &timeout))
224 timeout = 3;
225
226 return TestExec(programs, timeout) ? 0 : 1;
227 }
228 break;
229
230 default:
231 // syntax error
232 break;
233 }
234
235 return 0;
236 }
237