Log.cpp 3.1 KB

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