Skip to content

Commit

Permalink
Merge pull request #19354 from hrydgard/console-cleanup
Browse files Browse the repository at this point in the history
Console logging code cleanup
  • Loading branch information
hrydgard authored Jul 21, 2024
2 parents 5edd02e + 5939270 commit 121e3c1
Show file tree
Hide file tree
Showing 34 changed files with 230 additions and 64 deletions.
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -863,17 +863,19 @@ add_library(Common STATIC
Common/Common.h
Common/CommonFuncs.h
Common/CommonTypes.h
Common/ConsoleListener.cpp
Common/ConsoleListener.h
Common/DbgNew.h
Common/FakeEmitter.h
Common/FakeCPUDetect.cpp
Common/ExceptionHandlerSetup.cpp
Common/ExceptionHandlerSetup.h
Common/Log.h
Common/Log.cpp
Common/LogManager.cpp
Common/LogManager.h
Common/Log/ConsoleListener.cpp
Common/Log/ConsoleListener.h
Common/Log/StdioListener.cpp
Common/Log/StdioListener.h
Common/Log/LogManager.cpp
Common/Log/LogManager.h
Common/LogReporting.cpp
Common/LogReporting.h
Common/MemArenaAndroid.cpp
Expand Down
12 changes: 7 additions & 5 deletions Common/Common.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,8 @@
<ClInclude Include="CommonFuncs.h" />
<ClInclude Include="CommonTypes.h" />
<ClInclude Include="CommonWindows.h" />
<ClInclude Include="ConsoleListener.h" />
<ClInclude Include="Log\ConsoleListener.h" />
<ClInclude Include="Log\StdioListener.h" />
<ClInclude Include="CPUDetect.h" />
<ClInclude Include="Crypto\md5.h" />
<ClInclude Include="Crypto\sha1.h" />
Expand All @@ -557,7 +558,7 @@
<ClInclude Include="ExceptionHandlerSetup.h" />
<ClInclude Include="GraphicsContext.h" />
<ClInclude Include="Log.h" />
<ClInclude Include="LogManager.h" />
<ClInclude Include="Log\LogManager.h" />
<ClInclude Include="MachineContext.h" />
<ClInclude Include="MemArena.h" />
<ClInclude Include="MemoryUtil.h" />
Expand Down Expand Up @@ -979,7 +980,8 @@
<ClCompile Include="LoongArchCPUDetect.cpp" />
<ClCompile Include="Serialize\Serializer.cpp" />
<ClCompile Include="Data\Convert\ColorConv.cpp" />
<ClCompile Include="ConsoleListener.cpp" />
<ClCompile Include="Log\ConsoleListener.cpp" />
<ClCompile Include="Log\StdioListener.cpp" />
<ClCompile Include="CPUDetect.cpp" />
<ClCompile Include="MipsCPUDetect.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
Expand All @@ -1005,7 +1007,7 @@
<ClCompile Include="Crypto\sha1.cpp" />
<ClCompile Include="Crypto\sha256.cpp" />
<ClCompile Include="ExceptionHandlerSetup.cpp" />
<ClCompile Include="LogManager.cpp" />
<ClCompile Include="Log\LogManager.cpp" />
<ClCompile Include="MemArenaAndroid.cpp" />
<ClCompile Include="MemArenaPosix.cpp" />
<ClCompile Include="MemArenaWin32.cpp" />
Expand Down Expand Up @@ -1083,4 +1085,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
9 changes: 5 additions & 4 deletions Common/Common.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ClInclude Include="ConsoleListener.h" />
<ClInclude Include="CPUDetect.h" />
<ClInclude Include="Log.h" />
<ClInclude Include="LogManager.h" />
<ClInclude Include="Log\LogManager.h" />
<ClInclude Include="MemArena.h" />
<ClInclude Include="MemoryUtil.h" />
<ClInclude Include="StringUtils.h" />
Expand Down Expand Up @@ -563,11 +563,12 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="ABI.cpp" />
<ClCompile Include="ConsoleListener.cpp" />
<ClCompile Include="Log\ConsoleListener.cpp" />
<ClCompile Include="Log\StdioListener.cpp" />
<ClCompile Include="CPUDetect.cpp" />
<ClCompile Include="FakeCPUDetect.cpp" />
<ClCompile Include="MipsCPUDetect.cpp" />
<ClCompile Include="LogManager.cpp" />
<ClCompile Include="Log\LogManager.cpp" />
<ClCompile Include="MemoryUtil.cpp" />
<ClCompile Include="StringUtils.cpp" />
<ClCompile Include="Thunk.cpp" />
Expand Down Expand Up @@ -1207,4 +1208,4 @@
<Filter>Render\Text</Filter>
</None>
</ItemGroup>
</Project>
</Project>
11 changes: 8 additions & 3 deletions Common/ConsoleListener.cpp → Common/Log/ConsoleListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/

#include "ppsspp_config.h"

#if PPSSPP_PLATFORM(WINDOWS)

#include <atomic>
#include <algorithm> // min
#include <cstring>
#include <string> // System: To be able to add strings with "+"
#include <math.h>
#ifdef _WIN32
#include <process.h>
#include "CommonWindows.h"
#include "Common/CommonWindows.h"
#include <array>
#else
#include <stdarg.h>
Expand All @@ -31,11 +35,10 @@
#include <unistd.h>
#endif

#include "ppsspp_config.h"
#include "Common/Thread/ThreadUtil.h"
#include "Common/Data/Encoding/Utf8.h"
#include "Common/CommonTypes.h"
#include "Common/ConsoleListener.h"
#include "Common/Log/ConsoleListener.h"
#include "Common/StringUtils.h"

#if defined(USING_WIN_UI)
Expand Down Expand Up @@ -659,3 +662,5 @@ void ConsoleListener::ClearScreen(bool Cursor)
if (Cursor) SetConsoleCursorPosition(hConsole, coordScreen);
#endif
}

