Engine
Raylib based game framework
Loading...
Searching...
No Matches
Log.h
1#pragma once
2#include <chrono>
3#include <iomanip>
4#include <iostream>
5#include <mutex>
6#include <sstream>
7
8inline std::mutex timeMutex;
9
10inline void GetThreadSafeLocalTime(const time_t& timeInput, std::tm& timeInfo)
11{
12 std::lock_guard<std::mutex> lock(timeMutex);
13 std::tm* result = localtime(&timeInput);
14 if (result != nullptr)
15 {
16 timeInfo = *result;
17 }
18}
19
20inline std::string GetCurrentTimeString()
21{
22 auto now = std::chrono::system_clock::now();
23 auto inTimeT = std::chrono::system_clock::to_time_t(now);
24 auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
25
26 std::tm timeInfo;
27 std::stringstream ss;
28
29 GetThreadSafeLocalTime(inTimeT, timeInfo);
30
31 char buffer[9];
32 strftime(buffer, sizeof(buffer), "%H:%M:%S", &timeInfo);
33
34 ss << buffer << '.' << std::setw(3) << std::setfill('0') << static_cast<int>(milliseconds.count());
35
36 return ss.str();
37}
38
39inline const char* const ANSI_RESET = "\033[0m";
40inline const char* const ANSI_SAVE_CURSOR = "\033[s";
41inline const char* const ANSI_LOAD_CURSOR = "\033[u";
42inline const char* const ANSI_HIDE_CURSOR = "\033[?25l";
43inline const char* const ANSI_SHOW_CURSOR = "\033[?25h";
44inline const char* const ANSI_CLEAR_LINE = "\033[2K";
45inline const char* const ANSI_SCROLL_DOWN = "\033[D";
46
47inline const char* const LOG_GREEN = "\033[92m";
48inline const char* const LOG_BLUE = "\033[94m";
49inline const char* const LOG_YELLOW = "\033[93m";
50inline const char* const LOG_RED = "\033[91m";
51inline const char* const LOG_WHITE = "\033[97m";
52
53template <typename... Args>
54void Log(Args&&... args)
55{
56#ifdef LOGFILE
57 std::ofstream logFile("log.txt", std::ios_base::app);
58 logFile << "[" << GetCurrentTimeString() << "] ";
59 (logFile << ... << std::forward<Args>(args)) << '\n';
60#endif
61
62 std::cerr << LOG_WHITE << "[" << GetCurrentTimeString() << "] ";
63 (std::cerr << ... << std::forward<Args>(args)) << ANSI_RESET << '\n';
64}
65
66template <typename... Args>
67void LogColor(const char* color, Args&&... args)
68{
69#ifdef LOGFILE
70 std::ofstream logFile("log.txt", std::ios_base::app);
71 logFile << "[" << GetCurrentTimeString() << "] ";
72 (logFile << ... << std::forward<Args>(args)) << '\n';
73#endif
74
75 std::cerr << LOG_WHITE << "[" << GetCurrentTimeString() << "] ";
76 std::cerr << color;
77 (std::cerr << ... << std::forward<Args>(args));
78 std::cerr << ANSI_RESET << '\n';
79}
80
81template <typename... Args>
82void Output(Args&&... args)
83{
84 (std::cout << ... << std::forward<Args>(args)) << ANSI_RESET << '\n';
85}
86
87template <typename... Args>
88void OutputColor(const char* color, Args&&... args)
89{
90 std::cout << color;
91 (std::cout << ... << std::forward<Args>(args));
92 std::cout << ANSI_RESET << '\n';
93}
94
95template <typename... Args>
96void OutputErr(Args&&... args)
97{
98 (std::cerr << ... << std::forward<Args>(args)) << ANSI_RESET << '\n';
99}
100
101template <typename... Args>
102void OutputErrColor(const char* color, Args&&... args)
103{
104 std::cerr << color;
105 (std::cerr << ... << std::forward<Args>(args));
106 std::cerr << ANSI_RESET << '\n';
107}
108
109#ifdef _WIN32
110
111#ifndef _INC_WINDOWS
112
113extern "C"
114{
115 __declspec(dllimport) void* __stdcall GetStdHandle(unsigned long);
116 __declspec(dllimport) int __stdcall GetConsoleMode(void*, unsigned long*);
117 __declspec(dllimport) int __stdcall SetConsoleMode(void*, unsigned long);
118 __declspec(dllimport) int __stdcall SetConsoleTextAttribute(void*, unsigned short);
119}
120
121#endif
122
123class Ansi
124{
125
126public:
127
128 Ansi()
129 {
130 void* hOutput = GetStdHandle(-11);
131 unsigned long outMode;
132 GetConsoleMode(hOutput, &outMode);
133 SetConsoleMode(hOutput, outMode | 0x0004);
134
135 void* hError = GetStdHandle(-12);
136 unsigned long errMode;
137 GetConsoleMode(hError, &errMode);
138 SetConsoleMode(hError, errMode | 0x0004);
139 }
140
141 ~Ansi()
142 {
143 void* hOutput = GetStdHandle(-11);
144 unsigned long outMode;
145 GetConsoleMode(hOutput, &outMode);
146 SetConsoleMode(hOutput, outMode & ~0x0004);
147
148 void* hError = GetStdHandle(-12);
149 unsigned long errMode;
150 GetConsoleMode(hError, &errMode);
151 SetConsoleMode(hError, errMode & ~0x0004);
152 }
153};
154
155inline Ansi ansi;
156
157#endif