ElementLog.cpp 9.1 KB

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