| 70 | } |
| 71 | |
| 72 | void Logging::init() |
| 73 | { |
| 74 | startTime = std::chrono::steady_clock::now(); |
| 75 | |
| 76 | // Get current date for log filename |
| 77 | auto now = std::chrono::system_clock::now(); |
| 78 | auto now_time_t = std::chrono::system_clock::to_time_t(now); |
| 79 | std::tm* now_tm = std::localtime(&now_time_t); |
| 80 | |
| 81 | char dateBuf[9]; |
| 82 | std::strftime(dateBuf, sizeof(dateBuf), "%Y%m%d", now_tm); |
| 83 | |
| 84 | #if defined(__3DS__) |
| 85 | logFilePath = std::string("sdmc:/3ds/Checkpoint/logs/checkpoint_") + dateBuf + ".log"; |
| 86 | #elif defined(__SWITCH__) |
| 87 | logFilePath = std::string("/switch/Checkpoint/logs/checkpoint_") + dateBuf + ".log"; |
| 88 | #else |
| 89 | logFilePath = std::string("checkpoint_") + dateBuf + ".log"; |
| 90 | #endif |
| 91 | |
| 92 | logBuffer.reserve(LOG_BUFFER_SIZE); |
| 93 | |
| 94 | std::string versionInfo = std::format("Checkpoint v{:d}.{:d}.{:d}-{:s}", VERSION_MAJOR, VERSION_MINOR, VERSION_MICRO, GIT_REV); |
| 95 | info(versionInfo); |
| 96 | |
| 97 | #if defined(SERVER_HPP) |
| 98 | Server::registerHandler("/logs/memory", |
| 99 | [](const std::string& path, const std::string& requestData) -> Server::HttpResponse { return {200, "text/plain", getApplicationLogs()}; }); |
| 100 | |
| 101 | Server::registerHandler("/logs/file", [](const std::string& path, const std::string& requestData) -> Server::HttpResponse { |
| 102 | std::lock_guard<std::mutex> lock(logMutex); |
| 103 | flushLogBuffer(); |
| 104 | |
| 105 | // Read through the already-open append handle instead of a second fopen(): |
| 106 | // Switch's FS sysmodule refuses opening a file for read while it is open |
| 107 | // for write, so a separate read handle returns null there and 404s. The |
| 108 | // handle is opened "a+", so reads are non-destructive and writes still |
| 109 | // append; we fflush + rewind, read, then seek back to end. |
| 110 | if (logFile == nullptr) { |
| 111 | return {404, "text/plain", "Log file not found"}; |
| 112 | } |
| 113 | |
| 114 | fflush(logFile); |
| 115 | fseek(logFile, 0, SEEK_END); |
| 116 | long fileSize = ftell(logFile); |
| 117 | fseek(logFile, 0, SEEK_SET); |
| 118 | |
| 119 | std::string logData; |
| 120 | if (fileSize > 0) { |
| 121 | logData.resize(fileSize); |
| 122 | size_t read = fread(logData.data(), 1, fileSize, logFile); |
| 123 | logData.resize(read); |
| 124 | } |
| 125 | fseek(logFile, 0, SEEK_END); |
| 126 | |
| 127 | return {200, "text/plain", logData}; |
| 128 | }); |
| 129 | #endif |
nothing calls this directly
no test coverage detected