Log.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.h"
  30. #include <stdarg.h>
  31. #ifdef RMLUI_PLATFORM_WIN32
  32. #include <windows.h>
  33. #endif
  34. namespace Rml {
  35. namespace Core {
  36. // Initialises the logging interface.
  37. bool Log::Initialise()
  38. {
  39. return true;
  40. }
  41. // Shutdown the log interface.
  42. void Log::Shutdown()
  43. {
  44. }
  45. // Log the specified message via the registered log interface
  46. void Log::Message(Log::Type type, const char* fmt, ...)
  47. {
  48. const int buffer_size = 1024;
  49. char buffer[buffer_size];
  50. va_list argument_list;
  51. // Print the message to the buffer.
  52. va_start(argument_list, fmt);
  53. int len = vsnprintf(buffer, buffer_size - 2, fmt, argument_list);
  54. if (len < 0 || len > buffer_size - 2)
  55. {
  56. len = buffer_size - 2;
  57. }
  58. buffer[len] = '\0';
  59. va_end(argument_list);
  60. GetSystemInterface()->LogMessage(type, buffer);
  61. }
  62. // Log a parse error on the specified file and line number.
  63. void Log::ParseError(const String& filename, int line_number, const char* fmt, ...)
  64. {
  65. const int buffer_size = 1024;
  66. char buffer[buffer_size];
  67. va_list argument_list;
  68. // Print the message to the buffer.
  69. va_start(argument_list, fmt);
  70. int len = vsnprintf(buffer, buffer_size - 2, fmt, argument_list);
  71. if (len < 0 || len > buffer_size - 2)
  72. {
  73. len = buffer_size - 2;
  74. }
  75. buffer[len] = '\0';
  76. va_end(argument_list);
  77. if (line_number >= 0)
  78. Message(Log::LT_ERROR, "%s:%d: %s", filename.c_str(), line_number, buffer);
  79. else
  80. Message(Log::LT_ERROR, "%s: %s", filename.c_str(), buffer);
  81. }
  82. bool Assert(const char* msg, const char* file, int line)
  83. {
  84. Rml::Core::String message = CreateString(1024, "%s\n%s:%d", msg, file, line);
  85. return GetSystemInterface()->LogMessage(Log::LT_ASSERT, message);
  86. }
  87. }
  88. }