Skip to content

Commit

Permalink
Use std::format in xlog API
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalh committed Sep 16, 2024
1 parent c1c163e commit 3041cea
Show file tree
Hide file tree
Showing 83 changed files with 567 additions and 485 deletions.
1 change: 0 additions & 1 deletion common/include/common/utils/string-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstdarg>
#include <cctype>

#ifdef __GNUC__
Expand Down
10 changes: 7 additions & 3 deletions common/src/utils/os-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ std::string get_cpu_id()
#else
__cpuid(1, cpu_info[0], cpu_info[1], cpu_info[2], cpu_info[3]);
#endif
return std::format("{:08X} {:08X} {:08X} {:08X}", cpu_info[0], cpu_info[1], cpu_info[2], cpu_info[3]);
return std::format("{:08X} {:08X} {:08X} {:08X}",
static_cast<unsigned>(cpu_info[0]),
static_cast<unsigned>(cpu_info[1]),
static_cast<unsigned>(cpu_info[2]),
static_cast<unsigned>(cpu_info[3]));
}

std::string get_cpu_brand()
Expand Down Expand Up @@ -163,7 +167,7 @@ std::string get_module_pathname(HMODULE module)
for (int i = 0; i < max_attempts; ++i) {
auto num_copied = GetModuleFileNameA(module, buf.data(), buf.size());
if (num_copied == 0) {
xlog::error("GetModuleFileNameA failed (%lu)", GetLastError());
xlog::error("GetModuleFileNameA failed ({})", GetLastError());
return {};
}
if (num_copied < buf.size()) {
Expand All @@ -172,7 +176,7 @@ std::string get_module_pathname(HMODULE module)
}
buf.resize(buf.size() * 2);
}
xlog::error("GetModuleFileNameA is misbehaving or pathname is longer than %u...", buf.size());
xlog::error("GetModuleFileNameA is misbehaving or pathname is longer than {}...", buf.size());
return {};
}

Expand Down
16 changes: 9 additions & 7 deletions crash_handler_stub/crash_handler_stub.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define XLOG_PRINTF

#include "crash_handler_stub.h"
#include "crash_handler_stub/custom_exceptions.h"
#include <xlog/xlog.h>
Expand All @@ -11,10 +13,10 @@ static CrashHandlerConfig g_config;

