log.h 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: log.h
  3. // Purpose: interface of wxLog* classes
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. #if wxUSE_BASE
  8. /**
  9. Different standard log levels (you may also define your own) used with
  10. by standard wxLog functions wxLogGeneric(), wxLogError(), wxLogWarning(), etc...
  11. */
  12. enum wxLogLevelValues
  13. {
  14. wxLOG_FatalError, //!< program can't continue, abort immediately
  15. wxLOG_Error, //!< a serious error, user must be informed about it
  16. wxLOG_Warning, //!< user is normally informed about it but may be ignored
  17. wxLOG_Message, //!< normal message (i.e. normal output of a non GUI app)
  18. wxLOG_Status, //!< informational: might go to the status line of GUI app
  19. wxLOG_Info, //!< informational message (a.k.a. 'Verbose')
  20. wxLOG_Debug, //!< never shown to the user, disabled in release mode
  21. wxLOG_Trace, //!< trace messages are also only enabled in debug mode
  22. wxLOG_Progress, //!< used for progress indicator (not yet)
  23. wxLOG_User = 100, //!< user defined levels start here
  24. wxLOG_Max = 10000
  25. };
  26. /**
  27. The type used to specify a log level.
  28. Default values of ::wxLogLevel used by wxWidgets are contained in the
  29. ::wxLogLevelValues enumeration.
  30. */
  31. typedef unsigned long wxLogLevel;
  32. /**
  33. Information about a log record (unit of the log output).
  34. */
  35. class wxLogRecordInfo
  36. {
  37. public:
  38. /// The name of the file where this log message was generated.
  39. const char *filename;
  40. /// The line number at which this log message was generated.
  41. int line;
  42. /**
  43. The name of the function where the log record was generated.
  44. This field may be @NULL if the compiler doesn't support @c __FUNCTION__
  45. (but most modern compilers do).
  46. */
  47. const char *func;
  48. /// Time when the log message was generated.
  49. time_t timestamp;
  50. /**
  51. Id of the thread in which the message was generated.
  52. This field is only available if wxWidgets was built with threads
  53. support (<code>wxUSE_THREADS == 1</code>).
  54. @see wxThread::GetCurrentId()
  55. */
  56. wxThreadIdType threadId;
  57. };
  58. /**
  59. @class wxLogFormatter
  60. wxLogFormatter class is used to format the log messages. It implements the
  61. default formatting and can be derived from to create custom formatters.
  62. The default implementation formats the message into a string containing
  63. the time stamp, level-dependent prefix and the message itself.
  64. To change it, you can derive from it and override its Format() method. For
  65. example, to include the thread id in the log messages you can use
  66. @code
  67. class LogFormatterWithThread : public wxLogFormatter
  68. {
  69. virtual wxString Format(wxLogLevel level,
  70. const wxString& msg,
  71. const wxLogRecordInfo& info) const
  72. {
  73. return wxString::Format("[%d] %s(%d) : %s",
  74. info.threadId, info.filename, info.line, msg);
  75. }
  76. };
  77. @endcode
  78. And then associate it with wxLog instance using its SetFormatter(). Then,
  79. if you call:
  80. @code
  81. wxLogMessage(_("*** Application started ***"));
  82. @endcode
  83. the log output could be something like:
  84. @verbatim
  85. [7872] d:\testApp\src\testApp.cpp(85) : *** Application started ***
  86. @endverbatim
  87. @library{wxbase}
  88. @category{logging}
  89. @see @ref overview_log
  90. @since 2.9.4
  91. */
  92. class wxLogFormatter
  93. {
  94. public:
  95. /**
  96. The default ctor does nothing.
  97. */
  98. wxLogFormatter();
  99. /**
  100. This function creates the full log message string.
  101. Override it to customize the output string format.
  102. @param level
  103. The level of this log record, e.g. ::wxLOG_Error.
  104. @param msg
  105. The log message itself.
  106. @param info
  107. All the other information (such as time, component, location...)
  108. associated with this log record.
  109. @return
  110. The formated message.
  111. @note
  112. Time stamping is disabled for Visual C++ users in debug builds by
  113. default because otherwise it would be impossible to directly go to the line
  114. from which the log message was generated by simply clicking in the debugger
  115. window on the corresponding error message. If you wish to enable it, override
  116. FormatTime().
  117. */
  118. virtual wxString Format(wxLogLevel level,
  119. const wxString& msg,
  120. const wxLogRecordInfo& info) const;
  121. protected:
  122. /**
  123. This function formats the time stamp part of the log message.
  124. Override this function if you need to customize just the time stamp.
  125. @param time
  126. Time to format.
  127. @return
  128. The formated time string, may be empty.
  129. */
  130. virtual wxString FormatTime(time_t time) const;
  131. };
  132. /**
  133. @class wxLog
  134. wxLog class defines the interface for the <em>log targets</em> used by wxWidgets
  135. logging functions as explained in the @ref overview_log.
  136. The only situations when you need to directly use this class is when you want
  137. to derive your own log target because the existing ones don't satisfy your
  138. needs.
  139. Otherwise, it is completely hidden behind the @ref group_funcmacro_log "wxLogXXX() functions"
  140. and you may not even know about its existence.
  141. @note For console-mode applications, the default target is wxLogStderr, so
  142. that all @e wxLogXXX() functions print on @c stderr when @c wxUSE_GUI = 0.
  143. @library{wxbase}
  144. @category{logging}
  145. @see @ref overview_log, @ref group_funcmacro_log "wxLogXXX() functions"
  146. */
  147. class wxLog
  148. {
  149. public:
  150. /**
  151. @name Trace mask functions
  152. */
  153. //@{
  154. /**
  155. Add the @a mask to the list of allowed masks for wxLogTrace().
  156. @see RemoveTraceMask(), GetTraceMasks()
  157. */
  158. static void AddTraceMask(const wxString& mask);
  159. /**
  160. Removes all trace masks previously set with AddTraceMask().
  161. @see RemoveTraceMask()
  162. */
  163. static void ClearTraceMasks();
  164. /**
  165. Returns the currently allowed list of string trace masks.
  166. @see AddTraceMask().
  167. */
  168. static const wxArrayString& GetTraceMasks();
  169. /**
  170. Returns @true if the @a mask is one of allowed masks for wxLogTrace().
  171. See also: AddTraceMask(), RemoveTraceMask()
  172. */
  173. static bool IsAllowedTraceMask(const wxString& mask);
  174. /**
  175. Remove the @a mask from the list of allowed masks for
  176. wxLogTrace().
  177. @see AddTraceMask()
  178. */
  179. static void RemoveTraceMask(const wxString& mask);
  180. //@}
  181. /**
  182. @name Log target functions
  183. */
  184. //@{
  185. /**
  186. Instructs wxLog to not create new log targets on the fly if there is none
  187. currently (see GetActiveTarget()).
  188. (Almost) for internal use only: it is supposed to be called by the
  189. application shutdown code (where you don't want the log target to be
  190. automatically created anymore).
  191. Note that this function also calls ClearTraceMasks().
  192. */
  193. static void DontCreateOnDemand();
  194. /**
  195. Returns the pointer to the active log target (may be @NULL).
  196. Notice that if SetActiveTarget() hadn't been previously explicitly
  197. called, this function will by default try to create a log target by
  198. calling wxAppTraits::CreateLogTarget() which may be overridden in a
  199. user-defined traits class to change the default behaviour. You may also
  200. call DontCreateOnDemand() to disable this behaviour.
  201. When this function is called from threads other than main one,
  202. auto-creation doesn't happen. But if the thread has a thread-specific
  203. log target previously set by SetThreadActiveTarget(), it is returned
  204. instead of the global one. Otherwise, the global log target is
  205. returned.
  206. */
  207. static wxLog* GetActiveTarget();
  208. /**
  209. Sets the specified log target as the active one.
  210. Returns the pointer to the previous active log target (may be @NULL).
  211. To suppress logging use a new instance of wxLogNull not @NULL. If the
  212. active log target is set to @NULL a new default log target will be
  213. created when logging occurs.
  214. @see SetThreadActiveTarget()
  215. */
  216. static wxLog* SetActiveTarget(wxLog* logtarget);
  217. /**
  218. Sets a thread-specific log target.
  219. The log target passed to this function will be used for all messages
  220. logged by the current thread using the usual wxLog functions. This
  221. shouldn't be called from the main thread which never uses a thread-
  222. specific log target but can be used for the other threads to handle
  223. thread logging completely separately; instead of buffering thread log
  224. messages in the main thread logger.
  225. Notice that unlike for SetActiveTarget(), wxWidgets does not destroy
  226. the thread-specific log targets when the thread terminates so doing
  227. this is your responsibility.
  228. This method is only available if @c wxUSE_THREADS is 1, i.e. wxWidgets
  229. was compiled with threads support.
  230. @param logger
  231. The new thread-specific log target, possibly @NULL.
  232. @return
  233. The previous thread-specific log target, initially @NULL.
  234. @since 2.9.1
  235. */
  236. static wxLog *SetThreadActiveTarget(wxLog *logger);
  237. /**
  238. Flushes the current log target if any, does nothing if there is none.
  239. When this method is called from the main thread context, it also
  240. flushes any previously buffered messages logged by the other threads.
  241. When it is called from the other threads it simply calls Flush() on the
  242. currently active log target, so it mostly makes sense to do this if a
  243. thread has its own logger set with SetThreadActiveTarget().
  244. */
  245. static void FlushActive();
  246. /**
  247. Resumes logging previously suspended by a call to Suspend().
  248. All messages logged in the meanwhile will be flushed soon.
  249. */
  250. static void Resume();
  251. /**
  252. Suspends the logging until Resume() is called.
  253. Note that the latter must be called the same number of times as the former
  254. to undo it, i.e. if you call Suspend() twice you must call Resume() twice as well.
  255. Note that suspending the logging means that the log sink won't be flushed
  256. periodically, it doesn't have any effect if the current log target does the
  257. logging immediately without waiting for Flush() to be called (the standard
  258. GUI log target only shows the log dialog when it is flushed, so Suspend()
  259. works as expected with it).
  260. @see Resume(), wxLogNull
  261. */
  262. static void Suspend();
  263. //@}
  264. /**
  265. @name Log level functions
  266. */
  267. //@{
  268. /**
  269. Returns the current log level limit.
  270. All messages at levels strictly greater than the value returned by this
  271. function are not logged at all.
  272. @see SetLogLevel(), IsLevelEnabled()
  273. */
  274. static wxLogLevel GetLogLevel();
  275. /**
  276. Returns true if logging at this level is enabled for the current thread.
  277. This function only returns @true if logging is globally enabled and if
  278. @a level is less than or equal to the maximal log level enabled for the
  279. given @a component.
  280. @see IsEnabled(), SetLogLevel(), GetLogLevel(), SetComponentLevel()
  281. @since 2.9.1
  282. */
  283. static bool IsLevelEnabled(wxLogLevel level, wxString component);
  284. /**
  285. Sets the log level for the given component.
  286. For example, to disable all but error messages from wxWidgets network
  287. classes you may use
  288. @code
  289. wxLog::SetComponentLevel("wx/net", wxLOG_Error);
  290. @endcode
  291. SetLogLevel() may be used to set the global log level.
  292. @param component
  293. Non-empty component name, possibly using slashes (@c /) to separate
  294. it into several parts.
  295. @param level
  296. Maximal level of log messages from this component which will be
  297. handled instead of being simply discarded.
  298. @since 2.9.1
  299. */
  300. static void SetComponentLevel(const wxString& component, wxLogLevel level);
  301. /**
  302. Specifies that log messages with level greater (numerically) than
  303. @a logLevel should be ignored and not sent to the active log target.
  304. @see SetComponentLevel()
  305. */
  306. static void SetLogLevel(wxLogLevel logLevel);
  307. //@}
  308. /**
  309. @name Enable/disable features functions
  310. */
  311. //@{
  312. /**
  313. Globally enable or disable logging.
  314. Calling this function with @false argument disables all log messages
  315. for the current thread.
  316. @see wxLogNull, IsEnabled()
  317. @return
  318. The old state, i.e. @true if logging was previously enabled and
  319. @false if it was disabled.
  320. */
  321. static bool EnableLogging(bool enable = true);
  322. /**
  323. Returns true if logging is enabled at all now.
  324. @see IsLevelEnabled(), EnableLogging()
  325. */
  326. static bool IsEnabled();
  327. /**
  328. Returns whether the repetition counting mode is enabled.
  329. */
  330. static bool GetRepetitionCounting();
  331. /**
  332. Enables logging mode in which a log message is logged once, and in case exactly
  333. the same message successively repeats one or more times, only the number of
  334. repetitions is logged.
  335. */
  336. static void SetRepetitionCounting(bool repetCounting = true);
  337. /**
  338. Returns the current timestamp format string.
  339. Notice that the current time stamp is only used by the default log
  340. formatter and custom formatters may ignore this format.
  341. */
  342. static const wxString& GetTimestamp();
  343. /**
  344. Sets the timestamp format prepended by the default log targets to all
  345. messages. The string may contain any normal characters as well as %
  346. prefixed format specifiers, see @e strftime() manual for details.
  347. Passing an empty string to this function disables message time stamping.
  348. Notice that the current time stamp is only used by the default log
  349. formatter and custom formatters may ignore this format. You can also
  350. define a custom wxLogFormatter to customize the time stamp handling
  351. beyond changing its format.
  352. */
  353. static void SetTimestamp(const wxString& format);
  354. /**
  355. Disables time stamping of the log messages.
  356. Notice that the current time stamp is only used by the default log
  357. formatter and custom formatters may ignore calls to this function.
  358. @since 2.9.0
  359. */
  360. static void DisableTimestamp();
  361. /**
  362. Returns whether the verbose mode is currently active.
  363. */
  364. static bool GetVerbose();
  365. /**
  366. Activates or deactivates verbose mode in which the verbose messages are
  367. logged as the normal ones instead of being silently dropped.
  368. The verbose messages are the trace messages which are not disabled in the
  369. release mode and are generated by wxLogVerbose().
  370. @see @ref overview_log
  371. */
  372. static void SetVerbose(bool verbose = true);
  373. //@}
  374. /**
  375. Sets the specified formatter as the active one.
  376. @param formatter
  377. The new formatter. If @NULL, reset to the default formatter.
  378. Returns the pointer to the previous formatter. You must delete it
  379. if you don't plan to attach it again to a wxLog object later.
  380. @since 2.9.4
  381. */
  382. wxLogFormatter *SetFormatter(wxLogFormatter* formatter);
  383. /**
  384. Some of wxLog implementations, most notably the standard wxLogGui class,
  385. buffer the messages (for example, to avoid showing the user a zillion of modal
  386. message boxes one after another -- which would be really annoying).
  387. This function shows them all and clears the buffer contents.
  388. If the buffer is already empty, nothing happens.
  389. If you override this method in a derived class, call the base class
  390. version first, before doing anything else.
  391. */
  392. virtual void Flush();
  393. /**
  394. Log the given record.
  395. This function should only be called from the DoLog() implementations in
  396. the derived classes if they need to call DoLogRecord() on another log
  397. object (they can, of course, just use wxLog::DoLogRecord() call syntax
  398. to call it on the object itself). It should not be used for logging new
  399. messages which can be only sent to the currently active logger using
  400. OnLog() which also checks if the logging (for this level) is enabled
  401. while this method just directly calls DoLog().
  402. Example of use of this class from wxLogChain:
  403. @code
  404. void wxLogChain::DoLogRecord(wxLogLevel level,
  405. const wxString& msg,
  406. const wxLogRecordInfo& info)
  407. {
  408. // let the previous logger show it
  409. if ( m_logOld && IsPassingMessages() )
  410. m_logOld->LogRecord(level, msg, info);
  411. // and also send it to the new one
  412. if ( m_logNew && m_logNew != this )
  413. m_logNew->LogRecord(level, msg, info);
  414. }
  415. @endcode
  416. @since 2.9.1
  417. */
  418. void LogRecord(wxLogLevel level, const wxString& msg, const wxLogRecordInfo& info);
  419. protected:
  420. /**
  421. @name Logging callbacks.
  422. The functions which should be overridden by custom log targets.
  423. When defining a new log target, you have a choice between overriding
  424. DoLogRecord(), which provides maximal flexibility, DoLogTextAtLevel()
  425. which can be used if you don't intend to change the default log
  426. messages formatting but want to handle log messages of different levels
  427. differently or, in the simplest case, DoLogText().
  428. */
  429. //@{
  430. /**
  431. Called to log a new record.
  432. Any log message created by wxLogXXX() functions is passed to this
  433. method of the active log target. The default implementation prepends
  434. the timestamp and, for some log levels (e.g. error and warning), the
  435. corresponding prefix to @a msg and passes it to DoLogTextAtLevel().
  436. You may override this method to implement custom formatting of the
  437. log messages or to implement custom filtering of log messages (e.g. you
  438. could discard all log messages coming from the given source file).
  439. */
  440. virtual void DoLogRecord(wxLogLevel level,
  441. const wxString& msg,
  442. const wxLogRecordInfo& info);
  443. /**
  444. Called to log the specified string at given level.
  445. The base class versions logs debug and trace messages on the system
  446. default debug output channel and passes all the other messages to
  447. DoLogText().
  448. */
  449. virtual void DoLogTextAtLevel(wxLogLevel level, const wxString& msg);
  450. /**
  451. Called to log the specified string.
  452. A simple implementation might just send the string to @c stdout or
  453. @c stderr or save it in a file (of course, the already existing
  454. wxLogStderr can be used for this).
  455. The base class version of this function asserts so it must be
  456. overridden if you don't override DoLogRecord() or DoLogTextAtLevel().
  457. */
  458. virtual void DoLogText(const wxString& msg);
  459. //@}
  460. };
  461. /**
  462. @class wxLogChain
  463. This simple class allows you to chain log sinks, that is to install a new sink but
  464. keep passing log messages to the old one instead of replacing it completely as
  465. wxLog::SetActiveTarget does.
  466. It is especially useful when you want to divert the logs somewhere (for
  467. example to a file or a log window) but also keep showing the error messages
  468. using the standard dialogs as wxLogGui does by default.
  469. Example of usage:
  470. @code
  471. wxLogChain *logChain = new wxLogChain(new wxLogStderr);
  472. // all the log messages are sent to stderr and also processed as usually
  473. ...
  474. // don't delete logChain directly as this would leave a dangling
  475. // pointer as active log target, use SetActiveTarget() instead
  476. delete wxLog::SetActiveTarget(...something else or NULL...);
  477. @endcode
  478. @library{wxbase}
  479. @category{logging}
  480. */
  481. class wxLogChain : public wxLog
  482. {
  483. public:
  484. /**
  485. Sets the specified @c logger (which may be @NULL) as the default log
  486. target but the log messages are also passed to the previous log target if any.
  487. */
  488. wxLogChain(wxLog* logger);
  489. /**
  490. Destroys the previous log target.
  491. */
  492. virtual ~wxLogChain();
  493. /**
  494. Detaches the old log target so it won't be destroyed when the wxLogChain object
  495. is destroyed.
  496. */
  497. void DetachOldLog();
  498. /**
  499. Returns the pointer to the previously active log target (which may be @NULL).
  500. */
  501. wxLog* GetOldLog() const;
  502. /**
  503. Returns @true if the messages are passed to the previously active log
  504. target (default) or @false if PassMessages() had been called.
  505. */
  506. bool IsPassingMessages() const;
  507. /**
  508. By default, the log messages are passed to the previously active log target.
  509. Calling this function with @false parameter disables this behaviour
  510. (presumably temporarily, as you shouldn't use wxLogChain at all otherwise) and
  511. it can be reenabled by calling it again with @a passMessages set to @true.
  512. */
  513. void PassMessages(bool passMessages);
  514. /**
  515. Sets another log target to use (may be @NULL).
  516. The log target specified in the wxLogChain(wxLog*) constructor or in a
  517. previous call to this function is deleted.
  518. This doesn't change the old log target value (the one the messages are
  519. forwarded to) which still remains the same as was active when wxLogChain
  520. object was created.
  521. */
  522. void SetLog(wxLog* logger);
  523. };
  524. /**
  525. @class wxLogInterposer
  526. A special version of wxLogChain which uses itself as the new log target.
  527. It forwards log messages to the previously installed one in addition to
  528. processing them itself.
  529. Unlike wxLogChain which is usually used directly as is, this class must be
  530. derived from to implement wxLog::DoLog and/or wxLog::DoLogString methods.
  531. wxLogInterposer destroys the previous log target in its destructor.
  532. If you don't want this to happen, use wxLogInterposerTemp instead.
  533. @library{wxbase}
  534. @category{logging}
  535. */
  536. class wxLogInterposer : public wxLogChain
  537. {
  538. public:
  539. /**
  540. The default constructor installs this object as the current active log target.
  541. */
  542. wxLogInterposer();
  543. };
  544. /**
  545. @class wxLogInterposerTemp
  546. A special version of wxLogChain which uses itself as the new log target.
  547. It forwards log messages to the previously installed one in addition to
  548. processing them itself. Unlike wxLogInterposer, it doesn't delete the old
  549. target which means it can be used to temporarily redirect log output.
  550. As per wxLogInterposer, this class must be derived from to implement
  551. wxLog::DoLog and/or wxLog::DoLogString methods.
  552. @library{wxbase}
  553. @category{logging}
  554. */
  555. class wxLogInterposerTemp : public wxLogChain
  556. {
  557. public:
  558. /**
  559. The default constructor installs this object as the current active log target.
  560. */
  561. wxLogInterposerTemp();
  562. };
  563. /**
  564. @class wxLogStream
  565. This class can be used to redirect the log messages to a C++ stream.
  566. Please note that this class is only available if wxWidgets was compiled with
  567. the standard iostream library support (@c wxUSE_STD_IOSTREAM must be on).
  568. @library{wxbase}
  569. @category{logging}
  570. @see wxLogStderr, wxStreamToTextRedirector
  571. */
  572. class wxLogStream : public wxLog
  573. {
  574. public:
  575. /**
  576. Constructs a log target which sends all the log messages to the given
  577. output stream. If it is @NULL, the messages are sent to @c cerr.
  578. */
  579. wxLogStream(std::ostream *ostr = NULL);
  580. };
  581. /**
  582. @class wxLogStderr
  583. This class can be used to redirect the log messages to a C file stream (not to
  584. be confused with C++ streams).
  585. It is the default log target for the non-GUI wxWidgets applications which
  586. send all the output to @c stderr.
  587. @library{wxbase}
  588. @category{logging}
  589. @see wxLogStream
  590. */
  591. class wxLogStderr : public wxLog
  592. {
  593. public:
  594. /**
  595. Constructs a log target which sends all the log messages to the given
  596. @c FILE. If it is @NULL, the messages are sent to @c stderr.
  597. */
  598. wxLogStderr(FILE* fp = NULL);
  599. };
  600. /**
  601. @class wxLogBuffer
  602. wxLogBuffer is a very simple implementation of log sink which simply collects
  603. all the logged messages in a string (except the debug messages which are output
  604. in the usual way immediately as we're presumably not interested in collecting
  605. them for later). The messages from different log function calls are separated
  606. by the new lines.
  607. All the messages collected so far can be shown to the user (and the current
  608. buffer cleared) by calling the overloaded wxLogBuffer::Flush method.
  609. @library{wxbase}
  610. @category{logging}
  611. */
  612. class wxLogBuffer : public wxLog
  613. {
  614. public:
  615. /**
  616. The default ctor does nothing.
  617. */
  618. wxLogBuffer();
  619. /**
  620. Shows all the messages collected so far to the user (using a message box in the
  621. GUI applications or by printing them out to the console in text mode) and
  622. clears the internal buffer.
  623. */
  624. virtual void Flush();
  625. /**
  626. Returns the current buffer contains. Messages from different log function calls
  627. are separated with the new lines in the buffer.
  628. The buffer can be cleared by Flush() which will also show the current
  629. contents to the user.
  630. */
  631. const wxString& GetBuffer() const;
  632. };
  633. /**
  634. @class wxLogNull
  635. This class allows you to temporarily suspend logging. All calls to the log
  636. functions during the life time of an object of this class are just ignored.
  637. In particular, it can be used to suppress the log messages given by wxWidgets
  638. itself but it should be noted that it is rarely the best way to cope with this
  639. problem as @b all log messages are suppressed, even if they indicate a
  640. completely different error than the one the programmer wanted to suppress.
  641. For instance, the example of the overview:
  642. @code
  643. wxFile file;
  644. // wxFile.Open() normally complains if file can't be opened, we don't want it
  645. {
  646. wxLogNull logNo;
  647. if ( !file.Open("bar") )
  648. ... process error ourselves ...
  649. } // ~wxLogNull called, old log sink restored
  650. wxLogMessage("..."); // ok
  651. @endcode
  652. would be better written as:
  653. @code
  654. wxFile file;
  655. // don't try to open file if it doesn't exist, we are prepared to deal with
  656. // this ourselves - but all other errors are not expected
  657. if ( wxFile::Exists("bar") )
  658. {
  659. // gives an error message if the file couldn't be opened
  660. file.Open("bar");
  661. }
  662. else
  663. {
  664. ...
  665. }
  666. @endcode
  667. @library{wxbase}
  668. @category{logging}
  669. */
  670. class wxLogNull
  671. {
  672. public:
  673. /**
  674. Suspends logging.
  675. */
  676. wxLogNull();
  677. /**
  678. Resumes logging.
  679. */
  680. ~wxLogNull();
  681. };
  682. #endif // wxUSE_BASE
  683. #if wxUSE_GUI
  684. /**
  685. @class wxLogWindow
  686. This class represents a background log window: to be precise, it collects all
  687. log messages in the log frame which it manages but also passes them on to the
  688. log target which was active at the moment of its creation. This allows you, for
  689. example, to show all the log messages in a frame but still continue to process
  690. them normally by showing the standard log dialog.
  691. @library{wxcore}
  692. @category{logging}
  693. @see wxLogTextCtrl
  694. */
  695. class wxLogWindow : public wxLogInterposer
  696. {
  697. public:
  698. /**
  699. Creates the log frame window and starts collecting the messages in it.
  700. @param pParent
  701. The parent window for the log frame, may be @NULL
  702. @param szTitle
  703. The title for the log frame
  704. @param show
  705. @true to show the frame initially (default), otherwise
  706. Show() must be called later.
  707. @param passToOld
  708. @true to process the log messages normally in addition to logging them
  709. in the log frame (default), @false to only log them in the log frame.
  710. Note that if no targets were set using wxLog::SetActiveTarget() then
  711. wxLogWindow simply becomes the active one and messages won't be passed
  712. to other targets.
  713. */
  714. wxLogWindow(wxWindow* pParent, const wxString& szTitle, bool show = true,
  715. bool passToOld = true);
  716. /**
  717. Returns the associated log frame window. This may be used to position or resize
  718. it but use Show() to show or hide it.
  719. */
  720. wxFrame* GetFrame() const;
  721. /**
  722. Called if the user closes the window interactively, will not be
  723. called if it is destroyed for another reason (such as when program
  724. exits).
  725. Return @true from here to allow the frame to close, @false to
  726. prevent this from happening.
  727. @see OnFrameDelete()
  728. */
  729. virtual bool OnFrameClose(wxFrame* frame);
  730. /**
  731. Called right before the log frame is going to be deleted: will
  732. always be called unlike OnFrameClose().
  733. */
  734. virtual void OnFrameDelete(wxFrame* frame);
  735. /**
  736. Shows or hides the frame.
  737. */
  738. void Show(bool show = true);
  739. };
  740. /**
  741. @class wxLogGui
  742. This is the default log target for the GUI wxWidgets applications.
  743. Please see @ref overview_log_customize for explanation of how to change the
  744. default log target.
  745. An object of this class is used by default to show the log messages created
  746. by using wxLogMessage(), wxLogError() and other logging functions. It
  747. doesn't display the messages logged by them immediately however but
  748. accumulates all messages logged during an event handler execution and then
  749. shows them all at once when its Flush() method is called during the idle
  750. time processing. This has the important advantage of showing only a single
  751. dialog to the user even if several messages were logged because of a single
  752. error as it often happens (e.g. a low level function could log a message
  753. because it failed to open a file resulting in its caller logging another
  754. message due to the failure of higher level operation requiring the use of
  755. this file). If you need to force the display of all previously logged
  756. messages immediately you can use wxLog::FlushActive() to force the dialog
  757. display.
  758. Also notice that if an error message is logged when several informative
  759. messages had been already logged before, the informative messages are
  760. discarded on the assumption that they are not useful -- and may be
  761. confusing and hence harmful -- any more after the error. The warning
  762. and error messages are never discarded however and any informational
  763. messages logged after the first error one are also kept (as they may
  764. contain information about the error recovery). You may override DoLog()
  765. method to change this behaviour.
  766. At any rate, it is possible that that several messages were accumulated
  767. before this class Flush() method is called. If this is the case, Flush()
  768. uses a custom dialog which shows the last message directly and allows the
  769. user to view the previously logged ones by expanding the "Details"
  770. wxCollapsiblePane inside it. This custom dialog also provides the buttons
  771. for copying the log messages to the clipboard and saving them to a file.
  772. However if only a single message is present when Flush() is called, just a
  773. wxMessageBox() is used to show it. This has the advantage of being closer
  774. to the native behaviour but it doesn't give the user any possibility to
  775. copy or save the message (except for the recent Windows versions where @c
  776. Ctrl-C may be pressed in the message box to copy its contents to the
  777. clipboard) so you may want to override DoShowSingleLogMessage() to
  778. customize wxLogGui -- the dialogs sample shows how to do this.
  779. @library{wxcore}
  780. @category{logging}
  781. */
  782. class wxLogGui : public wxLog
  783. {
  784. public:
  785. /**
  786. Default constructor.
  787. */
  788. wxLogGui();
  789. /**
  790. Presents the accumulated log messages, if any, to the user.
  791. This method is called during the idle time and should show any messages
  792. accumulated in wxLogGui#m_aMessages field to the user.
  793. */
  794. virtual void Flush();
  795. protected:
  796. /**
  797. Returns the appropriate title for the dialog.
  798. The title is constructed from wxApp::GetAppDisplayName() and the
  799. severity string (e.g. "error" or "warning") appropriate for the current
  800. wxLogGui#m_bErrors and wxLogGui#m_bWarnings values.
  801. */
  802. wxString GetTitle() const;
  803. /**
  804. Returns wxICON_ERROR, wxICON_WARNING or wxICON_INFORMATION depending on
  805. the current maximal severity.
  806. This value is suitable to be used in the style parameter of
  807. wxMessageBox() function.
  808. */
  809. int GetSeverityIcon() const;
  810. /**
  811. Forgets all the currently stored messages.
  812. If you override Flush() (and don't call the base class version), you
  813. must call this method to avoid messages being logged over and over
  814. again.
  815. */
  816. void Clear();
  817. /**
  818. All currently accumulated messages.
  819. This array may be empty if no messages were logged.
  820. @see m_aSeverity, m_aTimes
  821. */
  822. wxArrayString m_aMessages;
  823. /**
  824. The severities of each logged message.
  825. This array is synchronized with wxLogGui#m_aMessages, i.e. the n-th
  826. element of this array corresponds to the severity of the n-th message.
  827. The possible severity values are @c wxLOG_XXX constants, e.g.
  828. wxLOG_Error, wxLOG_Warning, wxLOG_Message etc.
  829. */
  830. wxArrayInt m_aSeverity;
  831. /**
  832. The time stamps of each logged message.
  833. The elements of this array are time_t values corresponding to the time
  834. when the message was logged.
  835. */
  836. wxArrayLong m_aTimes;
  837. /**
  838. True if there any error messages.
  839. */
  840. bool m_bErrors;
  841. /**
  842. True if there any warning messages.
  843. If both wxLogGui#m_bErrors and this member are false, there are only
  844. informational messages to be shown.
  845. */
  846. bool m_bWarnings;
  847. /**
  848. True if there any messages to be shown to the user.
  849. This variable is used instead of simply checking whether
  850. wxLogGui#m_aMessages array is empty to allow blocking further calls to
  851. Flush() while a log dialog is already being shown, even if the messages
  852. array hasn't been emptied yet.
  853. */
  854. bool m_bHasMessages;
  855. private:
  856. /**
  857. Method called by Flush() to show a single log message.
  858. This function can be overridden to show the message in a different way.
  859. By default a simple wxMessageBox() call is used.
  860. @param message
  861. The message to show (it can contain multiple lines).
  862. @param title
  863. The suggested title for the dialog showing the message, see
  864. GetTitle().
  865. @param style
  866. One of @c wxICON_XXX constants, see GetSeverityIcon().
  867. */
  868. virtual void DoShowSingleLogMessage(const wxString& message,
  869. const wxString& title,
  870. int style);
  871. /**
  872. Method called by Flush() to show multiple log messages.
  873. This function can be overridden to show the messages in a different way.
  874. By default a special log dialog showing the most recent message and
  875. allowing the user to expand it to view the previously logged ones is
  876. used.
  877. @param messages
  878. Array of messages to show; it contains more than one element.
  879. @param severities
  880. Array of message severities containing @c wxLOG_XXX values.
  881. @param times
  882. Array of time_t values indicating when each message was logged.
  883. @param title
  884. The suggested title for the dialog showing the message, see
  885. GetTitle().
  886. @param style
  887. One of @c wxICON_XXX constants, see GetSeverityIcon().
  888. */
  889. virtual void DoShowMultipleLogMessages(const wxArrayString& messages,
  890. const wxArrayInt& severities,
  891. const wxArrayLong& times,
  892. const wxString& title,
  893. int style);
  894. };
  895. /**
  896. @class wxLogTextCtrl
  897. Using these target all the log messages can be redirected to a text control.
  898. The text control must have been created with @c wxTE_MULTILINE style by the
  899. caller previously.
  900. @library{wxcore}
  901. @category{logging}
  902. @see wxTextCtrl, wxStreamToTextRedirector
  903. */
  904. class wxLogTextCtrl : public wxLog
  905. {
  906. public:
  907. /**
  908. Constructs a log target which sends all the log messages to the given text
  909. control. The @a textctrl parameter cannot be @NULL.
  910. */
  911. wxLogTextCtrl(wxTextCtrl* pTextCtrl);
  912. };
  913. #endif // wxUSE_GUI
  914. #if wxUSE_BASE
  915. // ============================================================================
  916. // Global functions/macros
  917. // ============================================================================
  918. /** @addtogroup group_funcmacro_log */
  919. //@{
  920. /**
  921. This function shows a message to the user in a safe way and should be safe
  922. to call even before the application has been initialized or if it is
  923. currently in some other strange state (for example, about to crash). Under
  924. Windows this function shows a message box using a native dialog instead of
  925. wxMessageBox() (which might be unsafe to call), elsewhere it simply prints
  926. the message to the standard output using the title as prefix.
  927. @param title
  928. The title of the message box shown to the user or the prefix of the
  929. message string.
  930. @param text
  931. The text to show to the user.
  932. @see wxLogFatalError()
  933. @header{wx/log.h}
  934. */
  935. void wxSafeShowMessage(const wxString& title, const wxString& text);
  936. /**
  937. Returns the error code from the last system call. This function uses
  938. @c errno on Unix platforms and @c GetLastError under Win32.
  939. @see wxSysErrorMsg(), wxLogSysError()
  940. @header{wx/log.h}
  941. */
  942. unsigned long wxSysErrorCode();
  943. /**
  944. Returns the error message corresponding to the given system error code. If
  945. @a errCode is 0 (default), the last error code (as returned by
  946. wxSysErrorCode()) is used.
  947. @see wxSysErrorCode(), wxLogSysError()
  948. @header{wx/log.h}
  949. */
  950. const wxChar* wxSysErrorMsg(unsigned long errCode = 0);
  951. //@}
  952. /** @addtogroup group_funcmacro_log */
  953. //@{
  954. /**
  955. Logs a message with the given wxLogLevel.
  956. E.g. using @c wxLOG_Message as first argument, this function behaves like wxLogMessage().
  957. @header{wx/log.h}
  958. */
  959. void wxLogGeneric(wxLogLevel level, const char* formatString, ... );
  960. void wxVLogGeneric(wxLogLevel level, const char* formatString, va_list argPtr);
  961. //@}
  962. /** @addtogroup group_funcmacro_log */
  963. //@{
  964. /**
  965. For all normal, informational messages. They also appear in a message box
  966. by default (but it can be changed).
  967. @header{wx/log.h}
  968. */
  969. void wxLogMessage(const char* formatString, ... );
  970. void wxVLogMessage(const char* formatString, va_list argPtr);
  971. //@}
  972. /** @addtogroup group_funcmacro_log */
  973. //@{
  974. /**
  975. For verbose output. Normally, it is suppressed, but might be activated if
  976. the user wishes to know more details about the program progress (another,
  977. but possibly confusing name for the same function could be @c wxLogInfo).
  978. @header{wx/log.h}
  979. */
  980. void wxLogVerbose(const char* formatString, ... );
  981. void wxVLogVerbose(const char* formatString, va_list argPtr);
  982. //@}
  983. /** @addtogroup group_funcmacro_log */
  984. //@{
  985. /**
  986. For warnings - they are also normally shown to the user, but don't
  987. interrupt the program work.
  988. @header{wx/log.h}
  989. */
  990. void wxLogWarning(const char* formatString, ... );
  991. void wxVLogWarning(const char* formatString, va_list argPtr);
  992. //@}
  993. /** @addtogroup group_funcmacro_log */
  994. //@{
  995. /**
  996. Like wxLogError(), but also terminates the program with the exit code 3.
  997. Using @e abort() standard function also terminates the program with this
  998. exit code.
  999. @header{wx/log.h}
  1000. */
  1001. void wxLogFatalError(const char* formatString, ... );
  1002. void wxVLogFatalError(const char* formatString, va_list argPtr);
  1003. //@}
  1004. /** @addtogroup group_funcmacro_log */
  1005. //@{
  1006. /**
  1007. The functions to use for error messages, i.e. the messages that must be
  1008. shown to the user. The default processing is to pop up a message box to
  1009. inform the user about it.
  1010. @header{wx/log.h}
  1011. */
  1012. void wxLogError(const char* formatString, ... );
  1013. void wxVLogError(const char* formatString, va_list argPtr);
  1014. //@}
  1015. /** @addtogroup group_funcmacro_log */
  1016. //@{
  1017. /**
  1018. Log a message at @c wxLOG_Trace log level (see ::wxLogLevelValues enum).
  1019. Notice that the use of trace masks is not recommended any more as setting
  1020. the log components (please see @ref overview_log_enable) provides a way to
  1021. do the same thing for log messages of any level, and not just the tracing
  1022. ones.
  1023. Like wxLogDebug(), trace functions only do something in debug builds and
  1024. expand to nothing in the release one. The reason for making it a separate
  1025. function is that usually there are a lot of trace messages, so it might
  1026. make sense to separate them from other debug messages.
  1027. Trace messages can be separated into different categories; these functions in facts
  1028. only log the message if the given @a mask is currently enabled in wxLog.
  1029. This lets you selectively trace only some operations and not others by enabling the
  1030. desired trace masks with wxLog::AddTraceMask() or by setting the
  1031. @ref overview_envvars "@c WXTRACE environment variable".
  1032. The predefined string trace masks used by wxWidgets are:
  1033. @beginDefList
  1034. @itemdef{ wxTRACE_MemAlloc, Trace memory allocation (new/delete) }
  1035. @itemdef{ wxTRACE_Messages, Trace window messages/X callbacks }
  1036. @itemdef{ wxTRACE_ResAlloc, Trace GDI resource allocation }
  1037. @itemdef{ wxTRACE_RefCount, Trace various ref counting operations }
  1038. @itemdef{ wxTRACE_OleCalls, Trace OLE method calls (Win32 only) }
  1039. @endDefList
  1040. @header{wx/log.h}
  1041. */
  1042. void wxLogTrace(const char* mask, const char* formatString, ... );
  1043. void wxVLogTrace(const char* mask, const char* formatString, va_list argPtr);
  1044. //@}
  1045. /** @addtogroup group_funcmacro_log */
  1046. //@{
  1047. /**
  1048. Like wxLogDebug(), trace functions only do something in debug builds and
  1049. expand to nothing in the release one. The reason for making it a separate
  1050. function is that usually there are a lot of trace messages, so it might
  1051. make sense to separate them from other debug messages.
  1052. @deprecated
  1053. This version of wxLogTrace() only logs the message if all the bits
  1054. corresponding to the @a mask are set in the wxLog trace mask which can be
  1055. set by calling wxLog::SetTraceMask(). This version is less flexible than
  1056. wxLogTrace(const char*,const char*,...) because it doesn't allow defining
  1057. the user trace masks easily. This is why it is deprecated in favour of
  1058. using string trace masks.
  1059. The following bitmasks are defined for wxTraceMask:
  1060. @beginDefList
  1061. @itemdef{ wxTraceMemAlloc, Trace memory allocation (new/delete) }
  1062. @itemdef{ wxTraceMessages, Trace window messages/X callbacks }
  1063. @itemdef{ wxTraceResAlloc, Trace GDI resource allocation }
  1064. @itemdef{ wxTraceRefCount, Trace various ref counting operations }
  1065. @itemdef{ wxTraceOleCalls, Trace OLE method calls (Win32 only) }
  1066. @endDefList
  1067. @header{wx/log.h}
  1068. */
  1069. void wxLogTrace(wxTraceMask mask, const char* formatString, ... );
  1070. void wxVLogTrace(wxTraceMask mask, const char* formatString, va_list argPtr);
  1071. //@}
  1072. /** @addtogroup group_funcmacro_log */
  1073. //@{
  1074. /**
  1075. The right functions for debug output. They only do something in debug mode
  1076. (when the preprocessor symbol @c __WXDEBUG__ is defined) and expand to
  1077. nothing in release mode (otherwise).
  1078. @header{wx/log.h}
  1079. */
  1080. void wxLogDebug(const char* formatString, ... );
  1081. void wxVLogDebug(const char* formatString, va_list argPtr);
  1082. //@}
  1083. /** @addtogroup group_funcmacro_log */
  1084. //@{
  1085. /**
  1086. Messages logged by this function will appear in the statusbar of the
  1087. @a frame or of the top level application window by default (i.e. when using
  1088. the second version of the functions).
  1089. If the target frame doesn't have a statusbar, the message will be lost.
  1090. @header{wx/log.h}
  1091. */
  1092. void wxLogStatus(wxFrame* frame, const char* formatString, ... );
  1093. void wxVLogStatus(wxFrame* frame, const char* formatString, va_list argPtr);
  1094. void wxLogStatus(const char* formatString, ... );
  1095. void wxVLogStatus(const char* formatString, va_list argPtr);
  1096. //@}
  1097. /** @addtogroup group_funcmacro_log */
  1098. //@{
  1099. /**
  1100. Mostly used by wxWidgets itself, but might be handy for logging errors
  1101. after system call (API function) failure. It logs the specified message
  1102. text as well as the last system error code (@e errno or @e GetLastError()
  1103. depending on the platform) and the corresponding error message. The second
  1104. form of this function takes the error code explicitly as the first
  1105. argument.
  1106. @see wxSysErrorCode(), wxSysErrorMsg()
  1107. @header{wx/log.h}
  1108. */
  1109. void wxLogSysError(const char* formatString, ... );
  1110. void wxVLogSysError(const char* formatString, va_list argPtr);
  1111. //@}
  1112. /** @addtogroup group_funcmacro_debug */
  1113. //@{
  1114. /**
  1115. @def wxDISABLE_DEBUG_LOGGING_IN_RELEASE_BUILD()
  1116. Use this macro to disable logging at debug and trace levels in release
  1117. build when not using wxIMPLEMENT_APP().
  1118. @see wxDISABLE_DEBUG_SUPPORT(),
  1119. wxDISABLE_ASSERTS_IN_RELEASE_BUILD(),
  1120. @ref overview_debugging
  1121. @since 2.9.1
  1122. @header{wx/log.h}
  1123. */
  1124. #define wxDISABLE_DEBUG_LOGGING_IN_RELEASE_BUILD()
  1125. //@}
  1126. #endif // wxUSE_BASE