editor_log.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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/editor_node.h"
  35. #include "editor/editor_paths.h"
  36. #include "editor/editor_scale.h"
  37. #include "editor/editor_settings.h"
  38. #include "scene/gui/center_container.h"
  39. #include "scene/gui/separator.h"
  40. #include "scene/resources/font.h"
  41. 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) {
  42. EditorLog *self = static_cast<EditorLog *>(p_self);
  43. if (self->current != Thread::get_caller_id()) {
  44. return;
  45. }
  46. String err_str;
  47. if (p_errorexp && p_errorexp[0]) {
  48. err_str = String::utf8(p_errorexp);
  49. } else {
  50. err_str = String::utf8(p_file) + ":" + itos(p_line) + " - " + String::utf8(p_error);
  51. }
  52. if (p_editor_notify) {
  53. err_str += " (User)";
  54. }
  55. if (p_type == ERR_HANDLER_WARNING) {
  56. self->add_message(err_str, MSG_TYPE_WARNING);
  57. } else {
  58. self->add_message(err_str, MSG_TYPE_ERROR);
  59. }
  60. }
  61. void EditorLog::_update_theme() {
  62. const Ref<Font> normal_font = get_theme_font(SNAME("output_source"), SNAME("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"), SNAME("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"), SNAME("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"), SNAME("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"), SNAME("EditorFonts"));
  79. if (mono_font.is_valid()) {
  80. log->add_theme_font_override("mono_font", mono_font);
  81. }
  82. const int font_size = get_theme_font_size(SNAME("output_source_size"), SNAME("EditorFonts"));
  83. log->add_theme_font_size_override("normal_font_size", font_size);
  84. log->add_theme_font_size_override("bold_font_size", font_size);
  85. log->add_theme_font_size_override("italics_font_size", font_size);
  86. log->add_theme_font_size_override("mono_font_size", font_size);
  87. log->add_theme_color_override("selection_color", get_theme_color(SNAME("accent_color"), SNAME("Editor")) * Color(1, 1, 1, 0.4));
  88. type_filter_map[MSG_TYPE_STD]->toggle_button->set_icon(get_theme_icon(SNAME("Popup"), SNAME("EditorIcons")));
  89. type_filter_map[MSG_TYPE_ERROR]->toggle_button->set_icon(get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons")));
  90. type_filter_map[MSG_TYPE_WARNING]->toggle_button->set_icon(get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons")));
  91. type_filter_map[MSG_TYPE_EDITOR]->toggle_button->set_icon(get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")));
  92. type_filter_map[MSG_TYPE_STD]->toggle_button->set_theme_type_variation("EditorLogFilterButton");
  93. type_filter_map[MSG_TYPE_ERROR]->toggle_button->set_theme_type_variation("EditorLogFilterButton");
  94. type_filter_map[MSG_TYPE_WARNING]->toggle_button->set_theme_type_variation("EditorLogFilterButton");
  95. type_filter_map[MSG_TYPE_EDITOR]->toggle_button->set_theme_type_variation("EditorLogFilterButton");
  96. clear_button->set_icon(get_theme_icon(SNAME("Clear"), SNAME("EditorIcons")));
  97. copy_button->set_icon(get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")));
  98. collapse_button->set_icon(get_theme_icon(SNAME("CombineLines"), SNAME("EditorIcons")));
  99. show_search_button->set_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  100. search_box->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  101. theme_cache.error_color = get_theme_color(SNAME("error_color"), SNAME("Editor"));
  102. theme_cache.error_icon = get_theme_icon(SNAME("Error"), SNAME("EditorIcons"));
  103. theme_cache.warning_color = get_theme_color(SNAME("warning_color"), SNAME("Editor"));
  104. theme_cache.warning_icon = get_theme_icon(SNAME("Warning"), SNAME("EditorIcons"));
  105. theme_cache.message_color = get_theme_color(SNAME("font_color"), SNAME("Editor")) * Color(1, 1, 1, 0.6);
  106. }
  107. void EditorLog::_notification(int p_what) {
  108. switch (p_what) {
  109. case NOTIFICATION_ENTER_TREE: {
  110. _update_theme();
  111. _load_state();
  112. } break;
  113. case NOTIFICATION_THEME_CHANGED: {
  114. _update_theme();
  115. _rebuild_log();
  116. } break;
  117. }
  118. }
  119. void EditorLog::_set_collapse(bool p_collapse) {
  120. collapse = p_collapse;
  121. _start_state_save_timer();
  122. _rebuild_log();
  123. }
  124. void EditorLog::_start_state_save_timer() {
  125. if (!is_loading_state) {
  126. save_state_timer->start();
  127. }
  128. }
  129. void EditorLog::_save_state() {
  130. Ref<ConfigFile> config;
  131. config.instantiate();
  132. // Load and amend existing config if it exists.
  133. config->load(EditorPaths::get_singleton()->get_project_settings_dir().path_join("editor_layout.cfg"));
  134. const String section = "editor_log";
  135. for (const KeyValue<MessageType, LogFilter *> &E : type_filter_map) {
  136. config->set_value(section, "log_filter_" + itos(E.key), E.value->is_active());
  137. }
  138. config->set_value(section, "collapse", collapse);
  139. config->set_value(section, "show_search", search_box->is_visible());
  140. config->save(EditorPaths::get_singleton()->get_project_settings_dir().path_join("editor_layout.cfg"));
  141. }
  142. void EditorLog::_load_state() {
  143. is_loading_state = true;
  144. Ref<ConfigFile> config;
  145. config.instantiate();
  146. config->load(EditorPaths::get_singleton()->get_project_settings_dir().path_join("editor_layout.cfg"));
  147. // 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.
  148. const String section = "editor_log";
  149. for (const KeyValue<MessageType, LogFilter *> &E : type_filter_map) {
  150. E.value->set_active(config->get_value(section, "log_filter_" + itos(E.key), true));
  151. }
  152. collapse = config->get_value(section, "collapse", false);
  153. collapse_button->set_pressed(collapse);
  154. bool show_search = config->get_value(section, "show_search", true);
  155. search_box->set_visible(show_search);
  156. show_search_button->set_pressed(show_search);
  157. is_loading_state = false;
  158. }
  159. void EditorLog::_clear_request() {
  160. log->clear();
  161. messages.clear();
  162. _reset_message_counts();
  163. tool_button->set_icon(Ref<Texture2D>());
  164. }
  165. void EditorLog::_copy_request() {
  166. String text = log->get_selected_text();
  167. if (text.is_empty()) {
  168. text = log->get_parsed_text();
  169. }
  170. if (!text.is_empty()) {
  171. DisplayServer::get_singleton()->clipboard_set(text);
  172. }
  173. }
  174. void EditorLog::clear() {
  175. _clear_request();
  176. }
  177. void EditorLog::_process_message(const String &p_msg, MessageType p_type) {
  178. if (messages.size() > 0 && messages[messages.size() - 1].text == p_msg && messages[messages.size() - 1].type == p_type) {
  179. // If previous message is the same as the new one, increase previous count rather than adding another
  180. // instance to the messages list.
  181. LogMessage &previous = messages.write[messages.size() - 1];
  182. previous.count++;
  183. _add_log_line(previous, collapse);
  184. } else {
  185. // Different message to the previous one received.
  186. LogMessage message(p_msg, p_type);
  187. _add_log_line(message);
  188. messages.push_back(message);
  189. }
  190. type_filter_map[p_type]->set_message_count(type_filter_map[p_type]->get_message_count() + 1);
  191. }
  192. void EditorLog::add_message(const String &p_msg, MessageType p_type) {
  193. // Make text split by new lines their own message.
  194. // See #41321 for reasoning. At time of writing, multiple print()'s in running projects
  195. // get grouped together and sent to the editor log as one message. This can mess with the
  196. // search functionality (see the comments on the PR above for more details). This behavior
  197. // also matches that of other IDE's.
  198. Vector<String> lines = p_msg.split("\n", true);
  199. for (int i = 0; i < lines.size(); i++) {
  200. _process_message(lines[i], p_type);
  201. }
  202. }
  203. void EditorLog::set_tool_button(Button *p_tool_button) {
  204. tool_button = p_tool_button;
  205. }
  206. void EditorLog::register_undo_redo(UndoRedo *p_undo_redo) {
  207. p_undo_redo->set_commit_notify_callback(_undo_redo_cbk, this);
  208. }
  209. void EditorLog::_undo_redo_cbk(void *p_self, const String &p_name) {
  210. EditorLog *self = static_cast<EditorLog *>(p_self);
  211. self->add_message(p_name, EditorLog::MSG_TYPE_EDITOR);
  212. }
  213. void EditorLog::_rebuild_log() {
  214. log->clear();
  215. for (int msg_idx = 0; msg_idx < messages.size(); msg_idx++) {
  216. LogMessage msg = messages[msg_idx];
  217. if (collapse) {
  218. // If collapsing, only log one instance of the message.
  219. _add_log_line(msg);
  220. } else {
  221. // If not collapsing, log each instance on a line.
  222. for (int i = 0; i < msg.count; i++) {
  223. _add_log_line(msg);
  224. }
  225. }
  226. }
  227. }
  228. void EditorLog::_add_log_line(LogMessage &p_message, bool p_replace_previous) {
  229. if (!is_inside_tree()) {
  230. // The log will be built all at once when it enters the tree and has its theme items.
  231. return;
  232. }
  233. if (unlikely(log->is_updating())) {
  234. // The new message arrived during log RTL text processing/redraw (invalid BiDi control characters / font error), ignore it to avoid RTL data corruption.
  235. return;
  236. }
  237. // Only add the message to the log if it passes the filters.
  238. bool filter_active = type_filter_map[p_message.type]->is_active();
  239. String search_text = search_box->get_text();
  240. bool search_match = search_text.is_empty() || p_message.text.findn(search_text) > -1;
  241. if (!filter_active || !search_match) {
  242. return;
  243. }
  244. if (p_replace_previous) {
  245. // Remove last line if replacing, as it will be replace by the next added line.
  246. // 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,
  247. // but it still counts as a line. So if you remove the last line (count - 1) you are actually removing nothing...
  248. log->remove_paragraph(log->get_paragraph_count() - 2);
  249. }
  250. switch (p_message.type) {
  251. case MSG_TYPE_STD: {
  252. } break;
  253. case MSG_TYPE_STD_RICH: {
  254. } break;
  255. case MSG_TYPE_ERROR: {
  256. log->push_color(theme_cache.error_color);
  257. Ref<Texture2D> icon = theme_cache.error_icon;
  258. log->add_image(icon);
  259. log->add_text(" ");
  260. tool_button->set_icon(icon);
  261. } break;
  262. case MSG_TYPE_WARNING: {
  263. log->push_color(theme_cache.warning_color);
  264. Ref<Texture2D> icon = theme_cache.warning_icon;
  265. log->add_image(icon);
  266. log->add_text(" ");
  267. tool_button->set_icon(icon);
  268. } break;
  269. case MSG_TYPE_EDITOR: {
  270. // Distinguish editor messages from messages printed by the project
  271. log->push_color(theme_cache.message_color);
  272. } break;
  273. }
  274. // If collapsing, add the count of this message in bold at the start of the line.
  275. if (collapse && p_message.count > 1) {
  276. log->push_bold();
  277. log->add_text(vformat("(%s) ", itos(p_message.count)));
  278. log->pop();
  279. }
  280. if (p_message.type == MSG_TYPE_STD_RICH) {
  281. log->append_text(p_message.text);
  282. } else {
  283. log->add_text(p_message.text);
  284. }
  285. // Need to use pop() to exit out of the RichTextLabels current "push" stack.
  286. // We only "push" in the above switch when message type != STD and RICH, so only pop when that is the case.
  287. if (p_message.type != MSG_TYPE_STD && p_message.type != MSG_TYPE_STD_RICH) {
  288. log->pop();
  289. }
  290. log->add_newline();
  291. }
  292. void EditorLog::_set_filter_active(bool p_active, MessageType p_message_type) {
  293. type_filter_map[p_message_type]->set_active(p_active);
  294. _start_state_save_timer();
  295. _rebuild_log();
  296. }
  297. void EditorLog::_set_search_visible(bool p_visible) {
  298. search_box->set_visible(p_visible);
  299. if (p_visible) {
  300. search_box->grab_focus();
  301. }
  302. _start_state_save_timer();
  303. }
  304. void EditorLog::_search_changed(const String &p_text) {
  305. _rebuild_log();
  306. }
  307. void EditorLog::_reset_message_counts() {
  308. for (const KeyValue<MessageType, LogFilter *> &E : type_filter_map) {
  309. E.value->set_message_count(0);
  310. }
  311. }
  312. EditorLog::EditorLog() {
  313. save_state_timer = memnew(Timer);
  314. save_state_timer->set_wait_time(2);
  315. save_state_timer->set_one_shot(true);
  316. save_state_timer->connect("timeout", callable_mp(this, &EditorLog::_save_state));
  317. add_child(save_state_timer);
  318. HBoxContainer *hb = this;
  319. VBoxContainer *vb_left = memnew(VBoxContainer);
  320. vb_left->set_custom_minimum_size(Size2(0, 180) * EDSCALE);
  321. vb_left->set_v_size_flags(SIZE_EXPAND_FILL);
  322. vb_left->set_h_size_flags(SIZE_EXPAND_FILL);
  323. hb->add_child(vb_left);
  324. // Log - Rich Text Label.
  325. log = memnew(RichTextLabel);
  326. log->set_use_bbcode(true);
  327. log->set_scroll_follow(true);
  328. log->set_selection_enabled(true);
  329. log->set_focus_mode(FOCUS_CLICK);
  330. log->set_v_size_flags(SIZE_EXPAND_FILL);
  331. log->set_h_size_flags(SIZE_EXPAND_FILL);
  332. log->set_deselect_on_focus_loss_enabled(false);
  333. vb_left->add_child(log);
  334. // Search box
  335. search_box = memnew(LineEdit);
  336. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  337. search_box->set_placeholder(TTR("Filter Messages"));
  338. search_box->set_clear_button_enabled(true);
  339. search_box->set_visible(true);
  340. search_box->connect("text_changed", callable_mp(this, &EditorLog::_search_changed));
  341. vb_left->add_child(search_box);
  342. VBoxContainer *vb_right = memnew(VBoxContainer);
  343. hb->add_child(vb_right);
  344. // Tools grid
  345. HBoxContainer *hb_tools = memnew(HBoxContainer);
  346. hb_tools->set_h_size_flags(SIZE_SHRINK_CENTER);
  347. vb_right->add_child(hb_tools);
  348. // Clear.
  349. clear_button = memnew(Button);
  350. clear_button->set_flat(true);
  351. clear_button->set_focus_mode(FOCUS_NONE);
  352. clear_button->set_shortcut(ED_SHORTCUT("editor/clear_output", TTR("Clear Output"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::K));
  353. clear_button->set_shortcut_context(this);
  354. clear_button->connect("pressed", callable_mp(this, &EditorLog::_clear_request));
  355. hb_tools->add_child(clear_button);
  356. // Copy.
  357. copy_button = memnew(Button);
  358. copy_button->set_flat(true);
  359. copy_button->set_focus_mode(FOCUS_NONE);
  360. copy_button->set_shortcut(ED_SHORTCUT("editor/copy_output", TTR("Copy Selection"), KeyModifierMask::CMD_OR_CTRL | Key::C));
  361. copy_button->set_shortcut_context(this);
  362. copy_button->connect("pressed", callable_mp(this, &EditorLog::_copy_request));
  363. hb_tools->add_child(copy_button);
  364. // A second hbox to make a 2x2 grid of buttons.
  365. HBoxContainer *hb_tools2 = memnew(HBoxContainer);
  366. hb_tools2->set_h_size_flags(SIZE_SHRINK_CENTER);
  367. vb_right->add_child(hb_tools2);
  368. // Collapse.
  369. collapse_button = memnew(Button);
  370. collapse_button->set_flat(true);
  371. collapse_button->set_focus_mode(FOCUS_NONE);
  372. collapse_button->set_tooltip_text(TTR("Collapse duplicate messages into one log entry. Shows number of occurrences."));
  373. collapse_button->set_toggle_mode(true);
  374. collapse_button->set_pressed(false);
  375. collapse_button->connect("toggled", callable_mp(this, &EditorLog::_set_collapse));
  376. hb_tools2->add_child(collapse_button);
  377. // Show Search.
  378. show_search_button = memnew(Button);
  379. show_search_button->set_flat(true);
  380. show_search_button->set_focus_mode(FOCUS_NONE);
  381. show_search_button->set_toggle_mode(true);
  382. show_search_button->set_pressed(true);
  383. show_search_button->set_shortcut(ED_SHORTCUT("editor/open_search", TTR("Focus Search/Filter Bar"), KeyModifierMask::CMD_OR_CTRL | Key::F));
  384. show_search_button->set_shortcut_context(this);
  385. show_search_button->connect("toggled", callable_mp(this, &EditorLog::_set_search_visible));
  386. hb_tools2->add_child(show_search_button);
  387. // Message Type Filters.
  388. vb_right->add_child(memnew(HSeparator));
  389. LogFilter *std_filter = memnew(LogFilter(MSG_TYPE_STD));
  390. std_filter->initialize_button(TTR("Toggle visibility of standard output messages."), callable_mp(this, &EditorLog::_set_filter_active));
  391. vb_right->add_child(std_filter->toggle_button);
  392. type_filter_map.insert(MSG_TYPE_STD, std_filter);
  393. type_filter_map.insert(MSG_TYPE_STD_RICH, std_filter);
  394. LogFilter *error_filter = memnew(LogFilter(MSG_TYPE_ERROR));
  395. error_filter->initialize_button(TTR("Toggle visibility of errors."), callable_mp(this, &EditorLog::_set_filter_active));
  396. vb_right->add_child(error_filter->toggle_button);
  397. type_filter_map.insert(MSG_TYPE_ERROR, error_filter);
  398. LogFilter *warning_filter = memnew(LogFilter(MSG_TYPE_WARNING));
  399. warning_filter->initialize_button(TTR("Toggle visibility of warnings."), callable_mp(this, &EditorLog::_set_filter_active));
  400. vb_right->add_child(warning_filter->toggle_button);
  401. type_filter_map.insert(MSG_TYPE_WARNING, warning_filter);
  402. LogFilter *editor_filter = memnew(LogFilter(MSG_TYPE_EDITOR));
  403. editor_filter->initialize_button(TTR("Toggle visibility of editor messages."), callable_mp(this, &EditorLog::_set_filter_active));
  404. vb_right->add_child(editor_filter->toggle_button);
  405. type_filter_map.insert(MSG_TYPE_EDITOR, editor_filter);
  406. add_message(VERSION_FULL_NAME " (c) 2007-present Juan Linietsky, Ariel Manzur & Godot Contributors.");
  407. eh.errfunc = _error_handler;
  408. eh.userdata = this;
  409. add_error_handler(&eh);
  410. current = Thread::get_caller_id();
  411. }
  412. void EditorLog::deinit() {
  413. remove_error_handler(&eh);
  414. }
  415. EditorLog::~EditorLog() {
  416. for (const KeyValue<MessageType, LogFilter *> &E : type_filter_map) {
  417. // MSG_TYPE_STD_RICH is connected to the std_filter button, so we do this
  418. // to avoid it from being deleted twice, causing a crash on closing.
  419. if (E.key != MSG_TYPE_STD_RICH) {
  420. memdelete(E.value);
  421. }
  422. }
  423. }