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