void CrashHandlerStubProcessException(PEXCEPTION_POINTERS exception_ptrs, DWORD thread_id)
{
xlog::error("Unhandled exception: ExceptionAddress=0x%p ExceptionCode=0x%lX",
xlog::error("Unhandled exception: ExceptionAddress=0x{} ExceptionCode=0x{:x}",
exception_ptrs->ExceptionRecord->ExceptionAddress, exception_ptrs->ExceptionRecord->ExceptionCode);
for (unsigned i = 0; i < exception_ptrs->ExceptionRecord->NumberParameters; ++i)
xlog::error("ExceptionInformation[%d]=0x%lX", i, exception_ptrs->ExceptionRecord->ExceptionInformation[i]);
xlog::error("ExceptionInformation[{}]=0x{:x}", i, exception_ptrs->ExceptionRecord->ExceptionInformation[i]);
xlog::flush();

HANDLE process_handle = nullptr;
Expand All @@ -36,11 +38,11 @@ void CrashHandlerStubProcessException(PEXCEPTION_POINTERS exception_ptrs, DWORD
WCHAR cmd_line[256];
std::swprintf(cmd_line, std::size(cmd_line), L"%ls\\CrashHandler.exe 0x%p 0x%p %lu 0x%p 0x%p", g_module_path,
exception_ptrs, process_handle, thread_id, event_handle, &g_config);
xlog::info("Running crash handler: %ls", cmd_line);
xlog::infof("Running crash handler: %ls", cmd_line);

PROCESS_INFORMATION proc_info;
if (!CreateProcessW(nullptr, cmd_line, nullptr, nullptr, TRUE, 0, nullptr, nullptr, &startup_info, &proc_info)) {
xlog::error("Failed to start CrashHandler process - CreateProcessW %ls failed with error %lu", cmd_line, GetLastError());
xlog::errorf("Failed to start CrashHandler process - CreateProcessW %ls failed with error %lu", cmd_line, GetLastError());
break;
}

Expand All @@ -65,16 +67,16 @@ static LONG WINAPI CrashHandlerExceptionFilter(PEXCEPTION_POINTERS exception_ptr
static void InvalidParameterHandler(const wchar_t* expression, const wchar_t* function, const wchar_t* file,
unsigned line, [[maybe_unused]] uintptr_t reserved)
{
xlog::error("Invalid parameter detected in function %ls. File: %ls Line: %d", function, file, line);
xlog::error("Expression: %ls", expression);
xlog::errorf("Invalid parameter detected in function %ls. File: %ls Line: %d", function, file, line);
xlog::errorf("Expression: %ls", expression);
xlog::flush();
RaiseException(custom_exceptions::invalid_parameter, EXCEPTION_NONCONTINUABLE_EXCEPTION, 0, nullptr);
ExitProcess(0);
}

static void SignalHandler(int signal_number)
{
xlog::error("Abort signal (%d) received!", signal_number);
xlog::error("Abort signal ({}) received!", signal_number);
xlog::flush();
RaiseException(custom_exceptions::abort, EXCEPTION_NONCONTINUABLE_EXCEPTION, 0, nullptr);
ExitProcess(0);
Expand Down
8 changes: 4 additions & 4 deletions editor_patch/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ CodeInjection gr_d3d_line_3d_patch_1{
regs.eip = 0x004E1343;
}
else {
xlog::trace("Line drawing requires gr_d3d_prepare_buffers %d %d %d %d",
xlog::trace("Line drawing requires gr_d3d_prepare_buffers {} {} {} {}",
red::gr_d3d_buffers_locked, red::gr_d3d_primitive_type, red::gr_d3d_max_hw_vertex,
red::gr_d3d_max_hw_index + red::gr_d3d_num_indices);
}
Expand All @@ -135,7 +135,7 @@ CodeInjection gr_d3d_line_2d_patch_1{
regs.eip = 0x004E10C2;
}
else {
xlog::trace("Line drawing requires gr_d3d_prepare_buffers %d %d %d %d",
xlog::trace("Line drawing requires gr_d3d_prepare_buffers {} {} {} {}",
red::gr_d3d_buffers_locked, red::gr_d3d_primitive_type, red::gr_d3d_max_hw_vertex,
red::gr_d3d_max_hw_index + red::gr_d3d_num_indices);
}
Expand Down Expand Up @@ -227,7 +227,7 @@ FunHook<void(red::Matrix3*, red::Vector3*, float, bool, bool)> gr_setup_3d_hook{
horizontal_fov = h_fov_rad / pi * 180.0f;
// Clamp the value to avoid artifacts when view is very stretched
horizontal_fov = std::clamp(horizontal_fov, 60.0f, 120.0f);
//xlog::info("fov %f", horizontal_fov);
//xlog::info("fov {}", horizontal_fov);
}
gr_setup_3d_hook.call_target(viewer_orient, viewer_pos, horizontal_fov, zbuffer_flag, z_scale);
},
Expand All @@ -238,7 +238,7 @@ CodeInjection gr_d3d_init_load_library_injection{
[](auto& regs) {
extern HMODULE g_module;
auto d3d8to9_path = get_module_dir(g_module) + "\\d3d8to9.dll";
xlog::info("Loading d3d8to9.dll: %s", d3d8to9_path.c_str());
xlog::info("Loading d3d8to9.dll: {}", d3d8to9_path);
HMODULE d3d8to9_module = LoadLibraryA(d3d8to9_path.c_str());
if (d3d8to9_module) {
regs.eax = d3d8to9_module;
Expand Down
6 changes: 3 additions & 3 deletions editor_patch/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ CodeInjection CEditorApp_InitInstance_open_level_injection{
static auto& argc = addr_as_ref<int>(0x01DBF8E0);
const char* level_param = nullptr;
for (int i = 1; i < argc; ++i) {
xlog::trace("argv[%d] = %s", i, argv[i]);
xlog::trace("argv[{}] = {}", i, argv[i]);
std::string_view arg = argv[i];
if (arg == "-level") {
++i;
Expand Down Expand Up @@ -369,7 +369,7 @@ void LoadDashEditorPackfile()
std::string old_root_path = root_path;
std::strncpy(root_path, df_dir.c_str(), sizeof(root_path) - 1);
if (!vpackfile_add("dashfaction.vpp", nullptr)) {
xlog::error("Failed to load dashfaction.vpp from %s", df_dir.c_str());
xlog::error("Failed to load dashfaction.vpp from {}", df_dir);
}
std::strncpy(root_path, old_root_path.c_str(), sizeof(root_path) - 1);
}
Expand Down Expand Up @@ -588,7 +588,7 @@ extern "C" DWORD DF_DLL_EXPORT Init([[maybe_unused]] void* unused)

extern "C" void subhook_unk_opcode_handler(uint8_t* opcode)
{
xlog::error("SubHook unknown opcode 0x%X at 0x%p", *opcode, opcode);
xlog::error("SubHook unknown opcode 0x{:x} at {}", *opcode, static_cast<void*>(opcode));
}

BOOL WINAPI DllMain(HINSTANCE instance, [[maybe_unused]] DWORD reason, [[maybe_unused]] LPVOID reserved)
Expand Down
12 changes: 6 additions & 6 deletions game_patch/bmpman/bmpman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int bm_calculate_pitch(int w, rf::bm::Format format)
// 4x4 pixels per block, 128 bits per block
return (w + 3) / 4 * 128 / 8;
default:
xlog::warn("Unknown format %d in bm_calculate_pitch", format);
xlog::warn("Unknown format {} in bm_calculate_pitch", static_cast<int>(format));
return -1;
}
}
Expand Down Expand Up @@ -89,17 +89,17 @@ bm_read_header_hook{
std::string filename_without_ext{get_filename_without_ext(filename)};
auto dds_filename = filename_without_ext + ".dds";
if (dds_file.open(dds_filename.c_str()) == 0) {
xlog::trace("Loading %s", dds_filename.c_str());
xlog::trace("Loading {}", dds_filename);
auto bm_type = read_dds_header(dds_file, width_out, height_out, pixel_fmt_out, num_levels_out);
if (bm_type != rf::bm::TYPE_NONE) {
return bm_type;
}
}

xlog::trace("Loading bitmap header for '%s'", filename);
xlog::trace("Loading bitmap header for '{}'", filename);
auto bm_type = bm_read_header_hook.call_target(filename, width_out, height_out, pixel_fmt_out, num_levels_out,
num_levels_external_mips_out, num_frames_out, fps_out, total_bytes_m2v_out, vbm_ver_out, a11);
xlog::trace("Bitmap header for '%s': type %d size %dx%d pixel_fmt %d levels %d frames %d",
xlog::trace("Bitmap header for '{}': type {} size {}x{} pixel_fmt {} levels {} frames {}",
filename, bm_type, *width_out, *height_out, *pixel_fmt_out, *num_levels_out, *num_frames_out);

// Sanity checks
Expand All @@ -109,7 +109,7 @@ bm_read_header_hook{
}

if (bm_type == rf::bm::TYPE_NONE) {
xlog::warn("Failed load bitmap header for '%s'", filename);
xlog::warn("Failed load bitmap header for '{}'", filename);
}

return bm_type;
Expand Down Expand Up @@ -172,7 +172,7 @@ CodeInjection load_tga_alloc_fail_fix{
unsigned bpp = regs.esi;
auto num_total_pixels = addr_as_ref<size_t>(regs.ebp + 0x30);
auto num_bytes = num_total_pixels * bpp;
xlog::warn("Failed to allocate buffer for a bitmap: %d bytes!", num_bytes);
xlog::warn("Failed to allocate buffer for a bitmap: {} bytes!", num_bytes);
regs.eip = 0x00510944;
}
},
Expand Down
4 changes: 2 additions & 2 deletions game_patch/bmpman/bmpman.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ inline int bm_bytes_per_pixel(rf::bm::Format format)
case rf::bm::FORMAT_8888_ARGB:
return 4;
default:
xlog::warn("Unknown format in bm_bytes_per_pixel: %d", format);
xlog::warn("Unknown format in bm_bytes_per_pixel: {}", static_cast<int>(format));
return 2;
}
}
Expand All @@ -48,7 +48,7 @@ inline int bm_get_bytes_per_compressed_block(rf::bm::Format format)
case rf::bm::FORMAT_DXT5:
return 16;
default:
xlog::error("Unsupported format in bm_get_bytes_per_compressed_block: %d", format);
xlog::error("Unsupported format in bm_get_bytes_per_compressed_block: {}", static_cast<int>(format));
return 0;
}
}
18 changes: 9 additions & 9 deletions game_patch/bmpman/dds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ rf::bm::Type read_dds_header(rf::File& file, int *width_out, int *height_out, rf
{
auto magic = file.read<uint32_t>();
if (magic != DDS_MAGIC) {
xlog::warn("Invalid magic number in DDS file: %X", magic);
xlog::warn("Invalid magic number in DDS file: {:x}", magic);
return rf::bm::TYPE_NONE;
}
DDS_HEADER hdr;
file.read(&hdr, sizeof(hdr));
if (hdr.size != sizeof(DDS_HEADER)) {
xlog::warn("Invalid header size in DDS file: %X", hdr.size);
xlog::warn("Invalid header size in DDS file: {:x}", hdr.size);
return rf::bm::TYPE_NONE;
}
auto format = get_bm_format_from_dds_pixel_format(hdr.ddspf);
Expand All @@ -65,11 +65,11 @@ rf::bm::Type read_dds_header(rf::File& file, int *width_out, int *height_out, rf
}

if (!gr_is_texture_format_supported(format)) {
xlog::warn("Unsupported by video card DDS pixel format: %X", format);
xlog::warn("Unsupported by video card DDS pixel format: {:x}", static_cast<int>(format));
return rf::bm::TYPE_NONE;
}

xlog::trace("Using DDS format 0x%X", format);
xlog::trace("Using DDS format 0x{:x}", format);

*width_out = hdr.width;
*height_out = hdr.height;
Expand All @@ -78,7 +78,7 @@ rf::bm::Type read_dds_header(rf::File& file, int *width_out, int *height_out, rf
*num_levels_out = 1;
if (hdr.flags & DDS_HEADER_FLAGS_MIPMAP) {
*num_levels_out = hdr.mipMapCount;
xlog::trace("DDS mipmaps %u (%ux%u)", hdr.mipMapCount, hdr.width, hdr.height);
xlog::trace("DDS mipmaps {} ({}x{})", hdr.mipMapCount, hdr.width, hdr.height);
}
return rf::bm::TYPE_DDS;
}
Expand All @@ -89,15 +89,15 @@ int lock_dds_bitmap(rf::bm::BitmapEntry& bm_entry)
std::string filename_without_ext{get_filename_without_ext(bm_entry.name)};
auto dds_filename = filename_without_ext + ".dds";

xlog::trace("Locking DDS: %s", dds_filename.c_str());
xlog::trace("Locking DDS: {}", dds_filename);
if (file.open(dds_filename.c_str()) != 0) {
xlog::error("failed to open DDS file: %s", dds_filename.c_str());
xlog::error("failed to open DDS file: {}", dds_filename);
return -1;
}

auto magic = file.read<uint32_t>();
if (magic != DDS_MAGIC) {
xlog::error("Invalid magic number in %s: %x", dds_filename.c_str(), magic);
xlog::error("Invalid magic number in {}: {:x}", dds_filename, magic);
return -1;
}
DDS_HEADER hdr;
Expand Down Expand Up @@ -133,7 +133,7 @@ int lock_dds_bitmap(rf::bm::BitmapEntry& bm_entry)
bm_entry.locked_data = rf::rf_malloc(num_total_bytes);
file.read(bm_entry.locked_data, num_total_bytes);
if (file.error()) {
xlog::error("Unexpected EOF when reading %s", dds_filename.c_str());
xlog::error("Unexpected EOF when reading {}", dds_filename);
return -1;
}
return 0;
Expand Down
2 changes: 1 addition & 1 deletion game_patch/bmpman/fmt_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ bool bm_convert_format(void* dst_bits_ptr, rf::bm::Format dst_fmt, const void* s
return true;
}
catch (const std::exception& e) {
xlog::error("Pixel format conversion failed (%d -> %d): %s", src_fmt, dst_fmt, e.what());
xlog::error("Pixel format conversion failed ({} -> {}): {}", static_cast<int>(src_fmt), static_cast<int>(dst_fmt), e.what());
return false;
}
}
Expand Down
18 changes: 9 additions & 9 deletions game_patch/debug/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ size_t g_max_heap_usage = 0;
FunHook<int(size_t)> callnewh_hook{
0x0057A212,
[](size_t size) {
xlog::error("Failed to allocate %u bytes", size);
xlog::error("Failed to allocate {} bytes", size);
return callnewh_hook.call_target(size);
},
};
Expand All @@ -42,7 +42,7 @@ FunHook<void*(size_t, bool)> nh_malloc_hook{
g_max_heap_usage = std::max(g_max_heap_usage, g_current_heap_usage);
void* ptr = nh_malloc_hook.call_target(size + 12, unk);
if (!ptr) {
xlog::trace("Allocation of %u bytes failed!", size);
xlog::trace("Allocation of {} bytes failed!", size);
return nullptr;
}
auto bytes = static_cast<std::byte*>(ptr);
Expand All @@ -52,15 +52,15 @@ FunHook<void*(size_t, bool)> nh_malloc_hook{
// Overwrite old data to make detecting use-after-free errors easier
std::memset(bytes + 8, 0xCC, size);
auto result = bytes + 8;
xlog::trace("nh_malloc %x -> %p", size, result);
xlog::trace("nh_malloc {:x} -> {}", size, result);
return result;
},
};

FunHook<void(void*)> free_hook{
0x00573C71,
[](void* ptr) {
xlog::trace("free %p", ptr);
xlog::trace("free {}", ptr);
if (!ptr) {
return;
}
Expand All @@ -71,10 +71,10 @@ FunHook<void(void*)> free_hook{
auto tail_marker = *reinterpret_cast<uint32_t*>(bytes + 8 + size);
g_current_heap_usage -= size;
if (front_marker != BOUND_MARKER) {
xlog::warn("Memory corruption detected: front marker %x", front_marker);
xlog::warn("Memory corruption detected: front marker {:x}", front_marker);
}
if (tail_marker != BOUND_MARKER) {
xlog::warn("Memory corruption detected: tail marker %x", tail_marker);
xlog::warn("Memory corruption detected: tail marker {:x}", tail_marker);
}
// Overwrite old data to make detecting use-after-free errors easier
std::memset(bytes + 8, 0xCC, size);
Expand All @@ -94,15 +94,15 @@ FunHook<void*(void*, size_t)> realloc_hook{
auto out_ptr = realloc_hook.call_target(bytes, size + 12);
auto out_bytes = reinterpret_cast<std::byte*>(out_ptr);
out_ptr = out_bytes + 8;
xlog::trace("realloc %p %x -> %p", ptr, size, out_ptr);
xlog::trace("realloc {} {:x} -> {}", ptr, size, out_ptr);
return out_ptr;
},
};

FunHook<size_t(void*)> msize_hook{
0x00578BA2,
[](void* ptr) {
xlog::trace("msize %p", ptr);
xlog::trace("msize {}", ptr);
ptr = static_cast<std::byte*>(ptr) - 8;
return msize_hook.call_target(ptr) - 12;
},
Expand All @@ -127,7 +127,7 @@ CodeInjection VArray_Ptr__get_out_of_bounds_check{
int size = *static_cast<int*>(regs.ecx);
int index = *reinterpret_cast<int*>(regs.esp + 4);
if (index < 0 || index >= size) {
ERR("VArray out of bounds access! index %d size %d", index, size);
xlog::error("VArray out of bounds access! index {} size {}", index, size);
}
},
};
Expand Down
6 changes: 3 additions & 3 deletions game_patch/graphics/d3d11/gr_d3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ namespace df::gr::d3d11

init_error(device_);

xlog::info("D3D11 feature level: 0x%x", feature_level_supported);
xlog::info("D3D11 feature level: 0x{:x}", static_cast<int>(feature_level_supported));
}

void Renderer::init_swap_chain(HWND hwnd)
Expand Down Expand Up @@ -186,7 +186,7 @@ namespace df::gr::d3d11
else {
sc_desc1.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
}
xlog::info("D3D11 swap effect: %d", sc_desc1.SwapEffect);
xlog::info("D3D11 swap effect: {}", static_cast<int>(sc_desc1.SwapEffect));
sc_desc1.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;

DXGI_SWAP_CHAIN_FULLSCREEN_DESC sc_fs_desc;
Expand Down Expand Up @@ -321,7 +321,7 @@ namespace df::gr::d3d11
if (msaa_render_target_) {
context_->ResolveSubresource(back_buffer_, 0, msaa_render_target_, 0, swap_chain_format);
}
xlog::trace("Presenting frame %d", rf::frame_count);
xlog::trace("Presenting frame {}", rf::frame_count);
UINT sync_interval = g_game_config.vsync ? 1 : 0;
DF_GR_D3D11_CHECK_HR(
swap_chain_->Present(sync_interval, 0)
Expand Down
Loading

0 comments on commit 3041cea

Please sign in to comment.