Log.cpp 3.3 KB

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