ElementLog.cpp 9.0 KB

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