Logger.hpp 11 KB

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