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