logging NONE will now output without format string

v1
Brett 2023-07-25 14:06:04 -04:00
parent 1998ddbafe
commit cf7c6f2237
4 changed files with 113 additions and 7 deletions

View File

@ -19,7 +19,9 @@ namespace blt {
seed = (seed << 13) ^ seed; seed = (seed << 13) ^ seed;
return ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff); return ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff);
} }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
/** /**
* fast inverse sqrt * fast inverse sqrt
*/ */
@ -35,6 +37,8 @@ namespace blt {
y = y * (1.5f - (x * y * y)); y = y * (1.5f - (x * y * y));
return y; return y;
} }
#pragma GCC diagnostic pop
static inline constexpr double pow(int b, int p) { static inline constexpr double pow(int b, int p) {

View File

@ -17,6 +17,15 @@ namespace blt::nbt {
std::string readUTF8String(std::fstream& stream); std::string readUTF8String(std::fstream& stream);
// Used to grab the byte-data of any T element. Defaults to Big Endian, however can be configured to use little endian
template <typename T>
int toBytes(const T& in, char* out);
// Used to cast the binary data of any T object, into a T object.
template <typename T>
int fromBytes(const char* in, T* out);
enum class nbt_tag : char { enum class nbt_tag : char {
END = 0, END = 0,
BYTE = 1, BYTE = 1,
@ -33,8 +42,48 @@ namespace blt::nbt {
LONG_ARRAY = 12 LONG_ARRAY = 12
}; };
void writeName(std::fstream& out, const std::string& name); class tag_t {
std::string readName(std::fstream& in); protected:
nbt_tag type;
std::string name;
public:
explicit tag_t(nbt_tag type): type(type) {};
explicit tag_t(nbt_tag type, std::string name): type(type), name(std::move(name)) {}
virtual void writePayload(std::fstream& out) = 0;
virtual void readPayload(std::fstream& in) = 0;
void writeName(std::fstream& out);
void readName(std::fstream& in);
};
template<typename T>
class tag : public tag_t {
protected:
T t;
public:
explicit tag(nbt_tag type): tag_t(type) {};
explicit tag(nbt_tag type, std::string name): tag_t(type, std::move(name)) {}
[[nodiscard]] inline const T& get() const {return t;}
inline T& get() {return t;}
};
class tag_end : public tag<char> {
public:
void writePayload(std::fstream& out) final;
// nothing to read
void readPayload(std::fstream&) final {}
};
class tag_byte : public tag<char> {
public:
void writePayload(std::fstream& out) final;
void readPayload(std::fstream& in) final;
};
class tag_short : public tag<int16_t> {
public:
void writePayload(std::fstream& out) final;
void readPayload(std::fstream& in) final;
};
class NBTDecoder { class NBTDecoder {
private: private:

View File

@ -522,6 +522,11 @@ namespace blt::logging {
applyCFormatting(withoutLn, out, args); applyCFormatting(withoutLn, out, args);
if (level == log_level::NONE){
std::cout << out << std::endl;
return;
}
std::string finalFormattedOutput = applyFormatString(out, level, file, line); std::string finalFormattedOutput = applyFormatString(out, level, file, line);
if (loggingFormat.logToConsole) if (loggingFormat.logToConsole)

View File

@ -4,6 +4,9 @@
* See LICENSE file for license detail * See LICENSE file for license detail
*/ */
#include <blt/nbt/nbt.h> #include <blt/nbt/nbt.h>
#include <blt/std/logging.h>
#include <cstring>
#include <bit>
namespace blt::nbt { namespace blt::nbt {
void writeUTF8String(std::fstream& stream, const std::string& str) { void writeUTF8String(std::fstream& stream, const std::string& str) {
@ -28,15 +31,60 @@ namespace blt::nbt {
return strOut; return strOut;
} }
void writeName(std::fstream& out, const std::string& name) { template<typename T>
int toBytes(const T& in, char* const out) {
std::memcpy(out, (void*) &in, sizeof(T));
if constexpr (std::endian::native == std::endian::little) {
for (size_t i = 0; i < sizeof(T) / 2; i++)
std::swap(out[i], out[sizeof(T) - 1 - i]);
}
return 0;
}
template<typename T>
int fromBytes(const char* const in, T* const out) {
memcpy(out, in, sizeof(T));
if constexpr (std::endian::native == std::endian::little) {
for (size_t i = 0; i < sizeof(T) / 2; i++)
std::swap(((char*) (out))[i], ((char*) (out))[sizeof(T) - 1 - i]);
}
return 0;
}
void tag_t::writeName(std::fstream& out) {
writeUTF8String(out, name); writeUTF8String(out, name);
} }
std::string readName(std::fstream& in) { void tag_t::readName(std::fstream& in) {
return readUTF8String(in); name = readUTF8String(in);
} }
void writePayload(std::fstream& out) { void tag_end::writePayload(std::fstream& out) {
out.put('\0'); out.put('\0');
} }
void tag_byte::writePayload(std::fstream& out) {
// single byte no need for conversion
out.put(t);
}
void tag_byte::readPayload(std::fstream& in) {
in.read(&t, 1);
}
void tag_short::writePayload(std::fstream& out) {
char data[sizeof(t)];
toBytes(t, data);
out.write(data, sizeof(t));
}
void tag_short::readPayload(std::fstream& in) {
char data[sizeof(t)];
in.read(data, sizeof(t));
fromBytes(data, &t);
}
} }