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