Log.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Core/Log.h"
  29. #include "../../Include/RmlUi/Core/Core.h"
  30. #include "../../Include/RmlUi/Core/StringUtilities.h"
  31. #include "../../Include/RmlUi/Core/SystemInterface.h"
  32. #include <stdarg.h>
  33. #ifdef RMLUI_PLATFORM_WIN32
  34. #include <windows.h>
  35. #endif
  36. namespace Rml {
  37. // Initialises the logging interface.
  38. bool Log::Initialise()
  39. {
  40. return true;
  41. }
  42. // Shutdown the log interface.
  43. void Log::Shutdown()
  44. {
  45. }
  46. // Log the specified message via the registered log interface
  47. void Log::Message(Log::Type type, const char* fmt, ...)
  48. {
  49. const int buffer_size = 1024;
  50. char buffer[buffer_size];
  51. va_list argument_list;
  52. // Print the message to the buffer.
  53. va_start(argument_list, fmt);
  54. int len = vsnprintf(buffer, buffer_size - 2, fmt, argument_list);
  55. if (len < 0 || len > buffer_size - 2)
  56. {
  57. len = buffer_size - 2;
  58. }
  59. buffer[len] = '\0';
  60. va_end(argument_list);
  61. GetSystemInterface()->LogMessage(type, buffer);
  62. }
  63. // Log a parse error on the specified file and line number.
  64. void Log::ParseError(const String& filename, int line_number, const char* fmt, ...)
  65. {
  66. const int buffer_size = 1024;
  67. char buffer[buffer_size];
  68. va_list argument_list;
  69. // Print the message to the buffer.
  70. va_start(argument_list, fmt);
  71. int len = vsnprintf(buffer, buffer_size - 2, fmt, argument_list);
  72. if (len < 0 || len > buffer_size - 2)
  73. {
  74. len = buffer_size - 2;
  75. }
  76. buffer[len] = '\0';
  77. va_end(argument_list);
  78. if (line_number >= 0)
  79. Message(Log::LT_ERROR, "%s:%d: %s", filename.c_str(), line_number, buffer);
  80. else
  81. Message(Log::LT_ERROR, "%s: %s", filename.c_str(), buffer);
  82. }
  83. bool Assert(const char* msg, const char* file, int line)
  84. {
  85. String message = CreateString(1024, "%s\n%s:%d", msg, file, line);
  86. return GetSystemInterface()->LogMessage(Log::LT_ASSERT, message);
  87. }
  88. } // namespace Rml