Logger.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development Team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the ASSIMP team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the ASSIMP Development Team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file Logger.h
  34. * @brief Abstract base class 'Logger', base of the logging system.
  35. */
  36. #ifndef INCLUDED_AI_LOGGER_H
  37. #define INCLUDED_AI_LOGGER_H
  38. #include "aiTypes.h"
  39. namespace Assimp {
  40. class LogStream;
  41. // Maximum length of a log message. Longer messages are rejected.
  42. #define MAX_LOG_MESSAGE_LENGTH 1024u
  43. // ----------------------------------------------------------------------------------
  44. /** @brief CPP-API: Abstract interface for logger implementations.
  45. * Assimp provides a default implementation and uses it for almost all
  46. * logging stuff ('DefaultLogger'). This class defines just basic logging
  47. * behaviour and is not of interest for you. Instead, take a look at #DefaultLogger. */
  48. class ASSIMP_API Logger
  49. : public Intern::AllocateFromAssimpHeap {
  50. public:
  51. // ----------------------------------------------------------------------
  52. /** @enum LogSeverity
  53. * @brief Log severity to describe the granularity of logging.
  54. */
  55. enum LogSeverity
  56. {
  57. NORMAL, //!< Normal granularity of logging
  58. VERBOSE //!< Debug infos will be logged, too
  59. };
  60. // ----------------------------------------------------------------------
  61. /** @enum ErrorSeverity
  62. * @brief Description for severity of a log message.
  63. *
  64. * Every LogStream has a bitwise combination of these flags.
  65. * A LogStream doesn't receive any messages of a specific type
  66. * if it doesn't specify the corresponding ErrorSeverity flag.
  67. */
  68. enum ErrorSeverity
  69. {
  70. DEBUGGING = 1, //!< Debug log message
  71. INFO = 2, //!< Info log message
  72. WARN = 4, //!< Warn log message
  73. ERR = 8 //!< Error log message
  74. };
  75. public:
  76. /** @brief Virtual destructor */
  77. virtual ~Logger();
  78. // ----------------------------------------------------------------------
  79. /** @brief Writes a debug message
  80. * @param message Debug message*/
  81. void debug(const std::string &message);
  82. // ----------------------------------------------------------------------
  83. /** @brief Writes a info message
  84. * @param message Info message*/
  85. void info(const std::string &message);
  86. // ----------------------------------------------------------------------
  87. /** @brief Writes a warning message
  88. * @param message Warn message*/
  89. void warn(const std::string &message);
  90. // ----------------------------------------------------------------------
  91. /** @brief Writes an error message
  92. * @param message Error message*/
  93. void error(const std::string &message);
  94. // ----------------------------------------------------------------------
  95. /** @brief Set a new log severity.
  96. * @param log_severity New severity for logging*/
  97. void setLogSeverity(LogSeverity log_severity);
  98. // ----------------------------------------------------------------------
  99. /** @brief Get the current log severity*/
  100. LogSeverity getLogSeverity() const;
  101. // ----------------------------------------------------------------------
  102. /** @brief Attach a new logstream
  103. *
  104. * The logger takes ownership of the stream and is responsible
  105. * for its destruction (which is done when the logger itself
  106. * is destroyed). Call detachStream to detach a stream and to
  107. * gain ownership of it again.
  108. * @param pStream Logstream to attach
  109. * @param severity Message filter, specified which types of log
  110. * messages are dispatched to the stream. Provide a bitwise
  111. * combination of the ErrorSeverity flags.
  112. * @return true if the stream has been attached, false otherwise.*/
  113. virtual bool attachStream(LogStream *pStream,
  114. unsigned int severity = DEBUGGING | ERR | WARN | INFO) = 0;
  115. // ----------------------------------------------------------------------
  116. /** @brief Detach a still attached stream from the logger (or
  117. * modify the filter flags bits)
  118. * @param pStream Logstream instance for detaching
  119. * @param severity Provide a bitwise combination of the ErrorSeverity
  120. * flags. This value is &~ed with the current flags of the stream,
  121. * if the result is 0 the stream is detached from the Logger and
  122. * the caller retakes the possession of the stream.
  123. * @return true if the stream has been dettached, false otherwise.*/
  124. virtual bool detatchStream(LogStream *pStream,
  125. unsigned int severity = DEBUGGING | ERR | WARN | INFO) = 0;
  126. protected:
  127. /** Default constructor */
  128. Logger();
  129. /** Construction with a given log severity */
  130. Logger(LogSeverity severity);
  131. // ----------------------------------------------------------------------
  132. /** @brief Called as a request to write a specific debug message
  133. * @param message Debug message. Never longer than
  134. * MAX_LOG_MESSAGE_LENGTH characters (exluding the '0').
  135. * @note The message string is only valid until the scope of
  136. * the function is left.
  137. */
  138. virtual void OnDebug(const char* message)= 0;
  139. // ----------------------------------------------------------------------
  140. /** @brief Called as a request to write a specific info message
  141. * @param message Info message. Never longer than
  142. * MAX_LOG_MESSAGE_LENGTH characters (exluding the '0').
  143. * @note The message string is only valid until the scope of
  144. * the function is left.
  145. */
  146. virtual void OnInfo(const char* message) = 0;
  147. // ----------------------------------------------------------------------
  148. /** @brief Called as a request to write a specific warn message
  149. * @param message Warn message. Never longer than
  150. * MAX_LOG_MESSAGE_LENGTH characters (exluding the '0').
  151. * @note The message string is only valid until the scope of
  152. * the function is left.
  153. */
  154. virtual void OnWarn(const char* essage) = 0;
  155. // ----------------------------------------------------------------------
  156. /** @brief Called as a request to write a specific error message
  157. * @param message Error message. Never longer than
  158. * MAX_LOG_MESSAGE_LENGTH characters (exluding the '0').
  159. * @note The message string is only valid until the scope of
  160. * the function is left.
  161. */
  162. virtual void OnError(const char* message) = 0;
  163. protected:
  164. //! Logger severity
  165. LogSeverity m_Severity;
  166. };
  167. // ----------------------------------------------------------------------------------
  168. // Default constructor
  169. inline Logger::Logger() {
  170. setLogSeverity(NORMAL);
  171. }
  172. // ----------------------------------------------------------------------------------
  173. // Virtual destructor
  174. inline Logger::~Logger()
  175. {
  176. }
  177. // ----------------------------------------------------------------------------------
  178. // Construction with given logging severity
  179. inline Logger::Logger(LogSeverity severity) {
  180. setLogSeverity(severity);
  181. }
  182. // ----------------------------------------------------------------------------------
  183. // Log severity setter
  184. inline void Logger::setLogSeverity(LogSeverity log_severity){
  185. m_Severity = log_severity;
  186. }
  187. // ----------------------------------------------------------------------------------
  188. // Log severity getter
  189. inline Logger::LogSeverity Logger::getLogSeverity() const {
  190. return m_Severity;
  191. }
  192. // ----------------------------------------------------------------------------------
  193. } // Namespace Assimp
  194. #endif // !! INCLUDED_AI_LOGGER_H