summaryrefslogtreecommitdiff
path: root/Runtime/Debug/Log.cpp
blob: cc5b2e336b3366df8092193dbc8ed1988427fabf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "log.h"
#include <iostream>
#include <ctime>
#include <unordered_set>

using namespace std;

#ifdef GAMELAB_WIN
#include <windows.h>
static HANDLE s_ConsoleHandle = 0;
#endif

unordered_set<string> s_OpenTags;

#ifdef GAMELAB_DEBUG
// https://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
const std::string currentDateTime() {
	time_t     now = time(0);
	struct tm  tstruct;
	char       buf[80];
	tstruct = *localtime(&now);
	// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
	// for more information about date/time format
	strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);

	return buf;
}

void log_open_tag(std::string tag)
{
	s_OpenTags.insert(tag);
}

// https://stackoverflow.com/questions/4053837/colorizing-text-in-the-console-with-c
// https://en.wikipedia.org/wiki/ANSI_escape_code#Windows_and_DOS
// https://stackoverflow.com/questions/9262270/color-console-output-with-c-in-windows
// https://blog.csdn.net/odaynot/article/details/7722240

void log_info(std::string log) 
{
#ifdef GAMELAB_WIN
	if (s_ConsoleHandle == 0) {
		s_ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
	}
	SetConsoleTextAttribute(s_ConsoleHandle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
	cout << currentDateTime() << " [Info] " << log << endl;
#else 
	cout << "\x1B[97m" << currentDateTime() << " [Info] " << log << "\x1B[0m" << endl;
#endif
}

void log_warning(std::string log)
{
#ifdef GAMELAB_WIN
	if (s_ConsoleHandle == 0) {
		s_ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
	}
	SetConsoleTextAttribute(s_ConsoleHandle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
	cout << currentDateTime() << " [Info] " << log << endl;
#else 
	cout << "\x1B[93m" << currentDateTime() << " [Info] " << log << "\x1B[0m" << endl;
#endif
}

void log_error(std::string log)
{
#ifdef GAMELAB_WIN
	if (s_ConsoleHandle == 0) {
		s_ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
	}
	SetConsoleTextAttribute(s_ConsoleHandle, FOREGROUND_RED | FOREGROUND_INTENSITY);
	cout << currentDateTime() << " [Info] " << log << endl;
#else 
	cout << "\x1B[91m" << currentDateTime() << " [Info] " << log << "\x1B[0m" << endl;
#endif
}

void log_error_null_param(std::string funcName, std::string param)
{
	log_error("Null parameter in " + funcName + " called " + param);
}

void log_info(string tag, std::string log) 
{
	if (s_OpenTags.count(tag) <= 0)
		return;
	log_info("[" + tag + "] " + log);
}

void log_warning(string tag, std::string log)
{
	if (s_OpenTags.count(tag) <= 0)
		return;
	log_warning("[" + tag + "] " + log);
}
void log_error(string tag, std::string log)
{
	if (s_OpenTags.count(tag) <= 0)
		return;
	log_error("[" + tag + "] " + log);
}
#else 
void log_open_tag(std::string tag) {}
void log_info(std::string log)
{
}

void log_warning(std::string log)
{
}

void log_error(std::string log)
{
}

void log_error_null_param(std::string funcName, std::string param)
{
}

void log_info(string tag, std::string log) {}
void log_warning(string tag, std::string log) {}
void log_error(string tag, std::string log) {}

#endif