print_string.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**************************************************************************/
  2. /* print_string.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 "print_string.h"
  31. #include "core/core_globals.h"
  32. #include "core/os/os.h"
  33. #include <stdio.h>
  34. static PrintHandlerList *print_handler_list = nullptr;
  35. void add_print_handler(PrintHandlerList *p_handler) {
  36. _global_lock();
  37. p_handler->next = print_handler_list;
  38. print_handler_list = p_handler;
  39. _global_unlock();
  40. }
  41. void remove_print_handler(const PrintHandlerList *p_handler) {
  42. _global_lock();
  43. PrintHandlerList *prev = nullptr;
  44. PrintHandlerList *l = print_handler_list;
  45. while (l) {
  46. if (l == p_handler) {
  47. if (prev) {
  48. prev->next = l->next;
  49. } else {
  50. print_handler_list = l->next;
  51. }
  52. break;
  53. }
  54. prev = l;
  55. l = l->next;
  56. }
  57. //OS::get_singleton()->print("print handler list is %p\n",print_handler_list);
  58. _global_unlock();
  59. ERR_FAIL_COND(l == nullptr);
  60. }
  61. void __print_line(String p_string) {
  62. if (!CoreGlobals::print_line_enabled) {
  63. return;
  64. }
  65. OS::get_singleton()->print("%s\n", p_string.utf8().get_data());
  66. _global_lock();
  67. PrintHandlerList *l = print_handler_list;
  68. while (l) {
  69. l->printfunc(l->userdata, p_string, false, false);
  70. l = l->next;
  71. }
  72. _global_unlock();
  73. }
  74. void __print_line_rich(String p_string) {
  75. if (!CoreGlobals::print_line_enabled) {
  76. return;
  77. }
  78. // Convert a subset of BBCode tags to ANSI escape codes for correct display in the terminal.
  79. // Support of those ANSI escape codes varies across terminal emulators,
  80. // especially for italic and strikethrough.
  81. String p_string_ansi = p_string;
  82. p_string_ansi = p_string_ansi.replace("[b]", "\u001b[1m");
  83. p_string_ansi = p_string_ansi.replace("[/b]", "\u001b[22m");
  84. p_string_ansi = p_string_ansi.replace("[i]", "\u001b[3m");
  85. p_string_ansi = p_string_ansi.replace("[/i]", "\u001b[23m");
  86. p_string_ansi = p_string_ansi.replace("[u]", "\u001b[4m");
  87. p_string_ansi = p_string_ansi.replace("[/u]", "\u001b[24m");
  88. p_string_ansi = p_string_ansi.replace("[s]", "\u001b[9m");
  89. p_string_ansi = p_string_ansi.replace("[/s]", "\u001b[29m");
  90. p_string_ansi = p_string_ansi.replace("[indent]", " ");
  91. p_string_ansi = p_string_ansi.replace("[/indent]", "");
  92. p_string_ansi = p_string_ansi.replace("[code]", "\u001b[2m");
  93. p_string_ansi = p_string_ansi.replace("[/code]", "\u001b[22m");
  94. p_string_ansi = p_string_ansi.replace("[url]", "");
  95. p_string_ansi = p_string_ansi.replace("[/url]", "");
  96. p_string_ansi = p_string_ansi.replace("[center]", "\n\t\t\t");
  97. p_string_ansi = p_string_ansi.replace("[/center]", "");
  98. p_string_ansi = p_string_ansi.replace("[right]", "\n\t\t\t\t\t\t");
  99. p_string_ansi = p_string_ansi.replace("[/right]", "");
  100. if (p_string_ansi.contains("[color")) {
  101. p_string_ansi = p_string_ansi.replace("[color=black]", "\u001b[30m");
  102. p_string_ansi = p_string_ansi.replace("[color=red]", "\u001b[91m");
  103. p_string_ansi = p_string_ansi.replace("[color=green]", "\u001b[92m");
  104. p_string_ansi = p_string_ansi.replace("[color=lime]", "\u001b[92m");
  105. p_string_ansi = p_string_ansi.replace("[color=yellow]", "\u001b[93m");
  106. p_string_ansi = p_string_ansi.replace("[color=blue]", "\u001b[94m");
  107. p_string_ansi = p_string_ansi.replace("[color=magenta]", "\u001b[95m");
  108. p_string_ansi = p_string_ansi.replace("[color=pink]", "\u001b[38;5;218m");
  109. p_string_ansi = p_string_ansi.replace("[color=purple]", "\u001b[38;5;98m");
  110. p_string_ansi = p_string_ansi.replace("[color=cyan]", "\u001b[96m");
  111. p_string_ansi = p_string_ansi.replace("[color=white]", "\u001b[97m");
  112. p_string_ansi = p_string_ansi.replace("[color=orange]", "\u001b[38;5;208m");
  113. p_string_ansi = p_string_ansi.replace("[color=gray]", "\u001b[90m");
  114. p_string_ansi = p_string_ansi.replace("[/color]", "\u001b[39m");
  115. }
  116. if (p_string_ansi.contains("[bgcolor")) {
  117. p_string_ansi = p_string_ansi.replace("[bgcolor=black]", "\u001b[40m");
  118. p_string_ansi = p_string_ansi.replace("[bgcolor=red]", "\u001b[101m");
  119. p_string_ansi = p_string_ansi.replace("[bgcolor=green]", "\u001b[102m");
  120. p_string_ansi = p_string_ansi.replace("[bgcolor=lime]", "\u001b[102m");
  121. p_string_ansi = p_string_ansi.replace("[bgcolor=yellow]", "\u001b[103m");
  122. p_string_ansi = p_string_ansi.replace("[bgcolor=blue]", "\u001b[104m");
  123. p_string_ansi = p_string_ansi.replace("[bgcolor=magenta]", "\u001b[105m");
  124. p_string_ansi = p_string_ansi.replace("[bgcolor=pink]", "\u001b[48;5;218m");
  125. p_string_ansi = p_string_ansi.replace("[bgcolor=purple]", "\u001b[48;5;98m");
  126. p_string_ansi = p_string_ansi.replace("[bgcolor=cyan]", "\u001b[106m");
  127. p_string_ansi = p_string_ansi.replace("[bgcolor=white]", "\u001b[107m");
  128. p_string_ansi = p_string_ansi.replace("[bgcolor=orange]", "\u001b[48;5;208m");
  129. p_string_ansi = p_string_ansi.replace("[bgcolor=gray]", "\u001b[100m");
  130. p_string_ansi = p_string_ansi.replace("[/bgcolor]", "\u001b[49m");
  131. }
  132. if (p_string_ansi.contains("[fgcolor")) {
  133. p_string_ansi = p_string_ansi.replace("[fgcolor=black]", "\u001b[30;40m");
  134. p_string_ansi = p_string_ansi.replace("[fgcolor=red]", "\u001b[91;101m");
  135. p_string_ansi = p_string_ansi.replace("[fgcolor=green]", "\u001b[92;102m");
  136. p_string_ansi = p_string_ansi.replace("[fgcolor=lime]", "\u001b[92;102m");
  137. p_string_ansi = p_string_ansi.replace("[fgcolor=yellow]", "\u001b[93;103m");
  138. p_string_ansi = p_string_ansi.replace("[fgcolor=blue]", "\u001b[94;104m");
  139. p_string_ansi = p_string_ansi.replace("[fgcolor=magenta]", "\u001b[95;105m");
  140. p_string_ansi = p_string_ansi.replace("[fgcolor=pink]", "\u001b[38;5;218;48;5;218m");
  141. p_string_ansi = p_string_ansi.replace("[fgcolor=purple]", "\u001b[38;5;98;48;5;98m");
  142. p_string_ansi = p_string_ansi.replace("[fgcolor=cyan]", "\u001b[96;106m");
  143. p_string_ansi = p_string_ansi.replace("[fgcolor=white]", "\u001b[97;107m");
  144. p_string_ansi = p_string_ansi.replace("[fgcolor=orange]", "\u001b[38;5;208;48;5;208m");
  145. p_string_ansi = p_string_ansi.replace("[fgcolor=gray]", "\u001b[90;100m");
  146. p_string_ansi = p_string_ansi.replace("[/fgcolor]", "\u001b[39;49m");
  147. }
  148. OS::get_singleton()->print_rich("%s\n", p_string_ansi.utf8().get_data());
  149. _global_lock();
  150. PrintHandlerList *l = print_handler_list;
  151. while (l) {
  152. l->printfunc(l->userdata, p_string, false, true);
  153. l = l->next;
  154. }
  155. _global_unlock();
  156. }
  157. void print_error(String p_string) {
  158. if (!CoreGlobals::print_error_enabled) {
  159. return;
  160. }
  161. OS::get_singleton()->printerr("%s\n", p_string.utf8().get_data());
  162. _global_lock();
  163. PrintHandlerList *l = print_handler_list;
  164. while (l) {
  165. l->printfunc(l->userdata, p_string, true, false);
  166. l = l->next;
  167. }
  168. _global_unlock();
  169. }
  170. void print_verbose(String p_string) {
  171. if (OS::get_singleton()->is_stdout_verbose()) {
  172. print_line(p_string);
  173. }
  174. }
  175. String stringify_variants(Variant p_var) {
  176. return p_var.operator String();
  177. }