CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Log/StdioListener.cpp
Views: 1401
1
// Copyright (C) 2014- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official SVN repository and contact information can be found at
16
// http://code.google.com/p/dolphin-emu/
17
18
#include <atomic>
19
#include <algorithm> // min
20
#include <cstring>
21
#include <string> // System: To be able to add strings with "+"
22
#include <math.h>
23
#include <stdarg.h>
24
#ifndef _MSC_VER
25
#include <unistd.h>
26
#endif
27
28
#include "ppsspp_config.h"
29
#include "Common/Thread/ThreadUtil.h"
30
#include "Common/Data/Encoding/Utf8.h"
31
#include "Common/CommonTypes.h"
32
#include "Common/Log/StdioListener.h"
33
#include "Common/StringUtils.h"
34
35
StdioListener::StdioListener() {
36
#if PPSSPP_PLATFORM(IOS) || PPSSPP_PLATFORM(UWP) || PPSSPP_PLATFORM(SWITCH)
37
bUseColor = false;
38
#elif defined(_MSC_VER)
39
bUseColor = false;
40
#elif defined(__APPLE__)
41
// Xcode builtin terminal used for debugging does not support colours.
42
// Fortunately it can be detected with a TERM env variable.
43
bUseColor = isatty(fileno(stdout)) && getenv("TERM") != NULL;
44
#else
45
bUseColor = isatty(fileno(stdout));
46
#endif
47
}
48
49
void StdioListener::Log(const LogMessage &msg) {
50
char Text[2048];
51
snprintf(Text, sizeof(Text), "%s %s %s", msg.timestamp, msg.header, msg.msg.c_str());
52
Text[sizeof(Text) - 2] = '\n';
53
Text[sizeof(Text) - 1] = '\0';
54
55
char ColorAttr[16] = "";
56
char ResetAttr[16] = "";
57
58
if (bUseColor) {
59
strcpy(ResetAttr, "\033[0m");
60
switch (msg.level) {
61
case LogLevel::LNOTICE: // light green
62
strcpy(ColorAttr, "\033[92m");
63
break;
64
case LogLevel::LERROR: // light red
65
strcpy(ColorAttr, "\033[91m");
66
break;
67
case LogLevel::LWARNING: // light yellow
68
strcpy(ColorAttr, "\033[93m");
69
break;
70
case LogLevel::LINFO: // cyan
71
strcpy(ColorAttr, "\033[96m");
72
break;
73
case LogLevel::LDEBUG: // gray
74
strcpy(ColorAttr, "\033[90m");
75
break;
76
default:
77
break;
78
}
79
}
80
fprintf(stderr, "%s%s%s", ColorAttr, Text, ResetAttr);
81
}
82
83