#include <iostream>

#include "engine/util.h"

static int uuid_length = 6;

void Util::log(LogType type, std::string msg, std::string file, int line) {
    FILE *output_stream = stdout;
    if (type == ERROR) {
        output_stream = stderr;
    }

    fprintf(output_stream,
            "%s:%d [%s] %s\n",
            file.c_str(),
            line,
            logTypeToStr(type).c_str(),
            msg.c_str()
    );
}

std::string Util::generateUuid() {
    static const char alphanum[] =
            "0123456789"
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            "abcdefghijklmnopqrstuvwxyz";
    std::string tmp;
    tmp.reserve(uuid_length);

    for (int i = 0; i < uuid_length; ++i) {
        tmp += alphanum[rand() % (sizeof(alphanum) - 1)];
    }

    return tmp;
}

std::string Util::logTypeToStr(LogType type) {
    switch (type) {
        case (INFO):
            return "INFO";
        case (ERROR):
            return "ERROR";
        default:
            return "NULL";
    }
}