build system and module refactoring + simple histogram scratch (#20)
* scratch adding histogram to image views Histograms should come from some sort of histogram service. This is currently just a POC. * custom logger implementation w/o spdlog * missing cmake file * fix tests * use operator<< over direct stream exposure * rm print header * add threading test + refactor towards interface libraries omits the need for =target_include_directories= calls /everywhere/ * rm print header * rm constexpr * templated thread_pool * fix doxyfile * default enable doc building * czi reader refactor * rm erroneous include expression * clang-format * single lib include with PUBLIC visibility * compile imgui stdlib * clang format * documentation update centralize `LogLevelToString` to `ILog.hpp` update docs and examples
This commit is contained in:
committed by
Maximilian Kueffner
parent
b37814204f
commit
c00c2c71ac
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define __PRETTY_FUNCTION__ __FUNCTION__
|
||||
#endif
|
||||
|
||||
namespace pixelarium::utils::log
|
||||
{
|
||||
enum class LogLevel
|
||||
{
|
||||
kTrace = 1 << 0,
|
||||
kDebug = 1 << 1,
|
||||
kInfo = 1 << 2,
|
||||
kWarn = 1 << 3,
|
||||
kError = 1 << 4,
|
||||
};
|
||||
|
||||
constexpr auto LogLevelToString(LogLevel lvl) -> std::string
|
||||
{
|
||||
switch (lvl)
|
||||
{
|
||||
case LogLevel::kTrace:
|
||||
return "Trace";
|
||||
case LogLevel::kDebug:
|
||||
return "Debug";
|
||||
case LogLevel::kInfo:
|
||||
return "Info";
|
||||
case LogLevel::kWarn:
|
||||
return "Warning";
|
||||
case LogLevel::kError:
|
||||
return "Error";
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Interface for logging implementations.
|
||||
class ILog
|
||||
{
|
||||
public:
|
||||
virtual void Info(const std::string& msg) const = 0;
|
||||
virtual void Debug(const std::string& msg) const = 0;
|
||||
virtual void Warn(const std::string& msg) const = 0;
|
||||
virtual void Error(const std::string& msg) const = 0;
|
||||
virtual void ChangeLevel(LogLevel lvl) const = 0;
|
||||
|
||||
virtual ~ILog() {}
|
||||
};
|
||||
|
||||
} // namespace pixelarium::utils::log
|
||||
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#include "ILog.hpp"
|
||||
|
||||
namespace pixelarium::utils::log
|
||||
{
|
||||
class PixelariumLogger : public ILog
|
||||
{
|
||||
private:
|
||||
struct LogStream;
|
||||
|
||||
public:
|
||||
explicit PixelariumLogger(const std::string& name, const std::string& file_sink);
|
||||
~PixelariumLogger();
|
||||
|
||||
void Info(const std::string& msg) const override { this->Write(LogLevel::kInfo, msg); }
|
||||
void Debug(const std::string& msg) const override { this->Write(LogLevel::kDebug, msg); }
|
||||
void Warn(const std::string& msg) const override { this->Write(LogLevel::kWarn, msg); }
|
||||
void Error(const std::string& msg) const override { this->Write(LogLevel::kError, msg); }
|
||||
void ChangeLevel(LogLevel lvl) const override { this->level_ = lvl; }
|
||||
|
||||
private:
|
||||
void Write(LogLevel, const std::string&) const;
|
||||
|
||||
private:
|
||||
std::mutex mutable mutex_;
|
||||
std::string name_;
|
||||
std::string file_sink_;
|
||||
std::unique_ptr<LogStream> log_stream_{};
|
||||
LogLevel mutable level_{LogLevel::kDebug};
|
||||
};
|
||||
} // namespace pixelarium::utils::log
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include <spdlog/common.h>
|
||||
#include <spdlog/sinks/basic_file_sink.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "ILog.hpp"
|
||||
namespace pixelarium::utils::log
|
||||
{
|
||||
/// @brief Implements ILog using the spdlog library
|
||||
/// see https://github.com/gabime/spdlog
|
||||
class SpdLogger : public ILog
|
||||
{
|
||||
public:
|
||||
explicit SpdLogger(const std::string& file_sink, const std::string& name);
|
||||
|
||||
void Info(const std::string& msg) const override;
|
||||
void Debug(const std::string& msg) const override;
|
||||
void Warn(const std::string& msg) const override;
|
||||
void Error(const std::string& msg) const override;
|
||||
void ChangeLevel(LogLevel lvl) const override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<spdlog::logger> logger_;
|
||||
std::string file_;
|
||||
std::string name_;
|
||||
};
|
||||
} // namespace pixelarium::utils::log
|
||||
@@ -0,0 +1,128 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace pixelarium::utils
|
||||
{
|
||||
template <size_t N>
|
||||
class simple_thread_pool
|
||||
{
|
||||
public:
|
||||
simple_thread_pool()
|
||||
{
|
||||
static_assert(N > 0, "Pool must have at least one thread.");
|
||||
for (size_t i{0}; i < N; ++i)
|
||||
{
|
||||
workers_.emplace_back(
|
||||
[this]()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
std::function<void()> job;
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(thread_mutex_);
|
||||
|
||||
cv_.wait(lck, [this]() -> bool { return shutdown_ || !task_queue_.empty(); });
|
||||
|
||||
if (shutdown_ && task_queue_.empty()) return;
|
||||
|
||||
job = std::move(task_queue_.front());
|
||||
task_queue_.pop();
|
||||
++running_tasks_;
|
||||
}
|
||||
|
||||
job();
|
||||
--running_tasks_;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
simple_thread_pool(simple_thread_pool&) = delete;
|
||||
simple_thread_pool(const simple_thread_pool&) = delete;
|
||||
simple_thread_pool(simple_thread_pool&&) = delete;
|
||||
simple_thread_pool& operator=(simple_thread_pool&) = delete;
|
||||
simple_thread_pool& operator=(simple_thread_pool&&) = delete;
|
||||
~simple_thread_pool()
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(thread_mutex_);
|
||||
shutdown_ = true;
|
||||
}
|
||||
|
||||
cv_.notify_all();
|
||||
for (auto& th : workers_)
|
||||
{
|
||||
th.join();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
template <typename Callable>
|
||||
requires std::invocable<Callable>
|
||||
auto enqueue(Callable&& fun) -> void
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(thread_mutex_);
|
||||
task_queue_.emplace(std::forward<Callable>(fun));
|
||||
}
|
||||
|
||||
cv_.notify_one();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
decltype(auto) RunningTasks() const
|
||||
{
|
||||
return running_tasks_.load();
|
||||
}
|
||||
|
||||
decltype(auto) Joinable() const
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(thread_mutex_);
|
||||
|
||||
return task_queue_.empty() && RunningTasks() == 0;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::thread> workers_;
|
||||
std::condition_variable cv_;
|
||||
std::mutex mutable thread_mutex_;
|
||||
std::queue<std::function<void()>> task_queue_;
|
||||
bool shutdown_{false};
|
||||
std::atomic_size_t running_tasks_{0};
|
||||
};
|
||||
|
||||
class pixelarium_pool
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
static decltype(auto) RunningTasks()
|
||||
{
|
||||
return pixelarium_pool::instance().RunningTasks();
|
||||
}
|
||||
|
||||
static decltype(auto) Joinable() { return pixelarium_pool::instance().Joinable(); }
|
||||
|
||||
template <typename Callable>
|
||||
requires std::invocable<Callable>
|
||||
static auto enqueue(Callable&& fun) -> void
|
||||
{
|
||||
pixelarium_pool::instance().enqueue(std::forward<Callable>(fun));
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr auto kThreadCount{20};
|
||||
static simple_thread_pool<kThreadCount>& instance()
|
||||
{
|
||||
static simple_thread_pool<kThreadCount> pixelarium_pool_instance;
|
||||
return pixelarium_pool_instance;
|
||||
}
|
||||
};
|
||||
} // namespace pixelarium::utils
|
||||
Reference in New Issue
Block a user