diff --git a/src/include/logger.hpp b/src/include/logger.hpp index cc5a3ab..12bff01 100644 --- a/src/include/logger.hpp +++ b/src/include/logger.hpp @@ -17,8 +17,17 @@ class Logger { public: Logger(): - is_running_(true) + is_running_(false) {} + + ~Logger() { + if (is_running_) + join(); + } + + void start() + { + is_running_ = true; thread_ = std::thread([&]() { while (true) { @@ -36,34 +45,28 @@ public: }); } - ~Logger() - { - if (is_running_) - join(); - } - template - void cerr(Args... args) + void cerr(Args&&... args) { std::unique_lock lock(mutex_); msg_queue_.push(std::bind([](auto... args) { std::cerr << generateTimeFormatString(); ((std::cerr << args), ...); std::cerr << std::endl; - }, std::move(args)...)); + }, std::forward(args)...)); lock.unlock(); cv_.notify_all(); } template - void cout(Args... args) + void cout(Args&&... args) { std::unique_lock lock(mutex_); msg_queue_.push(std::bind([](auto... args) { std::cout << generateTimeFormatString(); ((std::cout << args), ...); std::cout << std::endl; - }, std::move(args)...)); + }, std::forward(args)...)); lock.unlock(); cv_.notify_all(); } diff --git a/src/include/thread_pool.hpp b/src/include/thread_pool.hpp index 8565383..4bf030c 100644 --- a/src/include/thread_pool.hpp +++ b/src/include/thread_pool.hpp @@ -16,8 +16,17 @@ class ThreadPool public: ThreadPool(): thread_num_(std::thread::hardware_concurrency() * 2), - is_running_(true) + is_running_(false) {} + + ~ThreadPool() noexcept { + if (is_running_) + join(); + } + + void start() + { + is_running_ = true; work_threads_ = std::make_unique(thread_num_); for (unsigned int i = 0; i < thread_num_; ++i) { @@ -40,12 +49,6 @@ public: } } - ~ThreadPool() noexcept - { - if (is_running_) - join(); - } - template void submit(Func&& func, Args&&... args) { diff --git a/src/main.cpp b/src/main.cpp index 8598fa9..0c06c71 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,7 +45,7 @@ void processFileTask(fs::path filePath, fs::path outputFolder) } catch (const std::invalid_argument &e) { - logger.cerr(BOLDRED, "[Exception] ", RESET, RED, e.what(), RESET, " '", filePath.u8string(), "'"); + logger.cerr(BOLDRED, "[Exception] ", RESET, RED, std::string(e.what()), RESET, " '", filePath.u8string(), "'"); } catch (...) { @@ -198,6 +198,8 @@ int main(int argc, char **argv) } } } + logger.start(); + thread_pool.start(); if (thread_pool.joinable()) thread_pool.join(); if (logger.joinable()) @@ -226,6 +228,8 @@ int main(int argc, char **argv) processFile(filePathU8, ""); } } + logger.start(); + thread_pool.start(); if (thread_pool.joinable()) thread_pool.join(); if (logger.joinable())