os.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /**************************************************************************/
  2. /* os.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 "os.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/io/dir_access.h"
  34. #include "core/io/file_access.h"
  35. #include "core/io/json.h"
  36. #include "core/os/midi_driver.h"
  37. #include "core/version_generated.gen.h"
  38. #include <stdarg.h>
  39. #ifdef MINGW_ENABLED
  40. #define MINGW_STDTHREAD_REDUNDANCY_WARNING
  41. #include "thirdparty/mingw-std-threads/mingw.thread.h"
  42. #define THREADING_NAMESPACE mingw_stdthread
  43. #else
  44. #include <thread>
  45. #define THREADING_NAMESPACE std
  46. #endif
  47. OS *OS::singleton = nullptr;
  48. uint64_t OS::target_ticks = 0;
  49. OS *OS::get_singleton() {
  50. return singleton;
  51. }
  52. uint64_t OS::get_ticks_msec() const {
  53. return get_ticks_usec() / 1000ULL;
  54. }
  55. double OS::get_unix_time() const {
  56. return 0;
  57. }
  58. void OS::_set_logger(CompositeLogger *p_logger) {
  59. if (_logger) {
  60. memdelete(_logger);
  61. }
  62. _logger = p_logger;
  63. }
  64. void OS::add_logger(Logger *p_logger) {
  65. if (!_logger) {
  66. Vector<Logger *> loggers;
  67. loggers.push_back(p_logger);
  68. _logger = memnew(CompositeLogger(loggers));
  69. } else {
  70. _logger->add_logger(p_logger);
  71. }
  72. }
  73. String OS::get_identifier() const {
  74. return get_name().to_lower();
  75. }
  76. void OS::print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify, Logger::ErrorType p_type) {
  77. if (!_stderr_enabled) {
  78. return;
  79. }
  80. if (_logger) {
  81. _logger->log_error(p_function, p_file, p_line, p_code, p_rationale, p_editor_notify, p_type);
  82. }
  83. }
  84. void OS::print(const char *p_format, ...) {
  85. if (!_stdout_enabled) {
  86. return;
  87. }
  88. va_list argp;
  89. va_start(argp, p_format);
  90. if (_logger) {
  91. _logger->logv(p_format, argp, false);
  92. }
  93. va_end(argp);
  94. }
  95. void OS::print_rich(const char *p_format, ...) {
  96. if (!_stdout_enabled) {
  97. return;
  98. }
  99. va_list argp;
  100. va_start(argp, p_format);
  101. if (_logger) {
  102. _logger->logv(p_format, argp, false);
  103. }
  104. va_end(argp);
  105. }
  106. void OS::printerr(const char *p_format, ...) {
  107. if (!_stderr_enabled) {
  108. return;
  109. }
  110. va_list argp;
  111. va_start(argp, p_format);
  112. if (_logger) {
  113. _logger->logv(p_format, argp, true);
  114. }
  115. va_end(argp);
  116. }
  117. void OS::alert(const String &p_alert, const String &p_title) {
  118. fprintf(stderr, "%s: %s\n", p_title.utf8().get_data(), p_alert.utf8().get_data());
  119. }
  120. void OS::set_low_processor_usage_mode(bool p_enabled) {
  121. low_processor_usage_mode = p_enabled;
  122. }
  123. bool OS::is_in_low_processor_usage_mode() const {
  124. return low_processor_usage_mode;
  125. }
  126. void OS::set_low_processor_usage_mode_sleep_usec(int p_usec) {
  127. low_processor_usage_mode_sleep_usec = p_usec;
  128. }
  129. int OS::get_low_processor_usage_mode_sleep_usec() const {
  130. return low_processor_usage_mode_sleep_usec;
  131. }
  132. void OS::set_delta_smoothing(bool p_enabled) {
  133. _delta_smoothing_enabled = p_enabled;
  134. }
  135. bool OS::is_delta_smoothing_enabled() const {
  136. return _delta_smoothing_enabled;
  137. }
  138. String OS::get_executable_path() const {
  139. return _execpath;
  140. }
  141. int OS::get_process_id() const {
  142. return -1;
  143. }
  144. bool OS::is_stdout_verbose() const {
  145. return _verbose_stdout;
  146. }
  147. bool OS::is_stdout_debug_enabled() const {
  148. return _debug_stdout;
  149. }
  150. bool OS::is_stdout_enabled() const {
  151. return _stdout_enabled;
  152. }
  153. bool OS::is_stderr_enabled() const {
  154. return _stderr_enabled;
  155. }
  156. void OS::set_stdout_enabled(bool p_enabled) {
  157. _stdout_enabled = p_enabled;
  158. }
  159. void OS::set_stderr_enabled(bool p_enabled) {
  160. _stderr_enabled = p_enabled;
  161. }
  162. int OS::get_exit_code() const {
  163. return _exit_code;
  164. }
  165. void OS::set_exit_code(int p_code) {
  166. _exit_code = p_code;
  167. }
  168. String OS::get_locale() const {
  169. return "en";
  170. }
  171. // Non-virtual helper to extract the 2 or 3-letter language code from
  172. // `get_locale()` in a way that's consistent for all platforms.
  173. String OS::get_locale_language() const {
  174. return get_locale().left(3).replace("_", "");
  175. }
  176. // Embedded PCK offset.
  177. uint64_t OS::get_embedded_pck_offset() const {
  178. return 0;
  179. }
  180. // Helper function to ensure that a dir name/path will be valid on the OS
  181. String OS::get_safe_dir_name(const String &p_dir_name, bool p_allow_paths) const {
  182. String safe_dir_name = p_dir_name;
  183. Vector<String> invalid_chars = String(": * ? \" < > |").split(" ");
  184. if (p_allow_paths) {
  185. // Dir separators are allowed, but disallow ".." to avoid going up the filesystem
  186. invalid_chars.push_back("..");
  187. safe_dir_name = safe_dir_name.replace("\\", "/").strip_edges();
  188. } else {
  189. invalid_chars.push_back("/");
  190. invalid_chars.push_back("\\");
  191. safe_dir_name = safe_dir_name.strip_edges();
  192. // These directory names are invalid.
  193. if (safe_dir_name == ".") {
  194. safe_dir_name = "dot";
  195. } else if (safe_dir_name == "..") {
  196. safe_dir_name = "twodots";
  197. }
  198. }
  199. for (int i = 0; i < invalid_chars.size(); i++) {
  200. safe_dir_name = safe_dir_name.replace(invalid_chars[i], "-");
  201. }
  202. return safe_dir_name;
  203. }
  204. // Path to data, config, cache, etc. OS-specific folders
  205. // Get properly capitalized engine name for system paths
  206. String OS::get_godot_dir_name() const {
  207. // Default to lowercase, so only override when different case is needed
  208. return String(VERSION_SHORT_NAME).to_lower();
  209. }
  210. // OS equivalent of XDG_DATA_HOME
  211. String OS::get_data_path() const {
  212. return ".";
  213. }
  214. // OS equivalent of XDG_CONFIG_HOME
  215. String OS::get_config_path() const {
  216. return ".";
  217. }
  218. // OS equivalent of XDG_CACHE_HOME
  219. String OS::get_cache_path() const {
  220. return ".";
  221. }
  222. // Path to macOS .app bundle resources
  223. String OS::get_bundle_resource_dir() const {
  224. return ".";
  225. }
  226. // Path to macOS .app bundle embedded icon
  227. String OS::get_bundle_icon_path() const {
  228. return String();
  229. }
  230. // OS specific path for user://
  231. String OS::get_user_data_dir() const {
  232. return ".";
  233. }
  234. // Absolute path to res://
  235. String OS::get_resource_dir() const {
  236. return ProjectSettings::get_singleton()->get_resource_path();
  237. }
  238. // Access system-specific dirs like Documents, Downloads, etc.
  239. String OS::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  240. return ".";
  241. }
  242. Error OS::shell_open(const String &p_uri) {
  243. return ERR_UNAVAILABLE;
  244. }
  245. Error OS::shell_show_in_file_manager(String p_path, bool p_open_folder) {
  246. p_path = p_path.trim_prefix("file://");
  247. if (!DirAccess::dir_exists_absolute(p_path)) {
  248. p_path = p_path.get_base_dir();
  249. }
  250. p_path = String("file://") + p_path;
  251. return shell_open(p_path);
  252. }
  253. // implement these with the canvas?
  254. uint64_t OS::get_static_memory_usage() const {
  255. return Memory::get_mem_usage();
  256. }
  257. uint64_t OS::get_static_memory_peak_usage() const {
  258. return Memory::get_mem_max_usage();
  259. }
  260. Error OS::set_cwd(const String &p_cwd) {
  261. return ERR_CANT_OPEN;
  262. }
  263. Dictionary OS::get_memory_info() const {
  264. Dictionary meminfo;
  265. meminfo["physical"] = -1;
  266. meminfo["free"] = -1;
  267. meminfo["available"] = -1;
  268. meminfo["stack"] = -1;
  269. return meminfo;
  270. }
  271. void OS::yield() {
  272. }
  273. void OS::ensure_user_data_dir() {
  274. String dd = get_user_data_dir();
  275. if (DirAccess::exists(dd)) {
  276. return;
  277. }
  278. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  279. Error err = da->make_dir_recursive(dd);
  280. ERR_FAIL_COND_MSG(err != OK, "Error attempting to create data dir: " + dd + ".");
  281. }
  282. String OS::get_model_name() const {
  283. return "GenericDevice";
  284. }
  285. void OS::set_cmdline(const char *p_execpath, const List<String> &p_args, const List<String> &p_user_args) {
  286. _execpath = String::utf8(p_execpath);
  287. _cmdline = p_args;
  288. _user_args = p_user_args;
  289. }
  290. String OS::get_unique_id() const {
  291. return "";
  292. }
  293. int OS::get_processor_count() const {
  294. return THREADING_NAMESPACE::thread::hardware_concurrency();
  295. }
  296. String OS::get_processor_name() const {
  297. return "";
  298. }
  299. void OS::set_has_server_feature_callback(HasServerFeatureCallback p_callback) {
  300. has_server_feature_callback = p_callback;
  301. }
  302. bool OS::has_feature(const String &p_feature) {
  303. // Feature tags are always lowercase for consistency.
  304. if (p_feature == get_identifier()) {
  305. return true;
  306. }
  307. if (p_feature == "movie") {
  308. return _writing_movie;
  309. }
  310. #ifdef DEBUG_ENABLED
  311. if (p_feature == "debug") {
  312. return true;
  313. }
  314. #endif // DEBUG_ENABLED
  315. #ifdef TOOLS_ENABLED
  316. if (p_feature == "editor") {
  317. return true;
  318. }
  319. if (p_feature == "editor_hint") {
  320. return _in_editor;
  321. } else if (p_feature == "editor_runtime") {
  322. return !_in_editor;
  323. }
  324. #else
  325. if (p_feature == "template") {
  326. return true;
  327. }
  328. #ifdef DEBUG_ENABLED
  329. if (p_feature == "template_debug") {
  330. return true;
  331. }
  332. #else
  333. if (p_feature == "template_release" || p_feature == "release") {
  334. return true;
  335. }
  336. #endif // DEBUG_ENABLED
  337. #endif // TOOLS_ENABLED
  338. #ifdef REAL_T_IS_DOUBLE
  339. if (p_feature == "double") {
  340. return true;
  341. }
  342. #else
  343. if (p_feature == "single") {
  344. return true;
  345. }
  346. #endif // REAL_T_IS_DOUBLE
  347. if (sizeof(void *) == 8 && p_feature == "64") {
  348. return true;
  349. }
  350. if (sizeof(void *) == 4 && p_feature == "32") {
  351. return true;
  352. }
  353. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(_M_X64)
  354. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
  355. if (p_feature == "x86_64") {
  356. return true;
  357. }
  358. #elif defined(__i386) || defined(__i386__) || defined(_M_IX86)
  359. if (p_feature == "x86_32") {
  360. return true;
  361. }
  362. #endif
  363. if (p_feature == "x86") {
  364. return true;
  365. }
  366. #elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
  367. #if defined(__aarch64__) || defined(_M_ARM64)
  368. if (p_feature == "arm64") {
  369. return true;
  370. }
  371. #elif defined(__arm__) || defined(_M_ARM)
  372. if (p_feature == "arm32") {
  373. return true;
  374. }
  375. #endif
  376. #if defined(__ARM_ARCH_7A__)
  377. if (p_feature == "armv7a" || p_feature == "armv7") {
  378. return true;
  379. }
  380. #endif
  381. #if defined(__ARM_ARCH_7S__)
  382. if (p_feature == "armv7s" || p_feature == "armv7") {
  383. return true;
  384. }
  385. #endif
  386. if (p_feature == "arm") {
  387. return true;
  388. }
  389. #elif defined(__riscv)
  390. #if __riscv_xlen == 8
  391. if (p_feature == "rv64") {
  392. return true;
  393. }
  394. #endif
  395. if (p_feature == "riscv") {
  396. return true;
  397. }
  398. #elif defined(__powerpc__)
  399. #if defined(__powerpc64__)
  400. if (p_feature == "ppc64") {
  401. return true;
  402. }
  403. #endif
  404. if (p_feature == "ppc") {
  405. return true;
  406. }
  407. #elif defined(__wasm__)
  408. #if defined(__wasm64__)
  409. if (p_feature == "wasm64") {
  410. return true;
  411. }
  412. #elif defined(__wasm32__)
  413. if (p_feature == "wasm32") {
  414. return true;
  415. }
  416. #endif
  417. if (p_feature == "wasm") {
  418. return true;
  419. }
  420. #endif
  421. #if defined(IOS_SIMULATOR)
  422. if (p_feature == "simulator") {
  423. return true;
  424. }
  425. #endif
  426. #ifdef THREADS_ENABLED
  427. if (p_feature == "threads") {
  428. return true;
  429. }
  430. #endif
  431. if (_check_internal_feature_support(p_feature)) {
  432. return true;
  433. }
  434. if (has_server_feature_callback && has_server_feature_callback(p_feature)) {
  435. return true;
  436. }
  437. if (ProjectSettings::get_singleton()->has_custom_feature(p_feature)) {
  438. return true;
  439. }
  440. return false;
  441. }
  442. bool OS::is_sandboxed() const {
  443. return false;
  444. }
  445. void OS::set_restart_on_exit(bool p_restart, const List<String> &p_restart_arguments) {
  446. restart_on_exit = p_restart;
  447. restart_commandline = p_restart_arguments;
  448. }
  449. bool OS::is_restart_on_exit_set() const {
  450. return restart_on_exit;
  451. }
  452. List<String> OS::get_restart_on_exit_arguments() const {
  453. return restart_commandline;
  454. }
  455. PackedStringArray OS::get_connected_midi_inputs() {
  456. if (MIDIDriver::get_singleton()) {
  457. return MIDIDriver::get_singleton()->get_connected_inputs();
  458. }
  459. PackedStringArray list;
  460. ERR_FAIL_V_MSG(list, vformat("MIDI input isn't supported on %s.", OS::get_singleton()->get_name()));
  461. }
  462. void OS::open_midi_inputs() {
  463. if (MIDIDriver::get_singleton()) {
  464. MIDIDriver::get_singleton()->open();
  465. } else {
  466. ERR_PRINT(vformat("MIDI input isn't supported on %s.", OS::get_singleton()->get_name()));
  467. }
  468. }
  469. void OS::close_midi_inputs() {
  470. if (MIDIDriver::get_singleton()) {
  471. MIDIDriver::get_singleton()->close();
  472. } else {
  473. ERR_PRINT(vformat("MIDI input isn't supported on %s.", OS::get_singleton()->get_name()));
  474. }
  475. }
  476. void OS::add_frame_delay(bool p_can_draw) {
  477. const uint32_t frame_delay = Engine::get_singleton()->get_frame_delay();
  478. if (frame_delay) {
  479. // Add fixed frame delay to decrease CPU/GPU usage. This doesn't take
  480. // the actual frame time into account.
  481. // Due to the high fluctuation of the actual sleep duration, it's not recommended
  482. // to use this as a FPS limiter.
  483. delay_usec(frame_delay * 1000);
  484. }
  485. // Add a dynamic frame delay to decrease CPU/GPU usage. This takes the
  486. // previous frame time into account for a smoother result.
  487. uint64_t dynamic_delay = 0;
  488. if (is_in_low_processor_usage_mode() || !p_can_draw) {
  489. dynamic_delay = get_low_processor_usage_mode_sleep_usec();
  490. }
  491. const int max_fps = Engine::get_singleton()->get_max_fps();
  492. if (max_fps > 0 && !Engine::get_singleton()->is_editor_hint()) {
  493. // Override the low processor usage mode sleep delay if the target FPS is lower.
  494. dynamic_delay = MAX(dynamic_delay, (uint64_t)(1000000 / max_fps));
  495. }
  496. if (dynamic_delay > 0) {
  497. target_ticks += dynamic_delay;
  498. uint64_t current_ticks = get_ticks_usec();
  499. if (current_ticks < target_ticks) {
  500. delay_usec(target_ticks - current_ticks);
  501. }
  502. current_ticks = get_ticks_usec();
  503. target_ticks = MIN(MAX(target_ticks, current_ticks - dynamic_delay), current_ticks + dynamic_delay);
  504. }
  505. }
  506. Error OS::setup_remote_filesystem(const String &p_server_host, int p_port, const String &p_password, String &r_project_path) {
  507. return default_rfs.synchronize_with_server(p_server_host, p_port, p_password, r_project_path);
  508. }
  509. OS::PreferredTextureFormat OS::get_preferred_texture_format() const {
  510. #if defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
  511. return PREFERRED_TEXTURE_FORMAT_ETC2_ASTC; // By rule, ARM hardware uses ETC texture compression.
  512. #elif defined(__x86_64__) || defined(_M_X64) || defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
  513. return PREFERRED_TEXTURE_FORMAT_S3TC_BPTC; // By rule, X86 hardware prefers S3TC and derivatives.
  514. #else
  515. return PREFERRED_TEXTURE_FORMAT_S3TC_BPTC; // Override in platform if needed.
  516. #endif
  517. }
  518. void OS::set_use_benchmark(bool p_use_benchmark) {
  519. use_benchmark = p_use_benchmark;
  520. }
  521. bool OS::is_use_benchmark_set() {
  522. return use_benchmark;
  523. }
  524. void OS::set_benchmark_file(const String &p_benchmark_file) {
  525. benchmark_file = p_benchmark_file;
  526. }
  527. String OS::get_benchmark_file() {
  528. return benchmark_file;
  529. }
  530. void OS::benchmark_begin_measure(const String &p_context, const String &p_what) {
  531. #ifdef TOOLS_ENABLED
  532. Pair<String, String> mark_key(p_context, p_what);
  533. ERR_FAIL_COND_MSG(benchmark_marks_from.has(mark_key), vformat("Benchmark key '%s:%s' already exists.", p_context, p_what));
  534. benchmark_marks_from[mark_key] = OS::get_singleton()->get_ticks_usec();
  535. #endif
  536. }
  537. void OS::benchmark_end_measure(const String &p_context, const String &p_what) {
  538. #ifdef TOOLS_ENABLED
  539. Pair<String, String> mark_key(p_context, p_what);
  540. ERR_FAIL_COND_MSG(!benchmark_marks_from.has(mark_key), vformat("Benchmark key '%s:%s' doesn't exist.", p_context, p_what));
  541. uint64_t total = OS::get_singleton()->get_ticks_usec() - benchmark_marks_from[mark_key];
  542. double total_f = double(total) / double(1000000);
  543. benchmark_marks_final[mark_key] = total_f;
  544. #endif
  545. }
  546. void OS::benchmark_dump() {
  547. #ifdef TOOLS_ENABLED
  548. if (!use_benchmark) {
  549. return;
  550. }
  551. if (!benchmark_file.is_empty()) {
  552. Ref<FileAccess> f = FileAccess::open(benchmark_file, FileAccess::WRITE);
  553. if (f.is_valid()) {
  554. Dictionary benchmark_marks;
  555. for (const KeyValue<Pair<String, String>, double> &E : benchmark_marks_final) {
  556. const String mark_key = vformat("[%s] %s", E.key.first, E.key.second);
  557. benchmark_marks[mark_key] = E.value;
  558. }
  559. Ref<JSON> json;
  560. json.instantiate();
  561. f->store_string(json->stringify(benchmark_marks, "\t", false, true));
  562. }
  563. } else {
  564. HashMap<String, String> results;
  565. for (const KeyValue<Pair<String, String>, double> &E : benchmark_marks_final) {
  566. if (E.key.first == "Startup" && !results.has(E.key.first)) {
  567. results.insert(E.key.first, "", true); // Hack to make sure "Startup" always comes first.
  568. }
  569. results[E.key.first] += vformat("\t\t- %s: %.3f msec.\n", E.key.second, (E.value * 1000));
  570. }
  571. print_line("BENCHMARK:");
  572. for (const KeyValue<String, String> &E : results) {
  573. print_line(vformat("\t[%s]\n%s", E.key, E.value));
  574. }
  575. }
  576. #endif
  577. }
  578. OS::OS() {
  579. singleton = this;
  580. Vector<Logger *> loggers;
  581. loggers.push_back(memnew(StdLogger));
  582. _set_logger(memnew(CompositeLogger(loggers)));
  583. }
  584. OS::~OS() {
  585. if (_logger) {
  586. memdelete(_logger);
  587. }
  588. singleton = nullptr;
  589. }