Skip to content

Commit

Permalink
feat: add safer/convenient console log function (#116)
Browse files Browse the repository at this point in the history
- this log function makes use of `std::format` and the benefits that
comes with (compile time checking, type representation, numbered fields,
etc.) compared to the printf style function.

```cpp
auto log = RE::ConsoleLog::GetSingleton();
log->Log("Value: {}", true);
```
```
Value: true
```
  • Loading branch information
qudix authored Sep 28, 2023
1 parent d1eb276 commit 0bafe79
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions CommonLibSF/include/RE/C/ConsoleLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ namespace RE
return *singleton;
}

void VPrint(const char* a_fmt, std::va_list a_args)
{
using func_t = decltype(&ConsoleLog::VPrint);
REL::Relocation<func_t> func{ REL::ID(166358) };
func(this, a_fmt, a_args);
}

// printf format rules, no compile time checking
void Print(const char* a_fmt, ...)
{
std::va_list args;
Expand All @@ -22,11 +30,11 @@ namespace RE
va_end(args);
}

void VPrint(const char* a_fmt, std::va_list a_args)
// std::format rules, compile time checking
template <class... Args>
void Log(const std::format_string<Args...> a_fmt, Args&&... a_args)
{
using func_t = decltype(&ConsoleLog::VPrint);
REL::Relocation<func_t> func{ REL::ID(166358) };
func(this, a_fmt, a_args);
Print(std::vformat(a_fmt.get(), std::make_format_args(a_args...)).c_str());
}
};
}

0 comments on commit 0bafe79

Please sign in to comment.