msgout.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/msgout.h
  3. // Purpose: interface of wxMessageOutput and derived classes
  4. // Author: Vadim Zeitlin
  5. // Copyright: (c) 2009 Vadim Zeitlin
  6. // Licence: wxWindows licence
  7. /////////////////////////////////////////////////////////////////////////////
  8. /**
  9. Simple class allowing to write strings to various output channels.
  10. wxMessageOutput is a low-level class and doesn't provide any of the
  11. conveniences of wxLog. It simply allows to write a message to some output
  12. channel: usually file or standard error but possibly also a message box.
  13. While use of wxLog and related functions is preferable in many cases
  14. sometimes this simple interface may be more convenient.
  15. This class itself is an abstract base class for various concrete derived
  16. classes:
  17. - wxMessageOutputStderr
  18. - wxMessageOutputBest
  19. - wxMessageOutputMessageBox
  20. - wxMessageOutputLog
  21. It also provides access to the global message output object which is
  22. created by wxAppTraits::CreateMessageOutput() which creates an object of
  23. class wxMessageOutputStderr in console applications and wxMessageOutputBest
  24. in the GUI ones but may be overridden in user-defined traits class.
  25. Example of using this class:
  26. @code
  27. wxMessageOutputDebug().Printf("name=%s, preparing to greet...", name);
  28. wxMessageOutput::Get()->Printf("Hello, %s!", name);
  29. @endcode
  30. @library{wxbase}
  31. @category{logging}
  32. */
  33. class wxMessageOutput
  34. {
  35. public:
  36. /**
  37. Return the global message output object.
  38. This object is never @NULL while the program is running but may be
  39. @NULL during initialization (before wxApp object is instantiated) or
  40. shutdown.(after wxApp destruction).
  41. @see wxAppTraits::CreateMessageOutput()
  42. */
  43. static wxMessageOutput* Get();
  44. /**
  45. Sets the global message output object.
  46. Using this function may be a simpler alternative to changing the
  47. message output object used for your program than overriding
  48. wxAppTraits::CreateMessageOutput().
  49. Remember to delete the returned pointer or restore it later with
  50. another call to Set().
  51. */
  52. static wxMessageOutput* Set(wxMessageOutput* msgout);
  53. /**
  54. Output a message.
  55. This function uses the same conventions as standard @c printf().
  56. */
  57. void Printf(const wxString& format, ...);
  58. /**
  59. Method called by Printf() to really output the text.
  60. This method is overridden in various derived classes and is also the
  61. one you should override if you implement a custom message output
  62. object.
  63. It may also be called directly instead of Printf(). This is especially
  64. useful when outputting a user-defined string because it can be simply
  65. called with this string instead of using
  66. @code
  67. msgout.Printf("%s", str);
  68. @endcode
  69. (notice that passing user-defined string to Printf() directly is, of
  70. course, a security risk).
  71. */
  72. virtual void Output(const wxString& str) = 0;
  73. };
  74. /**
  75. Output messages to stderr or another STDIO file stream.
  76. Implements wxMessageOutput by using stderr or specified file.
  77. @library{wxbase}
  78. @category{logging}
  79. */
  80. class wxMessageOutputStderr : public wxMessageOutput
  81. {
  82. public:
  83. /**
  84. Create a new message output object associated with standard error
  85. stream by default.
  86. @param fp
  87. Non-null STDIO file stream. Notice that this object does @e not
  88. take ownership of this pointer, i.e. the caller is responsible for
  89. both ensuring that its life-time is great er than life-time of this
  90. object and for deleting it if necessary.
  91. */
  92. wxMessageOutputStderr(FILE *fp = stderr);
  93. };
  94. /**
  95. Flags used with wxMessageOutputBest.
  96. See wxMessageOutputBest::wxMessageOutputBest().
  97. */
  98. enum wxMessageOutputFlags
  99. {
  100. wxMSGOUT_PREFER_STDERR = 0, ///< use stderr if available (this is the default)
  101. wxMSGOUT_PREFER_MSGBOX = 1 ///< always use message box if available
  102. };
  103. /**
  104. Output messages in the best possible way.
  105. Some systems (e.g. MSW) are capable of showing message boxes even from
  106. console programs. If this is the case, this class will use message box if
  107. standard error stream is not available (e.g. running console program not
  108. from console under Windows) or possibly even always, depending on the value
  109. of flags constructor argument.
  110. @library{wxbase}
  111. @category{logging}
  112. */
  113. class wxMessageOutputBest : public wxMessageOutputStderr
  114. {
  115. public:
  116. /**
  117. Create a new message output object.
  118. @param flags
  119. May be either @c wxMSGOUT_PREFER_STDERR (default) meaning that
  120. standard error will be used if it's available (e.g. program is
  121. being run from console under Windows) or @c wxMSGOUT_PREFER_MSGBOX
  122. meaning that a message box will always be used if the current
  123. system supports showing message boxes from console programs
  124. (currently only Windows does).
  125. */
  126. wxMessageOutputBest(wxMessageOutputFlags flags = wxMSGOUT_PREFER_STDERR);
  127. };
  128. /**
  129. Output messages to the system debug output channel.
  130. Under MSW this class outputs messages to the so called debug output. Under
  131. the other systems it simply uses the standard error stream.
  132. @library{wxbase}
  133. @category{logging}
  134. */
  135. class wxMessageOutputDebug : public wxMessageOutputStderr
  136. {
  137. public:
  138. /// Default constructor.
  139. wxMessageOutputDebug();
  140. };
  141. /**
  142. Output messages by showing them in a message box.
  143. This class is only available to GUI applications, unlike all the other
  144. wxMessageOutput-derived classes.
  145. @library{wxcore}
  146. @category{logging}
  147. */
  148. class wxMessageOutputMessageBox : public wxMessageOutput
  149. {
  150. public:
  151. /// Default constructor.
  152. wxMessageOutputMessageBox();
  153. };