BsDebug.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include "BsPath.h"
  8. #if BS_PLATFORM == BS_PLATFORM_WIN32 && BS_COMPILER == BS_COMPILER_MSVC
  9. #include <windows.h>
  10. #include <iostream>
  11. void logToIDEConsole(const BansheeEngine::String& message)
  12. {
  13. OutputDebugString(message.c_str());
  14. OutputDebugString("\n");
  15. // Also default output in case we're running without debugger attached
  16. std::cout << message << std::endl;
  17. }
  18. #else
  19. void logToIDEConsole(const BansheeEngine::String& message)
  20. {
  21. // Do nothing
  22. }
  23. #endif
  24. namespace BansheeEngine
  25. {
  26. void Debug::logDebug(const String& msg)
  27. {
  28. mLog.logMsg(msg, "GlobalDebug");
  29. logToIDEConsole(msg);
  30. }
  31. void Debug::logInfo(const String& msg)
  32. {
  33. mLog.logMsg(msg, "GlobalInfo");
  34. logToIDEConsole(msg);
  35. }
  36. void Debug::logWarning(const String& msg)
  37. {
  38. mLog.logMsg(msg, "GlobalWarning");
  39. logToIDEConsole(msg);
  40. }
  41. void Debug::logError(const String& msg)
  42. {
  43. mLog.logMsg(msg, "GlobalError");
  44. logToIDEConsole(msg);
  45. }
  46. void Debug::log(const String& msg, const String& channel)
  47. {
  48. mLog.logMsg(msg, channel);
  49. logToIDEConsole(msg);
  50. }
  51. void Debug::writeAsBMP(UINT8* rawPixels, UINT32 bytesPerPixel, UINT32 width, UINT32 height, const Path& filePath, bool overwrite) const
  52. {
  53. if(FileSystem::isFile(filePath))
  54. {
  55. if(overwrite)
  56. FileSystem::remove(filePath);
  57. else
  58. BS_EXCEPT(FileNotFoundException, "File already exists at specified location: " + filePath.toString());
  59. }
  60. DataStreamPtr ds = FileSystem::createAndOpenFile(filePath);
  61. UINT32 bmpDataSize = BitmapWriter::getBMPSize(width, height, bytesPerPixel);
  62. UINT8* bmpBuffer = bs_newN<UINT8, ScratchAlloc>(bmpDataSize);
  63. BitmapWriter::rawPixelsToBMP(rawPixels, bmpBuffer, width, height, bytesPerPixel);
  64. ds->write(bmpBuffer, bmpDataSize);
  65. ds->close();
  66. bs_deleteN<ScratchAlloc>(bmpBuffer, bmpDataSize);
  67. }
  68. BS_UTILITY_EXPORT Debug& gDebug()
  69. {
  70. static Debug debug;
  71. return debug;
  72. }
  73. }