Debian Codegraph @

editor/editor_log.cpp

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