]>
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 | ||
27fc802d | 253 | #ifdef wxUSE_NOGUI |
275bf4c1 VZ |
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 | |
c085e333 VZ |
261 | s_bInGetActiveTarget = FALSE; |
262 | ||
275bf4c1 VZ |
263 | // do nothing if it fails - what can we do? |
264 | } | |
c801d85f KB |
265 | } |
266 | ||
9ec05cc9 | 267 | return ms_pLogger; |
c801d85f KB |
268 | } |
269 | ||
c085e333 | 270 | wxLog *wxLog::SetActiveTarget(wxLog *pLogger) |
9ec05cc9 | 271 | { |
c085e333 VZ |
272 | if ( ms_pLogger != NULL ) { |
273 | // flush the old messages before changing because otherwise they might | |
274 | // get lost later if this target is not restored | |
c801d85f | 275 | ms_pLogger->Flush(); |
1c4a764c | 276 | } |
c801d85f | 277 | |
9ec05cc9 VZ |
278 | wxLog *pOldLogger = ms_pLogger; |
279 | ms_pLogger = pLogger; | |
c085e333 | 280 | |
9ec05cc9 | 281 | return pOldLogger; |
c801d85f KB |
282 | } |
283 | ||
fe7b1156 | 284 | wxString wxLog::TimeStamp() const |
c801d85f | 285 | { |
9ef3052c | 286 | wxString str; |
c801d85f | 287 | |
184b5d99 JS |
288 | /* Let's disable TimeStamp and see if anyone complains. |
289 | * If not, we'll remove it, since it's probably unlikely | |
290 | * to ever be used. -- JACS 22/11/98 | |
9ef3052c VZ |
291 | if ( !IsEmpty(m_szTimeFormat) ) { |
292 | char szBuf[128]; | |
293 | time_t timeNow; | |
294 | struct tm *ptmNow; | |
c801d85f | 295 | |
9ef3052c VZ |
296 | time(&timeNow); |
297 | ptmNow = localtime(&timeNow); | |
298 | ||
299 | strftime(szBuf, WXSIZEOF(szBuf), m_szTimeFormat, ptmNow); | |
300 | str = szBuf; | |
301 | } | |
184b5d99 | 302 | */ |
c801d85f | 303 | |
fe7b1156 VZ |
304 | return str; |
305 | } | |
306 | ||
307 | void wxLog::DoLog(wxLogLevel level, const char *szString) | |
308 | { | |
309 | // prepend a timestamp if not disabled | |
310 | wxString str = TimeStamp(); | |
311 | ||
c801d85f | 312 | switch ( level ) { |
9ef3052c | 313 | case wxLOG_FatalError: |
c801d85f KB |
314 | DoLogString(str << _("Fatal error: ") << szString); |
315 | DoLogString(_("Program aborted.")); | |
316 | Flush(); | |
317 | abort(); | |
318 | break; | |
319 | ||
9ef3052c | 320 | case wxLOG_Error: |
c801d85f KB |
321 | DoLogString(str << _("Error: ") << szString); |
322 | break; | |
323 | ||
9ef3052c | 324 | case wxLOG_Warning: |
c801d85f KB |
325 | DoLogString(str << _("Warning: ") << szString); |
326 | break; | |
327 | ||
9ef3052c | 328 | case wxLOG_Info: |
c801d85f | 329 | if ( GetVerbose() ) |
9ef3052c | 330 | case wxLOG_Message: |
c801d85f KB |
331 | DoLogString(str + szString); |
332 | // fall through | |
333 | ||
9ef3052c | 334 | case wxLOG_Status: |
c801d85f KB |
335 | // nothing to do |
336 | break; | |
337 | ||
9ef3052c VZ |
338 | case wxLOG_Trace: |
339 | case wxLOG_Debug: | |
b2aef89b | 340 | #ifdef __WXDEBUG__ |
7502ba29 | 341 | DoLogString(str << (level == wxLOG_Trace ? _("Trace") : _("Debug")) |
c801d85f KB |
342 | << ": " << szString); |
343 | #endif | |
9ec05cc9 | 344 | |
c801d85f KB |
345 | break; |
346 | ||
347 | default: | |
1a5a8367 | 348 | wxFAIL_MSG(_("unknown log level in wxLog::DoLog")); |
c801d85f KB |
349 | } |
350 | } | |
351 | ||
46dc76ba | 352 | void wxLog::DoLogString(const char *WXUNUSED(szString)) |
c801d85f | 353 | { |
1a5a8367 | 354 | wxFAIL_MSG(_("DoLogString must be overrided if it's called.")); |
c801d85f KB |
355 | } |
356 | ||
357 | void wxLog::Flush() | |
358 | { | |
359 | // do nothing | |
360 | } | |
361 | ||
362 | // ---------------------------------------------------------------------------- | |
363 | // wxLogStderr class implementation | |
364 | // ---------------------------------------------------------------------------- | |
365 | ||
366 | wxLogStderr::wxLogStderr(FILE *fp) | |
367 | { | |
368 | if ( fp == NULL ) | |
369 | m_fp = stderr; | |
370 | else | |
371 | m_fp = fp; | |
372 | } | |
373 | ||
374 | void wxLogStderr::DoLogString(const char *szString) | |
375 | { | |
376 | fputs(szString, m_fp); | |
377 | fputc('\n', m_fp); | |
378 | fflush(m_fp); | |
379 | } | |
380 | ||
381 | // ---------------------------------------------------------------------------- | |
382 | // wxLogStream implementation | |
383 | // ---------------------------------------------------------------------------- | |
384 | ||
385 | wxLogStream::wxLogStream(ostream *ostr) | |
386 | { | |
387 | if ( ostr == NULL ) | |
388 | m_ostr = &cerr; | |
389 | else | |
390 | m_ostr = ostr; | |
391 | } | |
392 | ||
393 | void wxLogStream::DoLogString(const char *szString) | |
394 | { | |
395 | (*m_ostr) << szString << endl << flush; | |
396 | } | |
397 | ||
03f38c58 | 398 | #ifndef wxUSE_NOGUI |
c801d85f KB |
399 | // ---------------------------------------------------------------------------- |
400 | // wxLogTextCtrl implementation | |
401 | // ---------------------------------------------------------------------------- | |
c801d85f | 402 | wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl) |
9ef3052c | 403 | // @@@ TODO: in wxGTK wxTextCtrl doesn't derive from streambuf |
5de76427 JS |
404 | |
405 | // Also, in DLL mode in wxMSW, can't use it. | |
406 | #if defined(NO_TEXT_WINDOW_STREAM) | |
407 | #else | |
c801d85f | 408 | : wxLogStream(new ostream(pTextCtrl)) |
5de76427 | 409 | #endif |
c801d85f KB |
410 | { |
411 | } | |
412 | ||
413 | wxLogTextCtrl::~wxLogTextCtrl() | |
414 | { | |
c5c16a30 | 415 | delete m_ostr; |
c801d85f | 416 | } |
c801d85f KB |
417 | |
418 | // ---------------------------------------------------------------------------- | |
419 | // wxLogGui implementation | |
420 | // ---------------------------------------------------------------------------- | |
421 | ||
c801d85f KB |
422 | wxLogGui::wxLogGui() |
423 | { | |
424 | m_bErrors = FALSE; | |
425 | } | |
426 | ||
427 | void wxLogGui::Flush() | |
428 | { | |
429 | if ( !m_bHasMessages ) | |
430 | return; | |
431 | ||
d553992a VZ |
432 | // do it right now to block any new calls to Flush() while we're here |
433 | m_bHasMessages = FALSE; | |
c9dac664 | 434 | |
c801d85f | 435 | // @@@ ugly... |
9ec05cc9 | 436 | |
c801d85f KB |
437 | // concatenate all strings (but not too many to not overfill the msg box) |
438 | wxString str; | |
c86f1403 | 439 | size_t nLines = 0, |
c801d85f KB |
440 | nMsgCount = m_aMessages.Count(); |
441 | ||
442 | // start from the most recent message | |
c86f1403 | 443 | for ( size_t n = nMsgCount; n > 0; n-- ) { |
c801d85f | 444 | // for Windows strings longer than this value are wrapped (NT 4.0) |
c86f1403 | 445 | const size_t nMsgLineWidth = 156; |
c801d85f KB |
446 | |
447 | nLines += (m_aMessages[n - 1].Len() + nMsgLineWidth - 1) / nMsgLineWidth; | |
448 | ||
449 | if ( nLines > 25 ) // don't put too many lines in message box | |
450 | break; | |
451 | ||
452 | str << m_aMessages[n - 1] << "\n"; | |
453 | } | |
454 | ||
455 | if ( m_bErrors ) { | |
456 | wxMessageBox(str, _("Error"), wxOK | wxICON_EXCLAMATION); | |
457 | } | |
458 | else { | |
459 | wxMessageBox(str, _("Information"), wxOK | wxICON_INFORMATION); | |
460 | } | |
461 | ||
462 | // no undisplayed messages whatsoever | |
c801d85f KB |
463 | m_bErrors = FALSE; |
464 | m_aMessages.Empty(); | |
465 | } | |
466 | ||
467 | // the default behaviour is to discard all informational messages if there | |
468 | // are any errors/warnings. | |
9ef3052c | 469 | void wxLogGui::DoLog(wxLogLevel level, const char *szString) |
c801d85f KB |
470 | { |
471 | switch ( level ) { | |
9ef3052c | 472 | case wxLOG_Info: |
c801d85f | 473 | if ( GetVerbose() ) |
9ef3052c | 474 | case wxLOG_Message: |
c801d85f KB |
475 | if ( !m_bErrors ) { |
476 | m_aMessages.Add(szString); | |
477 | m_bHasMessages = TRUE; | |
478 | } | |
479 | break; | |
480 | ||
9ef3052c | 481 | case wxLOG_Status: |
c801d85f KB |
482 | { |
483 | // find the top window and set it's status text if it has any | |
470b7da3 VZ |
484 | wxFrame *pFrame = gs_pFrame; |
485 | if ( pFrame == NULL ) { | |
486 | wxWindow *pWin = wxTheApp->GetTopWindow(); | |
487 | if ( pWin != NULL && pWin->IsKindOf(CLASSINFO(wxFrame)) ) { | |
488 | pFrame = (wxFrame *)pWin; | |
489 | } | |
c801d85f | 490 | } |
470b7da3 VZ |
491 | |
492 | if ( pFrame != NULL ) | |
493 | pFrame->SetStatusText(szString); | |
c801d85f KB |
494 | } |
495 | break; | |
496 | ||
9ef3052c VZ |
497 | case wxLOG_Trace: |
498 | case wxLOG_Debug: | |
b2aef89b | 499 | #ifdef __WXDEBUG__ |
fe7b1156 VZ |
500 | { |
501 | wxString strTime = TimeStamp(); | |
502 | ||
06405ac8 | 503 | #ifdef __WXMSW__ |
fe7b1156 VZ |
504 | // don't prepend debug/trace here: it goes to the debug window |
505 | // anyhow, but do put a timestamp | |
506 | OutputDebugString(strTime + szString + "\n\r"); | |
06405ac8 | 507 | #else |
fe7b1156 VZ |
508 | // send them to stderr |
509 | fprintf(stderr, "%s %s: %s\n", | |
510 | strTime.c_str(), | |
511 | level == wxLOG_Trace ? _("Trace") : _("Debug"), | |
512 | szString); | |
513 | fflush(stderr); | |
06405ac8 | 514 | #endif |
fe7b1156 | 515 | } |
c801d85f KB |
516 | #endif |
517 | break; | |
518 | ||
9ef3052c | 519 | case wxLOG_FatalError: |
c801d85f | 520 | // show this one immediately |
7502ba29 | 521 | wxMessageBox(szString, _("Fatal error"), wxICON_HAND); |
c801d85f KB |
522 | break; |
523 | ||
9ef3052c VZ |
524 | case wxLOG_Error: |
525 | case wxLOG_Warning: | |
c801d85f KB |
526 | // discard earlier informational messages if this is the 1st error |
527 | if ( !m_bErrors ) { | |
528 | m_aMessages.Empty(); | |
529 | m_bHasMessages = TRUE; | |
530 | m_bErrors = TRUE; | |
531 | } | |
532 | ||
533 | m_aMessages.Add(szString); | |
534 | break; | |
9ec05cc9 | 535 | |
c801d85f | 536 | default: |
1a5a8367 | 537 | wxFAIL_MSG(_("unknown log level in wxLogGui::DoLog")); |
c801d85f KB |
538 | } |
539 | } | |
540 | ||
9ef3052c | 541 | // ---------------------------------------------------------------------------- |
fe7b1156 | 542 | // wxLogWindow and wxLogFrame implementation |
9ef3052c VZ |
543 | // ---------------------------------------------------------------------------- |
544 | ||
545 | // log frame class | |
fe7b1156 | 546 | // --------------- |
9ef3052c VZ |
547 | class wxLogFrame : public wxFrame |
548 | { | |
549 | public: | |
fe7b1156 | 550 | // ctor & dtor |
470b7da3 | 551 | wxLogFrame(wxFrame *pParent, wxLogWindow *log, const char *szTitle); |
fe7b1156 | 552 | virtual ~wxLogFrame(); |
9ef3052c VZ |
553 | |
554 | // menu callbacks | |
555 | void OnClose(wxCommandEvent& event); | |
5260b1c5 | 556 | void OnCloseWindow(wxCloseEvent& event); |
9ef3052c VZ |
557 | void OnSave (wxCommandEvent& event); |
558 | void OnClear(wxCommandEvent& event); | |
559 | ||
fe7b1156 VZ |
560 | void OnIdle(wxIdleEvent&); |
561 | ||
9ef3052c VZ |
562 | // accessors |
563 | wxTextCtrl *TextCtrl() const { return m_pTextCtrl; } | |
564 | ||
565 | private: | |
566 | enum | |
567 | { | |
568 | Menu_Close = 100, | |
569 | Menu_Save, | |
570 | Menu_Clear | |
571 | }; | |
572 | ||
fe7b1156 VZ |
573 | // instead of closing just hide the window to be able to Show() it later |
574 | void DoClose() { Show(FALSE); } | |
575 | ||
576 | wxTextCtrl *m_pTextCtrl; | |
577 | wxLogWindow *m_log; | |
9ef3052c VZ |
578 | |
579 | DECLARE_EVENT_TABLE() | |
580 | }; | |
581 | ||
582 | BEGIN_EVENT_TABLE(wxLogFrame, wxFrame) | |
583 | // wxLogWindow menu events | |
584 | EVT_MENU(Menu_Close, wxLogFrame::OnClose) | |
585 | EVT_MENU(Menu_Save, wxLogFrame::OnSave) | |
586 | EVT_MENU(Menu_Clear, wxLogFrame::OnClear) | |
587 | ||
5260b1c5 | 588 | EVT_CLOSE(wxLogFrame::OnCloseWindow) |
9ec05cc9 | 589 | END_EVENT_TABLE() |
9ef3052c | 590 | |
470b7da3 VZ |
591 | wxLogFrame::wxLogFrame(wxFrame *pParent, wxLogWindow *log, const char *szTitle) |
592 | : wxFrame(pParent, -1, szTitle) | |
9ef3052c | 593 | { |
fe7b1156 | 594 | m_log = log; |
f42d2625 | 595 | |
9ef3052c | 596 | // @@ kludge: wxSIMPLE_BORDER is simply to prevent wxWindows from creating |
fe7b1156 | 597 | // a rich edit control instead of a normal one we want in wxMSW |
9ef3052c VZ |
598 | m_pTextCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition, |
599 | wxDefaultSize, | |
470b7da3 | 600 | //wxSIMPLE_BORDER | |
9ef3052c VZ |
601 | wxTE_MULTILINE | |
602 | wxHSCROLL | | |
603 | wxTE_READONLY); | |
9ef3052c VZ |
604 | |
605 | // create menu | |
606 | wxMenuBar *pMenuBar = new wxMenuBar; | |
607 | wxMenu *pMenu = new wxMenu; | |
fe7b1156 VZ |
608 | pMenu->Append(Menu_Save, _("&Save..."), _("Save log contents to file")); |
609 | pMenu->Append(Menu_Clear, _("C&lear"), _("Clear the log contents")); | |
9ef3052c | 610 | pMenu->AppendSeparator(); |
fe7b1156 | 611 | pMenu->Append(Menu_Close, _("&Close"), _("Close this window")); |
1a5a8367 | 612 | pMenuBar->Append(pMenu, _("&Log")); |
9ef3052c VZ |
613 | SetMenuBar(pMenuBar); |
614 | ||
fe7b1156 VZ |
615 | // status bar for menu prompts |
616 | CreateStatusBar(); | |
617 | ||
618 | m_log->OnFrameCreate(this); | |
9ef3052c VZ |
619 | } |
620 | ||
46dc76ba | 621 | void wxLogFrame::OnClose(wxCommandEvent& WXUNUSED(event)) |
9ef3052c | 622 | { |
fe7b1156 | 623 | DoClose(); |
9ef3052c VZ |
624 | } |
625 | ||
46dc76ba | 626 | void wxLogFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
5260b1c5 | 627 | { |
fe7b1156 VZ |
628 | DoClose(); |
629 | } | |
630 | ||
46dc76ba | 631 | void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event)) |
9ef3052c VZ |
632 | { |
633 | // get the file name | |
634 | // ----------------- | |
635 | const char *szFileName = wxSaveFileSelector("log", "txt", "log.txt"); | |
636 | if ( szFileName == NULL ) { | |
637 | // cancelled | |
638 | return; | |
639 | } | |
640 | ||
641 | // open file | |
642 | // --------- | |
643 | wxFile file; | |
c9dac664 | 644 | bool bOk = FALSE; |
9ef3052c | 645 | if ( wxFile::Exists(szFileName) ) { |
46dc76ba | 646 | bool bAppend = FALSE; |
9ef3052c VZ |
647 | wxString strMsg; |
648 | strMsg.Printf(_("Append log to file '%s' " | |
649 | "(choosing [No] will overwrite it)?"), szFileName); | |
1a5a8367 | 650 | switch ( wxMessageBox(strMsg, _("Question"), wxYES_NO | wxCANCEL) ) { |
9ef3052c VZ |
651 | case wxYES: |
652 | bAppend = TRUE; | |
653 | break; | |
654 | ||
655 | case wxNO: | |
656 | bAppend = FALSE; | |
657 | break; | |
658 | ||
659 | case wxCANCEL: | |
660 | return; | |
661 | ||
662 | default: | |
1a5a8367 | 663 | wxFAIL_MSG(_("invalid message box return value")); |
9ef3052c VZ |
664 | } |
665 | ||
666 | if ( bAppend ) { | |
667 | bOk = file.Open(szFileName, wxFile::write_append); | |
668 | } | |
669 | else { | |
670 | bOk = file.Create(szFileName, TRUE /* overwrite */); | |
671 | } | |
672 | } | |
673 | else { | |
674 | bOk = file.Create(szFileName); | |
675 | } | |
676 | ||
677 | // retrieve text and save it | |
678 | // ------------------------- | |
2049ba38 | 679 | #ifdef __WXGTK__ |
9ef3052c | 680 | // @@@@ TODO: no GetNumberOfLines and GetLineText in wxGTK yet |
1a5a8367 | 681 | wxLogError(_("Sorry, this function is not implemented under GTK")); |
f42d2625 | 682 | #else |
9ef3052c VZ |
683 | int nLines = m_pTextCtrl->GetNumberOfLines(); |
684 | for ( int nLine = 0; bOk && nLine < nLines; nLine++ ) { | |
685 | bOk = file.Write(m_pTextCtrl->GetLineText(nLine) + wxTextFile::GetEOL()); | |
686 | } | |
687 | #endif //GTK | |
9ec05cc9 | 688 | |
9ef3052c VZ |
689 | if ( bOk ) |
690 | bOk = file.Close(); | |
691 | ||
692 | if ( !bOk ) { | |
7502ba29 | 693 | wxLogError(_("Can't save log contents to file.")); |
9ef3052c VZ |
694 | return; |
695 | } | |
696 | } | |
697 | ||
46dc76ba | 698 | void wxLogFrame::OnClear(wxCommandEvent& WXUNUSED(event)) |
9ef3052c VZ |
699 | { |
700 | m_pTextCtrl->Clear(); | |
701 | } | |
702 | ||
fe7b1156 VZ |
703 | wxLogFrame::~wxLogFrame() |
704 | { | |
705 | m_log->OnFrameDelete(this); | |
706 | } | |
707 | ||
708 | // wxLogWindow | |
709 | // ----------- | |
470b7da3 VZ |
710 | wxLogWindow::wxLogWindow(wxFrame *pParent, |
711 | const char *szTitle, | |
712 | bool bShow, | |
713 | bool bDoPass) | |
9ef3052c | 714 | { |
a3622daa VZ |
715 | m_bPassMessages = bDoPass; |
716 | ||
470b7da3 | 717 | m_pLogFrame = new wxLogFrame(pParent, this, szTitle); |
a3622daa | 718 | m_pOldLog = wxLog::SetActiveTarget(this); |
9ec05cc9 | 719 | |
f42d2625 VZ |
720 | if ( bShow ) |
721 | m_pLogFrame->Show(TRUE); | |
9ef3052c VZ |
722 | } |
723 | ||
e51c4943 | 724 | void wxLogWindow::Show(bool bShow) |
9ef3052c VZ |
725 | { |
726 | m_pLogFrame->Show(bShow); | |
727 | } | |
728 | ||
fe7b1156 | 729 | void wxLogWindow::Flush() |
06db8ebd | 730 | { |
fe7b1156 VZ |
731 | if ( m_pOldLog != NULL ) |
732 | m_pOldLog->Flush(); | |
733 | ||
c9dac664 | 734 | m_bHasMessages = FALSE; |
06db8ebd VZ |
735 | } |
736 | ||
9ef3052c VZ |
737 | void wxLogWindow::DoLog(wxLogLevel level, const char *szString) |
738 | { | |
739 | // first let the previous logger show it | |
a3622daa | 740 | if ( m_pOldLog != NULL && m_bPassMessages ) { |
9ec05cc9 | 741 | // @@@ why can't we access protected wxLog method from here (we derive |
9ef3052c VZ |
742 | // from wxLog)? gcc gives "DoLog is protected in this context", what |
743 | // does this mean? Anyhow, the cast is harmless and let's us do what | |
744 | // we want. | |
745 | ((wxLogWindow *)m_pOldLog)->DoLog(level, szString); | |
746 | } | |
9ec05cc9 | 747 | |
1c4a764c VZ |
748 | if ( m_pLogFrame ) { |
749 | switch ( level ) { | |
750 | case wxLOG_Status: | |
751 | // by default, these messages are ignored by wxLog, so process | |
752 | // them ourselves | |
753 | { | |
754 | wxString str = TimeStamp(); | |
755 | str << _("Status: ") << szString; | |
756 | DoLogString(str); | |
757 | } | |
758 | break; | |
759 | ||
760 | // don't put trace messages in the text window for 2 reasons: | |
761 | // 1) there are too many of them | |
762 | // 2) they may provoke other trace messages thus sending a program | |
763 | // into an infinite loop | |
764 | case wxLOG_Trace: | |
765 | break; | |
766 | ||
767 | default: | |
768 | // and this will format it nicely and call our DoLogString() | |
769 | wxLog::DoLog(level, szString); | |
770 | } | |
3b926128 | 771 | } |
fe7b1156 VZ |
772 | |
773 | m_bHasMessages = TRUE; | |
9ef3052c VZ |
774 | } |
775 | ||
776 | void wxLogWindow::DoLogString(const char *szString) | |
777 | { | |
778 | // put the text into our window | |
779 | wxTextCtrl *pText = m_pLogFrame->TextCtrl(); | |
780 | ||
781 | // remove selection (WriteText is in fact ReplaceSelection) | |
2049ba38 | 782 | #ifdef __WXMSW__ |
f42d2625 VZ |
783 | long nLen = pText->GetLastPosition(); |
784 | pText->SetSelection(nLen, nLen); | |
785 | #endif // Windows | |
9ef3052c VZ |
786 | |
787 | pText->WriteText(szString); | |
788 | pText->WriteText("\n"); // "\n" ok here (_not_ "\r\n") | |
789 | ||
c085e333 | 790 | // TODO ensure that the line can be seen |
9ef3052c VZ |
791 | } |
792 | ||
fe7b1156 VZ |
793 | wxFrame *wxLogWindow::GetFrame() const |
794 | { | |
795 | return m_pLogFrame; | |
796 | } | |
797 | ||
8fdca65c | 798 | void wxLogWindow::OnFrameCreate(wxFrame *WXUNUSED(frame)) |
fe7b1156 VZ |
799 | { |
800 | } | |
801 | ||
8fdca65c | 802 | void wxLogWindow::OnFrameDelete(wxFrame *WXUNUSED(frame)) |
fe7b1156 | 803 | { |
c085e333 | 804 | m_pLogFrame = (wxLogFrame *)NULL; |
fe7b1156 VZ |
805 | } |
806 | ||
9ef3052c VZ |
807 | wxLogWindow::~wxLogWindow() |
808 | { | |
c085e333 VZ |
809 | delete m_pOldLog; |
810 | ||
fe7b1156 VZ |
811 | // may be NULL if log frame already auto destroyed itself |
812 | delete m_pLogFrame; | |
9ef3052c VZ |
813 | } |
814 | ||
27fc802d | 815 | #endif //wxUSE_NOGUI |
c801d85f KB |
816 | |
817 | // ============================================================================ | |
818 | // Global functions/variables | |
819 | // ============================================================================ | |
820 | ||
821 | // ---------------------------------------------------------------------------- | |
822 | // static variables | |
823 | // ---------------------------------------------------------------------------- | |
c67daf87 | 824 | wxLog *wxLog::ms_pLogger = (wxLog *) NULL; |
c085e333 | 825 | bool wxLog::ms_doLog = TRUE; |
275bf4c1 | 826 | bool wxLog::ms_bAutoCreate = TRUE; |
88f2aa37 | 827 | wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0; |
c801d85f KB |
828 | |
829 | // ---------------------------------------------------------------------------- | |
830 | // stdout error logging helper | |
831 | // ---------------------------------------------------------------------------- | |
832 | ||
833 | // helper function: wraps the message and justifies it under given position | |
834 | // (looks more pretty on the terminal). Also adds newline at the end. | |
835 | // | |
836 | // @@ this is now disabled until I find a portable way of determining the | |
9ef3052c | 837 | // terminal window size (ok, I found it but does anybody really cares?) |
c801d85f KB |
838 | #ifdef LOG_PRETTY_WRAP |
839 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz) | |
840 | { | |
841 | size_t nMax = 80; // @@@@ | |
842 | size_t nStart = strlen(pszPrefix); | |
843 | fputs(pszPrefix, f); | |
844 | ||
845 | size_t n; | |
846 | while ( *psz != '\0' ) { | |
847 | for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ ) | |
848 | putc(*psz++, f); | |
849 | ||
850 | // wrapped? | |
851 | if ( *psz != '\0' ) { | |
852 | /*putc('\n', f);*/ | |
853 | for ( n = 0; n < nStart; n++ ) | |
854 | putc(' ', f); | |
855 | ||
856 | // as we wrapped, squeeze all white space | |
857 | while ( isspace(*psz) ) | |
858 | psz++; | |
859 | } | |
860 | } | |
861 | ||
862 | putc('\n', f); | |
863 | } | |
864 | #endif //LOG_PRETTY_WRAP | |
865 | ||
866 | // ---------------------------------------------------------------------------- | |
867 | // error code/error message retrieval functions | |
868 | // ---------------------------------------------------------------------------- | |
869 | ||
870 | // get error code from syste | |
871 | unsigned long wxSysErrorCode() | |
872 | { | |
2049ba38 | 873 | #ifdef __WXMSW__ |
c801d85f KB |
874 | #ifdef __WIN32__ |
875 | return ::GetLastError(); | |
876 | #else //WIN16 | |
877 | // @@@@ what to do on Windows 3.1? | |
878 | return 0; | |
879 | #endif //WIN16/32 | |
880 | #else //Unix | |
881 | return errno; | |
882 | #endif //Win/Unix | |
883 | } | |
884 | ||
885 | // get error message from system | |
886 | const char *wxSysErrorMsg(unsigned long nErrCode) | |
887 | { | |
888 | if ( nErrCode == 0 ) | |
889 | nErrCode = wxSysErrorCode(); | |
890 | ||
2049ba38 | 891 | #ifdef __WXMSW__ |
9ef3052c VZ |
892 | #ifdef __WIN32__ |
893 | static char s_szBuf[LOG_BUFFER_SIZE / 2]; | |
894 | ||
895 | // get error message from system | |
896 | LPVOID lpMsgBuf; | |
897 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | |
9ec05cc9 | 898 | NULL, nErrCode, |
9ef3052c VZ |
899 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
900 | (LPTSTR)&lpMsgBuf, | |
901 | 0, NULL); | |
902 | ||
903 | // copy it to our buffer and free memory | |
904 | strncpy(s_szBuf, (const char *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1); | |
905 | s_szBuf[WXSIZEOF(s_szBuf) - 1] = '\0'; | |
906 | LocalFree(lpMsgBuf); | |
907 | ||
908 | // returned string is capitalized and ended with '\r\n' - bad | |
81d66cf3 | 909 | s_szBuf[0] = (char)wxToLower(s_szBuf[0]); |
9ef3052c VZ |
910 | size_t len = strlen(s_szBuf); |
911 | if ( len > 0 ) { | |
912 | // truncate string | |
913 | if ( s_szBuf[len - 2] == '\r' ) | |
914 | s_szBuf[len - 2] = '\0'; | |
915 | } | |
c801d85f | 916 | |
9ef3052c VZ |
917 | return s_szBuf; |
918 | #else //Win16 | |
919 | // TODO @@@@ | |
920 | return NULL; | |
921 | #endif // Win16/32 | |
922 | #else // Unix | |
923 | return strerror(nErrCode); | |
924 | #endif // Win/Unix | |
c801d85f KB |
925 | } |
926 | ||
927 | // ---------------------------------------------------------------------------- | |
928 | // debug helper | |
929 | // ---------------------------------------------------------------------------- | |
930 | ||
b2aef89b | 931 | #ifdef __WXDEBUG__ |
c801d85f | 932 | |
7502ba29 VZ |
933 | void Trap() |
934 | { | |
935 | #ifdef __WXMSW__ | |
936 | DebugBreak(); | |
34138703 JS |
937 | #elif defined(__WXSTUBS__) |
938 | // TODO | |
7502ba29 VZ |
939 | #else // Unix |
940 | raise(SIGTRAP); | |
941 | #endif // Win/Unix | |
942 | } | |
943 | ||
c801d85f KB |
944 | // this function is called when an assert fails |
945 | void wxOnAssert(const char *szFile, int nLine, const char *szMsg) | |
946 | { | |
947 | // this variable can be set to true to suppress "assert failure" messages | |
7502ba29 VZ |
948 | static bool s_bNoAsserts = FALSE; |
949 | static bool s_bInAssert = FALSE; | |
950 | ||
951 | if ( s_bInAssert ) { | |
952 | // He-e-e-e-elp!! we're trapped in endless loop | |
953 | Trap(); | |
c085e333 VZ |
954 | |
955 | return; | |
7502ba29 VZ |
956 | } |
957 | ||
958 | s_bInAssert = TRUE; | |
c801d85f KB |
959 | |
960 | char szBuf[LOG_BUFFER_SIZE]; | |
961 | sprintf(szBuf, _("Assert failed in file %s at line %d"), szFile, nLine); | |
962 | if ( szMsg != NULL ) { | |
963 | strcat(szBuf, ": "); | |
964 | strcat(szBuf, szMsg); | |
965 | } | |
966 | else { | |
967 | strcat(szBuf, "."); | |
968 | } | |
969 | ||
3078c3a6 | 970 | if ( !s_bNoAsserts ) { |
9ec05cc9 VZ |
971 | // send it to the normal log destination |
972 | wxLogDebug(szBuf); | |
973 | ||
03f38c58 VZ |
974 | #ifdef wxUSE_NOGUI |
975 | Trap(); | |
976 | #else | |
977 | strcat(szBuf, _("\nDo you want to stop the program?" | |
978 | "\nYou can also choose [Cancel] to suppress " | |
979 | "further warnings.")); | |
3078c3a6 | 980 | |
03f38c58 VZ |
981 | switch ( wxMessageBox(szBuf, _("Debug"), |
982 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) { | |
983 | case wxYES: | |
984 | Trap(); | |
985 | break; | |
c801d85f | 986 | |
03f38c58 VZ |
987 | case wxCANCEL: |
988 | s_bNoAsserts = TRUE; | |
989 | break; | |
9ec05cc9 | 990 | |
03f38c58 VZ |
991 | //case wxNO: nothing to do |
992 | } | |
993 | #endif // USE_NOGUI | |
3078c3a6 | 994 | } |
7502ba29 VZ |
995 | |
996 | s_bInAssert = FALSE; | |
c801d85f KB |
997 | } |
998 | ||
b2aef89b | 999 | #endif //WXDEBUG |
c801d85f | 1000 |