hkimi / src /common /base.cpp
Ozomozyan's picture
hkimi 0.1.0
68888be verified
Raw History Blame Contribute Delete
3.46 kB
#include "common/base.h"
#include <chrono>
#include <cmath>
#include <cstdarg>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <mutex>
#include <sstream>
namespace hk {
std::string strfmt(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
va_list ap2;
va_copy(ap2, ap);
int n = vsnprintf(nullptr, 0, fmt, ap);
va_end(ap);
std::string out;
if (n > 0) {
out.resize((size_t)n);
vsnprintf(&out[0], (size_t)n + 1, fmt, ap2);
}
va_end(ap2);
return out;
}
void fail_at(const char* file, int line, const std::string& msg) {
// file name only, build paths are noise in an error message
const char* base = file;
for (const char* p = file; *p; ++p)
if (*p == '/' || *p == '\\') base = p + 1;
throw Error(strfmt("%s (%s:%d)", msg.c_str(), base, line));
}
static LogLevel g_level = LogLevel::Info;
static std::mutex g_log_mutex;
void set_log_level(LogLevel level) { g_level = level; }
LogLevel log_level() { return g_level; }
void logf(LogLevel level, const char* fmt, ...) {
if ((int)level > (int)g_level) return;
static const char* tags[] = {"error", "warn", "info", "debug"};
char buf[2048];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
std::lock_guard<std::mutex> lock(g_log_mutex);
fprintf(stderr, "[%s] %s\n", tags[(int)level], buf);
fflush(stderr);
}
double now_sec() {
using clock = std::chrono::steady_clock;
static const clock::time_point t0 = clock::now();
return std::chrono::duration<double>(clock::now() - t0).count();
}
std::string human_bytes(double b) {
const char* units[] = {"B", "KB", "MB", "GB", "TB"};
int u = 0;
while (b >= 1000.0 && u < 4) {
b /= 1000.0;
++u;
}
return strfmt(u == 0 ? "%.0f %s" : "%.2f %s", b, units[u]);
}
std::string read_text_file(const std::string& path) {
std::ifstream f(std::filesystem::u8path(path), std::ios::binary);
HK_CHECK(f.good(), "cannot open %s", path.c_str());
std::ostringstream ss;
ss << f.rdbuf();
return ss.str();
}
void write_text_file(const std::string& path, const std::string& text) {
std::ofstream f(std::filesystem::u8path(path), std::ios::binary | std::ios::trunc);
HK_CHECK(f.good(), "cannot write %s", path.c_str());
f.write(text.data(), (std::streamsize)text.size());
HK_CHECK(f.good(), "write failed for %s", path.c_str());
}
bool file_exists(const std::string& path) {
std::error_code ec;
return std::filesystem::exists(std::filesystem::u8path(path), ec);
}
void make_dirs(const std::string& path) {
std::error_code ec;
std::filesystem::create_directories(std::filesystem::u8path(path), ec);
HK_CHECK(!ec, "cannot create directory %s: %s", path.c_str(), ec.message().c_str());
}
std::string path_join(const std::string& a, const std::string& b) {
if (a.empty()) return b;
char last = a.back();
if (last == '/' || last == '\\') return a + b;
return a + "/" + b;
}
double Rng::normal() {
double u1 = uniform(), u2 = uniform();
if (u1 < 1e-300) u1 = 1e-300;
return std::sqrt(-2.0 * std::log(u1)) * std::cos(6.283185307179586 * u2);
}
uint64_t fnv1a64(const void* data, size_t n, uint64_t h) {
const uint8_t* p = (const uint8_t*)data;
for (size_t i = 0; i < n; ++i) {
h ^= p[i];
h *= 0x100000001b3ull;
}
return h;
}
} // namespace hk