| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #pragma once |
| 4 | |
| 5 | #include "Prerequisites/BsPrerequisitesUtil.h" |
| 6 | #include "Debug/BsLog.h" |
| 7 | |
| 8 | namespace bs |
| 9 | { |
| 10 | class Log; |
| 11 | |
| 12 | /** @addtogroup Debug |
| 13 | * @{ |
| 14 | */ |
| 15 | |
| 16 | /** Available types of channels that debug messages can be logged to. */ |
| 17 | enum class DebugChannel |
| 18 | { |
| 19 | Debug, Warning, Error, CompilerWarning, CompilerError |
| 20 | }; |
| 21 | |
| 22 | /** |
| 23 | * Utility class providing various debug functionality. |
| 24 | * |
| 25 | * @note Thread safe. |
| 26 | */ |
| 27 | class BS_UTILITY_EXPORT Debug |
| 28 | { |
| 29 | public: |
| 30 | Debug() = default; |
| 31 | |
| 32 | /** Adds a log entry in the "Debug" channel. */ |
| 33 | void logDebug(const String& msg); |
| 34 | |
| 35 | /** Adds a log entry in the "Warning" channel. */ |
| 36 | void logWarning(const String& msg); |
| 37 | |
| 38 | /** Adds a log entry in the "Error" channel. */ |
| 39 | void logError(const String& msg); |
| 40 | |
| 41 | /** Adds a log entry in the specified channel. You may specify custom channels as needed. */ |
| 42 | void log(const String& msg, UINT32 channel); |
| 43 | |
| 44 | /** Retrieves the Log used by the Debug instance. */ |
| 45 | Log& getLog() { return mLog; } |
| 46 | |
| 47 | /** Converts raw pixels into a BMP image and saves it as a file */ |
| 48 | void writeAsBMP(UINT8* rawPixels, UINT32 bytesPerPixel, UINT32 width, UINT32 height, const Path& filePath, |
| 49 | bool overwrite = true) const; |
| 50 | |
| 51 | /** |
| 52 | * Saves a log about the current state of the application to the specified location. |
| 53 | * |
| 54 | * @param path Absolute path to the log filename. |
| 55 | */ |
| 56 | void saveLog(const Path& path) const; |
| 57 | |
| 58 | /** |
| 59 | * Triggered when a new entry in the log is added. |
| 60 | * |
| 61 | * @note Sim thread only. |
| 62 | */ |
| 63 | Event<void(const LogEntry&)> onLogEntryAdded; |
| 64 | |
| 65 | /** |
| 66 | * Triggered whenever one or multiple log entries were added or removed. Triggers only once per frame. |
| 67 | * |
| 68 | * @note Sim thread only. |
| 69 | */ |
| 70 | Event<void()> onLogModified; |
| 71 | |
| 72 | public: // ***** INTERNAL ****** |
| 73 | /** @name Internal |
| 74 | * @{ |
| 75 | */ |
| 76 | |
| 77 | /** |
| 78 | * Triggers callbacks that notify external code that a log entry was added. |
| 79 | * |
| 80 | * @note Sim thread only. |
| 81 | */ |
| 82 | void _triggerCallbacks(); |
| 83 | |
| 84 | /** @} */ |
| 85 | private: |
| 86 | UINT64 mLogHash = 0; |
| 87 | Log mLog; |
| 88 | }; |
| 89 | |
| 90 | /** A simpler way of accessing the Debug module. */ |
| 91 | BS_UTILITY_EXPORT Debug& gDebug(); |
| 92 | |
| 93 | /** Shortcut for logging a message in the debug channel. */ |
| 94 | #define LOGDBG(x) bs::gDebug().logDebug((x) + String("\n\t\t in ") + __PRETTY_FUNCTION__ + " [" + __FILE__ + ":" + toString(__LINE__) + "]\n"); |
| 95 | |
| 96 | /** Shortcut for logging a message in the warning channel. */ |
| 97 | #define LOGWRN(x) bs::gDebug().logWarning((x) + String("\n\t\t in ") + __PRETTY_FUNCTION__ + " [" + __FILE__ + ":" + toString(__LINE__) + "]\n"); |
| 98 | |
| 99 | /** Shortcut for logging a message in the error channel. */ |
| 100 | #define LOGERR(x) bs::gDebug().logError((x) + String("\n\t\t in ") + __PRETTY_FUNCTION__ + " [" + __FILE__ + ":" + toString(__LINE__) + "]\n"); |
| 101 | |
| 102 | /** Shortcut for logging a message in the debug channel, with support for formatting through StringUtil::format(). */ |
| 103 | #define LOGDBG_FMT(...) bs::gDebug().logDebug(StringUtil::format(__VA_ARGS__) + String("\n\t\t in ") + __PRETTY_FUNCTION__ + " [" + __FILE__ + ":" + toString(__LINE__) + "]\n"); |
| 104 | |
| 105 | /** Shortcut for logging a message in the warning channel, with support for formatting through StringUtil::format(). */ |
| 106 | #define LOGWRN_FMT(...) bs::gDebug().logWarning(StringUtil::format(__VA_ARGS__) + String("\n\t\t in ") + __PRETTY_FUNCTION__ + " [" + __FILE__ + ":" + toString(__LINE__) + "]\n"); |
| 107 | |
| 108 | /** Shortcut for logging a message in the error channel, with support for formatting through StringUtil::format(). */ |
| 109 | #define LOGERR_FMT(...) bs::gDebug().logError(StringUtil::format(__VA_ARGS__) + String("\n\t\t in ") + __PRETTY_FUNCTION__ + " [" + __FILE__ + ":" + toString(__LINE__) + "]\n"); |
| 110 | |
| 111 | /** Shortcut for logging a verbose message in the debug channel. Verbose messages can be ignored unlike other log messages. */ |
| 112 | #define LOGDBG_VERBOSE(x) ((void)0) |
| 113 | |
| 114 | /** Shortcut for logging a verbose message in the warning channel. Verbose messages can be ignored unlike other log messages. */ |
| 115 | #define LOGWRN_VERBOSE(x) ((void)0) |
| 116 | |
| 117 | /** @} */ |
| 118 | } |
| 119 | |