BsDebug.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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::logInfo(const String& msg)
  31. {
  32. mLog.logMsg(msg, (UINT32)DebugChannel::Info);
  33. logToIDEConsole(msg);
  34. }
  35. void Debug::logWarning(const String& msg)
  36. {
  37. mLog.logMsg(msg, (UINT32)DebugChannel::Warning);
  38. logToIDEConsole(msg);
  39. }
  40. void Debug::logError(const String& msg)
  41. {
  42. mLog.logMsg(msg, (UINT32)DebugChannel::Error);
  43. logToIDEConsole(msg);
  44. }
  45. void Debug::log(const String& msg, UINT32 channel)
  46. {
  47. mLog.logMsg(msg, channel);
  48. logToIDEConsole(msg);
  49. }
  50. void Debug::writeAsBMP(UINT8* rawPixels, UINT32 bytesPerPixel, UINT32 width, UINT32 height, const Path& filePath, bool overwrite) const
  51. {
  52. if(FileSystem::isFile(filePath))
  53. {
  54. if(overwrite)
  55. FileSystem::remove(filePath);
  56. else
  57. BS_EXCEPT(FileNotFoundException, "File already exists at specified location: " + filePath.toString());
  58. }
  59. DataStreamPtr ds = FileSystem::createAndOpenFile(filePath);
  60. UINT32 bmpDataSize = BitmapWriter::getBMPSize(width, height, bytesPerPixel);
  61. UINT8* bmpBuffer = bs_newN<UINT8>(bmpDataSize);
  62. BitmapWriter::rawPixelsToBMP(rawPixels, bmpBuffer, width, height, bytesPerPixel);
  63. ds->write(bmpBuffer, bmpDataSize);
  64. ds->close();
  65. bs_deleteN(bmpBuffer, bmpDataSize);
  66. }
  67. BS_UTILITY_EXPORT Debug& gDebug()
  68. {
  69. static Debug debug;
  70. return debug;
  71. }
  72. }