]>
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> | |
cf4219e7 | 35 | #include <wx/menu.h> |
9ef3052c | 36 | |
c801d85f | 37 | #include <wx/generic/msgdlgg.h> |
9ef3052c VZ |
38 | #include <wx/filedlg.h> |
39 | #include <wx/textctrl.h> | |
40 | #endif //WX_PRECOMP | |
c801d85f | 41 | |
9ef3052c VZ |
42 | #include <wx/file.h> |
43 | #include <wx/textfile.h> | |
81d66cf3 | 44 | #include <wx/utils.h> |
c801d85f KB |
45 | #include <wx/log.h> |
46 | ||
47 | // other standard headers | |
48 | #include <errno.h> | |
49 | #include <stdlib.h> | |
50 | #include <time.h> | |
51 | ||
2049ba38 | 52 | #ifdef __WXMSW__ |
9ef3052c | 53 | #include <windows.h> |
3078c3a6 VZ |
54 | #else //Unix |
55 | #include <signal.h> | |
56 | #endif //Win/Unix | |
c801d85f KB |
57 | |
58 | // ---------------------------------------------------------------------------- | |
59 | // non member functions | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | // define this to enable wrapping of log messages | |
63 | //#define LOG_PRETTY_WRAP | |
64 | ||
9ef3052c | 65 | #ifdef LOG_PRETTY_WRAP |
c801d85f KB |
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 | |
7502ba29 | 88 | void wxLogGeneric(wxLogLevel level, const char *szFormat, ...) |
c801d85f | 89 | { |
9ef3052c VZ |
90 | if ( wxLog::GetActiveTarget() != NULL ) { |
91 | va_list argptr; | |
7502ba29 VZ |
92 | va_start(argptr, szFormat); |
93 | vsprintf(s_szBuf, szFormat, argptr); | |
9ef3052c VZ |
94 | va_end(argptr); |
95 | ||
96 | wxLog::OnLog(level, s_szBuf); | |
97 | } | |
c801d85f KB |
98 | } |
99 | ||
100 | #define IMPLEMENT_LOG_FUNCTION(level) \ | |
7502ba29 | 101 | void wxLog##level(const char *szFormat, ...) \ |
c801d85f KB |
102 | { \ |
103 | if ( wxLog::GetActiveTarget() != NULL ) { \ | |
104 | va_list argptr; \ | |
7502ba29 VZ |
105 | va_start(argptr, szFormat); \ |
106 | vsprintf(s_szBuf, szFormat, argptr); \ | |
c801d85f KB |
107 | va_end(argptr); \ |
108 | \ | |
9ef3052c | 109 | wxLog::OnLog(wxLOG_##level, s_szBuf); \ |
c801d85f KB |
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 | ||
9ef3052c | 120 | // same as info, but only if 'verbose' mode is on |
7502ba29 | 121 | void wxLogVerbose(const char *szFormat, ...) |
9ef3052c VZ |
122 | { |
123 | wxLog *pLog = wxLog::GetActiveTarget(); | |
124 | if ( pLog != NULL && pLog->GetVerbose() ) { | |
125 | va_list argptr; | |
7502ba29 VZ |
126 | va_start(argptr, szFormat); |
127 | vsprintf(s_szBuf, szFormat, argptr); | |
9ef3052c VZ |
128 | va_end(argptr); |
129 | ||
130 | wxLog::OnLog(wxLOG_Info, s_szBuf); | |
131 | } | |
132 | } | |
133 | ||
134 | // debug functions | |
b2aef89b | 135 | #ifdef __WXDEBUG__ |
9ef3052c | 136 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \ |
c801d85f KB |
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 | \ | |
9ef3052c | 145 | wxLog::OnLog(wxLOG_##level, s_szBuf); \ |
c801d85f KB |
146 | } \ |
147 | } | |
148 | ||
9ef3052c VZ |
149 | void wxLogTrace(wxTraceMask mask, const char *szFormat, ...) |
150 | { | |
151 | wxLog *pLog = wxLog::GetActiveTarget(); | |
c801d85f | 152 | |
9ef3052c VZ |
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) | |
c801d85f | 178 | { |
9ef3052c VZ |
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)); | |
c801d85f | 182 | |
9ef3052c VZ |
183 | wxLog::OnLog(wxLOG_Error, s_szBuf); |
184 | } | |
c801d85f | 185 | |
7502ba29 | 186 | void WXDLLEXPORT wxLogSysError(const char *szFormat, ...) |
9ef3052c VZ |
187 | { |
188 | va_list argptr; | |
7502ba29 VZ |
189 | va_start(argptr, szFormat); |
190 | vsprintf(s_szBuf, szFormat, argptr); | |
9ef3052c VZ |
191 | va_end(argptr); |
192 | ||
193 | wxLogSysErrorHelper(wxSysErrorCode()); | |
c801d85f KB |
194 | } |
195 | ||
7502ba29 | 196 | void WXDLLEXPORT wxLogSysError(long lErrCode, const char *szFormat, ...) |
c801d85f | 197 | { |
9ef3052c | 198 | va_list argptr; |
7502ba29 VZ |
199 | va_start(argptr, szFormat); |
200 | vsprintf(s_szBuf, szFormat, argptr); | |
9ef3052c | 201 | va_end(argptr); |
c801d85f | 202 | |
9ef3052c | 203 | wxLogSysErrorHelper(lErrCode); |
c801d85f KB |
204 | } |
205 | ||
206 | // ---------------------------------------------------------------------------- | |
207 | // wxLog class implementation | |
208 | // ---------------------------------------------------------------------------- | |
209 | ||
210 | wxLog::wxLog() | |
211 | { | |
212 | m_bHasMessages = FALSE; | |
9ef3052c VZ |
213 | m_bVerbose = FALSE; |
214 | m_szTimeFormat = "[%d/%b/%y %H:%M:%S] "; | |
93da8c42 | 215 | m_ulTraceMask = (wxTraceMask)0; // -1 to set all bits |
c801d85f KB |
216 | } |
217 | ||
9ec05cc9 VZ |
218 | wxLog *wxLog::GetActiveTarget() |
219 | { | |
c801d85f | 220 | if ( !ms_bInitialized ) { |
9ef3052c VZ |
221 | // prevent infinite recursion if someone calls wxLogXXX() from |
222 | // wxApp::CreateLogTarget() | |
c801d85f KB |
223 | ms_bInitialized = TRUE; |
224 | ||
9ef3052c VZ |
225 | #ifdef WX_TEST_MINIMAL |
226 | ms_pLogger = new wxLogStderr; | |
227 | #else | |
228 | // ask the application to create a log target for us | |
c801d85f | 229 | ms_pLogger = wxTheApp->CreateLogTarget(); |
9ef3052c | 230 | #endif |
c801d85f KB |
231 | |
232 | // do nothing if it fails - what can we do? | |
233 | } | |
234 | ||
9ec05cc9 | 235 | return ms_pLogger; |
c801d85f KB |
236 | } |
237 | ||
238 | wxLog *wxLog::SetActiveTarget(wxLog *pLogger) | |
9ec05cc9 | 239 | { |
c801d85f KB |
240 | // flush the old messages before changing |
241 | if ( ms_pLogger != NULL ) | |
242 | ms_pLogger->Flush(); | |
243 | ||
244 | ms_bInitialized = TRUE; | |
245 | ||
9ec05cc9 VZ |
246 | wxLog *pOldLogger = ms_pLogger; |
247 | ms_pLogger = pLogger; | |
248 | return pOldLogger; | |
c801d85f KB |
249 | } |
250 | ||
9ef3052c | 251 | void wxLog::DoLog(wxLogLevel level, const char *szString) |
c801d85f | 252 | { |
9ef3052c | 253 | wxString str; |
c801d85f | 254 | |
9ef3052c VZ |
255 | // prepend a timestamp if not disabled |
256 | if ( !IsEmpty(m_szTimeFormat) ) { | |
257 | char szBuf[128]; | |
258 | time_t timeNow; | |
259 | struct tm *ptmNow; | |
c801d85f | 260 | |
9ef3052c VZ |
261 | time(&timeNow); |
262 | ptmNow = localtime(&timeNow); | |
263 | ||
264 | strftime(szBuf, WXSIZEOF(szBuf), m_szTimeFormat, ptmNow); | |
265 | str = szBuf; | |
266 | } | |
c801d85f KB |
267 | |
268 | switch ( level ) { | |
9ef3052c | 269 | case wxLOG_FatalError: |
c801d85f KB |
270 | DoLogString(str << _("Fatal error: ") << szString); |
271 | DoLogString(_("Program aborted.")); | |
272 | Flush(); | |
273 | abort(); | |
274 | break; | |
275 | ||
9ef3052c | 276 | case wxLOG_Error: |
c801d85f KB |
277 | DoLogString(str << _("Error: ") << szString); |
278 | break; | |
279 | ||
9ef3052c | 280 | case wxLOG_Warning: |
c801d85f KB |
281 | DoLogString(str << _("Warning: ") << szString); |
282 | break; | |
283 | ||
9ef3052c | 284 | case wxLOG_Info: |
c801d85f | 285 | if ( GetVerbose() ) |
9ef3052c | 286 | case wxLOG_Message: |
c801d85f KB |
287 | DoLogString(str + szString); |
288 | // fall through | |
289 | ||
9ef3052c | 290 | case wxLOG_Status: |
c801d85f KB |
291 | // nothing to do |
292 | break; | |
293 | ||
9ef3052c VZ |
294 | case wxLOG_Trace: |
295 | case wxLOG_Debug: | |
b2aef89b | 296 | #ifdef __WXDEBUG__ |
c801d85f KB |
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 | |
7502ba29 | 302 | DoLogString(str << (level == wxLOG_Trace ? _("Trace") : _("Debug")) |
c801d85f KB |
303 | << ": " << szString); |
304 | #endif | |
9ec05cc9 | 305 | |
c801d85f KB |
306 | break; |
307 | ||
308 | default: | |
309 | wxFAIL_MSG("unknown log level in wxLog::DoLog"); | |
310 | } | |
311 | } | |
312 | ||
46dc76ba | 313 | void wxLog::DoLogString(const char *WXUNUSED(szString)) |
c801d85f KB |
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 | // ---------------------------------------------------------------------------- | |
c801d85f | 362 | wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl) |
9ef3052c | 363 | // @@@ TODO: in wxGTK wxTextCtrl doesn't derive from streambuf |
2049ba38 | 364 | #ifndef __WXGTK__ |
c801d85f | 365 | : wxLogStream(new ostream(pTextCtrl)) |
9ef3052c | 366 | #endif //GTK |
c801d85f KB |
367 | { |
368 | } | |
369 | ||
370 | wxLogTextCtrl::~wxLogTextCtrl() | |
371 | { | |
2049ba38 | 372 | #ifndef __WXGTK__ |
9ef3052c VZ |
373 | delete m_ostr; |
374 | #endif //GTK | |
c801d85f | 375 | } |
c801d85f KB |
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... | |
9ec05cc9 | 394 | |
c801d85f KB |
395 | // concatenate all strings (but not too many to not overfill the msg box) |
396 | wxString str; | |
9ec05cc9 | 397 | uint nLines = 0, |
c801d85f KB |
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. | |
9ef3052c | 428 | void wxLogGui::DoLog(wxLogLevel level, const char *szString) |
c801d85f KB |
429 | { |
430 | switch ( level ) { | |
9ef3052c | 431 | case wxLOG_Info: |
c801d85f | 432 | if ( GetVerbose() ) |
9ef3052c | 433 | case wxLOG_Message: |
c801d85f KB |
434 | if ( !m_bErrors ) { |
435 | m_aMessages.Add(szString); | |
436 | m_bHasMessages = TRUE; | |
437 | } | |
438 | break; | |
439 | ||
9ef3052c | 440 | case wxLOG_Status: |
c801d85f KB |
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 | ||
9ef3052c VZ |
451 | case wxLOG_Trace: |
452 | case wxLOG_Debug: | |
b2aef89b | 453 | #ifdef __WXDEBUG__ |
c801d85f KB |
454 | #ifdef __WIN32__ |
455 | OutputDebugString(szString); | |
456 | OutputDebugString("\n\r"); | |
457 | #else //!WIN32 | |
458 | // send them to stderr | |
7502ba29 VZ |
459 | fprintf(stderr, "%s: %s\n", |
460 | level == wxLOG_Trace ? _("Trace") : _("Debug"), szString); | |
c801d85f KB |
461 | fflush(stderr); |
462 | #endif // WIN32 | |
463 | #endif | |
464 | break; | |
465 | ||
9ef3052c | 466 | case wxLOG_FatalError: |
c801d85f | 467 | // show this one immediately |
7502ba29 | 468 | wxMessageBox(szString, _("Fatal error"), wxICON_HAND); |
c801d85f KB |
469 | break; |
470 | ||
9ef3052c VZ |
471 | case wxLOG_Error: |
472 | case wxLOG_Warning: | |
c801d85f KB |
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; | |
9ec05cc9 | 482 | |
c801d85f KB |
483 | default: |
484 | wxFAIL_MSG("unknown log level in wxLogGui::DoLog"); | |
485 | } | |
486 | } | |
487 | ||
9ef3052c VZ |
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); | |
5260b1c5 | 501 | void OnCloseWindow(wxCloseEvent& event); |
9ef3052c VZ |
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 | ||
5260b1c5 | 527 | EVT_CLOSE(wxLogFrame::OnCloseWindow) |
9ec05cc9 | 528 | END_EVENT_TABLE() |
9ef3052c VZ |
529 | |
530 | wxLogFrame::wxLogFrame(const char *szTitle) | |
531 | : wxFrame(NULL, -1, szTitle) | |
532 | { | |
f42d2625 VZ |
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 | ||
9ef3052c VZ |
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 | ||
46dc76ba | 563 | void wxLogFrame::OnClose(wxCommandEvent& WXUNUSED(event)) |
9ef3052c VZ |
564 | { |
565 | // just hide the window | |
566 | Show(FALSE); | |
567 | } | |
568 | ||
46dc76ba | 569 | void wxLogFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
5260b1c5 JS |
570 | { |
571 | // just hide the window | |
572 | Show(FALSE); | |
573 | } | |
574 | ||
46dc76ba | 575 | void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event)) |
9ef3052c VZ |
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; | |
46dc76ba | 588 | bool bOk = FALSE; |
9ef3052c | 589 | if ( wxFile::Exists(szFileName) ) { |
46dc76ba | 590 | bool bAppend = FALSE; |
9ef3052c VZ |
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 | // ------------------------- | |
2049ba38 | 623 | #ifdef __WXGTK__ |
9ef3052c | 624 | // @@@@ TODO: no GetNumberOfLines and GetLineText in wxGTK yet |
f42d2625 VZ |
625 | wxLogError("Sorry, this function is not implemented under GTK"); |
626 | #else | |
9ef3052c VZ |
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 | |
9ec05cc9 | 632 | |
9ef3052c VZ |
633 | if ( bOk ) |
634 | bOk = file.Close(); | |
635 | ||
636 | if ( !bOk ) { | |
7502ba29 | 637 | wxLogError(_("Can't save log contents to file.")); |
9ef3052c VZ |
638 | return; |
639 | } | |
640 | } | |
641 | ||
46dc76ba | 642 | void wxLogFrame::OnClear(wxCommandEvent& WXUNUSED(event)) |
9ef3052c VZ |
643 | { |
644 | m_pTextCtrl->Clear(); | |
645 | } | |
646 | ||
7502ba29 | 647 | wxLogWindow::wxLogWindow(const char *szTitle, bool bShow) |
9ef3052c VZ |
648 | { |
649 | m_pOldLog = wxLog::GetActiveTarget(); | |
7502ba29 | 650 | m_pLogFrame = new wxLogFrame(szTitle); |
9ec05cc9 | 651 | |
f42d2625 VZ |
652 | if ( bShow ) |
653 | m_pLogFrame->Show(TRUE); | |
9ef3052c VZ |
654 | } |
655 | ||
e51c4943 | 656 | void wxLogWindow::Show(bool bShow) |
9ef3052c VZ |
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 ) { | |
9ec05cc9 | 665 | // @@@ why can't we access protected wxLog method from here (we derive |
9ef3052c VZ |
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 | } | |
9ec05cc9 | 671 | |
9ef3052c VZ |
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) | |
2049ba38 | 682 | #ifdef __WXMSW__ |
f42d2625 VZ |
683 | long nLen = pText->GetLastPosition(); |
684 | pText->SetSelection(nLen, nLen); | |
685 | #endif // Windows | |
9ef3052c VZ |
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 | ||
c801d85f KB |
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; | |
c801d85f KB |
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 | |
9ef3052c | 719 | // terminal window size (ok, I found it but does anybody really cares?) |
c801d85f KB |
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 | { | |
2049ba38 | 755 | #ifdef __WXMSW__ |
c801d85f KB |
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 | ||
2049ba38 | 773 | #ifdef __WXMSW__ |
9ef3052c VZ |
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, | |
9ec05cc9 | 780 | NULL, nErrCode, |
9ef3052c VZ |
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 | |
81d66cf3 | 791 | s_szBuf[0] = (char)wxToLower(s_szBuf[0]); |
9ef3052c VZ |
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 | } | |
c801d85f | 798 | |
9ef3052c VZ |
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 | |
c801d85f KB |
807 | } |
808 | ||
809 | // ---------------------------------------------------------------------------- | |
810 | // debug helper | |
811 | // ---------------------------------------------------------------------------- | |
812 | ||
b2aef89b | 813 | #ifdef __WXDEBUG__ |
c801d85f | 814 | |
7502ba29 VZ |
815 | void Trap() |
816 | { | |
817 | #ifdef __WXMSW__ | |
818 | DebugBreak(); | |
819 | #else // Unix | |
820 | raise(SIGTRAP); | |
821 | #endif // Win/Unix | |
822 | } | |
823 | ||
c801d85f KB |
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 | |
7502ba29 VZ |
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; | |
c801d85f KB |
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 | ||
3078c3a6 | 848 | if ( !s_bNoAsserts ) { |
9ec05cc9 VZ |
849 | // send it to the normal log destination |
850 | wxLogDebug(szBuf); | |
851 | ||
3078c3a6 VZ |
852 | strcat(szBuf, _("\nDo you want to stop the program?" |
853 | "\nYou can also choose [Cancel] to suppress " | |
854 | "further warnings.")); | |
855 | ||
9fd239ad | 856 | switch ( wxMessageBox(szBuf, _("Debug"), |
3078c3a6 VZ |
857 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) { |
858 | case wxYES: | |
7502ba29 | 859 | Trap(); |
3078c3a6 | 860 | break; |
c801d85f | 861 | |
3078c3a6 VZ |
862 | case wxCANCEL: |
863 | s_bNoAsserts = TRUE; | |
864 | break; | |
9ec05cc9 | 865 | |
3078c3a6 | 866 | //case wxNO: nothing to do |
c801d85f | 867 | } |
3078c3a6 | 868 | } |
7502ba29 VZ |
869 | |
870 | s_bInAssert = FALSE; | |
c801d85f KB |
871 | } |
872 | ||
b2aef89b | 873 | #endif //WXDEBUG |
c801d85f | 874 |