forked from shadps4-emu/shadPS4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add stdin/out/err as virtual devices
- Loading branch information
1 parent
b9c63b4
commit dc6d17d
Showing
10 changed files
with
219 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#include "common/logging/log.h" | ||
#include "core/libraries/kernel/file_system.h" | ||
#include "logger.h" | ||
|
||
namespace Core::Devices { | ||
|
||
BaseDevice* Logger::Create(u32 handle, const char* raw_path, int, u16) { | ||
std::string path{raw_path}; | ||
// remove /dev/ prefix | ||
if (path.starts_with("/dev/")) { | ||
path = path.substr(5); | ||
} | ||
bool is_err = path == "stderr"; | ||
return new Logger(handle, path, is_err); | ||
} | ||
|
||
Logger::Logger(u32 handle, std::string prefix, bool is_err) | ||
: handle(handle), prefix(std::move(prefix)), is_err(is_err) {} | ||
|
||
Logger::~Logger() = default; | ||
|
||
s64 Logger::write(const void* buf, size_t nbytes) { | ||
log(static_cast<const char*>(buf), nbytes); | ||
return nbytes; | ||
} | ||
size_t Logger::writev(const Libraries::Kernel::SceKernelIovec* iov, int iovcnt) { | ||
for (int i = 0; i < iovcnt; i++) { | ||
log(static_cast<const char*>(iov[i].iov_base), iov[i].iov_len); | ||
} | ||
return iovcnt; | ||
} | ||
|
||
s64 Logger::pwrite(const void* buf, size_t nbytes, u64 offset) { | ||
log(static_cast<const char*>(buf), nbytes); | ||
return nbytes; | ||
} | ||
|
||
s32 Logger::fsync() { | ||
log_flush(); | ||
return 0; | ||
} | ||
|
||
void Logger::log(const char* buf, size_t nbytes) { | ||
std::scoped_lock lock{mtx}; | ||
const char* end = buf + nbytes; | ||
for (const char* it = buf; it < end; ++it) { | ||
char c = *it; | ||
if (c == '\r') { | ||
continue; | ||
} | ||
if (c == '\n') { | ||
log_flush(); | ||
continue; | ||
} | ||
buffer.push_back(c); | ||
} | ||
} | ||
|
||
void Logger::log_flush() { | ||
std::scoped_lock lock{mtx}; | ||
if (buffer.empty()) { | ||
return; | ||
} | ||
if (is_err) { | ||
LOG_ERROR(Tty, "[{}] {}", prefix, std::string_view{buffer}); | ||
} else { | ||
LOG_INFO(Tty, "[{}] {}", prefix, std::string_view{buffer}); | ||
} | ||
buffer.clear(); | ||
} | ||
|
||
} // namespace Core::Devices |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#pragma once | ||
|
||
#include "base_device.h" | ||
|
||
#include <mutex> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace Core::Devices { | ||
|
||
class Logger final : BaseDevice { | ||
u32 handle; | ||
std::string prefix; | ||
bool is_err; | ||
|
||
std::recursive_mutex mtx; | ||
std::vector<char> buffer; | ||
|
||
public: | ||
static BaseDevice* Create(u32 handle, const char*, int, u16); | ||
|
||
explicit Logger(u32 handle, std::string prefix, bool is_err); | ||
|
||
~Logger() override; | ||
|
||
s64 write(const void* buf, size_t nbytes) override; | ||
size_t writev(const Libraries::Kernel::SceKernelIovec* iov, int iovcnt) override; | ||
s64 pwrite(const void* buf, size_t nbytes, u64 offset) override; | ||
|
||
s32 fsync() override; | ||
|
||
private: | ||
void log(const char* buf, size_t nbytes); | ||
void log_flush(); | ||
}; | ||
|
||
} // namespace Core::Devices |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#pragma once | ||
#include "base_device.h" | ||
|
||
namespace Core::Devices { | ||
|
||
class NopDevice final : BaseDevice { | ||
u32 handle; | ||
|
||
public: | ||
static BaseDevice* Create(u32 handle, const char*, int, u16) { | ||
return new NopDevice(handle); | ||
}; | ||
|
||
explicit NopDevice(u32 handle) : handle(handle) {} | ||
|
||
~NopDevice() override = default; | ||
|
||
int ioctl(u64 cmd, Common::VaCtx* args) override { | ||
return 0; | ||
} | ||
s64 write(const void* buf, size_t nbytes) override { | ||
return 0; | ||
} | ||
size_t readv(const Libraries::Kernel::SceKernelIovec* iov, int iovcnt) override { | ||
return 0; | ||
} | ||
size_t writev(const Libraries::Kernel::SceKernelIovec* iov, int iovcnt) override { | ||
return 0; | ||
} | ||
s64 preadv(const Libraries::Kernel::SceKernelIovec* iov, int iovcnt, u64 offset) override { | ||
return 0; | ||
} | ||
s64 lseek(s64 offset, int whence) override { | ||
return 0; | ||
} | ||
s64 read(void* buf, size_t nbytes) override { | ||
return 0; | ||
} | ||
int fstat(Libraries::Kernel::OrbisKernelStat* sb) override { | ||
return 0; | ||
} | ||
s32 fsync() override { | ||
return 0; | ||
} | ||
int ftruncate(s64 length) override { | ||
return 0; | ||
} | ||
int getdents(void* buf, u32 nbytes, s64* basep) override { | ||
return 0; | ||
} | ||
s64 pwrite(const void* buf, size_t nbytes, u64 offset) override { | ||
return 0; | ||
} | ||
}; | ||
|
||
} // namespace Core::Devices |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters