]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: log.cpp | |
3 | // Purpose: Assorted wxLogXXX functions, and wxLog (sink for logs) | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | #ifdef __GNUG__ | |
20 | #pragma implementation "log.h" | |
21 | #endif | |
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 | // wxWindows | |
31 | #ifndef WX_PRECOMP | |
32 | #include <wx/app.h> | |
33 | #include <wx/string.h> | |
34 | #include <wx/intl.h> | |
35 | #include <wx/menu.h> | |
36 | ||
37 | #include <wx/generic/msgdlgg.h> | |
38 | #include <wx/filedlg.h> | |
39 | #include <wx/textctrl.h> | |
40 | #endif //WX_PRECOMP | |
41 | ||
42 | #include <wx/file.h> | |
43 | #include <wx/textfile.h> | |
44 | #include <wx/utils.h> | |
45 | #include <wx/log.h> | |
46 | ||
47 | // other standard headers | |
48 | #include <errno.h> | |
49 | #include <stdlib.h> | |
50 | #include <time.h> | |
51 | ||
52 | #ifdef __WXMSW__ | |
53 | #include <windows.h> | |
54 | #else //Unix | |
55 | #include <signal.h> | |
56 | #endif //Win/Unix | |
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | // non member functions | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | // define this to enable wrapping of log messages | |
63 | //#define LOG_PRETTY_WRAP | |
64 | ||
65 | #ifdef LOG_PRETTY_WRAP | |
66 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz); | |
67 | #endif | |
68 | ||
69 | // ============================================================================ | |
70 | // implementation | |
71 | // ============================================================================ | |
72 | ||
73 | // ---------------------------------------------------------------------------- | |
74 | // implementation of Log functions | |
75 | // | |
76 | // NB: unfortunately we need all these distinct functions, we can't make them | |
77 | // macros and not all compilers inline vararg functions. | |
78 | // ---------------------------------------------------------------------------- | |
79 | ||
80 | // log functions can't allocate memory (LogError("out of memory...") should | |
81 | // work!), so we use a static buffer for all log messages | |
82 | #define LOG_BUFFER_SIZE (4096) | |
83 | ||
84 | // static buffer for error messages (@@@ MT-unsafe) | |
85 | static char s_szBuf[LOG_BUFFER_SIZE]; | |
86 | ||
87 | // generic log function | |
88 | void wxLogGeneric(wxLogLevel level, const char *szFormat, ...) | |
89 | { | |
90 | if ( wxLog::GetActiveTarget() != NULL ) { | |
91 | va_list argptr; | |
92 | va_start(argptr, szFormat); | |
93 | vsprintf(s_szBuf, szFormat, argptr); | |
94 | va_end(argptr); | |
95 | ||
96 | wxLog::OnLog(level, s_szBuf); | |
97 | } | |
98 | } | |
99 | ||
100 | #define IMPLEMENT_LOG_FUNCTION(level) \ | |
101 | void wxLog##level(const char *szFormat, ...) \ | |
102 | { \ | |
103 | if ( wxLog::GetActiveTarget() != NULL ) { \ | |
104 | va_list argptr; \ | |
105 | va_start(argptr, szFormat); \ | |
106 | vsprintf(s_szBuf, szFormat, argptr); \ | |
107 | va_end(argptr); \ | |
108 | \ | |
109 | wxLog::OnLog(wxLOG_##level, s_szBuf); \ | |
110 | } \ | |
111 | } | |
112 | ||
113 | IMPLEMENT_LOG_FUNCTION(FatalError) | |
114 | IMPLEMENT_LOG_FUNCTION(Error) | |
115 | IMPLEMENT_LOG_FUNCTION(Warning) | |
116 | IMPLEMENT_LOG_FUNCTION(Message) | |
117 | IMPLEMENT_LOG_FUNCTION(Info) | |
118 | IMPLEMENT_LOG_FUNCTION(Status) | |
119 | ||
120 | // same as info, but only if 'verbose' mode is on | |
121 | void wxLogVerbose(const char *szFormat, ...) | |
122 | { | |
123 | wxLog *pLog = wxLog::GetActiveTarget(); | |
124 | if ( pLog != NULL && pLog->GetVerbose() ) { | |
125 | va_list argptr; | |
126 | va_start(argptr, szFormat); | |
127 | vsprintf(s_szBuf, szFormat, argptr); | |
128 | va_end(argptr); | |
129 | ||
130 | wxLog::OnLog(wxLOG_Info, s_szBuf); | |
131 | } | |
132 | } | |
133 | ||
134 | // debug functions | |
135 | #ifdef __WXDEBUG__ | |
136 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \ | |
137 | void wxLog##level(const char *szFormat, ...) \ | |
138 | { \ | |
139 | if ( wxLog::GetActiveTarget() != NULL ) { \ | |
140 | va_list argptr; \ | |
141 | va_start(argptr, szFormat); \ | |
142 | vsprintf(s_szBuf, szFormat, argptr); \ | |
143 | va_end(argptr); \ | |
144 | \ | |
145 | wxLog::OnLog(wxLOG_##level, s_szBuf); \ | |
146 | } \ | |
147 | } | |
148 | ||
149 | void wxLogTrace(wxTraceMask mask, const char *szFormat, ...) | |
150 | { | |
151 | wxLog *pLog = wxLog::GetActiveTarget(); | |
152 | ||
153 | // we check that all of mask bits are set in the current mask, so | |
154 | // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something | |
155 | // if both bits are set. | |
156 | if ( pLog != NULL && (pLog->GetTraceMask() & mask == mask) ) { | |
157 | va_list argptr; | |
158 | va_start(argptr, szFormat); | |
159 | vsprintf(s_szBuf, szFormat, argptr); | |
160 | va_end(argptr); | |
161 | ||
162 | wxLog::OnLog(wxLOG_Trace, s_szBuf); | |
163 | } | |
164 | } | |
165 | ||
166 | #else // release | |
167 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) | |
168 | #endif | |
169 | ||
170 | IMPLEMENT_LOG_DEBUG_FUNCTION(Debug) | |
171 | IMPLEMENT_LOG_DEBUG_FUNCTION(Trace) | |
172 | ||
173 | // wxLogSysError: one uses the last error code, for other you must give it | |
174 | // explicitly | |
175 | ||
176 | // common part of both wxLogSysError | |
177 | void wxLogSysErrorHelper(long lErrCode) | |
178 | { | |
179 | char szErrMsg[LOG_BUFFER_SIZE / 2]; | |
180 | sprintf(szErrMsg, _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode)); | |
181 | strncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - strlen(s_szBuf)); | |
182 | ||
183 | wxLog::OnLog(wxLOG_Error, s_szBuf); | |
184 | } | |
185 | ||
186 | void WXDLLEXPORT wxLogSysError(const char *szFormat, ...) | |
187 | { | |
188 | va_list argptr; | |
189 | va_start(argptr, szFormat); | |
190 | vsprintf(s_szBuf, szFormat, argptr); | |
191 | va_end(argptr); | |
192 | ||
193 | wxLogSysErrorHelper(wxSysErrorCode()); | |
194 | } | |
195 | ||
196 | void WXDLLEXPORT wxLogSysError(long lErrCode, const char *szFormat, ...) | |
197 | { | |
198 | va_list argptr; | |
199 | va_start(argptr, szFormat); | |
200 | vsprintf(s_szBuf, szFormat, argptr); | |
201 | va_end(argptr); | |
202 | ||
203 | wxLogSysErrorHelper(lErrCode); | |
204 | } | |
205 | ||
206 | // ---------------------------------------------------------------------------- | |
207 | // wxLog class implementation | |
208 | // ---------------------------------------------------------------------------- | |
209 | ||
210 | wxLog::wxLog() | |
211 | { | |
212 | m_bHasMessages = FALSE; | |
213 | m_bVerbose = FALSE; | |
214 | m_szTimeFormat = "[%d/%b/%y %H:%M:%S] "; | |
215 | m_ulTraceMask = (wxTraceMask)0; // -1 to set all bits | |
216 | } | |
217 | ||
218 | wxLog *wxLog::GetActiveTarget() | |
219 | { | |
220 | if ( !ms_bInitialized ) { | |
221 | // prevent infinite recursion if someone calls wxLogXXX() from | |
222 | // wxApp::CreateLogTarget() | |
223 | ms_bInitialized = TRUE; | |
224 | ||
225 | #ifdef WX_TEST_MINIMAL | |
226 | ms_pLogger = new wxLogStderr; | |
227 | #else | |
228 | // ask the application to create a log target for us | |
229 | ms_pLogger = wxTheApp->CreateLogTarget(); | |
230 | #endif | |
231 | ||
232 | // do nothing if it fails - what can we do? | |
233 | } | |
234 | ||
235 | return ms_pLogger; | |
236 | } | |
237 | ||
238 | wxLog *wxLog::SetActiveTarget(wxLog *pLogger) | |
239 | { | |
240 | // flush the old messages before changing | |
241 | if ( ms_pLogger != NULL ) | |
242 | ms_pLogger->Flush(); | |
243 | ||
244 | ms_bInitialized = TRUE; | |
245 | ||
246 | wxLog *pOldLogger = ms_pLogger; | |
247 | ms_pLogger = pLogger; | |
248 | return pOldLogger; | |
249 | } | |
250 | ||
251 | void wxLog::DoLog(wxLogLevel level, const char *szString) | |
252 | { | |
253 | wxString str; | |
254 | ||
255 | // prepend a timestamp if not disabled | |
256 | if ( !IsEmpty(m_szTimeFormat) ) { | |
257 | char szBuf[128]; | |
258 | time_t timeNow; | |
259 | struct tm *ptmNow; | |
260 | ||
261 | time(&timeNow); | |
262 | ptmNow = localtime(&timeNow); | |
263 | ||
264 | strftime(szBuf, WXSIZEOF(szBuf), m_szTimeFormat, ptmNow); | |
265 | str = szBuf; | |
266 | } | |
267 | ||
268 | switch ( level ) { | |
269 | case wxLOG_FatalError: | |
270 | DoLogString(str << _("Fatal error: ") << szString); | |
271 | DoLogString(_("Program aborted.")); | |
272 | Flush(); | |
273 | abort(); | |
274 | break; | |
275 | ||
276 | case wxLOG_Error: | |
277 | DoLogString(str << _("Error: ") << szString); | |
278 | break; | |
279 | ||
280 | case wxLOG_Warning: | |
281 | DoLogString(str << _("Warning: ") << szString); | |
282 | break; | |
283 | ||
284 | case wxLOG_Info: | |
285 | if ( GetVerbose() ) | |
286 | case wxLOG_Message: | |
287 | DoLogString(str + szString); | |
288 | // fall through | |
289 | ||
290 | case wxLOG_Status: | |
291 | // nothing to do | |
292 | break; | |
293 | ||
294 | case wxLOG_Trace: | |
295 | case wxLOG_Debug: | |
296 | #ifdef __WXDEBUG__ | |
297 | #ifdef __WIN32__ | |
298 | // in addition to normal logging, also send the string to debugger | |
299 | // (don't prepend "Debug" here: it will go to debug window anyhow) | |
300 | ::OutputDebugString(str + szString + "\n\r"); | |
301 | #endif //Win32 | |
302 | DoLogString(str << (level == wxLOG_Trace ? _("Trace") : _("Debug")) | |
303 | << ": " << szString); | |
304 | #endif | |
305 | ||
306 | break; | |
307 | ||
308 | default: | |
309 | wxFAIL_MSG("unknown log level in wxLog::DoLog"); | |
310 | } | |
311 | } | |
312 | ||
313 | void wxLog::DoLogString(const char *WXUNUSED(szString)) | |
314 | { | |
315 | wxFAIL_MSG("DoLogString must be overrided if it's called."); | |
316 | } | |
317 | ||
318 | void wxLog::Flush() | |
319 | { | |
320 | // do nothing | |
321 | } | |
322 | ||
323 | // ---------------------------------------------------------------------------- | |
324 | // wxLogStderr class implementation | |
325 | // ---------------------------------------------------------------------------- | |
326 | ||
327 | wxLogStderr::wxLogStderr(FILE *fp) | |
328 | { | |
329 | if ( fp == NULL ) | |
330 | m_fp = stderr; | |
331 | else | |
332 | m_fp = fp; | |
333 | } | |
334 | ||
335 | void wxLogStderr::DoLogString(const char *szString) | |
336 | { | |
337 | fputs(szString, m_fp); | |
338 | fputc('\n', m_fp); | |
339 | fflush(m_fp); | |
340 | } | |
341 | ||
342 | // ---------------------------------------------------------------------------- | |
343 | // wxLogStream implementation | |
344 | // ---------------------------------------------------------------------------- | |
345 | ||
346 | wxLogStream::wxLogStream(ostream *ostr) | |
347 | { | |
348 | if ( ostr == NULL ) | |
349 | m_ostr = &cerr; | |
350 | else | |
351 | m_ostr = ostr; | |
352 | } | |
353 | ||
354 | void wxLogStream::DoLogString(const char *szString) | |
355 | { | |
356 | (*m_ostr) << szString << endl << flush; | |
357 | } | |
358 | ||
359 | // ---------------------------------------------------------------------------- | |
360 | // wxLogTextCtrl implementation | |
361 | // ---------------------------------------------------------------------------- | |
362 | wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl) | |
363 | // @@@ TODO: in wxGTK wxTextCtrl doesn't derive from streambuf | |
364 | #ifndef __WXGTK__ | |
365 | : wxLogStream(new ostream(pTextCtrl)) | |
366 | #endif //GTK | |
367 | { | |
368 | } | |
369 | ||
370 | wxLogTextCtrl::~wxLogTextCtrl() | |
371 | { | |
372 | #ifndef __WXGTK__ | |
373 | delete m_ostr; | |
374 | #endif //GTK | |
375 | } | |
376 | ||
377 | // ---------------------------------------------------------------------------- | |
378 | // wxLogGui implementation | |
379 | // ---------------------------------------------------------------------------- | |
380 | ||
381 | #ifndef WX_TEST_MINIMAL | |
382 | ||
383 | wxLogGui::wxLogGui() | |
384 | { | |
385 | m_bErrors = FALSE; | |
386 | } | |
387 | ||
388 | void wxLogGui::Flush() | |
389 | { | |
390 | if ( !m_bHasMessages ) | |
391 | return; | |
392 | ||
393 | // @@@ ugly... | |
394 | ||
395 | // concatenate all strings (but not too many to not overfill the msg box) | |
396 | wxString str; | |
397 | uint nLines = 0, | |
398 | nMsgCount = m_aMessages.Count(); | |
399 | ||
400 | // start from the most recent message | |
401 | for ( uint n = nMsgCount; n > 0; n-- ) { | |
402 | // for Windows strings longer than this value are wrapped (NT 4.0) | |
403 | const uint nMsgLineWidth = 156; | |
404 | ||
405 | nLines += (m_aMessages[n - 1].Len() + nMsgLineWidth - 1) / nMsgLineWidth; | |
406 | ||
407 | if ( nLines > 25 ) // don't put too many lines in message box | |
408 | break; | |
409 | ||
410 | str << m_aMessages[n - 1] << "\n"; | |
411 | } | |
412 | ||
413 | if ( m_bErrors ) { | |
414 | wxMessageBox(str, _("Error"), wxOK | wxICON_EXCLAMATION); | |
415 | } | |
416 | else { | |
417 | wxMessageBox(str, _("Information"), wxOK | wxICON_INFORMATION); | |
418 | } | |
419 | ||
420 | // no undisplayed messages whatsoever | |
421 | m_bHasMessages = | |
422 | m_bErrors = FALSE; | |
423 | m_aMessages.Empty(); | |
424 | } | |
425 | ||
426 | // the default behaviour is to discard all informational messages if there | |
427 | // are any errors/warnings. | |
428 | void wxLogGui::DoLog(wxLogLevel level, const char *szString) | |
429 | { | |
430 | switch ( level ) { | |
431 | case wxLOG_Info: | |
432 | if ( GetVerbose() ) | |
433 | case wxLOG_Message: | |
434 | if ( !m_bErrors ) { | |
435 | m_aMessages.Add(szString); | |
436 | m_bHasMessages = TRUE; | |
437 | } | |
438 | break; | |
439 | ||
440 | case wxLOG_Status: | |
441 | { | |
442 | // find the top window and set it's status text if it has any | |
443 | wxWindow *pWin = wxTheApp->GetTopWindow(); | |
444 | if ( pWin != NULL && pWin->IsKindOf(CLASSINFO(wxFrame)) ) { | |
445 | wxFrame *pFrame = (wxFrame *)pWin; | |
446 | pFrame->SetStatusText(szString); | |
447 | } | |
448 | } | |
449 | break; | |
450 | ||
451 | case wxLOG_Trace: | |
452 | case wxLOG_Debug: | |
453 | #ifdef __WXDEBUG__ | |
454 | #ifdef __WIN32__ | |
455 | OutputDebugString(szString); | |
456 | OutputDebugString("\n\r"); | |
457 | #else //!WIN32 | |
458 | // send them to stderr | |
459 | fprintf(stderr, "%s: %s\n", | |
460 | level == wxLOG_Trace ? _("Trace") : _("Debug"), szString); | |
461 | fflush(stderr); | |
462 | #endif // WIN32 | |
463 | #endif | |
464 | break; | |
465 | ||
466 | case wxLOG_FatalError: | |
467 | // show this one immediately | |
468 | wxMessageBox(szString, _("Fatal error"), wxICON_HAND); | |
469 | break; | |
470 | ||
471 | case wxLOG_Error: | |
472 | case wxLOG_Warning: | |
473 | // discard earlier informational messages if this is the 1st error | |
474 | if ( !m_bErrors ) { | |
475 | m_aMessages.Empty(); | |
476 | m_bHasMessages = TRUE; | |
477 | m_bErrors = TRUE; | |
478 | } | |
479 | ||
480 | m_aMessages.Add(szString); | |
481 | break; | |
482 | ||
483 | default: | |
484 | wxFAIL_MSG("unknown log level in wxLogGui::DoLog"); | |
485 | } | |
486 | } | |
487 | ||
488 | // ---------------------------------------------------------------------------- | |
489 | // wxLogWindow implementation | |
490 | // ---------------------------------------------------------------------------- | |
491 | ||
492 | // log frame class | |
493 | class wxLogFrame : public wxFrame | |
494 | { | |
495 | public: | |
496 | // ctor | |
497 | wxLogFrame(const char *szTitle); | |
498 | ||
499 | // menu callbacks | |
500 | void OnClose(wxCommandEvent& event); | |
501 | void OnCloseWindow(wxCloseEvent& event); | |
502 | void OnSave (wxCommandEvent& event); | |
503 | void OnClear(wxCommandEvent& event); | |
504 | ||
505 | // accessors | |
506 | wxTextCtrl *TextCtrl() const { return m_pTextCtrl; } | |
507 | ||
508 | private: | |
509 | enum | |
510 | { | |
511 | Menu_Close = 100, | |
512 | Menu_Save, | |
513 | Menu_Clear | |
514 | }; | |
515 | ||
516 | wxTextCtrl *m_pTextCtrl; | |
517 | ||
518 | DECLARE_EVENT_TABLE() | |
519 | }; | |
520 | ||
521 | BEGIN_EVENT_TABLE(wxLogFrame, wxFrame) | |
522 | // wxLogWindow menu events | |
523 | EVT_MENU(Menu_Close, wxLogFrame::OnClose) | |
524 | EVT_MENU(Menu_Save, wxLogFrame::OnSave) | |
525 | EVT_MENU(Menu_Clear, wxLogFrame::OnClear) | |
526 | ||
527 | EVT_CLOSE(wxLogFrame::OnCloseWindow) | |
528 | END_EVENT_TABLE() | |
529 | ||
530 | wxLogFrame::wxLogFrame(const char *szTitle) | |
531 | : wxFrame(NULL, -1, szTitle) | |
532 | { | |
533 | // we don't want to be a top-level frame because it would prevent the | |
534 | // application termination when all other frames are closed | |
535 | wxTopLevelWindows.DeleteObject(this); | |
536 | ||
537 | // @@ kludge: wxSIMPLE_BORDER is simply to prevent wxWindows from creating | |
538 | // a rich edit control instead of a normal one we want | |
539 | m_pTextCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition, | |
540 | wxDefaultSize, | |
541 | wxSIMPLE_BORDER | | |
542 | wxTE_MULTILINE | | |
543 | wxHSCROLL | | |
544 | wxTE_READONLY); | |
545 | /* | |
546 | m_pTextCtrl->SetEditable(FALSE); | |
547 | m_pTextCtrl->SetRichEdit(FALSE); | |
548 | */ | |
549 | ||
550 | // create menu | |
551 | wxMenuBar *pMenuBar = new wxMenuBar; | |
552 | wxMenu *pMenu = new wxMenu; | |
553 | pMenu->Append(Menu_Save, "&Save..."); | |
554 | pMenu->Append(Menu_Clear, "C&lear"); | |
555 | pMenu->AppendSeparator(); | |
556 | pMenu->Append(Menu_Close, "&Close"); | |
557 | pMenuBar->Append(pMenu, "&Log"); | |
558 | SetMenuBar(pMenuBar); | |
559 | ||
560 | // @@ what about status bar? needed (for menu prompts)? | |
561 | } | |
562 | ||
563 | void wxLogFrame::OnClose(wxCommandEvent& WXUNUSED(event)) | |
564 | { | |
565 | // just hide the window | |
566 | Show(FALSE); | |
567 | } | |
568 | ||
569 | void wxLogFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
570 | { | |
571 | // just hide the window | |
572 | Show(FALSE); | |
573 | } | |
574 | ||
575 | void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event)) | |
576 | { | |
577 | // get the file name | |
578 | // ----------------- | |
579 | const char *szFileName = wxSaveFileSelector("log", "txt", "log.txt"); | |
580 | if ( szFileName == NULL ) { | |
581 | // cancelled | |
582 | return; | |
583 | } | |
584 | ||
585 | // open file | |
586 | // --------- | |
587 | wxFile file; | |
588 | bool bOk = FALSE; | |
589 | if ( wxFile::Exists(szFileName) ) { | |
590 | bool bAppend = FALSE; | |
591 | wxString strMsg; | |
592 | strMsg.Printf(_("Append log to file '%s' " | |
593 | "(choosing [No] will overwrite it)?"), szFileName); | |
594 | switch ( wxMessageBox(strMsg, "Question", wxYES_NO | wxCANCEL) ) { | |
595 | case wxYES: | |
596 | bAppend = TRUE; | |
597 | break; | |
598 | ||
599 | case wxNO: | |
600 | bAppend = FALSE; | |
601 | break; | |
602 | ||
603 | case wxCANCEL: | |
604 | return; | |
605 | ||
606 | default: | |
607 | wxFAIL_MSG("invalid message box return value"); | |
608 | } | |
609 | ||
610 | if ( bAppend ) { | |
611 | bOk = file.Open(szFileName, wxFile::write_append); | |
612 | } | |
613 | else { | |
614 | bOk = file.Create(szFileName, TRUE /* overwrite */); | |
615 | } | |
616 | } | |
617 | else { | |
618 | bOk = file.Create(szFileName); | |
619 | } | |
620 | ||
621 | // retrieve text and save it | |
622 | // ------------------------- | |
623 | #ifdef __WXGTK__ | |
624 | // @@@@ TODO: no GetNumberOfLines and GetLineText in wxGTK yet | |
625 | wxLogError("Sorry, this function is not implemented under GTK"); | |
626 | #else | |
627 | int nLines = m_pTextCtrl->GetNumberOfLines(); | |
628 | for ( int nLine = 0; bOk && nLine < nLines; nLine++ ) { | |
629 | bOk = file.Write(m_pTextCtrl->GetLineText(nLine) + wxTextFile::GetEOL()); | |
630 | } | |
631 | #endif //GTK | |
632 | ||
633 | if ( bOk ) | |
634 | bOk = file.Close(); | |
635 | ||
636 | if ( !bOk ) { | |
637 | wxLogError(_("Can't save log contents to file.")); | |
638 | return; | |
639 | } | |
640 | } | |
641 | ||
642 | void wxLogFrame::OnClear(wxCommandEvent& WXUNUSED(event)) | |
643 | { | |
644 | m_pTextCtrl->Clear(); | |
645 | } | |
646 | ||
647 | wxLogWindow::wxLogWindow(const char *szTitle, bool bShow) | |
648 | { | |
649 | m_pOldLog = wxLog::GetActiveTarget(); | |
650 | m_pLogFrame = new wxLogFrame(szTitle); | |
651 | ||
652 | if ( bShow ) | |
653 | m_pLogFrame->Show(TRUE); | |
654 | } | |
655 | ||
656 | void wxLogWindow::Show(bool bShow) | |
657 | { | |
658 | m_pLogFrame->Show(bShow); | |
659 | } | |
660 | ||
661 | void wxLogWindow::DoLog(wxLogLevel level, const char *szString) | |
662 | { | |
663 | // first let the previous logger show it | |
664 | if ( m_pOldLog != NULL ) { | |
665 | // @@@ why can't we access protected wxLog method from here (we derive | |
666 | // from wxLog)? gcc gives "DoLog is protected in this context", what | |
667 | // does this mean? Anyhow, the cast is harmless and let's us do what | |
668 | // we want. | |
669 | ((wxLogWindow *)m_pOldLog)->DoLog(level, szString); | |
670 | } | |
671 | ||
672 | // and this will format it nicely and call our DoLogString() | |
673 | wxLog::DoLog(level, szString); | |
674 | } | |
675 | ||
676 | void wxLogWindow::DoLogString(const char *szString) | |
677 | { | |
678 | // put the text into our window | |
679 | wxTextCtrl *pText = m_pLogFrame->TextCtrl(); | |
680 | ||
681 | // remove selection (WriteText is in fact ReplaceSelection) | |
682 | #ifdef __WXMSW__ | |
683 | long nLen = pText->GetLastPosition(); | |
684 | pText->SetSelection(nLen, nLen); | |
685 | #endif // Windows | |
686 | ||
687 | pText->WriteText(szString); | |
688 | pText->WriteText("\n"); // "\n" ok here (_not_ "\r\n") | |
689 | ||
690 | // ensure that the line can be seen | |
691 | // @@@ TODO | |
692 | } | |
693 | ||
694 | wxLogWindow::~wxLogWindow() | |
695 | { | |
696 | m_pLogFrame->Close(TRUE); | |
697 | } | |
698 | ||
699 | #endif //WX_TEST_MINIMAL | |
700 | ||
701 | // ============================================================================ | |
702 | // Global functions/variables | |
703 | // ============================================================================ | |
704 | ||
705 | // ---------------------------------------------------------------------------- | |
706 | // static variables | |
707 | // ---------------------------------------------------------------------------- | |
708 | wxLog *wxLog::ms_pLogger = NULL; | |
709 | bool wxLog::ms_bInitialized = FALSE; | |
710 | ||
711 | // ---------------------------------------------------------------------------- | |
712 | // stdout error logging helper | |
713 | // ---------------------------------------------------------------------------- | |
714 | ||
715 | // helper function: wraps the message and justifies it under given position | |
716 | // (looks more pretty on the terminal). Also adds newline at the end. | |
717 | // | |
718 | // @@ this is now disabled until I find a portable way of determining the | |
719 | // terminal window size (ok, I found it but does anybody really cares?) | |
720 | #ifdef LOG_PRETTY_WRAP | |
721 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz) | |
722 | { | |
723 | size_t nMax = 80; // @@@@ | |
724 | size_t nStart = strlen(pszPrefix); | |
725 | fputs(pszPrefix, f); | |
726 | ||
727 | size_t n; | |
728 | while ( *psz != '\0' ) { | |
729 | for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ ) | |
730 | putc(*psz++, f); | |
731 | ||
732 | // wrapped? | |
733 | if ( *psz != '\0' ) { | |
734 | /*putc('\n', f);*/ | |
735 | for ( n = 0; n < nStart; n++ ) | |
736 | putc(' ', f); | |
737 | ||
738 | // as we wrapped, squeeze all white space | |
739 | while ( isspace(*psz) ) | |
740 | psz++; | |
741 | } | |
742 | } | |
743 | ||
744 | putc('\n', f); | |
745 | } | |
746 | #endif //LOG_PRETTY_WRAP | |
747 | ||
748 | // ---------------------------------------------------------------------------- | |
749 | // error code/error message retrieval functions | |
750 | // ---------------------------------------------------------------------------- | |
751 | ||
752 | // get error code from syste | |
753 | unsigned long wxSysErrorCode() | |
754 | { | |
755 | #ifdef __WXMSW__ | |
756 | #ifdef __WIN32__ | |
757 | return ::GetLastError(); | |
758 | #else //WIN16 | |
759 | // @@@@ what to do on Windows 3.1? | |
760 | return 0; | |
761 | #endif //WIN16/32 | |
762 | #else //Unix | |
763 | return errno; | |
764 | #endif //Win/Unix | |
765 | } | |
766 | ||
767 | // get error message from system | |
768 | const char *wxSysErrorMsg(unsigned long nErrCode) | |
769 | { | |
770 | if ( nErrCode == 0 ) | |
771 | nErrCode = wxSysErrorCode(); | |
772 | ||
773 | #ifdef __WXMSW__ | |
774 | #ifdef __WIN32__ | |
775 | static char s_szBuf[LOG_BUFFER_SIZE / 2]; | |
776 | ||
777 | // get error message from system | |
778 | LPVOID lpMsgBuf; | |
779 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | |
780 | NULL, nErrCode, | |
781 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
782 | (LPTSTR)&lpMsgBuf, | |
783 | 0, NULL); | |
784 | ||
785 | // copy it to our buffer and free memory | |
786 | strncpy(s_szBuf, (const char *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1); | |
787 | s_szBuf[WXSIZEOF(s_szBuf) - 1] = '\0'; | |
788 | LocalFree(lpMsgBuf); | |
789 | ||
790 | // returned string is capitalized and ended with '\r\n' - bad | |
791 | s_szBuf[0] = (char)wxToLower(s_szBuf[0]); | |
792 | size_t len = strlen(s_szBuf); | |
793 | if ( len > 0 ) { | |
794 | // truncate string | |
795 | if ( s_szBuf[len - 2] == '\r' ) | |
796 | s_szBuf[len - 2] = '\0'; | |
797 | } | |
798 | ||
799 | return s_szBuf; | |
800 | #else //Win16 | |
801 | // TODO @@@@ | |
802 | return NULL; | |
803 | #endif // Win16/32 | |
804 | #else // Unix | |
805 | return strerror(nErrCode); | |
806 | #endif // Win/Unix | |
807 | } | |
808 | ||
809 | // ---------------------------------------------------------------------------- | |
810 | // debug helper | |
811 | // ---------------------------------------------------------------------------- | |
812 | ||
813 | #ifdef __WXDEBUG__ | |
814 | ||
815 | void Trap() | |
816 | { | |
817 | #ifdef __WXMSW__ | |
818 | DebugBreak(); | |
819 | #else // Unix | |
820 | raise(SIGTRAP); | |
821 | #endif // Win/Unix | |
822 | } | |
823 | ||
824 | // this function is called when an assert fails | |
825 | void wxOnAssert(const char *szFile, int nLine, const char *szMsg) | |
826 | { | |
827 | // this variable can be set to true to suppress "assert failure" messages | |
828 | static bool s_bNoAsserts = FALSE; | |
829 | static bool s_bInAssert = FALSE; | |
830 | ||
831 | if ( s_bInAssert ) { | |
832 | // He-e-e-e-elp!! we're trapped in endless loop | |
833 | Trap(); | |
834 | } | |
835 | ||
836 | s_bInAssert = TRUE; | |
837 | ||
838 | char szBuf[LOG_BUFFER_SIZE]; | |
839 | sprintf(szBuf, _("Assert failed in file %s at line %d"), szFile, nLine); | |
840 | if ( szMsg != NULL ) { | |
841 | strcat(szBuf, ": "); | |
842 | strcat(szBuf, szMsg); | |
843 | } | |
844 | else { | |
845 | strcat(szBuf, "."); | |
846 | } | |
847 | ||
848 | if ( !s_bNoAsserts ) { | |
849 | // send it to the normal log destination | |
850 | wxLogDebug(szBuf); | |
851 | ||
852 | strcat(szBuf, _("\nDo you want to stop the program?" | |
853 | "\nYou can also choose [Cancel] to suppress " | |
854 | "further warnings.")); | |
855 | ||
856 | switch ( wxMessageBox(szBuf, _("Debug"), | |
857 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) { | |
858 | case wxYES: | |
859 | Trap(); | |
860 | break; | |
861 | ||
862 | case wxCANCEL: | |
863 | s_bNoAsserts = TRUE; | |
864 | break; | |
865 | ||
866 | //case wxNO: nothing to do | |
867 | } | |
868 | } | |
869 | ||
870 | s_bInAssert = FALSE; | |
871 | } | |
872 | ||
873 | #endif //WXDEBUG | |
874 |