blt logging

v1
Brett 2023-06-26 21:33:42 -04:00
parent 2ad00c8895
commit a1331db389
2 changed files with 42 additions and 25 deletions

View File

@ -26,7 +26,7 @@ namespace blt {
template<typename T, uint32_t size> template<typename T, uint32_t size>
struct vec { struct vec {
private: private:
T elements[size]{}; T elements[size];
public: public:
vec() { vec() {
@ -48,7 +48,7 @@ namespace blt {
template<typename o_T, uint32_t o_size> template<typename o_T, uint32_t o_size>
explicit vec(const vec<o_T, o_size>& copy) { explicit vec(const vec<o_T, o_size>& copy) {
for (uint32_t i = 0; i < o_size; i++) for (uint32_t i = 0; i < std::min(o_size, size); i++)
elements[i] = copy[i]; elements[i] = copy[i];
} }

View File

@ -33,14 +33,18 @@ namespace blt::logging {
const char* m_directory = "./"; const char* m_directory = "./";
LOG_LEVEL minLevel = BLT_TRACE; LOG_LEVEL minLevel = BLT_TRACE;
explicit constexpr LOG_PROPERTIES(bool useColor, bool logToConsole, bool logToFile, const char* directory): explicit constexpr LOG_PROPERTIES(
m_useColor(useColor), m_logToConsole(logToConsole), m_logToFile(logToFile), m_directory(directory) {} bool useColor, bool logToConsole, bool logToFile, const char* directory
):
m_useColor(useColor), m_logToConsole(logToConsole), m_logToFile(logToFile),
m_directory(directory) {}
explicit constexpr LOG_PROPERTIES() = default; explicit constexpr LOG_PROPERTIES() = default;
}; };
struct logger { struct logger {
LOG_LEVEL level; LOG_LEVEL level;
void log_internal(const std::string& str) const; void log_internal(const std::string& str) const;
// evil hack, todo: better way // evil hack, todo: better way
#ifdef BLT_DISABLE_LOGGING #ifdef BLT_DISABLE_LOGGING
@ -48,11 +52,15 @@ namespace blt::logging {
} }
#else #else
void log(const std::string& str) const { void log(const std::string& str) const {
log_internal(str); log_internal(str);
} }
#endif #endif
void flush() const; void flush() const;
static void flush_all(); static void flush_all();
}; };
@ -82,7 +90,11 @@ namespace blt::logging {
} }
void init(LOG_PROPERTIES properties); void init(LOG_PROPERTIES properties);
void log_internal(const std::string& format, LOG_LEVEL level, const char* file, int currentLine, int auto_line, ...);
void log_internal(
const std::string& format, LOG_LEVEL level, const char* file, int currentLine,
int auto_line, ...
);
// template voodoo magic (SFINAE, "Substitution Failure Is Not An Error") // template voodoo magic (SFINAE, "Substitution Failure Is Not An Error")
// https://stackoverflow.com/questions/44848011/c-limit-template-type-to-numbers // https://stackoverflow.com/questions/44848011/c-limit-template-type-to-numbers
@ -98,9 +110,12 @@ namespace blt::logging {
* @param auto_line put a new line at the end if none exists if true * @param auto_line put a new line at the end if none exists if true
*/ */
template<typename T, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr> template<typename T, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
inline void log_internal(T t, LOG_LEVEL level, const char* file, int currentLine, int auto_line){ inline void log_internal(
T t, LOG_LEVEL level, const char* file, int currentLine, int auto_line
) {
log_internal(std::to_string(t), level, file, currentLine, auto_line); log_internal(std::to_string(t), level, file, currentLine, auto_line);
} }
/** /**
* Will flush all buffers! This might cause issues with threads! * Will flush all buffers! This might cause issues with threads!
*/ */
@ -109,7 +124,7 @@ namespace blt::logging {
void testLogging(); void testLogging();
} }
#ifdef BLT_DISABLE_LOGGING #ifndef BLT_ENABLE_LOGGING
#define BLT_TRACE(format, ...) #define BLT_TRACE(format, ...)
#define BLT_DEBUG(format, ...) #define BLT_DEBUG(format, ...)
#define BLT_INFO(format, ...) #define BLT_INFO(format, ...)
@ -117,6 +132,7 @@ namespace blt::logging {
#define BLT_ERROR(format, ...) #define BLT_ERROR(format, ...)
#define BLT_FATAL(format, ...) #define BLT_FATAL(format, ...)
#else #else
#ifndef BLT_DISABLE_LOGGING
#define BLT_TRACE(format, ...) log_internal(format, blt::logging::BLT_TRACE, __FILE__, __LINE__, true, ##__VA_ARGS__) #define BLT_TRACE(format, ...) log_internal(format, blt::logging::BLT_TRACE, __FILE__, __LINE__, true, ##__VA_ARGS__)
#define BLT_DEBUG(format, ...) log_internal(format, blt::logging::BLT_DEBUG, __FILE__, __LINE__, true, ##__VA_ARGS__) #define BLT_DEBUG(format, ...) log_internal(format, blt::logging::BLT_DEBUG, __FILE__, __LINE__, true, ##__VA_ARGS__)
#define BLT_INFO(format, ...) log_internal(format, blt::logging::BLT_INFO, __FILE__, __LINE__, true, ##__VA_ARGS__) #define BLT_INFO(format, ...) log_internal(format, blt::logging::BLT_INFO, __FILE__, __LINE__, true, ##__VA_ARGS__)
@ -124,5 +140,6 @@ namespace blt::logging {
#define BLT_ERROR(format, ...) log_internal(format, blt::logging::BLT_ERROR, __FILE__, __LINE__, true, ##__VA_ARGS__) #define BLT_ERROR(format, ...) log_internal(format, blt::logging::BLT_ERROR, __FILE__, __LINE__, true, ##__VA_ARGS__)
#define BLT_FATAL(format, ...) log_internal(format, blt::logging::BLT_FATAL, __FILE__, __LINE__, true, ##__VA_ARGS__) #define BLT_FATAL(format, ...) log_internal(format, blt::logging::BLT_FATAL, __FILE__, __LINE__, true, ##__VA_ARGS__)
#endif #endif
#endif
#endif //BLT_LOGGING_H #endif //BLT_LOGGING_H