Skip to content

Commit

Permalink
handle chinese logging library fiasco
Browse files Browse the repository at this point in the history
  • Loading branch information
moonshadow565 committed Jul 20, 2023
1 parent 8046bb6 commit 6fe99e8
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 11 deletions.
2 changes: 2 additions & 0 deletions cslol-tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ add_library(cslol-lib STATIC
lib/lol/error.hpp
lib/lol/fs.cpp
lib/lol/fs.hpp
lib/lol/log.hpp
lib/lol/log.cpp

lib/lol/hash/dict.cpp
lib/lol/hash/dict.hpp
Expand Down
6 changes: 3 additions & 3 deletions cslol-tools/dep/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ FetchContent_Declare(
FetchContent_GetProperties(fmtlog)
if(NOT fmtlog_POPULATED)
FetchContent_Populate(fmtlog)
add_library(fmtlog STATIC ${fmtlog_SOURCE_DIR}/fmtlog.cc)
target_include_directories(fmtlog PUBLIC ${fmtlog_SOURCE_DIR})
target_link_libraries(fmtlog PUBLIC fmt)
add_library(fmtlog INTERFACE)
target_include_directories(fmtlog INTERFACE ${fmtlog_SOURCE_DIR})
target_link_libraries(fmtlog INTERFACE fmt)
endif()
11 changes: 11 additions & 0 deletions cslol-tools/lib/lol/common.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#include <chrono>
#include <lol/common.hpp>
#include <thread>

using namespace lol;

#ifdef _WIN32
extern "C" {
extern void Sleep(unsigned ms);
}
void lol::sleep_ms(std::uint32_t ms) { Sleep(ms); }
#else
void lol::sleep_ms(std::uint32_t ms) { std::this_thread::sleep_for(std::chrono::milliseconds{ms}); }
#endif
4 changes: 3 additions & 1 deletion cslol-tools/lib/lol/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
#include <span>
#include <utility>

namespace lol {}
namespace lol {
void sleep_ms(std::uint32_t ms);
}
21 changes: 21 additions & 0 deletions cslol-tools/lib/lol/log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <fmtlog-inl.h>

#include <lol/common.hpp>
#include <lol/log.hpp>

void lol::init_logging_thread() {
fmtlogDetailWrapper<>::impl.stopPollingThread();
fmtlogDetailWrapper<>::impl.threadRunning = true;
fmtlogDetailWrapper<>::impl.thr = std::thread([]() {
while (fmtlogDetailWrapper<>::impl.threadRunning) {
fmtlogDetailWrapper<>::impl.poll(false);
// Why are we using our own polling thread instead of builtin one:
// This chinese logging library uses nanoseconds to poll.
// As it turns out windows is utterly incapable of sleeping below 1ms.
// As a consequence std::this_thread_sleep sleeps by doing a busy wait on high precision system clock.
// This absolutely destroys CPU performance on certain machines as it pins the core to 100%.
lol::sleep_ms(1);
}
fmtlogDetailWrapper<>::impl.poll(true);
});
}
8 changes: 8 additions & 0 deletions cslol-tools/lib/lol/log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
#include <fmtlog.h>

#include <lol/common.hpp>

namespace lol {
void init_logging_thread();
}
2 changes: 1 addition & 1 deletion cslol-tools/lib/lol/patcher/patcher_dummy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ auto patcher::run(std::function<void(Message, char const*)> update,
(void)game_path;
for (;;) {
update(M_WAIT_START, "");
std::this_thread::sleep_for(250ms);
sleep_ms(250);
}
}

Expand Down
2 changes: 1 addition & 1 deletion cslol-tools/lib/lol/patcher/patcher_macos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ auto patcher::run(std::function<void(Message, char const*)> update,
auto pid = Process::FindPid("/LeagueofLegends");
if (!pid) {
update(M_WAIT_START, "");
std::this_thread::sleep_for(10ms);
sleep_ms(10);
continue;
}

Expand Down
6 changes: 4 additions & 2 deletions cslol-tools/lib/lol/patcher/utility/delay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <cstdint>
#include <stdexcept>

#include "lol/common.hpp"

namespace lol {
template <std::size_t S>
struct Intervals {
Expand All @@ -14,7 +16,7 @@ namespace lol {
: intervals{static_cast<std::int64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(intervals).count())...} {}

std::array<std::int64_t, S> intervals{1.0};
std::array<std::int64_t, S> intervals{1};
};

template <typename... T>
Expand All @@ -31,7 +33,7 @@ namespace lol {
if (auto result = poll()) {
return result;
}
std::this_thread::sleep_for(std::chrono::milliseconds{interval});
sleep_ms(interval);
slice -= interval;
}
}
Expand Down
3 changes: 2 additions & 1 deletion cslol-tools/src/main_mod_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <lol/fs.hpp>
#include <lol/hash/dict.hpp>
#include <lol/io/file.hpp>
#include <lol/log.hpp>
#include <lol/patcher/patcher.hpp>
#include <lol/utility/cli.hpp>
#include <lol/utility/zip.hpp>
Expand Down Expand Up @@ -294,7 +295,7 @@ int main(int argc, char** argv) {
utility::set_binary_io();
fmtlog::setHeaderPattern("[{l}] ");
fmtlog::setLogFile(stdout, false);
fmtlog::startPollingThread();
lol::init_logging_thread();
try {
fs::path exe, cmd, src, dst;
auto flags = utility::argv_parse(utility::argv_fix(argc, argv), exe, cmd, src, dst);
Expand Down
3 changes: 2 additions & 1 deletion cslol-tools/src/main_wad_extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <lol/error.hpp>
#include <lol/fs.hpp>
#include <lol/hash/dict.hpp>
#include <lol/log.hpp>
#include <lol/utility/cli.hpp>
#include <lol/wad/index.hpp>

Expand Down Expand Up @@ -43,7 +44,7 @@ int main(int argc, char** argv) {
utility::set_binary_io();
fmtlog::setHeaderPattern("[{l}] ");
fmtlog::setLogFile(stdout, false);
fmtlog::startPollingThread();
lol::init_logging_thread();
try {
fs::path exe, src, dst, hashdict;
auto flags = utility::argv_parse(utility::argv_fix(argc, argv), exe, src, dst, hashdict);
Expand Down
3 changes: 2 additions & 1 deletion cslol-tools/src/main_wad_make.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cstdio>
#include <lol/error.hpp>
#include <lol/fs.hpp>
#include <lol/log.hpp>
#include <lol/utility/cli.hpp>
#include <lol/wad/archive.hpp>

Expand Down Expand Up @@ -35,7 +36,7 @@ int main(int argc, char** argv) {
utility::set_binary_io();
fmtlog::setHeaderPattern("[{l}] ");
fmtlog::setLogFile(stdout, false);
fmtlog::startPollingThread();
lol::init_logging_thread();
try {
fs::path exe, src, dst;
auto flags = utility::argv_parse(utility::argv_fix(argc, argv), exe, src, dst);
Expand Down

0 comments on commit 6fe99e8

Please sign in to comment.