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