#ifndef SHARED_SHARED_HH_ #define SHARED_SHARED_HH_ #include #include #include #include #include #include #include #include #include #include #include #include #include "shared/math.hh" // Tiny thread safe printing. namespace shared { std::string make_string_lower(std::string str) noexcept; std::string read_file(const std::string& path); void compress_string(std::string& str) noexcept; void decompress_string(std::string& str) noexcept; std::ofstream open_file(const std::string& dir, const std::ios_base::openmode mode = std::ios::trunc); constexpr unsigned long MAX_SAY_LENGTH = 32ul; constexpr unsigned long MAX_USER_PASS_LENGTH = 64; class print { private: static inline std::mutex lock; public: enum class colour { black = 30, red = 31, green = 32, yellow = 33, blue = 34, magenta = 35, cyan = 36, white = 37, none = 39, grey = 90, bright_red = 91, bright_green = 92, bright_yellow = 93, bright_blue = 94, bright_magenta = 95, bright_cyan = 96, bright_white = 97 }; private: static void print_message(const std::string_view message, const bool print_time, const std::string_view colour, const bool cerr = false) noexcept { const std::lock_guard guard(lock); if (print_time) { const auto epoch = std::time(nullptr); struct tm* local = localtime(&epoch); std::cout << std::put_time(local, "[%F %T] "); } static std::string none = make_colour(colour::none); if (cerr) { std::cerr << colour << message << none; return; } std::cout << colour << message << none; } static std::string make_colour(const colour clr) noexcept { return {"\033[" + std::to_string(static_cast(clr)) + "m"}; } public: print() noexcept = delete; static void message(const std::string_view msg, const bool print_time = true) noexcept { static const std::string grey = make_colour(colour::white); print_message(msg, print_time, grey); } static void notify(const std::string_view msg, const bool print_time = true) noexcept { static const std::string cyan = make_colour(colour::cyan); print_message(msg, print_time, cyan); } static void warn(const std::string_view msg, const bool print_time = true) noexcept { static const std::string b_yellow = make_colour(colour::bright_yellow); print_message(msg, print_time, b_yellow); } static void fault(const std::string_view msg, const bool print_time = true) noexcept { static const std::string bright_red = make_colour(colour::bright_red); print_message(msg, print_time, bright_red, true); } static void debug(const std::string_view msg, const bool print_time = true) noexcept { static const std::string magenta = make_colour(colour::magenta); print_message(msg, print_time, magenta, true); } static void custom(const std::string_view msg, const enum colour colour, const bool print_time = true) noexcept { print_message(msg, print_time, make_colour(colour)); } }; inline std::atomic should_exit = false; }; // namespace shared #endif