editor_log.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*************************************************************************/
  2. /* editor_log.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "editor_log.h"
  31. #include "core/os/keyboard.h"
  32. #include "core/version.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_paths.h"
  35. #include "editor/editor_scale.h"
  36. #include "editor/editor_settings.h"
  37. #include "scene/gui/center_container.h"
  38. #include "scene/gui/separator.h"
  39. #include "scene/resources/font.h"
  40. void EditorLog::_error_handler(void *p_self, const char *p_func, const char *p_file, int p_line, const char *p_error, const char *p_errorexp, bool p_editor_notify, ErrorHandlerType p_type) {
  41. EditorLog *self = static_cast<EditorLog *>(p_self);
  42. if (self->current != Thread::get_caller_id()) {
  43. return;
  44. }
  45. String err_str;
  46. if (p_errorexp && p_errorexp[0]) {
  47. err_str = String::utf8(p_errorexp);
  48. } else {
  49. err_str = String::utf8(p_file) + ":" + itos(p_line) + " - " + String::utf8(p_error);
  50. }
  51. if (p_editor_notify) {
  52. err_str += " (User)";
  53. }
  54. if (p_type == ERR_HANDLER_WARNING) {
  55. self->add_message(err_str, MSG_TYPE_WARNING);
  56. } else {
  57. self->add_message(err_str, MSG_TYPE_ERROR);
  58. }
  59. }
  60. void EditorLog::_update_theme() {
  61. Ref<Font> normal_font = get_theme_font(SNAME("output_source"), SNAME("EditorFonts"));
  62. if (normal_font.is_valid()) {
  63. log->add_theme_font_override("normal_font", normal_font);
  64. }
  65. log->add_theme_font_size_override("normal_font_size", get_theme_font_size(SNAME("output_source_size"), SNAME("EditorFonts")));
  66. log->add_theme_color_override("selection_color", get_theme_color(SNAME("accent_color"), SNAME("Editor")) * Color(1, 1, 1, 0.4));
  67. Ref<Font> bold_font = get_theme_font(SNAME("bold"), SNAME("EditorFonts"));
  68. if (bold_font.is_valid()) {
  69. log->add_theme_font_override("bold_font", bold_font);
  70. }
  71. type_filter_map[MSG_TYPE_STD]->toggle_button->set_icon(get_theme_icon(SNAME("Popup"), SNAME("EditorIcons")));
  72. type_filter_map[MSG_TYPE_ERROR]->toggle_button->set_icon(get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons")));
  73. type_filter_map[MSG_TYPE_WARNING]->toggle_button->set_icon(get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons")));
  74. type_filter_map[MSG_TYPE_EDITOR]->toggle_button->set_icon(get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")));
  75. type_filter_map[MSG_TYPE_STD]->toggle_button->set_theme_type_variation("EditorLogFilterButton");
  76. type_filter_map[MSG_TYPE_ERROR]->toggle_button->set_theme_type_variation("EditorLogFilterButton");
  77. type_filter_map[MSG_TYPE_WARNING]->toggle_button->set_theme_type_variation("EditorLogFilterButton");
  78. type_filter_map[MSG_TYPE_EDITOR]->toggle_button->set_theme_type_variation("EditorLogFilterButton");
  79. clear_button->set_icon(get_theme_icon(SNAME("Clear"), SNAME("EditorIcons")));
  80. copy_button->set_icon(get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")));
  81. collapse_button->set_icon(get_theme_icon(SNAME("CombineLines"), SNAME("EditorIcons")));
  82. show_search_button->set_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  83. search_box->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  84. theme_cache.error_color = get_theme_color(SNAME("error_color"), SNAME("Editor"));
  85. theme_cache.error_icon = get_theme_icon(SNAME("Error"), SNAME("EditorIcons"));
  86. theme_cache.warning_color = get_theme_color(SNAME("warning_color"), SNAME("Editor"));
  87. theme_cache.warning_icon = get_theme_icon(SNAME("Warning"), SNAME("EditorIcons"));
  88. theme_cache.message_color = get_theme_color(SNAME("font_color"), SNAME("Editor")) * Color(1, 1, 1, 0.6);
  89. }
  90. void EditorLog::_notification(int p_what) {
  91. switch (p_what) {
  92. case NOTIFICATION_ENTER_TREE: {
  93. _update_theme();
  94. _load_state();
  95. } break;
  96. case NOTIFICATION_THEME_CHANGED: {
  97. _update_theme();
  98. _rebuild_log();
  99. } break;
  100. }
  101. }
  102. void EditorLog::_set_collapse(bool p_collapse) {
  103. collapse = p_collapse;
  104. _start_state_save_timer();
  105. _rebuild_log();
  106. }
  107. void EditorLog::_start_state_save_timer() {
  108. if (!is_loading_state) {
  109. save_state_timer->start();
  110. }
  111. }
  112. void EditorLog::_save_state() {
  113. Ref<ConfigFile> config;
  114. config.instantiate();
  115. // Load and amend existing config if it exists.
  116. config->load(EditorPaths::get_singleton()->get_project_settings_dir().path_join("editor_layout.cfg"));
  117. const String section = "editor_log";
  118. for (const KeyValue<MessageType, LogFilter *> &E : type_filter_map) {
  119. config->set_value(section, "log_filter_" + itos(E.key), E.value->is_active());
  120. }
  121. config->set_value(section, "collapse", collapse);
  122. config->set_value(section, "show_search", search_box->is_visible());
  123. config->save(EditorPaths::get_singleton()->get_project_settings_dir().path_join("editor_layout.cfg"));
  124. }
  125. void EditorLog::_load_state() {
  126. is_loading_state = true;
  127. Ref<ConfigFile> config;
  128. config.instantiate();
  129. config->load(EditorPaths::get_singleton()->get_project_settings_dir().path_join("editor_layout.cfg"));
  130. // Run the below code even if config->load returns an error, since we want the defaults to be set even if the file does not exist yet.
  131. const String section = "editor_log";
  132. for (const KeyValue<MessageType, LogFilter *> &E : type_filter_map) {
  133. E.value->set_active(config->get_value(section, "log_filter_" + itos(E.key), true));
  134. }
  135. collapse = config->get_value(section, "collapse", false);
  136. collapse_button->set_pressed(collapse);
  137. bool show_search = config->get_value(section, "show_search", true);
  138. search_box->set_visible(show_search);
  139. show_search_button->set_pressed(show_search);
  140. is_loading_state = false;
  141. }
  142. void EditorLog::_clear_request() {
  143. log->clear();
  144. messages.clear();
  145. _reset_message_counts();
  146. tool_button->set_icon(Ref<Texture2D>());
  147. }
  148. void EditorLog::_copy_request() {
  149. String text = log->get_selected_text();
  150. if (text.is_empty()) {
  151. text = log->get_parsed_text();
  152. }
  153. if (!text.is_empty()) {
  154. DisplayServer::get_singleton()->clipboard_set(text);
  155. }
  156. }
  157. void EditorLog::clear() {
  158. _clear_request();
  159. }
  160. void EditorLog::_process_message(const String &p_msg, MessageType p_type) {
  161. if (messages.size() > 0 && messages[messages.size() - 1].text == p_msg && messages[messages.size() - 1].type == p_type) {
  162. // If previous message is the same as the new one, increase previous count rather than adding another
  163. // instance to the messages list.
  164. LogMessage &previous = messages.write[messages.size() - 1];
  165. previous.count++;
  166. _add_log_line(previous, collapse);
  167. } else {
  168. // Different message to the previous one received.
  169. LogMessage message(p_msg, p_type);
  170. _add_log_line(message);
  171. messages.push_back(message);
  172. }
  173. type_filter_map[p_type]->set_message_count(type_filter_map[p_type]->get_message_count() + 1);
  174. }
  175. void EditorLog::add_message(const String &p_msg, MessageType p_type) {
  176. // Make text split by new lines their own message.
  177. // See #41321 for reasoning. At time of writing, multiple print()'s in running projects
  178. // get grouped together and sent to the editor log as one message. This can mess with the
  179. // search functionality (see the comments on the PR above for more details). This behaviour
  180. // also matches that of other IDE's.
  181. Vector<String> lines = p_msg.split("\n", true);
  182. for (int i = 0; i < lines.size(); i++) {
  183. _process_message(lines[i], p_type);
  184. }
  185. }
  186. void EditorLog::set_tool_button(Button *p_tool_button) {
  187. tool_button = p_tool_button;
  188. }
  189. void EditorLog::register_undo_redo(UndoRedo *p_undo_redo) {
  190. p_undo_redo->set_commit_notify_callback(_undo_redo_cbk, this);
  191. }
  192. void EditorLog::_undo_redo_cbk(void *p_self, const String &p_name) {
  193. EditorLog *self = static_cast<EditorLog *>(p_self);
  194. self->add_message(p_name, EditorLog::MSG_TYPE_EDITOR);
  195. }
  196. void EditorLog::_rebuild_log() {
  197. log->clear();
  198. for (int msg_idx = 0; msg_idx < messages.size(); msg_idx++) {
  199. LogMessage msg = messages[msg_idx];
  200. if (collapse) {
  201. // If collapsing, only log one instance of the message.
  202. _add_log_line(msg);
  203. } else {
  204. // If not collapsing, log each instance on a line.
  205. for (int i = 0; i < msg.count; i++) {
  206. _add_log_line(msg);
  207. }
  208. }
  209. }
  210. }
  211. void EditorLog::_add_log_line(LogMessage &p_message, bool p_replace_previous) {
  212. if (!is_inside_tree()) {
  213. // The log will be built all at once when it enters the tree and has its theme items.
  214. return;
  215. }
  216. // Only add the message to the log if it passes the filters.
  217. bool filter_active = type_filter_map[p_message.type]->is_active();
  218. String search_text = search_box->get_text();
  219. bool search_match = search_text.is_empty() || p_message.text.findn(search_text) > -1;
  220. if (!filter_active || !search_match) {
  221. return;
  222. }
  223. if (p_replace_previous) {
  224. // Remove last line if replacing, as it will be replace by the next added line.
  225. // Why "- 2"? RichTextLabel is weird. When you add a line with add_newline(), it also adds an element to the list of lines which is null/blank,
  226. // but it still counts as a line. So if you remove the last line (count - 1) you are actually removing nothing...
  227. log->remove_line(log->get_paragraph_count() - 2);
  228. }
  229. switch (p_message.type) {
  230. case MSG_TYPE_STD: {
  231. } break;
  232. case MSG_TYPE_STD_RICH: {
  233. } break;
  234. case MSG_TYPE_ERROR: {
  235. log->push_color(theme_cache.error_color);
  236. Ref<Texture2D> icon = theme_cache.error_icon;
  237. log->add_image(icon);
  238. log->add_text(" ");
  239. tool_button->set_icon(icon);
  240. } break;
  241. case MSG_TYPE_WARNING: {
  242. log->push_color(theme_cache.warning_color);
  243. Ref<Texture2D> icon = theme_cache.warning_icon;
  244. log->add_image(icon);
  245. log->add_text(" ");
  246. tool_button->set_icon(icon);
  247. } break;
  248. case MSG_TYPE_EDITOR: {
  249. // Distinguish editor messages from messages printed by the project
  250. log->push_color(theme_cache.message_color);
  251. } break;
  252. }
  253. // If collapsing, add the count of this message in bold at the start of the line.
  254. if (collapse && p_message.count > 1) {
  255. log->push_bold();
  256. log->add_text(vformat("(%s) ", itos(p_message.count)));
  257. log->pop();
  258. }
  259. if (p_message.type == MSG_TYPE_STD_RICH) {
  260. log->append_text(p_message.text);
  261. } else {
  262. log->add_text(p_message.text);
  263. }
  264. // Need to use pop() to exit out of the RichTextLabels current "push" stack.
  265. // We only "push" in the above switch when message type != STD and RICH, so only pop when that is the case.
  266. if (p_message.type != MSG_TYPE_STD && p_message.type != MSG_TYPE_STD_RICH) {
  267. log->pop();
  268. }
  269. log->add_newline();
  270. }
  271. void EditorLog::_set_filter_active(bool p_active, MessageType p_message_type) {
  272. type_filter_map[p_message_type]->set_active(p_active);
  273. _start_state_save_timer();
  274. _rebuild_log();
  275. }
  276. void EditorLog::_set_search_visible(bool p_visible) {
  277. search_box->set_visible(p_visible);
  278. if (p_visible) {
  279. search_box->grab_focus();
  280. }
  281. _start_state_save_timer();
  282. }
  283. void EditorLog::_search_changed(const String &p_text) {
  284. _rebuild_log();
  285. }
  286. void EditorLog::_reset_message_counts() {
  287. for (const KeyValue<MessageType, LogFilter *> &E : type_filter_map) {
  288. E.value->set_message_count(0);
  289. }
  290. }
  291. void EditorLog::_bind_methods() {
  292. ADD_SIGNAL(MethodInfo("clear_request"));
  293. ADD_SIGNAL(MethodInfo("copy_request"));
  294. }
  295. EditorLog::EditorLog() {
  296. save_state_timer = memnew(Timer);
  297. save_state_timer->set_wait_time(2);
  298. save_state_timer->set_one_shot(true);
  299. save_state_timer->connect("timeout", callable_mp(this, &EditorLog::_save_state));
  300. add_child(save_state_timer);
  301. HBoxContainer *hb = this;
  302. VBoxContainer *vb_left = memnew(VBoxContainer);
  303. vb_left->set_custom_minimum_size(Size2(0, 180) * EDSCALE);
  304. vb_left->set_v_size_flags(SIZE_EXPAND_FILL);
  305. vb_left->set_h_size_flags(SIZE_EXPAND_FILL);
  306. hb->add_child(vb_left);
  307. // Log - Rich Text Label.
  308. log = memnew(RichTextLabel);
  309. log->set_use_bbcode(true);
  310. log->set_scroll_follow(true);
  311. log->set_selection_enabled(true);
  312. log->set_focus_mode(FOCUS_CLICK);
  313. log->set_v_size_flags(SIZE_EXPAND_FILL);
  314. log->set_h_size_flags(SIZE_EXPAND_FILL);
  315. log->set_deselect_on_focus_loss_enabled(false);
  316. vb_left->add_child(log);
  317. // Search box
  318. search_box = memnew(LineEdit);
  319. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  320. search_box->set_placeholder(TTR("Filter Messages"));
  321. search_box->set_clear_button_enabled(true);
  322. search_box->set_visible(true);
  323. search_box->connect("text_changed", callable_mp(this, &EditorLog::_search_changed));
  324. vb_left->add_child(search_box);
  325. VBoxContainer *vb_right = memnew(VBoxContainer);
  326. hb->add_child(vb_right);
  327. // Tools grid
  328. HBoxContainer *hb_tools = memnew(HBoxContainer);
  329. hb_tools->set_h_size_flags(SIZE_SHRINK_CENTER);
  330. vb_right->add_child(hb_tools);
  331. // Clear.
  332. clear_button = memnew(Button);
  333. clear_button->set_flat(true);
  334. clear_button->set_focus_mode(FOCUS_NONE);
  335. clear_button->set_shortcut(ED_SHORTCUT("editor/clear_output", TTR("Clear Output"), KeyModifierMask::CMD | KeyModifierMask::SHIFT | Key::K));
  336. clear_button->set_shortcut_context(this);
  337. clear_button->connect("pressed", callable_mp(this, &EditorLog::_clear_request));
  338. hb_tools->add_child(clear_button);
  339. // Copy.
  340. copy_button = memnew(Button);
  341. copy_button->set_flat(true);
  342. copy_button->set_focus_mode(FOCUS_NONE);
  343. copy_button->set_shortcut(ED_SHORTCUT("editor/copy_output", TTR("Copy Selection"), KeyModifierMask::CMD | Key::C));
  344. copy_button->set_shortcut_context(this);
  345. copy_button->connect("pressed", callable_mp(this, &EditorLog::_copy_request));
  346. hb_tools->add_child(copy_button);
  347. // A second hbox to make a 2x2 grid of buttons.
  348. HBoxContainer *hb_tools2 = memnew(HBoxContainer);
  349. hb_tools2->set_h_size_flags(SIZE_SHRINK_CENTER);
  350. vb_right->add_child(hb_tools2);
  351. // Collapse.
  352. collapse_button = memnew(Button);
  353. collapse_button->set_flat(true);
  354. collapse_button->set_focus_mode(FOCUS_NONE);
  355. collapse_button->set_tooltip_text(TTR("Collapse duplicate messages into one log entry. Shows number of occurrences."));
  356. collapse_button->set_toggle_mode(true);
  357. collapse_button->set_pressed(false);
  358. collapse_button->connect("toggled", callable_mp(this, &EditorLog::_set_collapse));
  359. hb_tools2->add_child(collapse_button);
  360. // Show Search.
  361. show_search_button = memnew(Button);
  362. show_search_button->set_flat(true);
  363. show_search_button->set_focus_mode(FOCUS_NONE);
  364. show_search_button->set_toggle_mode(true);
  365. show_search_button->set_pressed(true);
  366. show_search_button->set_shortcut(ED_SHORTCUT("editor/open_search", TTR("Focus Search/Filter Bar"), KeyModifierMask::CMD | Key::F));
  367. show_search_button->set_shortcut_context(this);
  368. show_search_button->connect("toggled", callable_mp(this, &EditorLog::_set_search_visible));
  369. hb_tools2->add_child(show_search_button);
  370. // Message Type Filters.
  371. vb_right->add_child(memnew(HSeparator));
  372. LogFilter *std_filter = memnew(LogFilter(MSG_TYPE_STD));
  373. std_filter->initialize_button(TTR("Toggle visibility of standard output messages."), callable_mp(this, &EditorLog::_set_filter_active));
  374. vb_right->add_child(std_filter->toggle_button);
  375. type_filter_map.insert(MSG_TYPE_STD, std_filter);
  376. type_filter_map.insert(MSG_TYPE_STD_RICH, std_filter);
  377. LogFilter *error_filter = memnew(LogFilter(MSG_TYPE_ERROR));
  378. error_filter->initialize_button(TTR("Toggle visibility of errors."), callable_mp(this, &EditorLog::_set_filter_active));
  379. vb_right->add_child(error_filter->toggle_button);
  380. type_filter_map.insert(MSG_TYPE_ERROR, error_filter);
  381. LogFilter *warning_filter = memnew(LogFilter(MSG_TYPE_WARNING));
  382. warning_filter->initialize_button(TTR("Toggle visibility of warnings."), callable_mp(this, &EditorLog::_set_filter_active));
  383. vb_right->add_child(warning_filter->toggle_button);
  384. type_filter_map.insert(MSG_TYPE_WARNING, warning_filter);
  385. LogFilter *editor_filter = memnew(LogFilter(MSG_TYPE_EDITOR));
  386. editor_filter->initialize_button(TTR("Toggle visibility of editor messages."), callable_mp(this, &EditorLog::_set_filter_active));
  387. vb_right->add_child(editor_filter->toggle_button);
  388. type_filter_map.insert(MSG_TYPE_EDITOR, editor_filter);
  389. add_message(VERSION_FULL_NAME " (c) 2007-2022 Juan Linietsky, Ariel Manzur & Godot Contributors.");
  390. eh.errfunc = _error_handler;
  391. eh.userdata = this;
  392. add_error_handler(&eh);
  393. current = Thread::get_caller_id();
  394. }
  395. void EditorLog::deinit() {
  396. remove_error_handler(&eh);
  397. }
  398. EditorLog::~EditorLog() {
  399. for (const KeyValue<MessageType, LogFilter *> &E : type_filter_map) {
  400. // MSG_TYPE_STD_RICH is connected to the std_filter button, so we do this
  401. // to avoid it from being deleted twice, causing a crash on closing.
  402. if (E.key != MSG_TYPE_STD_RICH) {
  403. memdelete(E.value);
  404. }
  405. }
  406. }