Logger.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. /** @class Logger
  45. * @brief Abstract interface for logger implementations.
  46. * Assimp provides a default implementation and uses it for almost all
  47. * logging stuff ('DefaultLogger'). This class defines just basic logging
  48. * behaviour and is not of interest for you.
  49. */
  50. class ASSIMP_API Logger
  51. : public Intern::AllocateFromAssimpHeap
  52. {
  53. public:
  54. // ----------------------------------------------------------------------
  55. /** @enum LogSeverity
  56. * @brief Log severity to describe the granularity of logging.
  57. */
  58. enum LogSeverity
  59. {
  60. NORMAL, //!< Normal granularity of logging
  61. VERBOSE //!< Debug infos will be logged, too
  62. };
  63. // ----------------------------------------------------------------------
  64. /** @enum ErrorSeverity
  65. * @brief Description for severity of a log message.
  66. *
  67. * Every LogStream has a bitwise combination of these flags.
  68. * A LogStream doesn't receive any messages of a specific type
  69. * if it doesn't specify the corresponding ErrorSeverity flag.
  70. */
  71. enum ErrorSeverity
  72. {
  73. DEBUGGING = 1, //!< Debug log message
  74. INFO = 2, //!< Info log message
  75. WARN = 4, //!< Warn log message
  76. ERR = 8 //!< Error log message
  77. };
  78. public:
  79. /** @brief Virtual destructor */
  80. virtual ~Logger();
  81. // ----------------------------------------------------------------------
  82. /** @brief Writes a debug message
  83. * @param message Debug message
  84. */
  85. void debug(const std::string &message);
  86. // ----------------------------------------------------------------------
  87. /** @brief Writes a info message
  88. * @param message Info message
  89. */
  90. void info(const std::string &message);
  91. // ----------------------------------------------------------------------
  92. /** @brief Writes a warning message
  93. * @param message Warn message
  94. */
  95. void warn(const std::string &message);
  96. // ----------------------------------------------------------------------
  97. /** @brief Writes an error message
  98. * @param message Error message
  99. */
  100. void error(const std::string &message);
  101. // ----------------------------------------------------------------------
  102. /** @brief Set a new log severity.
  103. * @param log_severity New severity for logging
  104. */
  105. void setLogSeverity(LogSeverity log_severity);
  106. // ----------------------------------------------------------------------
  107. /** @brief Get the current log severity
  108. */
  109. LogSeverity getLogSeverity() const;
  110. // ----------------------------------------------------------------------
  111. /** @brief Attach a new logstream
  112. *
  113. * The logger takes ownership of the stream and is responsible
  114. * for its destruction (which is done when the logger itself
  115. * is destroyed). Call detachStream to detach a stream and to
  116. * gain ownership of it again.
  117. * @param pStream Logstream to attach
  118. * @param severity Message filter, specified which types of log
  119. * messages are dispatched to the stream. Provide a bitwise
  120. * combination of the ErrorSeverity flags.
  121. * @return true if the stream has been attached, false otherwise.
  122. */
  123. virtual bool attachStream(LogStream *pStream,
  124. unsigned int severity = DEBUGGING | ERR | WARN | INFO) = 0;
  125. // ----------------------------------------------------------------------
  126. /** @brief Detach a still attached stream from the logger (or
  127. * modify the filter flags bits)
  128. * @param pStream Logstream instance for detaching
  129. * @param severity Provide a bitwise combination of the ErrorSeverity
  130. * flags. This value is &~ed with the current flags of the stream,
  131. * if the result is 0 the stream is detached from the Logger and
  132. * the caller retakes the possession of the stream.
  133. * @return true if the stream has been dettached, false otherwise.
  134. */
  135. virtual bool detatchStream(LogStream *pStream,
  136. unsigned int severity = DEBUGGING | ERR | WARN | INFO) = 0;
  137. protected:
  138. /** Default constructor */
  139. Logger();
  140. /** Construction with a given log severity */
  141. Logger(LogSeverity severity);
  142. // ----------------------------------------------------------------------
  143. /** @brief Called as a request to write a specific debug message
  144. * @param message Debug message. Never longer than
  145. * MAX_LOG_MESSAGE_LENGTH characters (exluding the '0').
  146. * @note The message string is only valid until the scope of
  147. * the function is left.
  148. */
  149. virtual void OnDebug(const char* message)= 0;
  150. // ----------------------------------------------------------------------
  151. /** @brief Called as a request to write a specific info message
  152. * @param message Info message. Never longer than
  153. * MAX_LOG_MESSAGE_LENGTH characters (exluding the '0').
  154. * @note The message string is only valid until the scope of
  155. * the function is left.
  156. */
  157. virtual void OnInfo(const char* message) = 0;
  158. // ----------------------------------------------------------------------
  159. /** @brief Called as a request to write a specific warn message
  160. * @param message Warn message. Never longer than
  161. * MAX_LOG_MESSAGE_LENGTH characters (exluding the '0').
  162. * @note The message string is only valid until the scope of
  163. * the function is left.
  164. */
  165. virtual void OnWarn(const char* essage) = 0;
  166. // ----------------------------------------------------------------------
  167. /** @brief Called as a request to write a specific error message
  168. * @param message Error message. Never longer than
  169. * MAX_LOG_MESSAGE_LENGTH characters (exluding the '0').
  170. * @note The message string is only valid until the scope of
  171. * the function is left.
  172. */
  173. virtual void OnError(const char* message) = 0;
  174. protected:
  175. //! Logger severity
  176. LogSeverity m_Severity;
  177. };
  178. // ----------------------------------------------------------------------------------
  179. // Default constructor
  180. inline Logger::Logger() {
  181. setLogSeverity(NORMAL);
  182. }
  183. // ----------------------------------------------------------------------------------
  184. // Virtual destructor
  185. inline Logger::~Logger()
  186. {
  187. }
  188. // ----------------------------------------------------------------------------------
  189. // Construction with given logging severity
  190. inline Logger::Logger(LogSeverity severity) {
  191. setLogSeverity(severity);
  192. }
  193. // ----------------------------------------------------------------------------------
  194. // Log severity setter
  195. inline void Logger::setLogSeverity(LogSeverity log_severity){
  196. m_Severity = log_severity;
  197. }
  198. // ----------------------------------------------------------------------------------
  199. // Log severity getter
  200. inline Logger::LogSeverity Logger::getLogSeverity() const {
  201. return m_Severity;
  202. }
  203. // ----------------------------------------------------------------------------------
  204. } // Namespace Assimp
  205. #endif // !! INCLUDED_AI_LOGGER_H