public release
This commit is contained in:
72
sphaira/source/log.cpp
Normal file
72
sphaira/source/log.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "log.hpp"
|
||||
#include <cstdio>
|
||||
#include <cstdarg>
|
||||
#include <unistd.h>
|
||||
#include <mutex>
|
||||
#include <switch.h>
|
||||
|
||||
#if sphaira_USE_LOG
|
||||
namespace {
|
||||
|
||||
constexpr const char* logpath = "/config/sphaira/log.txt";
|
||||
|
||||
std::FILE* file{};
|
||||
int nxlink_socket{};
|
||||
std::mutex mutex{};
|
||||
|
||||
} // namespace
|
||||
|
||||
auto log_file_init() -> bool {
|
||||
std::scoped_lock lock{mutex};
|
||||
if (file) {
|
||||
return false;
|
||||
}
|
||||
|
||||
file = std::fopen(logpath, "w");
|
||||
return file != nullptr;
|
||||
}
|
||||
|
||||
auto log_nxlink_init() -> bool {
|
||||
std::scoped_lock lock{mutex};
|
||||
if (nxlink_socket) {
|
||||
return false;
|
||||
}
|
||||
|
||||
nxlink_socket = nxlinkConnectToHost(true, false);
|
||||
return nxlink_socket != 0;
|
||||
}
|
||||
|
||||
void log_file_exit() {
|
||||
std::scoped_lock lock{mutex};
|
||||
if (file) {
|
||||
std::fclose(file);
|
||||
file = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void log_nxlink_exit() {
|
||||
std::scoped_lock lock{mutex};
|
||||
if (nxlink_socket) {
|
||||
close(nxlink_socket);
|
||||
nxlink_socket = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void log_write(const char* s, ...) {
|
||||
std::scoped_lock lock{mutex};
|
||||
if (!file && !nxlink_socket) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::va_list v{};
|
||||
va_start(v, s);
|
||||
if (file) {
|
||||
std::vfprintf(file, s, v);
|
||||
std::fflush(file);
|
||||
}
|
||||
if (nxlink_socket) {
|
||||
std::vprintf(s, v);
|
||||
}
|
||||
va_end(v);
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user