File size: 3,459 Bytes
68888be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#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