editor_log.cpp 24 KB

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