ElementLog.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "ElementLog.h"
  29. #include "CommonSource.h"
  30. #include "BeaconSource.h"
  31. #include "LogSource.h"
  32. #include "../../Include/RmlUi/Core/Context.h"
  33. #include "../../Include/RmlUi/Core/Factory.h"
  34. #include <limits.h>
  35. namespace Rml {
  36. namespace Debugger {
  37. const int MAX_LOG_MESSAGES = 50;
  38. ElementLog::ElementLog(const Core::String& tag) : Core::ElementDocument(tag)
  39. {
  40. dirty_logs = false;
  41. beacon = nullptr;
  42. current_beacon_level = Core::Log::LT_MAX;
  43. auto_scroll = true;
  44. message_content = nullptr;
  45. current_index = 0;
  46. // Set up the log type buttons.
  47. log_types[Core::Log::LT_ALWAYS].visible = true;
  48. log_types[Core::Log::LT_ALWAYS].class_name = "error";
  49. log_types[Core::Log::LT_ALWAYS].alert_contents = "A";
  50. log_types[Core::Log::LT_ERROR].visible = true;
  51. log_types[Core::Log::LT_ERROR].class_name = "error";
  52. log_types[Core::Log::LT_ERROR].alert_contents = "!";
  53. log_types[Core::Log::LT_ERROR].button_name = "error_button";
  54. log_types[Core::Log::LT_ASSERT].visible = true;
  55. log_types[Core::Log::LT_ASSERT].class_name = "error";
  56. log_types[Core::Log::LT_ASSERT].alert_contents = "!";
  57. log_types[Core::Log::LT_WARNING].visible = true;
  58. log_types[Core::Log::LT_WARNING].class_name = "warning";
  59. log_types[Core::Log::LT_WARNING].alert_contents = "!";
  60. log_types[Core::Log::LT_WARNING].button_name = "warning_button";
  61. log_types[Core::Log::LT_INFO].visible = false;
  62. log_types[Core::Log::LT_INFO].class_name = "info";
  63. log_types[Core::Log::LT_INFO].alert_contents = "i";
  64. log_types[Core::Log::LT_INFO].button_name = "info_button";
  65. log_types[Core::Log::LT_DEBUG].visible = true;
  66. log_types[Core::Log::LT_DEBUG].class_name = "debug";
  67. log_types[Core::Log::LT_DEBUG].alert_contents = "?";
  68. log_types[Core::Log::LT_DEBUG].button_name = "debug_button";
  69. }
  70. ElementLog::~ElementLog()
  71. {
  72. }
  73. // Initialises the log element.
  74. bool ElementLog::Initialise()
  75. {
  76. SetInnerRML(log_rml);
  77. SetId("rmlui-debug-log");
  78. message_content = GetElementById("content");
  79. if (message_content)
  80. {
  81. message_content->AddEventListener(Core::EventId::Resize, this);
  82. }
  83. Core::SharedPtr<Core::StyleSheet> style_sheet = Core::Factory::InstanceStyleSheetString(Core::String(common_rcss) + Core::String(log_rcss));
  84. if (!style_sheet)
  85. return false;
  86. SetStyleSheet(std::move(style_sheet));
  87. AddEventListener(Core::EventId::Click, this);
  88. // Create the log beacon.
  89. beacon = GetContext()->CreateDocument();
  90. if (!beacon)
  91. return false;
  92. beacon->SetId("rmlui-debug-log-beacon");
  93. beacon->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  94. beacon->SetInnerRML(beacon_rml);
  95. Core::Element* button = beacon->GetFirstChild();
  96. if (button)
  97. beacon->GetFirstChild()->AddEventListener(Core::EventId::Click, this);
  98. style_sheet = Core::Factory::InstanceStyleSheetString(Core::String(common_rcss) + Core::String(beacon_rcss));
  99. if (!style_sheet)
  100. {
  101. GetContext()->UnloadDocument(beacon);
  102. beacon = nullptr;
  103. return false;
  104. }
  105. beacon->SetStyleSheet(style_sheet);
  106. return true;
  107. }
  108. // Adds a log message to the debug log.
  109. void ElementLog::AddLogMessage(Core::Log::Type type, const Core::String& message)
  110. {
  111. using Core::StringUtilities::Replace;
  112. // Add the message to the list of messages for the specified log type.
  113. LogMessage log_message;
  114. log_message.index = current_index++;
  115. log_message.message = Replace(Replace(Core::String(message), "<", "&lt;"), ">", "&gt;");
  116. log_types[type].log_messages.push_back(log_message);
  117. if (log_types[type].log_messages.size() >= MAX_LOG_MESSAGES)
  118. {
  119. log_types[type].log_messages.pop_front();
  120. }
  121. // If this log type is invisible, and there is a button for this log type, then change its text from
  122. // "Off" to "Off*" to signal that there are unread logs.
  123. if (!log_types[type].visible)
  124. {
  125. if (!log_types[type].button_name.empty())
  126. {
  127. Rml::Core::Element* button = GetElementById(log_types[type].button_name);
  128. if (button)
  129. {
  130. button->SetInnerRML("Off*");
  131. }
  132. }
  133. }
  134. // Trigger the beacon if we're hidden. Override any lower-level log type if it is already visible.
  135. else
  136. {
  137. if (!IsVisible())
  138. {
  139. if (beacon != nullptr)
  140. {
  141. if (type < current_beacon_level)
  142. {
  143. beacon->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Visible));
  144. current_beacon_level = type;
  145. Rml::Core::Element* beacon_button = beacon->GetFirstChild();
  146. if (beacon_button)
  147. {
  148. beacon_button->SetClassNames(log_types[type].class_name);
  149. beacon_button->SetInnerRML(log_types[type].alert_contents);
  150. }
  151. }
  152. }
  153. }
  154. }
  155. // Force a refresh of the RML.
  156. dirty_logs = true;
  157. }
  158. void ElementLog::OnUpdate()
  159. {
  160. Core::ElementDocument::OnUpdate();
  161. if (dirty_logs)
  162. {
  163. // Set the log content:
  164. Core::String messages;
  165. if (message_content)
  166. {
  167. unsigned int log_pointers[Core::Log::LT_MAX];
  168. for (int i = 0; i < Core::Log::LT_MAX; i++)
  169. log_pointers[i] = 0;
  170. int next_type = FindNextEarliestLogType(log_pointers);
  171. int num_messages = 0;
  172. while (next_type != -1 && num_messages < MAX_LOG_MESSAGES)
  173. {
  174. messages += Core::CreateString(128, "<div class=\"log-entry\"><div class=\"icon %s\">%s</div><p class=\"message\">", log_types[next_type].class_name.c_str(), log_types[next_type].alert_contents.c_str());
  175. messages += log_types[next_type].log_messages[log_pointers[next_type]].message;
  176. messages += "</p></div>";
  177. log_pointers[next_type]++;
  178. next_type = FindNextEarliestLogType(log_pointers);
  179. num_messages++;
  180. }
  181. if (message_content->HasChildNodes())
  182. {
  183. float last_element_top = message_content->GetLastChild()->GetAbsoluteTop();
  184. auto_scroll = message_content->GetAbsoluteTop() + message_content->GetAbsoluteTop() > last_element_top;
  185. }
  186. else
  187. auto_scroll = true;
  188. message_content->SetInnerRML(messages);
  189. dirty_logs = false;
  190. }
  191. }
  192. }
  193. void ElementLog::ProcessEvent(Core::Event& event)
  194. {
  195. // Only process events if we're visible
  196. if (beacon != nullptr)
  197. {
  198. if (event == Core::EventId::Click)
  199. {
  200. if (event.GetTargetElement() == beacon->GetFirstChild())
  201. {
  202. if (!IsVisible())
  203. SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Visible));
  204. beacon->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  205. current_beacon_level = Core::Log::LT_MAX;
  206. }
  207. else if (event.GetTargetElement()->GetId() == "close_button")
  208. {
  209. SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  210. }
  211. else if (event.GetTargetElement()->GetId() == "clear_button")
  212. {
  213. for (int i = 0; i < Core::Log::LT_MAX; i++)
  214. {
  215. log_types[i].log_messages.clear();
  216. if (!log_types[i].visible)
  217. {
  218. if (Element * button = GetElementById(log_types[i].button_name))
  219. button->SetInnerRML("Off");
  220. }
  221. }
  222. dirty_logs = true;
  223. }
  224. else
  225. {
  226. for (int i = 0; i < Core::Log::LT_MAX; i++)
  227. {
  228. if (!log_types[i].button_name.empty() && event.GetTargetElement()->GetId() == log_types[i].button_name)
  229. {
  230. log_types[i].visible = !log_types[i].visible;
  231. if (log_types[i].visible)
  232. event.GetTargetElement()->SetInnerRML("On");
  233. else
  234. event.GetTargetElement()->SetInnerRML("Off");
  235. dirty_logs = true;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. if (event == Core::EventId::Resize && auto_scroll)
  242. {
  243. if (message_content != nullptr &&
  244. message_content->HasChildNodes())
  245. message_content->GetLastChild()->ScrollIntoView();
  246. }
  247. }
  248. int ElementLog::FindNextEarliestLogType(unsigned int log_pointers[Core::Log::LT_MAX])
  249. {
  250. int log_channel = -1;
  251. unsigned int index = UINT_MAX;
  252. for (int i = 0; i < Core::Log::LT_MAX; i++)
  253. {
  254. if (log_types[i].visible)
  255. {
  256. if (log_pointers[i] < log_types[i].log_messages.size())
  257. {
  258. if (log_types[i].log_messages[log_pointers[i]].index < index)
  259. {
  260. index = log_types[i].log_messages[log_pointers[i]].index;
  261. log_channel = i;
  262. }
  263. }
  264. }
  265. }
  266. return log_channel;
  267. }
  268. }
  269. }