5#define LOG_FILE_LIMIT 5
6#define LOG_FILE_MAX_SIZE 5 * 1024 * 1024
13#include <unordered_map>
20 static inline std::unordered_map<std::string, FILE*> LogFiles;
21 static inline FILE* LogFile =
nullptr;
23 static std::string getCurrentTime() {
24 auto now = std::chrono::system_clock::now();
25 auto in_time_t = std::chrono::system_clock::to_time_t(now);
27 return fmt::format(
"{:%Y-%m-%d %H:%M:%S}", fmt::localtime(in_time_t));;
30 static FILE* OpenAndReturn(std::string fileName){
32 errno_t err = fopen_s(&f, fileName.c_str(),
"a+");
34 std::cerr <<
"Logger: Error opening file " << fileName << std::endl;
36 LogFiles[fileName] = f;
55 errno_t err = fopen_s(&LogFile,
"Engine.log",
"a+");
57 std::cout <<
"Fatal: Failed to open log file\n";
67 for(
const auto& file : LogFiles){
79 for(
const auto& file : LogFiles){
86 template<
typename ...Args>
87 static void FileLog(std::string fileName,
const char* message, Args&& ...args){
88 FILE* file = LogFiles[fileName];
89 file = (file ? file : OpenAndReturn(fileName));
90 std::cout << fmt::format(message, std::forward<Args>(args)...) << std::endl;
91 fmt::println(file,
"({})[ThreadID: {}] Fatal: {}", getCurrentTime(), std::this_thread::get_id(), fmt::format(message, std::forward<Args>(args)...));
104 static void Log(
const char* message) {
105 const auto formatted = fmt::format(
"({})[ThreadID: {}] Info: {}", getCurrentTime(), std::this_thread::get_id(), message);
106 std::cout << formatted << std::endl;
107 if (!LogFile)
return;
108 fmt::println(LogFile,
"{}", formatted);
112 static void Log(
const std::string& message) {
113 const auto formatted = fmt::format(
"({})[ThreadID: {}] Info: {}", getCurrentTime(), std::this_thread::get_id(), message.c_str());
114 std::cout << formatted << std::endl;
115 if (!LogFile)
return;
116 fmt::println(LogFile,
"{}", formatted);
120 template<
typename ...Args>
121 static void Log(fmt::format_string<Args...> message, Args&&... args) {
123 const auto formatted = fmt::format(message, std::forward<Args>(args)...);
127 static void DbgLog(
const std::string& message){
132 template<
typename ...Args>
133 static void DbgLog(fmt::format_string<Args...> message, Args&&... args){
134 Log(message, std::forward(args)...);
146 template<
typename ...Args>
147 static void Fatal(fmt::format_string<Args...> message, Args&&... args) {
148 const auto formatted = fmt::format(message, std::forward<Args>(args)...);
149 std::cerr << fmt::format(
"({})[ThreadID: {}] Fatal: {}\n", getCurrentTime(), std::this_thread::get_id(), formatted);
150 if (!LogFile)
return;
151 fmt::println(LogFile,
"({})[ThreadID: {}] Fatal: {}", getCurrentTime(), std::this_thread::get_id(), formatted);
static void Log(const std::string &message)
static void DbgLog(const std::string &message)
static void Log(const char *message)
static void DbgLog(fmt::format_string< Args... > message, Args &&... args)
static void Start()
Starts the logger.
static void Fatal(fmt::format_string< Args... > message, Args &&... args)
static void Log(fmt::format_string< Args... > message, Args &&... args)
static void Flush()
Flushes and keeps the files open.
static void FileLog(std::string fileName, const char *message, Args &&...args)
Logs in a file, if the file is used for the first time, it is opened and kept in cache.
static void Close()
Flushes and closes all the files used for logging.
The Hubris Engine main namespace.