11#pragma once
22#include < cstring>
33#include < iostream>
4+ #include < mutex>
5+
6+ #ifdef _WIN32
7+ #include " Windows.h"
8+ #endif
49
510namespace injector
611{
712#define LOG_ARGS template <typename ...Args>
813
914 class Logger
1015 {
16+ mutable std::mutex mutex;
17+
1118 public:
1219 enum class LogLevel {
1320 Verbose,
@@ -19,13 +26,15 @@ namespace injector
1926
2027 Logger (LogLevel logLevel)
2128 {
29+ #ifdef _WIN32
2230 HANDLE console_handle = GetStdHandle (STD_OUTPUT_HANDLE);
2331
2432 DWORD console_mode;
2533 GetConsoleMode (console_handle, &console_mode);
2634
2735 console_mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;
2836 SetConsoleMode (console_handle, console_mode);
37+ #endif
2938
3039 this ->m_log_level = logLevel;
3140 }
@@ -35,31 +44,31 @@ namespace injector
3544 }
3645
3746 LOG_ARGS
38- void critical (const char * service, const char * format, Args&& ...args)
47+ void critical (const char * service, const char * format, Args&& ...args)
3948 {
4049 this ->log (LogLevel::Critical, service, format, std::forward<Args>(args)...);
4150 }
4251
4352 LOG_ARGS
44- void error (const char * service, const char * format, Args&& ...args)
53+ void error (const char * service, const char * format, Args&& ...args)
4554 {
4655 this ->log (LogLevel::Error, service, format, std::forward<Args>(args)...);
4756 }
4857
4958 LOG_ARGS
50- void info (const char * service, const char * format, Args&& ...args)
59+ void info (const char * service, const char * format, Args&& ...args)
5160 {
5261 this ->log (LogLevel::Info, service, format, std::forward<Args>(args)...);
5362 }
5463
5564 LOG_ARGS
56- void verbose (const char * service, const char * format, Args&& ...args)
65+ void verbose (const char * service, const char * format, Args&& ...args)
5766 {
5867 this ->log (LogLevel::Verbose, service, format, std::forward<Args>(args)...);
5968 }
6069
6170 LOG_ARGS
62- void warning (const char * service, const char * format, Args&& ...args)
71+ void warning (const char * service, const char * format, Args&& ...args)
6372 {
6473 this ->log (LogLevel::Warning, service, format, std::forward<Args>(args)...);
6574 }
@@ -80,47 +89,53 @@ namespace injector
8089 LogLevel m_log_level;
8190
8291 LOG_ARGS
83- void log (LogLevel level, const char * service, const char * format, Args&& ...args)
92+ void log (LogLevel level, const char * service, const char * format, Args&& ...args)
8493 {
85- const uint8_t alloc_size = 16 ;
94+ const size_t alloc_size = 8 ;
8695
8796 char color[alloc_size];
8897 char level_string[alloc_size];
8998
9099 switch (level)
91100 {
92101 case LogLevel::Verbose:
93- strcpy_s (color, alloc_size , blue);
94- strcpy_s (level_string, alloc_size , " VERB" );
102+ strcpy (color, blue);
103+ strcpy (level_string, " VERB" );
95104
96105 break ;
97106 case LogLevel::Info:
98- strcpy_s (color, alloc_size , green);
99- strcpy_s (level_string, alloc_size , " INFO" );
107+ strcpy (color, green);
108+ strcpy (level_string, " INFO" );
100109
101110 break ;
102111 case LogLevel::Warning:
103- strcpy_s (color, alloc_size , yellow);
104- strcpy_s (level_string, alloc_size , " WARN" );
112+ strcpy (color, yellow);
113+ strcpy (level_string, " WARN" );
105114
106115 break ;
107116 case LogLevel::Error:
108- strcpy_s (color, alloc_size , red);
109- strcpy_s (level_string, alloc_size , " ERR" );
117+ strcpy (color, red);
118+ strcpy (level_string, " ERR" );
110119
111120 break ;
112121 case LogLevel::Critical:
113- strcpy_s (color, alloc_size , red);
114- strcpy_s (level_string, alloc_size , " CRIT" );
122+ strcpy (color, red);
123+ strcpy (level_string, " CRIT" );
115124
116125 break ;
117126 }
118127
119- char message[512 ];
128+ size_t size_needed = snprintf (nullptr , 0 , format, std::forward<Args>(args)...);
129+ char * message = (char *)malloc (sizeof (char ) * size_needed);
120130 sprintf (message, format, std::forward<Args>(args)...);
131+
132+ std::lock_guard<std::mutex> lock (this ->mutex );
121133 std::cout << color << " [" << level_string << " /" << service << " ] " << reset << message << std::endl;
134+
135+ // be a good boy and free memory
136+ free (message);
122137 }
123138 };
124139
125140 inline Logger* g_log = new Logger(Logger::LogLevel::Verbose);
126- }
141+ }
0 commit comments