]>
Commit | Line | Data |
---|---|---|
dd85fc6b VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: logg.cpp | |
3 | // Purpose: wxLog-derived classes which need GUI support (the rest is in | |
4 | // src/common/log.cpp) | |
5 | // Author: Vadim Zeitlin | |
6 | // Modified by: | |
7 | // Created: 20.09.99 (extracted from src/common/log.cpp) | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
10 | // Licence: wxWindows license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | // ============================================================================ | |
14 | // declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
21 | // no #pragma implementation "log.h" because it's already in src/common/log.cpp | |
22 | ||
23 | // For compilers that support precompilation, includes "wx.h". | |
24 | #include "wx/wxprec.h" | |
25 | ||
26 | #ifdef __BORLANDC__ | |
27 | #pragma hdrstop | |
28 | #endif | |
29 | ||
30 | #ifdef wxUSE_NOGUI | |
31 | #error "This file can't be compiled in NOGUI mode!" | |
32 | #endif | |
33 | ||
34 | #ifndef WX_PRECOMP | |
35 | #include "wx/app.h" | |
36 | #include "wx/intl.h" | |
37 | #include "wx/log.h" | |
38 | #include "wx/menu.h" | |
39 | #include "wx/frame.h" | |
40 | #include "wx/filedlg.h" | |
41 | #include "wx/msgdlg.h" | |
42 | #include "wx/textctrl.h" | |
43 | #endif // WX_PRECOMP | |
44 | ||
45 | #include "wx/file.h" | |
46 | #include "wx/textfile.h" | |
47 | ||
48 | // ---------------------------------------------------------------------------- | |
49 | // global variables | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
52 | // we use a global variable to store the frame pointer for wxLogStatus - bad, | |
53 | // but it's he easiest way | |
54 | static wxFrame *gs_pFrame; // FIXME MT-unsafe | |
55 | ||
56 | // ============================================================================ | |
57 | // implementation | |
58 | // ============================================================================ | |
59 | ||
60 | // ---------------------------------------------------------------------------- | |
61 | // global functions | |
62 | // ---------------------------------------------------------------------------- | |
63 | ||
64 | // accepts an additional argument which tells to which frame the output should | |
65 | // be directed | |
66 | void wxLogStatus(wxFrame *pFrame, const wxChar *szFormat, ...) | |
67 | { | |
68 | wxString msg; | |
69 | ||
70 | wxLog *pLog = wxLog::GetActiveTarget(); | |
71 | if ( pLog != NULL ) { | |
72 | va_list argptr; | |
73 | va_start(argptr, szFormat); | |
74 | msg.PrintfV(szFormat, argptr); | |
75 | va_end(argptr); | |
76 | ||
77 | wxASSERT( gs_pFrame == NULL ); // should be reset! | |
78 | gs_pFrame = pFrame; | |
79 | wxLog::OnLog(wxLOG_Status, msg, time(NULL)); | |
80 | gs_pFrame = (wxFrame *) NULL; | |
81 | } | |
82 | } | |
83 | ||
84 | // ---------------------------------------------------------------------------- | |
85 | // wxLogTextCtrl implementation | |
86 | // ---------------------------------------------------------------------------- | |
87 | ||
88 | wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl) | |
89 | { | |
90 | m_pTextCtrl = pTextCtrl; | |
91 | } | |
92 | ||
5e0201ea | 93 | void wxLogTextCtrl::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
dd85fc6b VZ |
94 | { |
95 | wxString msg; | |
96 | TimeStamp(&msg); | |
97 | msg << szString << _T('\n'); | |
98 | ||
99 | m_pTextCtrl->AppendText(msg); | |
100 | } | |
101 | ||
102 | // ---------------------------------------------------------------------------- | |
103 | // wxLogGui implementation (FIXME MT-unsafe) | |
104 | // ---------------------------------------------------------------------------- | |
105 | ||
106 | wxLogGui::wxLogGui() | |
107 | { | |
108 | Clear(); | |
109 | } | |
110 | ||
111 | void wxLogGui::Clear() | |
112 | { | |
113 | m_bErrors = m_bWarnings = FALSE; | |
114 | m_aMessages.Empty(); | |
115 | m_aTimes.Empty(); | |
116 | } | |
117 | ||
118 | void wxLogGui::Flush() | |
119 | { | |
120 | if ( !m_bHasMessages ) | |
121 | return; | |
122 | ||
123 | // do it right now to block any new calls to Flush() while we're here | |
124 | m_bHasMessages = FALSE; | |
125 | ||
126 | // concatenate all strings (but not too many to not overfill the msg box) | |
127 | wxString str; | |
128 | size_t nLines = 0, | |
129 | nMsgCount = m_aMessages.Count(); | |
130 | ||
131 | // start from the most recent message | |
132 | for ( size_t n = nMsgCount; n > 0; n-- ) { | |
133 | // for Windows strings longer than this value are wrapped (NT 4.0) | |
134 | const size_t nMsgLineWidth = 156; | |
135 | ||
136 | nLines += (m_aMessages[n - 1].Len() + nMsgLineWidth - 1) / nMsgLineWidth; | |
137 | ||
138 | if ( nLines > 25 ) // don't put too many lines in message box | |
139 | break; | |
140 | ||
141 | str << m_aMessages[n - 1] << _T("\n"); | |
142 | } | |
143 | ||
144 | const wxChar *title; | |
145 | long style; | |
146 | ||
147 | if ( m_bErrors ) { | |
148 | title = _("Error"); | |
149 | style = wxICON_STOP; | |
150 | } | |
151 | else if ( m_bWarnings ) { | |
152 | title = _("Warning"); | |
153 | style = wxICON_EXCLAMATION; | |
154 | } | |
155 | else { | |
156 | title = _("Information"); | |
157 | style = wxICON_INFORMATION; | |
158 | } | |
159 | ||
160 | wxMessageBox(str, title, wxOK | style); | |
161 | ||
162 | // no undisplayed messages whatsoever | |
163 | Clear(); | |
164 | ||
165 | // do it here again | |
166 | m_bHasMessages = FALSE; | |
167 | } | |
168 | ||
169 | // the default behaviour is to discard all informational messages if there | |
170 | // are any errors/warnings. | |
171 | void wxLogGui::DoLog(wxLogLevel level, const wxChar *szString, time_t t) | |
172 | { | |
173 | switch ( level ) { | |
174 | case wxLOG_Info: | |
175 | if ( GetVerbose() ) | |
176 | case wxLOG_Message: | |
177 | if ( !m_bErrors ) { | |
178 | m_aMessages.Add(szString); | |
179 | m_aTimes.Add((long)t); | |
180 | m_bHasMessages = TRUE; | |
181 | } | |
182 | break; | |
183 | ||
184 | case wxLOG_Status: | |
185 | #if wxUSE_STATUSBAR | |
186 | { | |
187 | // find the top window and set it's status text if it has any | |
188 | wxFrame *pFrame = gs_pFrame; | |
189 | if ( pFrame == NULL ) { | |
190 | wxWindow *pWin = wxTheApp->GetTopWindow(); | |
191 | if ( pWin != NULL && pWin->IsKindOf(CLASSINFO(wxFrame)) ) { | |
192 | pFrame = (wxFrame *)pWin; | |
193 | } | |
194 | } | |
195 | ||
196 | if ( pFrame != NULL ) | |
197 | pFrame->SetStatusText(szString); | |
198 | } | |
199 | #endif // wxUSE_STATUSBAR | |
200 | break; | |
201 | ||
202 | case wxLOG_Trace: | |
203 | case wxLOG_Debug: | |
204 | #ifdef __WXDEBUG__ | |
205 | { | |
206 | #ifdef __WXMSW__ | |
207 | // don't prepend debug/trace here: it goes to the | |
208 | // debug window anyhow, but do put a timestamp | |
209 | wxString str; | |
210 | TimeStamp(&str); | |
211 | str << szString << _T("\n\r"); | |
212 | OutputDebugString(str); | |
213 | #else | |
214 | // send them to stderr | |
215 | wxFprintf(stderr, _T("%s: %s\n"), | |
216 | level == wxLOG_Trace ? _T("Trace") | |
217 | : _T("Debug"), | |
218 | szString); | |
219 | fflush(stderr); | |
220 | #endif | |
221 | } | |
222 | #endif // __WXDEBUG__ | |
223 | ||
224 | break; | |
225 | ||
226 | case wxLOG_FatalError: | |
227 | // show this one immediately | |
228 | wxMessageBox(szString, _("Fatal error"), wxICON_HAND); | |
229 | break; | |
230 | ||
231 | case wxLOG_Error: | |
232 | // discard earlier informational messages if this is the 1st | |
233 | // error because they might not make sense any more | |
234 | if ( !m_bErrors ) { | |
235 | m_aMessages.Empty(); | |
236 | m_aTimes.Empty(); | |
237 | m_bErrors = TRUE; | |
238 | } | |
239 | // fall through | |
240 | ||
241 | case wxLOG_Warning: | |
242 | if ( !m_bErrors ) { | |
243 | // for the warning we don't discard the info messages | |
244 | m_bWarnings = TRUE; | |
245 | } | |
246 | ||
247 | m_aMessages.Add(szString); | |
248 | m_aTimes.Add((long)t); | |
249 | m_bHasMessages = TRUE; | |
250 | break; | |
251 | } | |
252 | } | |
253 | ||
254 | // ---------------------------------------------------------------------------- | |
255 | // wxLogWindow and wxLogFrame implementation | |
256 | // ---------------------------------------------------------------------------- | |
257 | ||
258 | // log frame class | |
259 | // --------------- | |
260 | class wxLogFrame : public wxFrame | |
261 | { | |
262 | public: | |
263 | // ctor & dtor | |
264 | wxLogFrame(wxFrame *pParent, wxLogWindow *log, const wxChar *szTitle); | |
265 | virtual ~wxLogFrame(); | |
266 | ||
267 | // menu callbacks | |
268 | void OnClose(wxCommandEvent& event); | |
269 | void OnCloseWindow(wxCloseEvent& event); | |
270 | #if wxUSE_FILE | |
271 | void OnSave (wxCommandEvent& event); | |
272 | #endif // wxUSE_FILE | |
273 | void OnClear(wxCommandEvent& event); | |
274 | ||
275 | void OnIdle(wxIdleEvent&); | |
276 | ||
277 | // accessors | |
278 | wxTextCtrl *TextCtrl() const { return m_pTextCtrl; } | |
279 | ||
280 | private: | |
281 | enum | |
282 | { | |
283 | Menu_Close = 100, | |
284 | Menu_Save, | |
285 | Menu_Clear | |
286 | }; | |
287 | ||
288 | // instead of closing just hide the window to be able to Show() it later | |
289 | void DoClose() { Show(FALSE); } | |
290 | ||
291 | wxTextCtrl *m_pTextCtrl; | |
292 | wxLogWindow *m_log; | |
293 | ||
294 | DECLARE_EVENT_TABLE() | |
295 | }; | |
296 | ||
297 | BEGIN_EVENT_TABLE(wxLogFrame, wxFrame) | |
298 | // wxLogWindow menu events | |
299 | EVT_MENU(Menu_Close, wxLogFrame::OnClose) | |
300 | #if wxUSE_FILE | |
301 | EVT_MENU(Menu_Save, wxLogFrame::OnSave) | |
302 | #endif // wxUSE_FILE | |
303 | EVT_MENU(Menu_Clear, wxLogFrame::OnClear) | |
304 | ||
305 | EVT_CLOSE(wxLogFrame::OnCloseWindow) | |
306 | END_EVENT_TABLE() | |
307 | ||
308 | wxLogFrame::wxLogFrame(wxFrame *pParent, wxLogWindow *log, const wxChar *szTitle) | |
309 | : wxFrame(pParent, -1, szTitle) | |
310 | { | |
311 | m_log = log; | |
312 | ||
313 | m_pTextCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition, | |
314 | wxDefaultSize, | |
315 | wxTE_MULTILINE | | |
316 | wxHSCROLL | | |
317 | wxTE_READONLY); | |
318 | ||
319 | // create menu | |
320 | wxMenuBar *pMenuBar = new wxMenuBar; | |
321 | wxMenu *pMenu = new wxMenu; | |
322 | #if wxUSE_FILE | |
323 | pMenu->Append(Menu_Save, _("&Save..."), _("Save log contents to file")); | |
324 | #endif // wxUSE_FILE | |
325 | pMenu->Append(Menu_Clear, _("C&lear"), _("Clear the log contents")); | |
326 | pMenu->AppendSeparator(); | |
327 | pMenu->Append(Menu_Close, _("&Close"), _("Close this window")); | |
328 | pMenuBar->Append(pMenu, _("&Log")); | |
329 | SetMenuBar(pMenuBar); | |
330 | ||
331 | #if wxUSE_STATUSBAR | |
332 | // status bar for menu prompts | |
333 | CreateStatusBar(); | |
334 | #endif // wxUSE_STATUSBAR | |
335 | ||
336 | m_log->OnFrameCreate(this); | |
337 | } | |
338 | ||
339 | void wxLogFrame::OnClose(wxCommandEvent& WXUNUSED(event)) | |
340 | { | |
341 | DoClose(); | |
342 | } | |
343 | ||
344 | void wxLogFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
345 | { | |
346 | DoClose(); | |
347 | } | |
348 | ||
349 | #if wxUSE_FILE | |
350 | void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event)) | |
351 | { | |
352 | // get the file name | |
353 | // ----------------- | |
354 | const wxChar *szFileName = wxSaveFileSelector(_T("log"), _T("txt"), _T("log.txt")); | |
355 | if ( szFileName == NULL ) { | |
356 | // cancelled | |
357 | return; | |
358 | } | |
359 | ||
360 | // open file | |
361 | // --------- | |
362 | wxFile file; | |
363 | bool bOk = FALSE; | |
364 | if ( wxFile::Exists(szFileName) ) { | |
365 | bool bAppend = FALSE; | |
366 | wxString strMsg; | |
367 | strMsg.Printf(_("Append log to file '%s' " | |
368 | "(choosing [No] will overwrite it)?"), szFileName); | |
369 | switch ( wxMessageBox(strMsg, _("Question"), wxYES_NO | wxCANCEL) ) { | |
370 | case wxYES: | |
371 | bAppend = TRUE; | |
372 | break; | |
373 | ||
374 | case wxNO: | |
375 | bAppend = FALSE; | |
376 | break; | |
377 | ||
378 | case wxCANCEL: | |
379 | return; | |
380 | ||
381 | default: | |
382 | wxFAIL_MSG(_("invalid message box return value")); | |
383 | } | |
384 | ||
385 | if ( bAppend ) { | |
386 | bOk = file.Open(szFileName, wxFile::write_append); | |
387 | } | |
388 | else { | |
389 | bOk = file.Create(szFileName, TRUE /* overwrite */); | |
390 | } | |
391 | } | |
392 | else { | |
393 | bOk = file.Create(szFileName); | |
394 | } | |
395 | ||
396 | // retrieve text and save it | |
397 | // ------------------------- | |
398 | int nLines = m_pTextCtrl->GetNumberOfLines(); | |
399 | for ( int nLine = 0; bOk && nLine < nLines; nLine++ ) { | |
400 | bOk = file.Write(m_pTextCtrl->GetLineText(nLine) + | |
401 | // we're not going to pull in the whole wxTextFile if all we need is this... | |
402 | #if wxUSE_TEXTFILE | |
403 | wxTextFile::GetEOL() | |
404 | #else // !wxUSE_TEXTFILE | |
405 | '\n' | |
406 | #endif // wxUSE_TEXTFILE | |
407 | ); | |
408 | } | |
409 | ||
410 | if ( bOk ) | |
411 | bOk = file.Close(); | |
412 | ||
413 | if ( !bOk ) { | |
414 | wxLogError(_("Can't save log contents to file.")); | |
415 | } | |
416 | else { | |
417 | wxLogStatus(this, _("Log saved to the file '%s'."), szFileName); | |
418 | } | |
419 | } | |
420 | #endif // wxUSE_FILE | |
421 | ||
422 | void wxLogFrame::OnClear(wxCommandEvent& WXUNUSED(event)) | |
423 | { | |
424 | m_pTextCtrl->Clear(); | |
425 | } | |
426 | ||
427 | wxLogFrame::~wxLogFrame() | |
428 | { | |
429 | m_log->OnFrameDelete(this); | |
430 | } | |
431 | ||
432 | // wxLogWindow | |
433 | // ----------- | |
434 | wxLogWindow::wxLogWindow(wxFrame *pParent, | |
435 | const wxChar *szTitle, | |
436 | bool bShow, | |
437 | bool bDoPass) | |
438 | { | |
439 | m_bPassMessages = bDoPass; | |
440 | ||
441 | m_pLogFrame = new wxLogFrame(pParent, this, szTitle); | |
442 | m_pOldLog = wxLog::SetActiveTarget(this); | |
443 | ||
444 | if ( bShow ) | |
445 | m_pLogFrame->Show(TRUE); | |
446 | } | |
447 | ||
448 | void wxLogWindow::Show(bool bShow) | |
449 | { | |
450 | m_pLogFrame->Show(bShow); | |
451 | } | |
452 | ||
453 | void wxLogWindow::Flush() | |
454 | { | |
455 | if ( m_pOldLog != NULL ) | |
456 | m_pOldLog->Flush(); | |
457 | ||
458 | m_bHasMessages = FALSE; | |
459 | } | |
460 | ||
461 | void wxLogWindow::DoLog(wxLogLevel level, const wxChar *szString, time_t t) | |
462 | { | |
463 | // first let the previous logger show it | |
464 | if ( m_pOldLog != NULL && m_bPassMessages ) { | |
465 | // FIXME why can't we access protected wxLog method from here (we derive | |
466 | // from wxLog)? gcc gives "DoLog is protected in this context", what | |
467 | // does this mean? Anyhow, the cast is harmless and let's us do what | |
468 | // we want. | |
469 | ((wxLogWindow *)m_pOldLog)->DoLog(level, szString, t); | |
470 | } | |
471 | ||
472 | if ( m_pLogFrame ) { | |
473 | switch ( level ) { | |
474 | case wxLOG_Status: | |
475 | // by default, these messages are ignored by wxLog, so process | |
476 | // them ourselves | |
477 | if ( !wxIsEmpty(szString) ) | |
478 | { | |
479 | wxString str; | |
480 | str << _("Status: ") << szString; | |
481 | DoLogString(str, t); | |
482 | } | |
483 | break; | |
484 | ||
485 | // don't put trace messages in the text window for 2 reasons: | |
486 | // 1) there are too many of them | |
487 | // 2) they may provoke other trace messages thus sending a program | |
488 | // into an infinite loop | |
489 | case wxLOG_Trace: | |
490 | break; | |
491 | ||
492 | default: | |
493 | // and this will format it nicely and call our DoLogString() | |
494 | wxLog::DoLog(level, szString, t); | |
495 | } | |
496 | } | |
497 | ||
498 | m_bHasMessages = TRUE; | |
499 | } | |
500 | ||
501 | void wxLogWindow::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) | |
502 | { | |
503 | // put the text into our window | |
504 | wxTextCtrl *pText = m_pLogFrame->TextCtrl(); | |
505 | ||
506 | // remove selection (WriteText is in fact ReplaceSelection) | |
507 | #ifdef __WXMSW__ | |
508 | long nLen = pText->GetLastPosition(); | |
509 | pText->SetSelection(nLen, nLen); | |
510 | #endif // Windows | |
511 | ||
512 | wxString msg; | |
513 | TimeStamp(&msg); | |
514 | msg << szString << _T('\n'); | |
515 | ||
516 | pText->AppendText(msg); | |
517 | ||
518 | // TODO ensure that the line can be seen | |
519 | } | |
520 | ||
521 | wxFrame *wxLogWindow::GetFrame() const | |
522 | { | |
523 | return m_pLogFrame; | |
524 | } | |
525 | ||
526 | void wxLogWindow::OnFrameCreate(wxFrame * WXUNUSED(frame)) | |
527 | { | |
528 | } | |
529 | ||
530 | void wxLogWindow::OnFrameDelete(wxFrame * WXUNUSED(frame)) | |
531 | { | |
532 | m_pLogFrame = (wxLogFrame *)NULL; | |
533 | } | |
534 | ||
535 | wxLogWindow::~wxLogWindow() | |
536 | { | |
537 | delete m_pOldLog; | |
538 | ||
539 | // may be NULL if log frame already auto destroyed itself | |
540 | delete m_pLogFrame; | |
541 | } | |
542 |