print_string.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. static PrintHandlerList *print_handler_list = nullptr;
  34. static thread_local bool is_printing = false;
  35. static void __print_fallback(const String &p_string, bool p_err) {
  36. fprintf(p_err ? stderr : stdout, "While attempting to print a message, another message was printed:\n%s\n", p_string.utf8().get_data());
  37. }
  38. void add_print_handler(PrintHandlerList *p_handler) {
  39. _global_lock();
  40. p_handler->next = print_handler_list;
  41. print_handler_list = p_handler;
  42. _global_unlock();
  43. }
  44. void remove_print_handler(const PrintHandlerList *p_handler) {
  45. _global_lock();
  46. PrintHandlerList *prev = nullptr;
  47. PrintHandlerList *l = print_handler_list;
  48. while (l) {
  49. if (l == p_handler) {
  50. if (prev) {
  51. prev->next = l->next;
  52. } else {
  53. print_handler_list = l->next;
  54. }
  55. break;
  56. }
  57. prev = l;
  58. l = l->next;
  59. }
  60. //OS::get_singleton()->print("print handler list is %p\n",print_handler_list);
  61. _global_unlock();
  62. ERR_FAIL_NULL(l);
  63. }
  64. void __print_line(const String &p_string) {
  65. if (!CoreGlobals::print_line_enabled) {
  66. return;
  67. }
  68. if (is_printing) {
  69. __print_fallback(p_string, false);
  70. return;
  71. }
  72. is_printing = true;
  73. OS::get_singleton()->print("%s\n", p_string.utf8().get_data());
  74. _global_lock();
  75. PrintHandlerList *l = print_handler_list;
  76. while (l) {
  77. l->printfunc(l->userdata, p_string, false, false);
  78. l = l->next;
  79. }
  80. _global_unlock();
  81. is_printing = false;
  82. }
  83. void __print_line_rich(const String &p_string) {
  84. if (!CoreGlobals::print_line_enabled) {
  85. return;
  86. }
  87. // Convert a subset of BBCode tags to ANSI escape codes for correct display in the terminal.
  88. // Support of those ANSI escape codes varies across terminal emulators,
  89. // especially for italic and strikethrough.
  90. String output;
  91. int pos = 0;
  92. while (pos <= p_string.length()) {
  93. int brk_pos = p_string.find_char('[', pos);
  94. if (brk_pos < 0) {
  95. brk_pos = p_string.length();
  96. }
  97. String txt = brk_pos > pos ? p_string.substr(pos, brk_pos - pos) : "";
  98. if (brk_pos == p_string.length()) {
  99. output += txt;
  100. break;
  101. }
  102. int brk_end = p_string.find_char(']', brk_pos + 1);
  103. if (brk_end == -1) {
  104. txt += p_string.substr(brk_pos);
  105. output += txt;
  106. break;
  107. }
  108. pos = brk_end + 1;
  109. output += txt;
  110. String tag = p_string.substr(brk_pos + 1, brk_end - brk_pos - 1);
  111. if (tag == "b") {
  112. output += "\u001b[1m";
  113. } else if (tag == "/b") {
  114. output += "\u001b[22m";
  115. } else if (tag == "i") {
  116. output += "\u001b[3m";
  117. } else if (tag == "/i") {
  118. output += "\u001b[23m";
  119. } else if (tag == "u") {
  120. output += "\u001b[4m";
  121. } else if (tag == "/u") {
  122. output += "\u001b[24m";
  123. } else if (tag == "s") {
  124. output += "\u001b[9m";
  125. } else if (tag == "/s") {
  126. output += "\u001b[29m";
  127. } else if (tag == "indent") {
  128. output += " ";
  129. } else if (tag == "/indent") {
  130. output += "";
  131. } else if (tag == "code") {
  132. output += "\u001b[2m";
  133. } else if (tag == "/code") {
  134. output += "\u001b[22m";
  135. } else if (tag == "url") {
  136. output += "";
  137. } else if (tag == "/url") {
  138. output += "";
  139. } else if (tag == "center") {
  140. output += "\n\t\t\t";
  141. } else if (tag == "/center") {
  142. output += "";
  143. } else if (tag == "right") {
  144. output += "\n\t\t\t\t\t\t";
  145. } else if (tag == "/right") {
  146. output += "";
  147. } else if (tag.begins_with("color=")) {
  148. String color_name = tag.trim_prefix("color=");
  149. if (color_name == "black") {
  150. output += "\u001b[30m";
  151. } else if (color_name == "red") {
  152. output += "\u001b[91m";
  153. } else if (color_name == "green") {
  154. output += "\u001b[92m";
  155. } else if (color_name == "lime") {
  156. output += "\u001b[92m";
  157. } else if (color_name == "yellow") {
  158. output += "\u001b[93m";
  159. } else if (color_name == "blue") {
  160. output += "\u001b[94m";
  161. } else if (color_name == "magenta") {
  162. output += "\u001b[95m";
  163. } else if (color_name == "pink") {
  164. output += "\u001b[38;5;218m";
  165. } else if (color_name == "purple") {
  166. output += "\u001b[38;5;98m";
  167. } else if (color_name == "cyan") {
  168. output += "\u001b[96m";
  169. } else if (color_name == "white") {
  170. output += "\u001b[97m";
  171. } else if (color_name == "orange") {
  172. output += "\u001b[38;5;208m";
  173. } else if (color_name == "gray") {
  174. output += "\u001b[90m";
  175. } else {
  176. Color c = Color::from_string(color_name, Color());
  177. output += vformat("\u001b[38;2;%d;%d;%dm", c.r * 255, c.g * 255, c.b * 255);
  178. }
  179. } else if (tag == "/color") {
  180. output += "\u001b[39m";
  181. } else if (tag.begins_with("bgcolor=")) {
  182. String color_name = tag.trim_prefix("bgcolor=");
  183. if (color_name == "black") {
  184. output += "\u001b[40m";
  185. } else if (color_name == "red") {
  186. output += "\u001b[101m";
  187. } else if (color_name == "green") {
  188. output += "\u001b[102m";
  189. } else if (color_name == "lime") {
  190. output += "\u001b[102m";
  191. } else if (color_name == "yellow") {
  192. output += "\u001b[103m";
  193. } else if (color_name == "blue") {
  194. output += "\u001b[104m";
  195. } else if (color_name == "magenta") {
  196. output += "\u001b[105m";
  197. } else if (color_name == "pink") {
  198. output += "\u001b[48;5;218m";
  199. } else if (color_name == "purple") {
  200. output += "\u001b[48;5;98m";
  201. } else if (color_name == "cyan") {
  202. output += "\u001b[106m";
  203. } else if (color_name == "white") {
  204. output += "\u001b[107m";
  205. } else if (color_name == "orange") {
  206. output += "\u001b[48;5;208m";
  207. } else if (color_name == "gray") {
  208. output += "\u001b[100m";
  209. } else {
  210. Color c = Color::from_string(color_name, Color());
  211. output += vformat("\u001b[48;2;%d;%d;%dm", c.r * 255, c.g * 255, c.b * 255);
  212. }
  213. } else if (tag == "/bgcolor") {
  214. output += "\u001b[49m";
  215. } else if (tag.begins_with("fgcolor=")) {
  216. String color_name = tag.trim_prefix("fgcolor=");
  217. if (color_name == "black") {
  218. output += "\u001b[30;40m";
  219. } else if (color_name == "red") {
  220. output += "\u001b[91;101m";
  221. } else if (color_name == "green") {
  222. output += "\u001b[92;102m";
  223. } else if (color_name == "lime") {
  224. output += "\u001b[92;102m";
  225. } else if (color_name == "yellow") {
  226. output += "\u001b[93;103m";
  227. } else if (color_name == "blue") {
  228. output += "\u001b[94;104m";
  229. } else if (color_name == "magenta") {
  230. output += "\u001b[95;105m";
  231. } else if (color_name == "pink") {
  232. output += "\u001b[38;5;218;48;5;218m";
  233. } else if (color_name == "purple") {
  234. output += "\u001b[38;5;98;48;5;98m";
  235. } else if (color_name == "cyan") {
  236. output += "\u001b[96;106m";
  237. } else if (color_name == "white") {
  238. output += "\u001b[97;107m";
  239. } else if (color_name == "orange") {
  240. output += "\u001b[38;5;208;48;5;208m";
  241. } else if (color_name == "gray") {
  242. output += "\u001b[90;100m";
  243. } else {
  244. Color c = Color::from_string(color_name, Color());
  245. output += vformat("\u001b[38;2;%d;%d;%d;48;2;%d;%d;%dm", c.r * 255, c.g * 255, c.b * 255, c.r * 255, c.g * 255, c.b * 255);
  246. }
  247. } else if (tag == "/fgcolor") {
  248. output += "\u001b[39;49m";
  249. } else {
  250. output += "[";
  251. pos = brk_pos + 1;
  252. }
  253. }
  254. output += "\u001b[0m"; // Reset.
  255. if (is_printing) {
  256. __print_fallback(output, false);
  257. return;
  258. }
  259. is_printing = true;
  260. OS::get_singleton()->print_rich("%s\n", output.utf8().get_data());
  261. _global_lock();
  262. PrintHandlerList *l = print_handler_list;
  263. while (l) {
  264. l->printfunc(l->userdata, p_string, false, true);
  265. l = l->next;
  266. }
  267. _global_unlock();
  268. is_printing = false;
  269. }
  270. void print_error(const String &p_string) {
  271. if (!CoreGlobals::print_error_enabled) {
  272. return;
  273. }
  274. if (is_printing) {
  275. __print_fallback(p_string, true);
  276. return;
  277. }
  278. is_printing = true;
  279. OS::get_singleton()->printerr("%s\n", p_string.utf8().get_data());
  280. _global_lock();
  281. PrintHandlerList *l = print_handler_list;
  282. while (l) {
  283. l->printfunc(l->userdata, p_string, true, false);
  284. l = l->next;
  285. }
  286. _global_unlock();
  287. is_printing = false;
  288. }
  289. bool is_print_verbose_enabled() {
  290. return OS::get_singleton()->is_stdout_verbose();
  291. }
  292. String stringify_variants(const Variant &p_var) {
  293. return p_var.operator String();
  294. }