|
| 1 | +// Copyright (c) 2025 Vitaly Anasenko |
| 2 | +// Distributed under the MIT License, see accompanying file LICENSE.txt |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#ifndef _DEBUG |
| 7 | +# undef ENABLE_MEMORY_PROFILING |
| 8 | +#endif |
| 9 | + |
| 10 | +#ifdef _WIN32 |
| 11 | +# ifndef WIN32_LEAN_AND_MEAN |
| 12 | +# error Requires WIN32_LEAN_AND_MEAN definition |
| 13 | +# endif |
| 14 | +# ifndef NOMINMAX |
| 15 | +# error Requires NOMINMAX definition |
| 16 | +# endif |
| 17 | +# include <windows.h> |
| 18 | +# include <io.h> |
| 19 | +# include <fcntl.h> |
| 20 | +# include <clocale> |
| 21 | +# if defined(ENABLE_MEMORY_PROFILING) && defined(_DEBUG) && (defined(_MSC_VER) || defined(__clang__)) |
| 22 | +# include <crtdbg.h> |
| 23 | +# include <iostream> |
| 24 | +# else |
| 25 | +# undef ENABLE_MEMORY_PROFILING |
| 26 | +# endif |
| 27 | +#else |
| 28 | +# undef ENABLE_MEMORY_PROFILING |
| 29 | +#endif |
| 30 | + |
| 31 | +namespace init { |
| 32 | + inline void console() { |
| 33 | +#ifdef _WIN32 |
| 34 | + ::SetConsoleOutputCP(CP_UTF8); |
| 35 | + ::SetConsoleCP(CP_UTF8); |
| 36 | + ::setlocale(LC_ALL, ".UTF8"); |
| 37 | +#endif |
| 38 | + } |
| 39 | + |
| 40 | + inline void profiler() { |
| 41 | +#ifdef ENABLE_MEMORY_PROFILING |
| 42 | + std::cout << "!! Enabled memory profiling !!" << std::endl; |
| 43 | + constexpr auto report_mode = /*_CRTDBG_MODE_DEBUG |*/ _CRTDBG_MODE_FILE /*| _CRTDBG_MODE_WNDW*/; |
| 44 | + ::_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_LEAK_CHECK_DF); |
| 45 | + ::_CrtSetReportMode(_CRT_ASSERT, report_mode); |
| 46 | + ::_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); |
| 47 | + ::_CrtSetReportMode(_CRT_WARN, report_mode); |
| 48 | + ::_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR); |
| 49 | + ::_CrtSetReportMode(_CRT_ERROR, report_mode); |
| 50 | + ::_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR); |
| 51 | +#endif |
| 52 | + } |
| 53 | +} |
0 commit comments