editor_log.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /**************************************************************************/
  2. /* editor_log.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/object/undo_redo.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/version.h"
  34. #include "editor/docks/editor_dock.h"
  35. #include "editor/docks/inspector_dock.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_string_names.h"
  38. #include "editor/file_system/editor_paths.h"
  39. #include "editor/script/script_editor_plugin.h"
  40. #include "editor/settings/editor_command_palette.h"
  41. #include "editor/settings/editor_settings.h"
  42. #include "editor/themes/editor_scale.h"
  43. #include "modules/regex/regex.h"
  44. #include "scene/gui/box_container.h"
  45. #include "scene/gui/separator.h"
  46. #include "scene/main/timer.h"
  47. #include "scene/resources/font.h"
  48. 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) {
  49. EditorLog *self = static_cast<EditorLog *>(p_self);
  50. String err_str;
  51. if (p_errorexp && p_errorexp[0]) {
  52. err_str = String::utf8(p_errorexp).replace("[", "[lb]");
  53. } else {
  54. err_str = vformat("[url]%s:%d[/url] - %s", String::utf8(p_file).replace("[", "[lb]"), p_line, String::utf8(p_error).replace("[", "[lb]"));
  55. }
  56. MessageType message_type = p_type == ERR_HANDLER_WARNING ? MSG_TYPE_WARNING : MSG_TYPE_ERROR;
  57. if (!Thread::is_main_thread()) {
  58. MessageQueue::get_main_singleton()->push_callable(callable_mp(self, &EditorLog::add_message), err_str, message_type);
  59. } else {
  60. self->add_message(err_str, message_type);
  61. }
  62. }
  63. void EditorLog::_update_theme() {
  64. const Ref<Font> normal_font = get_theme_font(SNAME("output_source"), EditorStringName(EditorFonts));
  65. if (normal_font.is_valid()) {
  66. log->add_theme_font_override("normal_font", normal_font);
  67. }
  68. const Ref<Font> bold_font = get_theme_font(SNAME("output_source_bold"), EditorStringName(EditorFonts));
  69. if (bold_font.is_valid()) {
  70. log->add_theme_font_override("bold_font", bold_font);
  71. }
  72. const Ref<Font> italics_font = get_theme_font(SNAME("output_source_italic"), EditorStringName(EditorFonts));
  73. if (italics_font.is_valid()) {
  74. log->add_theme_font_override("italics_font", italics_font);
  75. }
  76. const Ref<Font> bold_italics_font = get_theme_font(SNAME("output_source_bold_italic"), EditorStringName(EditorFonts));
  77. if (bold_italics_font.is_valid()) {
  78. log->add_theme_font_override("bold_italics_font", bold_italics_font);
  79. }
  80. const Ref<Font> mono_font = get_theme_font(SNAME("output_source_mono"), EditorStringName(EditorFonts));
  81. if (mono_font.is_valid()) {
  82. log->add_theme_font_override("mono_font", mono_font);
  83. }
  84. // Disable padding for highlighted background/foreground to prevent highlights from overlapping on close lines.
  85. // This also better matches terminal output, which does not use any form of padding.
  86. log->add_theme_constant_override("text_highlight_h_padding", 0);
  87. log->add_theme_constant_override("text_highlight_v_padding", 0);
  88. const int font_size = get_theme_font_size(SNAME("output_source_size"), EditorStringName(EditorFonts));
  89. log->begin_bulk_theme_override();
  90. log->add_theme_font_size_override("normal_font_size", font_size);
  91. log->add_theme_font_size_override("bold_font_size", font_size);
  92. log->add_theme_font_size_override("italics_font_size", font_size);
  93. log->add_theme_font_size_override("mono_font_size", font_size);
  94. log->end_bulk_theme_override();
  95. type_filter_map[MSG_TYPE_STD]->toggle_button->set_button_icon(get_editor_theme_icon(SNAME("Popup")));
  96. type_filter_map[MSG_TYPE_ERROR]->toggle_button->set_button_icon(get_editor_theme_icon(SNAME("StatusError")));
  97. type_filter_map[MSG_TYPE_WARNING]->toggle_button->set_button_icon(get_editor_theme_icon(SNAME("StatusWarning")));
  98. type_filter_map[MSG_TYPE_EDITOR]->toggle_button->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  99. type_filter_map[MSG_TYPE_STD]->toggle_button->set_theme_type_variation("EditorLogFilterButton");
  100. type_filter_map[MSG_TYPE_ERROR]->toggle_button->set_theme_type_variation("EditorLogFilterButton");
  101. type_filter_map[MSG_TYPE_WARNING]->toggle_button->set_theme_type_variation("EditorLogFilterButton");
  102. type_filter_map[MSG_TYPE_EDITOR]->toggle_button->set_theme_type_variation("EditorLogFilterButton");
  103. clear_button->set_button_icon(get_editor_theme_icon(SNAME("Clear")));
  104. copy_button->set_button_icon(get_editor_theme_icon(SNAME("ActionCopy")));
  105. collapse_button->set_button_icon(get_editor_theme_icon(SNAME("CombineLines")));
  106. show_search_button->set_button_icon(get_editor_theme_icon(SNAME("Search")));
  107. search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  108. theme_cache.error_color = get_theme_color(SNAME("error_color"), EditorStringName(Editor));
  109. theme_cache.error_icon = get_editor_theme_icon(SNAME("Error"));
  110. theme_cache.warning_color = get_theme_color(SNAME("warning_color"), EditorStringName(Editor));
  111. theme_cache.warning_icon = get_editor_theme_icon(SNAME("Warning"));
  112. theme_cache.message_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor)) * Color(1, 1, 1, 0.6);
  113. }
  114. void EditorLog::_editor_settings_changed() {
  115. int new_line_limit = int(EDITOR_GET("run/output/max_lines"));
  116. if (new_line_limit != line_limit) {
  117. line_limit = new_line_limit;
  118. _rebuild_log();
  119. }
  120. }
  121. void EditorLog::_notification(int p_what) {
  122. switch (p_what) {
  123. case NOTIFICATION_ENTER_TREE: {
  124. _update_theme();
  125. _load_state();
  126. } break;
  127. case NOTIFICATION_THEME_CHANGED: {
  128. _update_theme();
  129. _rebuild_log();
  130. } break;
  131. }
  132. }
  133. void EditorLog::_set_collapse(bool p_collapse) {
  134. collapse = p_collapse;
  135. _start_state_save_timer();
  136. _rebuild_log();
  137. }
  138. void EditorLog::_start_state_save_timer() {
  139. if (!is_loading_state) {
  140. save_state_timer->start();
  141. }
  142. }
  143. void EditorLog::_save_state() {
  144. Ref<ConfigFile> config;
  145. config.instantiate();
  146. // Load and amend existing config if it exists.
  147. config->load(EditorPaths::get_singleton()->get_project_settings_dir().path_join("editor_layout.cfg"));
  148. const String section = "editor_log";
  149. for (const KeyValue<MessageType, LogFilter *> &E : type_filter_map) {
  150. config->set_value(section, "log_filter_" + itos(E.key), E.value->is_active());
  151. }
  152. config->set_value(section, "collapse", collapse);
  153. config->set_value(section, "show_search", search_box->is_visible());
  154. config->save(EditorPaths::get_singleton()->get_project_settings_dir().path_join("editor_layout.cfg"));
  155. }
  156. void EditorLog::_load_state() {
  157. is_loading_state = true;
  158. Ref<ConfigFile> config;
  159. config.instantiate();
  160. config->load(EditorPaths::get_singleton()->get_project_settings_dir().path_join("editor_layout.cfg"));
  161. // 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.
  162. const String section = "editor_log";
  163. for (const KeyValue<MessageType, LogFilter *> &E : type_filter_map) {
  164. E.value->set_active(config->get_value(section, "log_filter_" + itos(E.key), true));
  165. }
  166. collapse = config->get_value(section, "collapse", false);
  167. collapse_button->set_pressed(collapse);
  168. bool show_search = config->get_value(section, "show_search", true);
  169. search_box->set_visible(show_search);
  170. show_search_button->set_pressed(show_search);
  171. is_loading_state = false;
  172. }
  173. void EditorLog::_meta_clicked(const String &p_meta) {
  174. if (!p_meta.contains_char(':')) {
  175. return;
  176. }
  177. const PackedStringArray parts = p_meta.rsplit(":", true, 1);
  178. String path = parts[0];
  179. const int line = parts[1].to_int() - 1;
  180. if (path.begins_with("res://")) {
  181. if (ResourceLoader::exists(path)) {
  182. const Ref<Resource> res = ResourceLoader::load(path);
  183. ScriptEditor::get_singleton()->edit(res, line, 0);
  184. InspectorDock::get_singleton()->edit_resource(res);
  185. }
  186. } else if (path.has_extension("cpp") || path.has_extension("h") || path.has_extension("mm") || path.has_extension("hpp")) {
  187. // Godot source file. Try to open it in external editor.
  188. if (path.begins_with("./") || path.begins_with(".\\")) {
  189. // Relative path. Convert to absolute, using executable path as reference.
  190. path = path.trim_prefix("./").trim_prefix(".\\");
  191. path = OS::get_singleton()->get_executable_path().get_base_dir().get_base_dir().path_join(path);
  192. }
  193. if (!ScriptEditorPlugin::open_in_external_editor(path, line, -1, true)) {
  194. OS::get_singleton()->shell_open(path);
  195. }
  196. } else {
  197. OS::get_singleton()->shell_open(p_meta);
  198. }
  199. }
  200. void EditorLog::_clear_request() {
  201. log->clear();
  202. messages.clear();
  203. _reset_message_counts();
  204. _set_dock_tab_icon(Ref<Texture2D>());
  205. }
  206. void EditorLog::_copy_request() {
  207. String text = log->get_selected_text();
  208. if (text.is_empty()) {
  209. text = log->get_parsed_text();
  210. }
  211. if (!text.is_empty()) {
  212. DisplayServer::get_singleton()->clipboard_set(text);
  213. }
  214. }
  215. void EditorLog::clear() {
  216. _clear_request();
  217. }
  218. void EditorLog::_process_message(const String &p_msg, MessageType p_type, bool p_clear) {
  219. if (messages.size() > 0 && messages[messages.size() - 1].text == p_msg && messages[messages.size() - 1].type == p_type) {
  220. // If previous message is the same as the new one, increase previous count rather than adding another
  221. // instance to the messages list.
  222. LogMessage &previous = messages.write[messages.size() - 1];
  223. previous.count++;
  224. _add_log_line(previous, collapse);
  225. } else {
  226. // Different message to the previous one received.
  227. LogMessage message(p_msg, p_type, p_clear);
  228. _add_log_line(message);
  229. messages.push_back(message);
  230. }
  231. type_filter_map[p_type]->set_message_count(type_filter_map[p_type]->get_message_count() + 1);
  232. }
  233. void EditorLog::add_message(const String &p_msg, MessageType p_type) {
  234. // Make text split by new lines their own message.
  235. // See #41321 for reasoning. At time of writing, multiple print()'s in running projects
  236. // get grouped together and sent to the editor log as one message. This can mess with the
  237. // search functionality (see the comments on the PR above for more details). This behavior
  238. // also matches that of other IDE's.
  239. Vector<String> lines = p_msg.split("\n", true);
  240. int line_count = lines.size();
  241. for (int i = 0; i < line_count; i++) {
  242. _process_message(lines[i], p_type, i == line_count - 1);
  243. }
  244. }
  245. void EditorLog::_set_dock_tab_icon(Ref<Texture2D> p_icon) {
  246. set_dock_icon(p_icon);
  247. set_force_show_icon(p_icon.is_valid());
  248. }
  249. void EditorLog::register_undo_redo(UndoRedo *p_undo_redo) {
  250. p_undo_redo->set_commit_notify_callback(_undo_redo_cbk, this);
  251. }
  252. void EditorLog::_undo_redo_cbk(void *p_self, const String &p_name) {
  253. EditorLog *self = static_cast<EditorLog *>(p_self);
  254. self->add_message(p_name, EditorLog::MSG_TYPE_EDITOR);
  255. }
  256. void EditorLog::_rebuild_log() {
  257. if (messages.is_empty()) {
  258. return;
  259. }
  260. log->clear();
  261. int line_count = 0;
  262. int start_message_index = 0;
  263. int initial_skip = 0;
  264. // Search backward for starting place.
  265. for (start_message_index = messages.size() - 1; start_message_index >= 0; start_message_index--) {
  266. LogMessage msg = messages[start_message_index];
  267. if (collapse) {
  268. if (_check_display_message(msg)) {
  269. line_count++;
  270. }
  271. } else {
  272. // If not collapsing, log each instance on a line.
  273. for (int i = 0; i < msg.count; i++) {
  274. if (_check_display_message(msg)) {
  275. line_count++;
  276. }
  277. }
  278. }
  279. if (line_count >= line_limit) {
  280. initial_skip = line_count - line_limit;
  281. break;
  282. }
  283. if (start_message_index == 0) {
  284. break;
  285. }
  286. }
  287. for (int msg_idx = start_message_index; msg_idx < messages.size(); msg_idx++) {
  288. LogMessage msg = messages[msg_idx];
  289. if (collapse) {
  290. // If collapsing, only log one instance of the message.
  291. _add_log_line(msg);
  292. } else {
  293. // If not collapsing, log each instance on a line.
  294. for (int i = initial_skip; i < msg.count; i++) {
  295. initial_skip = 0;
  296. _add_log_line(msg);
  297. }
  298. }
  299. }
  300. }
  301. bool EditorLog::_check_display_message(LogMessage &p_message) {
  302. bool filter_active = type_filter_map[p_message.type]->is_active();
  303. String search_text = search_box->get_text();
  304. if (search_text.is_empty()) {
  305. return filter_active;
  306. }
  307. bool search_match = p_message.text.containsn(search_text);
  308. // If not found and message contains BBCode tags, also check the parsed text
  309. if (!search_match && p_message.text.contains_char('[')) {
  310. // Lazy initialize the BBCode parser
  311. if (!bbcode_parser) {
  312. bbcode_parser = memnew(RichTextLabel);
  313. bbcode_parser->set_use_bbcode(true);
  314. }
  315. // Ensure clean state for each message
  316. bbcode_parser->clear();
  317. bbcode_parser->parse_bbcode(p_message.text);
  318. String parsed_text = bbcode_parser->get_parsed_text();
  319. search_match = parsed_text.containsn(search_text);
  320. }
  321. return filter_active && search_match;
  322. }
  323. void EditorLog::_add_log_line(LogMessage &p_message, bool p_replace_previous) {
  324. if (!is_inside_tree()) {
  325. // The log will be built all at once when it enters the tree and has its theme items.
  326. return;
  327. }
  328. if (unlikely(log->is_updating())) {
  329. // The new message arrived during log RTL text processing/redraw (invalid BiDi control characters / font error), ignore it to avoid RTL data corruption.
  330. return;
  331. }
  332. // Only add the message to the log if it passes the filters.
  333. if (!_check_display_message(p_message)) {
  334. return;
  335. }
  336. if (p_replace_previous) {
  337. // Remove last line if replacing, as it will be replace by the next added line.
  338. // 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,
  339. // but it still counts as a line. So if you remove the last line (count - 1) you are actually removing nothing...
  340. log->remove_paragraph(log->get_paragraph_count() - 2);
  341. }
  342. switch (p_message.type) {
  343. case MSG_TYPE_STD: {
  344. } break;
  345. case MSG_TYPE_STD_RICH: {
  346. } break;
  347. case MSG_TYPE_ERROR: {
  348. log->push_color(theme_cache.error_color);
  349. Ref<Texture2D> icon = theme_cache.error_icon;
  350. log->add_image(icon);
  351. log->push_bold();
  352. log->add_text(" ERROR: ");
  353. log->pop(); // bold
  354. _set_dock_tab_icon(icon);
  355. } break;
  356. case MSG_TYPE_WARNING: {
  357. log->push_color(theme_cache.warning_color);
  358. Ref<Texture2D> icon = theme_cache.warning_icon;
  359. log->add_image(icon);
  360. log->push_bold();
  361. log->add_text(" WARNING: ");
  362. log->pop(); // bold
  363. _set_dock_tab_icon(icon);
  364. } break;
  365. case MSG_TYPE_EDITOR: {
  366. // Distinguish editor messages from messages printed by the project
  367. log->push_color(theme_cache.message_color);
  368. } break;
  369. }
  370. // If collapsing, add the count of this message in bold at the start of the line.
  371. if (collapse && p_message.count > 1) {
  372. log->push_bold();
  373. log->add_text(vformat("(%s) ", itos(p_message.count)));
  374. log->pop();
  375. }
  376. // Note that errors and warnings only support BBCode in the file part of the message.
  377. if (p_message.type == MSG_TYPE_STD_RICH || p_message.type == MSG_TYPE_ERROR || p_message.type == MSG_TYPE_WARNING) {
  378. log->append_text(p_message.text);
  379. } else {
  380. log->add_text(p_message.text);
  381. }
  382. if (p_message.clear || p_message.type != MSG_TYPE_STD_RICH) {
  383. log->pop_all(); // Pop all unclosed tags.
  384. }
  385. log->add_newline();
  386. if (p_replace_previous) {
  387. // Force sync last line update (skip if number of unprocessed log messages is too large to avoid editor lag).
  388. if (log->get_pending_paragraphs() < 100) {
  389. log->wait_until_finished();
  390. }
  391. }
  392. while (log->get_paragraph_count() > line_limit + 1) {
  393. log->remove_paragraph(0, true);
  394. }
  395. }
  396. void EditorLog::_set_filter_active(bool p_active, MessageType p_message_type) {
  397. type_filter_map[p_message_type]->set_active(p_active);
  398. _start_state_save_timer();
  399. _rebuild_log();
  400. }
  401. void EditorLog::_set_search_visible(bool p_visible) {
  402. search_box->set_visible(p_visible);
  403. if (p_visible) {
  404. search_box->grab_focus();
  405. }
  406. _start_state_save_timer();
  407. }
  408. void EditorLog::_search_changed(const String &p_text) {
  409. _rebuild_log();
  410. }
  411. void EditorLog::_reset_message_counts() {
  412. for (const KeyValue<MessageType, LogFilter *> &E : type_filter_map) {
  413. E.value->set_message_count(0);
  414. }
  415. }
  416. EditorLog::EditorLog() {
  417. set_name(TTRC("Output"));
  418. set_icon_name("Output");
  419. set_dock_shortcut(ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_output_bottom_panel", TTRC("Toggle Output Dock"), KeyModifierMask::ALT | Key::O));
  420. set_default_slot(EditorDock::DOCK_SLOT_BOTTOM);
  421. set_available_layouts(EditorDock::DOCK_LAYOUT_HORIZONTAL | EditorDock::DOCK_LAYOUT_FLOATING);
  422. save_state_timer = memnew(Timer);
  423. save_state_timer->set_wait_time(2);
  424. save_state_timer->set_one_shot(true);
  425. save_state_timer->connect("timeout", callable_mp(this, &EditorLog::_save_state));
  426. add_child(save_state_timer);
  427. line_limit = int(EDITOR_GET("run/output/max_lines"));
  428. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &EditorLog::_editor_settings_changed));
  429. HBoxContainer *hb = memnew(HBoxContainer);
  430. add_child(hb);
  431. VBoxContainer *vb_left = memnew(VBoxContainer);
  432. vb_left->set_custom_minimum_size(Size2(0, 180) * EDSCALE);
  433. vb_left->set_v_size_flags(SIZE_EXPAND_FILL);
  434. vb_left->set_h_size_flags(SIZE_EXPAND_FILL);
  435. hb->add_child(vb_left);
  436. // Log - Rich Text Label.
  437. log = memnew(RichTextLabel);
  438. log->set_threaded(true);
  439. log->set_use_bbcode(true);
  440. log->set_scroll_follow(true);
  441. log->set_selection_enabled(true);
  442. log->set_context_menu_enabled(true);
  443. log->set_focus_mode(FOCUS_CLICK);
  444. log->set_v_size_flags(SIZE_EXPAND_FILL);
  445. log->set_h_size_flags(SIZE_EXPAND_FILL);
  446. log->set_deselect_on_focus_loss_enabled(false);
  447. log->connect("meta_clicked", callable_mp(this, &EditorLog::_meta_clicked));
  448. vb_left->add_child(log);
  449. // Search box
  450. search_box = memnew(LineEdit);
  451. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  452. search_box->set_placeholder(TTR("Filter Messages"));
  453. search_box->set_accessibility_name(TTRC("Filter Messages"));
  454. search_box->set_clear_button_enabled(true);
  455. search_box->set_visible(true);
  456. search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorLog::_search_changed));
  457. vb_left->add_child(search_box);
  458. VBoxContainer *vb_right = memnew(VBoxContainer);
  459. hb->add_child(vb_right);
  460. // Tools grid
  461. HBoxContainer *hb_tools = memnew(HBoxContainer);
  462. hb_tools->set_h_size_flags(SIZE_SHRINK_CENTER);
  463. vb_right->add_child(hb_tools);
  464. // Clear.
  465. clear_button = memnew(Button);
  466. clear_button->set_accessibility_name(TTRC("Clear Log"));
  467. clear_button->set_theme_type_variation(SceneStringName(FlatButton));
  468. clear_button->set_focus_mode(FOCUS_ACCESSIBILITY);
  469. clear_button->set_shortcut(ED_SHORTCUT("editor/clear_output", TTRC("Clear Output"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::K));
  470. clear_button->connect(SceneStringName(pressed), callable_mp(this, &EditorLog::_clear_request));
  471. hb_tools->add_child(clear_button);
  472. // Copy.
  473. copy_button = memnew(Button);
  474. copy_button->set_accessibility_name(TTRC("Copy Selection"));
  475. copy_button->set_theme_type_variation(SceneStringName(FlatButton));
  476. copy_button->set_focus_mode(FOCUS_ACCESSIBILITY);
  477. copy_button->set_shortcut(ED_SHORTCUT("editor/copy_output", TTRC("Copy Selection"), KeyModifierMask::CMD_OR_CTRL | Key::C));
  478. copy_button->set_shortcut_context(this);
  479. copy_button->connect(SceneStringName(pressed), callable_mp(this, &EditorLog::_copy_request));
  480. hb_tools->add_child(copy_button);
  481. // Separate toggle buttons from normal buttons.
  482. vb_right->add_child(memnew(HSeparator));
  483. // A second hbox to make a 2x2 grid of buttons.
  484. HBoxContainer *hb_tools2 = memnew(HBoxContainer);
  485. hb_tools2->set_h_size_flags(SIZE_SHRINK_CENTER);
  486. vb_right->add_child(hb_tools2);
  487. // Collapse.
  488. collapse_button = memnew(Button);
  489. collapse_button->set_theme_type_variation(SceneStringName(FlatButton));
  490. collapse_button->set_focus_mode(FOCUS_ACCESSIBILITY);
  491. collapse_button->set_tooltip_text(TTR("Collapse duplicate messages into one log entry. Shows number of occurrences."));
  492. collapse_button->set_toggle_mode(true);
  493. collapse_button->set_pressed(false);
  494. collapse_button->connect(SceneStringName(toggled), callable_mp(this, &EditorLog::_set_collapse));
  495. hb_tools2->add_child(collapse_button);
  496. // Show Search.
  497. show_search_button = memnew(Button);
  498. show_search_button->set_accessibility_name(TTRC("Show Search"));
  499. show_search_button->set_theme_type_variation(SceneStringName(FlatButton));
  500. show_search_button->set_focus_mode(FOCUS_ACCESSIBILITY);
  501. show_search_button->set_toggle_mode(true);
  502. show_search_button->set_pressed(true);
  503. show_search_button->set_shortcut(ED_SHORTCUT("editor/open_search", TTRC("Focus Search/Filter Bar"), KeyModifierMask::CMD_OR_CTRL | Key::F));
  504. show_search_button->set_shortcut_context(this);
  505. show_search_button->connect(SceneStringName(toggled), callable_mp(this, &EditorLog::_set_search_visible));
  506. hb_tools2->add_child(show_search_button);
  507. // Message Type Filters.
  508. vb_right->add_child(memnew(HSeparator));
  509. LogFilter *std_filter = memnew(LogFilter(MSG_TYPE_STD));
  510. std_filter->initialize_button(TTRC("Standard Messages"), TTRC("Toggle visibility of standard output messages."), callable_mp(this, &EditorLog::_set_filter_active));
  511. vb_right->add_child(std_filter->toggle_button);
  512. type_filter_map.insert(MSG_TYPE_STD, std_filter);
  513. type_filter_map.insert(MSG_TYPE_STD_RICH, std_filter);
  514. LogFilter *error_filter = memnew(LogFilter(MSG_TYPE_ERROR));
  515. error_filter->initialize_button(TTRC("Errors"), TTRC("Toggle visibility of errors."), callable_mp(this, &EditorLog::_set_filter_active));
  516. vb_right->add_child(error_filter->toggle_button);
  517. type_filter_map.insert(MSG_TYPE_ERROR, error_filter);
  518. LogFilter *warning_filter = memnew(LogFilter(MSG_TYPE_WARNING));
  519. warning_filter->initialize_button(TTRC("Warnings"), TTRC("Toggle visibility of warnings."), callable_mp(this, &EditorLog::_set_filter_active));
  520. vb_right->add_child(warning_filter->toggle_button);
  521. type_filter_map.insert(MSG_TYPE_WARNING, warning_filter);
  522. LogFilter *editor_filter = memnew(LogFilter(MSG_TYPE_EDITOR));
  523. editor_filter->initialize_button(TTRC("Editor Messages"), TTRC("Toggle visibility of editor messages."), callable_mp(this, &EditorLog::_set_filter_active));
  524. vb_right->add_child(editor_filter->toggle_button);
  525. type_filter_map.insert(MSG_TYPE_EDITOR, editor_filter);
  526. add_message(GODOT_VERSION_FULL_NAME " (c) 2007-present Juan Linietsky, Ariel Manzur & Godot Contributors.");
  527. eh.errfunc = _error_handler;
  528. eh.userdata = this;
  529. add_error_handler(&eh);
  530. }
  531. void EditorLog::deinit() {
  532. remove_error_handler(&eh);
  533. }
  534. EditorLog::~EditorLog() {
  535. if (bbcode_parser) {
  536. memdelete(bbcode_parser);
  537. }
  538. for (const KeyValue<MessageType, LogFilter *> &E : type_filter_map) {
  539. // MSG_TYPE_STD_RICH is connected to the std_filter button, so we do this
  540. // to avoid it from being deleted twice, causing a crash on closing.
  541. if (E.key != MSG_TYPE_STD_RICH) {
  542. memdelete(E.value);
  543. }
  544. }
  545. }