2023-01-23 10:15:37 -05:00
|
|
|
/*
|
|
|
|
* Created by Brett on 23/01/23.
|
|
|
|
* Licensed under GNU General Public License V3.0
|
|
|
|
* See LICENSE file for license detail
|
|
|
|
*/
|
|
|
|
|
2023-01-29 15:24:33 -05:00
|
|
|
#ifndef BLT_LOGGING_H
|
|
|
|
#define BLT_LOGGING_H
|
2023-01-23 10:15:37 -05:00
|
|
|
|
2023-01-24 15:16:47 -05:00
|
|
|
#include <string>
|
2023-02-08 17:22:27 -05:00
|
|
|
#include <type_traits>
|
2023-04-08 12:44:31 -04:00
|
|
|
#include <blt/config.h>
|
2023-01-24 15:16:47 -05:00
|
|
|
|
2023-01-23 17:52:32 -05:00
|
|
|
namespace blt::logging {
|
|
|
|
|
|
|
|
enum LOG_LEVEL {
|
2023-04-08 12:44:31 -04:00
|
|
|
// normal
|
|
|
|
BLT_TRACE = 0, BLT_DEBUG = 1, BLT_INFO = 2,
|
|
|
|
// errors
|
|
|
|
BLT_WARN = 3, BLT_ERROR = 4, BLT_FATAL = 5,
|
|
|
|
// default
|
|
|
|
BLT_NONE = 6
|
2023-01-23 17:52:32 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
struct LOG_PROPERTIES {
|
|
|
|
bool m_useColor = true;
|
|
|
|
bool m_logToConsole = true;
|
|
|
|
bool m_logToFile = true;
|
2023-02-08 17:22:27 -05:00
|
|
|
// include file + line data?
|
|
|
|
bool m_logWithData = true;
|
|
|
|
// print the whole path or just the file name?
|
|
|
|
bool m_logFullPath = false;
|
2023-01-29 23:36:42 -05:00
|
|
|
const char* m_directory = "./";
|
2023-04-08 12:44:31 -04:00
|
|
|
LOG_LEVEL minLevel = BLT_TRACE;
|
2023-01-23 17:52:32 -05:00
|
|
|
|
2023-06-26 21:33:42 -04:00
|
|
|
explicit constexpr LOG_PROPERTIES(
|
|
|
|
bool useColor, bool logToConsole, bool logToFile, const char* directory
|
|
|
|
):
|
|
|
|
m_useColor(useColor), m_logToConsole(logToConsole), m_logToFile(logToFile),
|
|
|
|
m_directory(directory) {}
|
2023-01-23 17:52:32 -05:00
|
|
|
|
|
|
|
explicit constexpr LOG_PROPERTIES() = default;
|
|
|
|
};
|
|
|
|
|
2023-01-24 23:01:34 -05:00
|
|
|
struct logger {
|
|
|
|
LOG_LEVEL level;
|
2023-06-26 21:33:42 -04:00
|
|
|
|
2023-02-08 17:22:27 -05:00
|
|
|
void log_internal(const std::string& str) const;
|
2023-01-24 23:01:34 -05:00
|
|
|
// evil hack, todo: better way
|
2023-06-26 21:33:42 -04:00
|
|
|
#ifdef BLT_DISABLE_LOGGING
|
|
|
|
void log(const std::string& str) const {
|
|
|
|
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
void log(const std::string& str) const {
|
|
|
|
log_internal(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void flush() const;
|
|
|
|
|
|
|
|
static void flush_all();
|
2023-01-24 23:01:34 -05:00
|
|
|
};
|
|
|
|
|
2023-04-08 12:44:31 -04:00
|
|
|
static logger std_out{BLT_NONE};
|
2023-06-26 21:33:42 -04:00
|
|
|
|
2023-04-08 12:44:31 -04:00
|
|
|
static logger trace{BLT_TRACE};
|
|
|
|
static logger debug{BLT_DEBUG};
|
|
|
|
static logger info{BLT_INFO};
|
|
|
|
static logger warn{BLT_WARN};
|
|
|
|
static logger error{BLT_ERROR};
|
|
|
|
static logger fatal{BLT_FATAL};
|
2023-01-24 23:01:34 -05:00
|
|
|
|
2023-02-08 17:22:27 -05:00
|
|
|
static inline logger& getLoggerFromLevel(LOG_LEVEL level) {
|
2023-04-05 17:21:19 -04:00
|
|
|
static logger loggerLevelDecode[7]{trace, debug, info, warn, error, fatal, std_out};
|
2023-02-08 17:22:27 -05:00
|
|
|
return loggerLevelDecode[level];
|
2023-01-24 23:01:34 -05:00
|
|
|
}
|
|
|
|
|
2023-02-08 17:22:27 -05:00
|
|
|
static inline logger& operator<<(logger& out, const std::string& str) {
|
|
|
|
out.log(str);
|
2023-01-24 23:01:34 -05:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2023-02-08 17:22:27 -05:00
|
|
|
template<typename T, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
|
|
|
|
static inline logger& operator<<(logger& out, T t) {
|
|
|
|
out.log(std::to_string(t));
|
2023-01-24 23:01:34 -05:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2023-01-23 17:52:32 -05:00
|
|
|
void init(LOG_PROPERTIES properties);
|
2023-06-26 21:33:42 -04:00
|
|
|
|
|
|
|
void log_internal(
|
|
|
|
const std::string& format, LOG_LEVEL level, const char* file, int currentLine,
|
|
|
|
int auto_line, ...
|
|
|
|
);
|
2023-02-08 17:22:27 -05:00
|
|
|
|
|
|
|
// template voodoo magic (SFINAE, "Substitution Failure Is Not An Error")
|
|
|
|
// https://stackoverflow.com/questions/44848011/c-limit-template-type-to-numbers
|
|
|
|
// std::enable_if has a member called type if the condition is true.
|
|
|
|
// What I am doing is saying that if the condition is true then the template has a non type
|
|
|
|
// template parameter of type type* (which is void* since the default type for enable_if is void) and it's value is nullptr.
|
|
|
|
// if the condition is false, the substitution fails, and the entire template is silently (no compiler error) discarded from the overload set.
|
|
|
|
/**
|
|
|
|
* Logs an is_arithmetic type to the console.
|
|
|
|
* @tparam T type to log
|
|
|
|
* @param t the value to log
|
|
|
|
* @param level logger level to log at
|
|
|
|
* @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>
|
2023-06-26 21:33:42 -04:00
|
|
|
inline void log_internal(
|
|
|
|
T t, LOG_LEVEL level, const char* file, int currentLine, int auto_line
|
|
|
|
) {
|
2023-02-08 17:22:27 -05:00
|
|
|
log_internal(std::to_string(t), level, file, currentLine, auto_line);
|
|
|
|
}
|
2023-06-26 21:33:42 -04:00
|
|
|
|
2023-02-08 14:12:21 -05:00
|
|
|
/**
|
|
|
|
* Will flush all buffers! This might cause issues with threads!
|
|
|
|
*/
|
|
|
|
void flush();
|
2023-04-05 17:21:19 -04:00
|
|
|
|
|
|
|
void testLogging();
|
2023-01-23 10:15:37 -05:00
|
|
|
}
|
|
|
|
|
2023-06-26 21:33:42 -04:00
|
|
|
#ifndef BLT_ENABLE_LOGGING
|
2023-01-26 00:59:36 -05:00
|
|
|
#define BLT_TRACE(format, ...)
|
|
|
|
#define BLT_DEBUG(format, ...)
|
|
|
|
#define BLT_INFO(format, ...)
|
|
|
|
#define BLT_WARN(format, ...)
|
|
|
|
#define BLT_ERROR(format, ...)
|
|
|
|
#define BLT_FATAL(format, ...)
|
2023-01-23 17:52:32 -05:00
|
|
|
#else
|
2023-06-26 21:33:42 -04:00
|
|
|
#ifndef BLT_DISABLE_LOGGING
|
|
|
|
#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_INFO(format, ...) log_internal(format, blt::logging::BLT_INFO, __FILE__, __LINE__, true, ##__VA_ARGS__)
|
|
|
|
#define BLT_WARN(format, ...) log_internal(format, blt::logging::BLT_WARN, __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__)
|
|
|
|
#endif
|
2023-01-23 17:52:32 -05:00
|
|
|
#endif
|
2023-01-23 10:15:37 -05:00
|
|
|
|
2023-02-08 17:22:27 -05:00
|
|
|
#endif //BLT_LOGGING_H
|