BsDebug.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "BsDebug.h"
  2. #include "BsLog.h"
  3. #include "BsException.h"
  4. #include "BsBitmapWriter.h"
  5. #include "BsFileSystem.h"
  6. #include "BsDataStream.h"
  7. #if BS_PLATFORM == BS_PLATFORM_WIN32 && BS_COMPILER == BS_COMPILER_MSVC
  8. #include <windows.h>
  9. #include <iostream>
  10. void logToIDEConsole(const BansheeEngine::String& message)
  11. {
  12. OutputDebugString(message.c_str());
  13. OutputDebugString("\n");
  14. // Also default output in case we're running without debugger attached
  15. std::cout << message << std::endl;
  16. }
  17. #else
  18. void logToIDEConsole(const BansheeEngine::String& message)
  19. {
  20. // Do nothing
  21. }
  22. #endif
  23. namespace BansheeEngine
  24. {
  25. void Debug::logDebug(const String& msg)
  26. {
  27. mLog.logMsg(msg, (UINT32)DebugChannel::Debug);
  28. logToIDEConsole(msg);
  29. }
  30. void Debug::logWarning(const String& msg)
  31. {
  32. mLog.logMsg(msg, (UINT32)DebugChannel::Warning);
  33. logToIDEConsole(msg);
  34. }
  35. void Debug::logError(const String& msg)
  36. {
  37. mLog.logMsg(msg, (UINT32)DebugChannel::Error);
  38. logToIDEConsole(msg);
  39. }
  40. void Debug::log(const String& msg, UINT32 channel)
  41. {
  42. mLog.logMsg(msg, channel);
  43. logToIDEConsole(msg);
  44. }
  45. void Debug::writeAsBMP(UINT8* rawPixels, UINT32 bytesPerPixel, UINT32 width, UINT32 height, const Path& filePath, bool overwrite) const
  46. {
  47. if(FileSystem::isFile(filePath))
  48. {
  49. if(overwrite)
  50. FileSystem::remove(filePath);
  51. else
  52. BS_EXCEPT(FileNotFoundException, "File already exists at specified location: " + filePath.toString());
  53. }
  54. DataStreamPtr ds = FileSystem::createAndOpenFile(filePath);
  55. UINT32 bmpDataSize = BitmapWriter::getBMPSize(width, height, bytesPerPixel);
  56. UINT8* bmpBuffer = bs_newN<UINT8>(bmpDataSize);
  57. BitmapWriter::rawPixelsToBMP(rawPixels, bmpBuffer, width, height, bytesPerPixel);
  58. ds->write(bmpBuffer, bmpDataSize);
  59. ds->close();
  60. bs_deleteN(bmpBuffer, bmpDataSize);
  61. }
  62. void Debug::_triggerCallbacks()
  63. {
  64. LogEntry entry;
  65. while (mLog.getUnreadEntry(entry))
  66. {
  67. onLogEntryAdded(entry);
  68. }
  69. }
  70. void Debug::saveLog(const Path& path)
  71. {
  72. static const char* style =
  73. R"(html {
  74. font-family: sans-serif;
  75. }
  76. table
  77. {
  78. border-collapse: collapse;
  79. border-spacing: 0;
  80. empty-cells: show;
  81. border: 1px solid #cbcbcb;
  82. width:100%;
  83. table-layout:fixed;
  84. }
  85. table caption
  86. {
  87. color: #000;
  88. font: italic 85%/1 arial, sans-serif;
  89. padding: 1em 0;
  90. text-align: center;
  91. }
  92. table td,
  93. table th
  94. {
  95. border-left: 1px solid #cbcbcb;/* inner column border */
  96. border-width: 0 0 0 1px;
  97. font-size: inherit;
  98. margin: 0;
  99. overflow: visible; /*to make ths where the title is really long work*/
  100. padding: 0.5em 1em; /* cell padding */
  101. }
  102. table td:first-child,
  103. table th:first-child
  104. {
  105. border-left-width: 0;
  106. }
  107. table thead
  108. {
  109. background-color: #e0e0e0;
  110. color: #000;
  111. text-align: left;
  112. vertical-align: bottom;
  113. }
  114. table td
  115. {
  116. background-color: transparent;
  117. word-wrap:break-word;
  118. vertical-align: top;
  119. color: #7D7D7D;
  120. }
  121. .debug-row td {
  122. background-color: #FFFFFF;
  123. }
  124. .debug-alt-row td {
  125. background-color: #f2f2f2;
  126. }
  127. .warn-row td {
  128. background-color: #ffc016;
  129. color: #5F5F5F;
  130. }
  131. .warn-alt-row td {
  132. background-color: #fdcb41;
  133. color: #5F5F5F;
  134. }
  135. .error-row td {
  136. background-color: #9f1621;
  137. color: #9F9F9F;
  138. }
  139. .error-alt-row td {
  140. background-color: #ae1621;
  141. color: #9F9F9F;
  142. }
  143. )";
  144. static const char* htmlPreStyleHeader =
  145. R"(<!DOCTYPE html>
  146. <html lang="en">
  147. <head>
  148. <style type="text/css">
  149. )";
  150. static const char* htmlPostStyleHeader =
  151. R"(</style>
  152. <title>Banshee Engine Log</title>
  153. </head>
  154. <body>
  155. <h1>Banshee Engine Log</h1>
  156. <table border="1" cellpadding="1" cellspacing="1">
  157. <thead>
  158. <tr>
  159. <th scope="col" style="width:60px">Type</th>
  160. <th scope="col">Description</th>
  161. </tr>
  162. </thead>
  163. <tbody>
  164. )";
  165. static const char* htmlFooter =
  166. R"( </tbody>
  167. </table>
  168. </body>
  169. </html>)";
  170. StringStream stream;
  171. stream << htmlPreStyleHeader;
  172. stream << style;
  173. stream << htmlPostStyleHeader;
  174. bool alternate = false;
  175. for (auto& entry : mLog.mEntries)
  176. {
  177. String channelName;
  178. if (entry->getChannel() == (UINT32)DebugChannel::Error)
  179. {
  180. if (!alternate)
  181. stream << R"( <tr class="error-row">)" << std::endl;
  182. else
  183. stream << R"( <tr class="error-alt-row">)" << std::endl;
  184. stream << R"( <td>Error</td>)" << std::endl;
  185. }
  186. else if (entry->getChannel() == (UINT32)DebugChannel::Warning)
  187. {
  188. if (!alternate)
  189. stream << R"( <tr class="warn-row">)" << std::endl;
  190. else
  191. stream << R"( <tr class="warn-alt-row">)" << std::endl;
  192. stream << R"( <td>Warning</td>)" << std::endl;
  193. }
  194. else
  195. {
  196. if (!alternate)
  197. stream << R"( <tr class="debug-row">)" << std::endl;
  198. else
  199. stream << R"( <tr class="debug-alt-row">)" << std::endl;
  200. stream << R"( <td>Debug</td>)" << std::endl;
  201. }
  202. String parsedMessage = StringUtil::replaceAll(entry->getMessage(), "\n", "<br>\n");
  203. stream << R"( <td>)" << parsedMessage << "</td>" << std::endl;
  204. stream << R"( </tr>)" << std::endl;
  205. alternate = !alternate;
  206. }
  207. stream << htmlFooter;
  208. DataStreamPtr fileStream = FileSystem::createAndOpenFile(path);
  209. fileStream->writeString(stream.str());
  210. }
  211. BS_UTILITY_EXPORT Debug& gDebug()
  212. {
  213. static Debug debug;
  214. return debug;
  215. }
  216. }