#endif // PPSSPP_PLATFORM(WINDOWS)
17 changes: 10 additions & 7 deletions Common/ConsoleListener.h → Common/Log/ConsoleListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@

#pragma once

#include <atomic>
// Windows-only.

#include "LogManager.h"
#include "ppsspp_config.h"

#ifdef _WIN32
#include "CommonWindows.h"
#endif
#if PPSSPP_PLATFORM(WINDOWS)

#include <atomic>

#include "Common/Log/LogManager.h"
#include "Common/CommonWindows.h"

class ConsoleListener : public LogListener {
public:
Expand All @@ -38,9 +41,7 @@ class ConsoleListener : public LogListener {
void LetterSpace(int Width, int Height);
void BufferWidthHeight(int BufferWidth, int BufferHeight, int ScreenWidth, int ScreenHeight, bool BufferFirst);
void PixelSpace(int Left, int Top, int Width, int Height, bool);
#if defined(USING_WIN_UI)
COORD GetCoordinates(int BytesRead, int BufferWidth);
#endif
void Log(const LogMessage &message) override;
void ClearScreen(bool Cursor = true);

Expand Down Expand Up @@ -73,3 +74,5 @@ class ConsoleListener : public LogListener {
bool bHidden;
bool bUseColor;
};

#endif
23 changes: 20 additions & 3 deletions Common/LogManager.cpp → Common/Log/LogManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@

#include "Common/Data/Encoding/Utf8.h"

#include "Common/LogManager.h"
#include "Common/ConsoleListener.h"
#include "Common/Log/LogManager.h"

#if PPSSPP_PLATFORM(WINDOWS)
#include "Common/Log/ConsoleListener.h"
#endif

#include "Common/Log/StdioListener.h"
#include "Common/TimeUtil.h"
#include "Common/File/FileUtil.h"
#include "Common/StringUtils.h"
Expand Down Expand Up @@ -137,18 +142,24 @@ LogManager::LogManager(bool *enabledSetting) {
#else
#if !defined(MOBILE_DEVICE) || defined(_DEBUG)
fileLog_ = new FileLogListener("");
#if PPSSPP_PLATFORM(WINDOWS)
consoleLog_ = new ConsoleListener();
#ifdef _WIN32
if (IsDebuggerPresent())
debuggerLog_ = new OutputDebugStringLogListener();
#else
stdioLog_ = new StdioListener();
#endif
#endif
ringLog_ = new RingbufferLogListener();
#endif

#if !defined(MOBILE_DEVICE) || defined(_DEBUG)
AddListener(fileLog_);
#if PPSSPP_PLATFORM(WINDOWS)
AddListener(consoleLog_);
#else
AddListener(stdioLog_);
#endif
#if defined(_MSC_VER) && (defined(USING_WIN_UI) || PPSSPP_PLATFORM(UWP))
if (IsDebuggerPresent() && debuggerLog_ && LOG_MSC_OUTPUTDEBUG)
AddListener(debuggerLog_);
Expand All @@ -161,7 +172,10 @@ LogManager::~LogManager() {
for (int i = 0; i < (int)Log::NUMBER_OF_LOGS; ++i) {
#if !defined(MOBILE_DEVICE) || defined(_DEBUG)
RemoveListener(fileLog_);
#if PPSSPP_PLATFORM(WINDOWS)
RemoveListener(consoleLog_);
#endif
RemoveListener(stdioLog_);
#if defined(_MSC_VER) && defined(USING_WIN_UI)
RemoveListener(debuggerLog_);
#endif
Expand All @@ -174,7 +188,10 @@ LogManager::~LogManager() {
if (fileLog_)
delete fileLog_;
#if !defined(MOBILE_DEVICE) || defined(_DEBUG)
#if PPSSPP_PLATFORM(WINDOWS)
delete consoleLog_;
#endif
delete stdioLog_;
delete debuggerLog_;
#endif
delete ringLog_;
Expand Down
10 changes: 10 additions & 0 deletions Common/LogManager.h → Common/Log/LogManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ struct LogChannel {
};

class ConsoleListener;
class StdioListener;

class LogManager {
private:
Expand All @@ -116,7 +117,10 @@ class LogManager {

LogChannel log_[(size_t)Log::NUMBER_OF_LOGS];
FileLogListener *fileLog_ = nullptr;
#if PPSSPP_PLATFORM(WINDOWS)
ConsoleListener *consoleLog_ = nullptr;
#endif
StdioListener *stdioLog_ = nullptr;
OutputDebugStringLogListener *debuggerLog_ = nullptr;
RingbufferLogListener *ringLog_ = nullptr;
static LogManager *logManager_; // Singleton. Ugh.
Expand Down Expand Up @@ -157,9 +161,15 @@ class LogManager {
return log_[(size_t)type].level;
}

#if PPSSPP_PLATFORM(WINDOWS)
ConsoleListener *GetConsoleListener() const {
return consoleLog_;
}
#endif

StdioListener *GetStdioListener() const {
return stdioLog_;
}

OutputDebugStringLogListener *GetDebuggerListener() const {
return debuggerLog_;
Expand Down
81 changes: 81 additions & 0 deletions Common/Log/StdioListener.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (C) 2014- PPSSPP Project.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/

#include <atomic>
#include <algorithm> // min
#include <cstring>
#include <string> // System: To be able to add strings with "+"
#include <math.h>
#include <stdarg.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif

#include "ppsspp_config.h"
#include "Common/Thread/ThreadUtil.h"
#include "Common/Data/Encoding/Utf8.h"
#include "Common/CommonTypes.h"
#include "Common/Log/StdioListener.h"
#include "Common/StringUtils.h"

StdioListener::StdioListener() {
#if PPSSPP_PLATFORM(IOS) || PPSSPP_PLATFORM(UWP) || PPSSPP_PLATFORM(SWITCH)
bUseColor = false;
#elif defined(_MSC_VER)
bUseColor = false;
#elif defined(__APPLE__)
// Xcode builtin terminal used for debugging does not support colours.
// Fortunately it can be detected with a TERM env variable.
bUseColor = isatty(fileno(stdout)) && getenv("TERM") != NULL;
#else
bUseColor = isatty(fileno(stdout));
#endif
}

void StdioListener::Log(const LogMessage &msg) {
char Text[2048];
snprintf(Text, sizeof(Text), "%s %s %s", msg.timestamp, msg.header, msg.msg.c_str());
Text[sizeof(Text) - 2] = '\n';
Text[sizeof(Text) - 1] = '\0';

char ColorAttr[16] = "";
char ResetAttr[16] = "";

if (bUseColor) {
strcpy(ResetAttr, "\033[0m");
switch (msg.level) {
case LogLevel::LNOTICE: // light green
strcpy(ColorAttr, "\033[92m");
break;
case LogLevel::LERROR: // light red
strcpy(ColorAttr, "\033[91m");
break;
case LogLevel::LWARNING: // light yellow
strcpy(ColorAttr, "\033[93m");
break;
case LogLevel::LINFO: // cyan
strcpy(ColorAttr, "\033[96m");
break;
case LogLevel::LDEBUG: // gray
strcpy(ColorAttr, "\033[90m");
break;
default:
break;
}
}
fprintf(stderr, "%s%s%s", ColorAttr, Text, ResetAttr);
}
36 changes: 36 additions & 0 deletions Common/Log/StdioListener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

// Copyright (C) 2003 Dolphin Project.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/

#pragma once

#include <atomic>

#include "Common/Log/LogManager.h"

#ifdef _WIN32
#include "Common/CommonWindows.h"
#endif

class StdioListener : public LogListener {
public:
StdioListener();
void Log(const LogMessage &message) override;
private:
bool bUseColor = true;
};
Loading

0 comments on commit 121e3c1

Please sign in to comment.