editor_log.cpp 16 KB

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