TazGraph Project v0.1.0
Loading...
Searching...
No Matches
ConsoleLogger.h
1#pragma once
2
3#include "../pch.h"
4#include <fstream>
5#include <mutex>
6
7namespace TazGraphEngine {
9 public:
10 static void init() {
11 std::lock_guard<std::mutex> lock(fileLog_mutex);
12
13 std::ofstream file("file_log.txt",
14 std::ios::out | std::ios::trunc); // clear file
15 }
16
17 static void log(const std::string& message) {
18 //printCurrentTime();
19 std::cout << " " << message << std::endl;
20
21 writeToFile(message);
22 }
23
24 static void error(const std::string& errorMessage) {
25 //printCurrentTime();
26 std::cerr << " [ERROR] " << errorMessage << std::endl;
27
28 writeToFile(errorMessage);
29
30 std::abort();
31 }
32
33 static void writeToFile(const std::string& line) {
34 std::lock_guard<std::mutex> lock(fileLog_mutex);
35
36 std::ofstream file("file_log.txt",
37 std::ios::out | std::ios::app);
38
39 if (file.is_open()) {
40 file << line << std::endl;
41 file.flush(); // CRITICAL for crash debugging
42 }
43 }
44
45 private:
46 static inline std::mutex fileLog_mutex;
47
48 static void printCurrentTime() {
49 std::time_t now = std::time(nullptr);
50 struct tm timeInfo;
51
52#if defined(_WIN32) || defined(_WIN64)
53 localtime_s(&timeInfo, &now);
54#else
55 localtime_r(&now, &timeInfo);
56#endif
57
58 char timestamp[20]; // Sufficient space for the timestamp
59 strftime(timestamp, sizeof(timestamp), "[%Y-%m-%d %H:%M:%S]", &timeInfo);
60
61 std::cout << timestamp;
62 }
63 };
64}
65
66#define TAZ_LOG(message) TazGraphEngine::ConsoleLogger::log(message)
67#define TAZ_ERROR(message) TazGraphEngine::ConsoleLogger::error(message)
Definition ConsoleLogger.h:8