Logger.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2019, assimp 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 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.hpp
  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 <assimp/types.h>
  39. #include <assimp/TinyFormatter.h>
  40. namespace Assimp {
  41. class LogStream;
  42. // Maximum length of a log message. Longer messages are rejected.
  43. #define MAX_LOG_MESSAGE_LENGTH 1024u
  44. // ----------------------------------------------------------------------------------
  45. /** @brief CPP-API: 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. * behavior and is not of interest for you. Instead, take a look at #DefaultLogger. */
  49. class ASSIMP_API Logger
  50. #ifndef SWIG
  51. : public Intern::AllocateFromAssimpHeap
  52. #endif
  53. {
  54. public:
  55. // ----------------------------------------------------------------------
  56. /** @enum LogSeverity
  57. * @brief Log severity to describe the granularity of logging.
  58. */
  59. enum LogSeverity {
  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. Debugging = 1, //!< Debug log message
  73. Info = 2, //!< Info log message
  74. Warn = 4, //!< Warn log message
  75. Err = 8 //!< Error log message
  76. };
  77. public:
  78. /** @brief Virtual destructor */
  79. virtual ~Logger();
  80. // ----------------------------------------------------------------------
  81. /** @brief Writes a debug message
  82. * @param message Debug message*/
  83. void debug(const char* message);
  84. void debug(const std::string &message);
  85. // ----------------------------------------------------------------------
  86. /** @brief Writes a info message
  87. * @param message Info message*/
  88. void info(const char* message);
  89. void info(const std::string &message);
  90. // ----------------------------------------------------------------------
  91. /** @brief Writes a warning message
  92. * @param message Warn message*/
  93. void warn(const char* message);
  94. void warn(const std::string &message);
  95. // ----------------------------------------------------------------------
  96. /** @brief Writes an error message
  97. * @param message Error message*/
  98. void error(const char* message);
  99. void error(const std::string &message);
  100. // ----------------------------------------------------------------------
  101. /** @brief Set a new log severity.
  102. * @param log_severity New severity for logging*/
  103. void setLogSeverity(LogSeverity log_severity);
  104. // ----------------------------------------------------------------------
  105. /** @brief Get the current log severity*/
  106. LogSeverity getLogSeverity() const;
  107. // ----------------------------------------------------------------------
  108. /** @brief Attach a new log-stream
  109. *
  110. * The logger takes ownership of the stream and is responsible
  111. * for its destruction (which is done using ::delete when the logger
  112. * itself is destroyed). Call detachStream to detach a stream and to
  113. * gain ownership of it again.
  114. * @param pStream Log-stream to attach
  115. * @param severity Message filter, specified which types of log
  116. * messages are dispatched to the stream. Provide a bitwise
  117. * combination of the ErrorSeverity flags.
  118. * @return true if the stream has been attached, false otherwise.*/
  119. virtual bool attachStream(LogStream *pStream,
  120. unsigned int severity = Debugging | Err | Warn | Info) = 0;
  121. // ----------------------------------------------------------------------
  122. /** @brief Detach a still attached stream from the logger (or
  123. * modify the filter flags bits)
  124. * @param pStream Log-stream instance for detaching
  125. * @param severity Provide a bitwise combination of the ErrorSeverity
  126. * flags. This value is &~ed with the current flags of the stream,
  127. * if the result is 0 the stream is detached from the Logger and
  128. * the caller retakes the possession of the stream.
  129. * @return true if the stream has been detached, false otherwise.*/
  130. virtual bool detatchStream(LogStream *pStream,
  131. unsigned int severity = Debugging | Err | Warn | Info) = 0;
  132. protected:
  133. /**
  134. * Default constructor
  135. */
  136. Logger() AI_NO_EXCEPT;
  137. /**
  138. * Construction with a given log severity
  139. */
  140. explicit Logger(LogSeverity severity);
  141. // ----------------------------------------------------------------------
  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 (excluding 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. /**
  152. * @brief Called as a request to write a specific info message
  153. * @param message Info message. Never longer than
  154. * MAX_LOG_MESSAGE_LENGTH characters (ecxluding the '0').
  155. * @note The message string is only valid until the scope of
  156. * the function is left.
  157. */
  158. virtual void OnInfo(const char* message) = 0;
  159. // ----------------------------------------------------------------------
  160. /**
  161. * @brief Called as a request to write a specific warn message
  162. * @param message Warn message. Never longer than
  163. * MAX_LOG_MESSAGE_LENGTH characters (exluding the '0').
  164. * @note The message string is only valid until the scope of
  165. * the function is left.
  166. */
  167. virtual void OnWarn(const char* essage) = 0;
  168. // ----------------------------------------------------------------------
  169. /**
  170. * @brief Called as a request to write a specific error message
  171. * @param message Error message. Never longer than
  172. * MAX_LOG_MESSAGE_LENGTH characters (exluding the '0').
  173. * @note The message string is only valid until the scope of
  174. * the function is left.
  175. */
  176. virtual void OnError(const char* message) = 0;
  177. protected:
  178. LogSeverity m_Severity;
  179. };
  180. // ----------------------------------------------------------------------------------
  181. // Default constructor
  182. inline
  183. Logger::Logger() AI_NO_EXCEPT
  184. : m_Severity(NORMAL) {
  185. // empty
  186. }
  187. // ----------------------------------------------------------------------------------
  188. // Virtual destructor
  189. inline
  190. Logger::~Logger() {
  191. // empty
  192. }
  193. // ----------------------------------------------------------------------------------
  194. // Construction with given logging severity
  195. inline
  196. Logger::Logger(LogSeverity severity)
  197. : m_Severity(severity) {
  198. // empty
  199. }
  200. // ----------------------------------------------------------------------------------
  201. // Log severity setter
  202. inline
  203. void Logger::setLogSeverity(LogSeverity log_severity){
  204. m_Severity = log_severity;
  205. }
  206. // ----------------------------------------------------------------------------------
  207. // Log severity getter
  208. inline
  209. Logger::LogSeverity Logger::getLogSeverity() const {
  210. return m_Severity;
  211. }
  212. // ----------------------------------------------------------------------------------
  213. inline
  214. void Logger::debug(const std::string &message) {
  215. return debug(message.c_str());
  216. }
  217. // ----------------------------------------------------------------------------------
  218. inline
  219. void Logger::error(const std::string &message) {
  220. return error(message.c_str());
  221. }
  222. // ----------------------------------------------------------------------------------
  223. inline
  224. void Logger::warn(const std::string &message) {
  225. return warn(message.c_str());
  226. }
  227. // ----------------------------------------------------------------------------------
  228. inline
  229. void Logger::info(const std::string &message) {
  230. return info(message.c_str());
  231. }
  232. // ------------------------------------------------------------------------------------------------
  233. #define ASSIMP_LOG_WARN_F(string,...)\
  234. DefaultLogger::get()->warn((Formatter::format(string),__VA_ARGS__))
  235. #define ASSIMP_LOG_ERROR_F(string,...)\
  236. DefaultLogger::get()->error((Formatter::format(string),__VA_ARGS__))
  237. #define ASSIMP_LOG_DEBUG_F(string,...)\
  238. DefaultLogger::get()->debug((Formatter::format(string),__VA_ARGS__))
  239. #define ASSIMP_LOG_INFO_F(string,...)\
  240. DefaultLogger::get()->info((Formatter::format(string),__VA_ARGS__))
  241. #define ASSIMP_LOG_WARN(string)\
  242. DefaultLogger::get()->warn(string)
  243. #define ASSIMP_LOG_ERROR(string)\
  244. DefaultLogger::get()->error(string)
  245. #define ASSIMP_LOG_DEBUG(string)\
  246. DefaultLogger::get()->debug(string)
  247. #define ASSIMP_LOG_INFO(string)\
  248. DefaultLogger::get()->info(string)
  249. } // Namespace Assimp
  250. #endif // !! INCLUDED_AI_LOGGER_H