aboutsummaryrefslogtreecommitdiff
path: root/src/shared/shared.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/shared.hh')
-rw-r--r--src/shared/shared.hh123
1 files changed, 68 insertions, 55 deletions
diff --git a/src/shared/shared.hh b/src/shared/shared.hh
index 2d64814..f070425 100644
--- a/src/shared/shared.hh
+++ b/src/shared/shared.hh
@@ -4,34 +4,51 @@
#include <algorithm>
#include <atomic>
#include <chrono>
+#include <cstdlib>
+#include <execinfo.h>
#include <fstream>
+#include <getopt.h>
#include <iomanip>
#include <iostream>
#include <mutex>
+#include <optional>
#include <ranges>
+#include <signal.h>
#include <string>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
-#include "shared/math.hh"
+#include "shared/math/math.hh"
+#include "shared/net/proto.hh"
-// Tiny thread safe printing.
namespace shared {
+using time_point_t = decltype(std::chrono::steady_clock::now());
+using time_duration_t = decltype(std::chrono::steady_clock::now() -
+ std::chrono::steady_clock::now());
+float get_duration_seconds(const time_duration_t& duration) noexcept;
+
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;
+
+[[nodiscard]] std::string compress_string(const std::string& str);
+[[nodiscard]] std::optional<std::string>
+maybe_decompress_string(const 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:
+using tick_t = std::uint32_t;
+
+// Thread safe printing - this MESS will be cleaned up when we have std::print.
+namespace print {
+class print_base {
+protected:
static inline std::mutex lock;
public:
@@ -54,65 +71,61 @@ public:
bright_cyan = 96,
bright_white = 97
};
+ static std::string make_colour(const colour clr) noexcept {
+ return {"\033[" + std::to_string(static_cast<int>(clr)) + "m"};
+ }
-private:
- static void print_message(const std::string_view message,
- const bool print_time,
- const std::string_view colour,
+protected:
+ static void print_message(std::ostream& os, const std::string_view colour,
const bool cerr = false) noexcept {
const std::lock_guard<std::mutex> 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<int>(clr)) + "m"};
+ static const std::string none = make_colour(colour::none);
+ (cerr ? std::cerr : std::cout) << colour << os.rdbuf() << none;
}
+};
+class print_time { // operator<< returns time as stringstream
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);
+ friend std::ostream& operator<<(std::ostream& os,
+ const print_time&) noexcept {
+ const auto epoch = std::time(nullptr);
+ struct tm* local = localtime(&epoch);
+
+ using pb = print_base;
+ static const std::string white = pb::make_colour(pb::colour::white);
+ static const std::string none = pb::make_colour(pb::colour::none);
+ os << white << std::put_time(local, "[%F %T] ") << none;
+ return os;
}
- 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));
+};
+
+class print_colour : public print_base {
+private:
+ enum print_base::colour colour;
+ bool cerr;
+
+public:
+ print_colour(const decltype(colour) colour,
+ const decltype(cerr) cerr) noexcept
+ : colour(colour), cerr(cerr) {}
+
+ template <typename T>
+ print_colour& operator<<(const T& obj) noexcept {
+ std::stringstream ss{};
+ ss << obj;
+ print_base::print_message(ss, make_colour(this->colour), this->cerr);
+ return *this;
}
};
+inline auto time = print_time{};
+inline print_colour message{print_base::colour::white, false};
+inline print_colour notify{print_base::colour::cyan, false};
+inline print_colour warn{print_base::colour::bright_yellow, true};
+inline print_colour fault{print_base::colour::bright_red, true};
+inline print_colour debug{print_base::colour::magenta, true};
+} // namespace print
+
inline std::atomic<bool> should_exit = false;
}; // namespace shared