BsDebug.cpp 5.7 KB

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