2021-03-07 23:22:49 +00:00
|
|
|
/*
|
2021-03-26 04:35:14 +00:00
|
|
|
* nxdt_log.c
|
2021-03-07 23:22:49 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) 2020-2021, DarkMatterCore <pabloacurielz@gmail.com>.
|
|
|
|
*
|
|
|
|
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
|
|
|
|
*
|
2021-03-25 19:26:58 +00:00
|
|
|
* nxdumptool 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, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2021-03-07 23:22:49 +00:00
|
|
|
*
|
2021-03-25 19:26:58 +00:00
|
|
|
* nxdumptool 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 for more details.
|
2021-03-07 23:22:49 +00:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-03-25 19:26:58 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2021-03-07 23:22:49 +00:00
|
|
|
*/
|
|
|
|
|
2021-03-26 04:35:14 +00:00
|
|
|
#include "nxdt_utils.h"
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
/* Global variables. */
|
|
|
|
|
|
|
|
static Mutex g_logMutex = 0;
|
|
|
|
|
2021-03-08 14:44:11 +00:00
|
|
|
static char g_lastLogMsg[0x100] = {0};
|
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
static FsFile g_logFile = {0};
|
|
|
|
static s64 g_logFileOffset = 0;
|
|
|
|
|
|
|
|
static char *g_logBuffer = NULL;
|
|
|
|
static size_t g_logBufferLength = 0;
|
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
static const char *g_logStrFormat = "[%d-%02d-%02d %02d:%02d:%02d.%09lu] %s -> ";
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
/* Function prototypes. */
|
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
static void _logWriteStringToLogFile(const char *src);
|
|
|
|
static void _logWriteFormattedStringToLogFile(bool save, const char *func_name, const char *fmt, va_list args);
|
2021-03-07 23:22:49 +00:00
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
static void _logFlushLogFile(void);
|
2021-03-07 23:22:49 +00:00
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
static bool logAllocateLogBuffer(void);
|
|
|
|
static bool logOpenLogFile(void);
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
void logWriteStringToLogFile(const char *src)
|
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
SCOPED_LOCK(&g_logMutex) _logWriteStringToLogFile(src);
|
2021-03-07 23:22:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 19:27:35 +01:00
|
|
|
__attribute__((format(printf, 2, 3))) void logWriteFormattedStringToLogFile(const char *func_name, const char *fmt, ...)
|
2021-03-07 23:22:49 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2021-05-18 13:32:43 +01:00
|
|
|
SCOPED_LOCK(&g_logMutex) _logWriteFormattedStringToLogFile(true, func_name, fmt, args);
|
2021-03-07 23:22:49 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2021-03-29 19:27:35 +01:00
|
|
|
__attribute__((format(printf, 4, 5))) void logWriteFormattedStringToBuffer(char **dst, size_t *dst_size, const char *func_name, const char *fmt, ...)
|
2021-03-07 23:22:49 +00:00
|
|
|
{
|
|
|
|
if (!dst || !dst_size || (!*dst && *dst_size) || (*dst && !*dst_size) || !func_name || !*func_name || !fmt || !*fmt) return;
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
int str1_len = 0, str2_len = 0;
|
|
|
|
size_t log_str_len = 0;
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
char *dst_ptr = *dst, *tmp_str = NULL;
|
|
|
|
size_t dst_cur_size = *dst_size, dst_str_len = (dst_ptr ? strlen(dst_ptr) : 0);
|
|
|
|
|
|
|
|
if (dst_str_len >= dst_cur_size) return;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
|
|
|
/* Get current time with nanosecond precision. */
|
|
|
|
struct timespec now = {0};
|
|
|
|
clock_gettime(CLOCK_REALTIME, &now);
|
|
|
|
|
|
|
|
/* Get local time. */
|
|
|
|
struct tm *ts = localtime(&(now.tv_sec));
|
|
|
|
ts->tm_year += 1900;
|
|
|
|
ts->tm_mon++;
|
|
|
|
|
|
|
|
/* Get formatted string length. */
|
|
|
|
str1_len = snprintf(NULL, 0, g_logStrFormat, ts->tm_year, ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec, now.tv_nsec, func_name);
|
2021-03-08 11:11:28 +00:00
|
|
|
if (str1_len <= 0) goto end;
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
str2_len = vsnprintf(NULL, 0, fmt, args);
|
2021-03-08 11:11:28 +00:00
|
|
|
if (str2_len <= 0) goto end;
|
2021-03-07 23:22:49 +00:00
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
log_str_len = (size_t)(str1_len + str2_len + 3);
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
if (!dst_cur_size || log_str_len > (dst_cur_size - dst_str_len))
|
|
|
|
{
|
|
|
|
/* Update buffer size. */
|
|
|
|
dst_cur_size = (dst_str_len + log_str_len);
|
|
|
|
|
|
|
|
/* Reallocate buffer. */
|
|
|
|
tmp_str = realloc(dst_ptr, dst_cur_size);
|
|
|
|
if (!tmp_str) goto end;
|
|
|
|
|
|
|
|
dst_ptr = tmp_str;
|
|
|
|
tmp_str = NULL;
|
|
|
|
|
|
|
|
/* Clear allocated area. */
|
|
|
|
memset(dst_ptr + dst_str_len, 0, log_str_len);
|
|
|
|
|
|
|
|
/* Update pointers. */
|
|
|
|
*dst = dst_ptr;
|
|
|
|
*dst_size = dst_cur_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate formatted string. */
|
|
|
|
sprintf(dst_ptr + dst_str_len, g_logStrFormat, ts->tm_year, ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec, now.tv_nsec, func_name);
|
2021-03-08 11:11:28 +00:00
|
|
|
vsprintf(dst_ptr + dst_str_len + (size_t)str1_len, fmt, args);
|
2021-07-21 16:04:18 +01:00
|
|
|
strcat(dst_ptr, CRLF);
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
end:
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2021-03-29 19:27:35 +01:00
|
|
|
__attribute__((format(printf, 4, 5))) void logWriteBinaryDataToLogFile(const void *data, size_t data_size, const char *func_name, const char *fmt, ...)
|
2021-03-07 23:22:49 +00:00
|
|
|
{
|
|
|
|
if (!data || !data_size || !func_name || !*func_name || !fmt || !*fmt) return;
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
size_t data_str_size = ((data_size * 2) + 3);
|
|
|
|
char *data_str = NULL;
|
|
|
|
|
|
|
|
/* Allocate memory for the hex string representation of the provided binary data. */
|
|
|
|
data_str = calloc(data_str_size, sizeof(char));
|
|
|
|
if (!data_str) goto end;
|
|
|
|
|
|
|
|
/* Generate hex string representation. */
|
2021-05-11 23:36:15 +01:00
|
|
|
utilsGenerateHexStringFromData(data_str, data_str_size, data, data_size, true);
|
2021-07-21 16:04:18 +01:00
|
|
|
strcat(data_str, CRLF);
|
2021-03-07 23:22:49 +00:00
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
SCOPED_LOCK(&g_logMutex)
|
|
|
|
{
|
|
|
|
/* Write formatted string. */
|
|
|
|
va_start(args, fmt);
|
|
|
|
_logWriteFormattedStringToLogFile(false, func_name, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
/* Write hex string representation. */
|
|
|
|
_logWriteStringToLogFile(data_str);
|
|
|
|
}
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
end:
|
|
|
|
if (data_str) free(data_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void logFlushLogFile(void)
|
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
SCOPED_LOCK(&g_logMutex) _logFlushLogFile();
|
2021-03-07 23:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void logCloseLogFile(void)
|
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
SCOPED_LOCK(&g_logMutex)
|
2021-03-07 23:22:49 +00:00
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
/* Flush log buffer. */
|
|
|
|
_logFlushLogFile();
|
2021-03-07 23:22:49 +00:00
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
/* Close logfile. */
|
|
|
|
if (serviceIsActive(&(g_logFile.s)))
|
|
|
|
{
|
|
|
|
fsFileClose(&g_logFile);
|
|
|
|
memset(&g_logFile, 0, sizeof(FsFile));
|
|
|
|
|
|
|
|
/* Commit SD card filesystem changes. */
|
|
|
|
utilsCommitSdCardFileSystemChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free log buffer. */
|
|
|
|
if (g_logBuffer)
|
|
|
|
{
|
|
|
|
free(g_logBuffer);
|
|
|
|
g_logBuffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset logfile offset. */
|
|
|
|
g_logFileOffset = 0;
|
2021-03-07 23:22:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 14:44:11 +00:00
|
|
|
void logGetLastMessage(char *dst, size_t dst_size)
|
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
SCOPED_LOCK(&g_logMutex)
|
|
|
|
{
|
|
|
|
if (dst && dst_size > 1 && *g_lastLogMsg) snprintf(dst, dst_size, "%s", g_lastLogMsg);
|
|
|
|
}
|
2021-03-08 14:44:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
void logControlMutex(bool lock)
|
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
bool locked = mutexIsLockedByCurrentThread(&g_logMutex);
|
|
|
|
|
|
|
|
if (!locked && lock)
|
2021-03-07 23:22:49 +00:00
|
|
|
{
|
|
|
|
mutexLock(&g_logMutex);
|
2021-05-18 13:32:43 +01:00
|
|
|
} else
|
|
|
|
if (locked && !lock)
|
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
mutexUnlock(&g_logMutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
static void _logWriteStringToLogFile(const char *src)
|
2021-03-07 23:22:49 +00:00
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
/* Make sure we have allocated memory for the log buffer and opened the logfile. */
|
|
|
|
if (!src || !*src || !logAllocateLogBuffer() || !logOpenLogFile()) return;
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
Result rc = 0;
|
|
|
|
size_t src_len = strlen(src), tmp_len = 0;
|
|
|
|
|
|
|
|
/* Check if the formatted string length is lower than the log buffer size. */
|
|
|
|
if (src_len < LOG_BUF_SIZE)
|
|
|
|
{
|
|
|
|
/* Flush log buffer contents (if needed). */
|
|
|
|
if ((g_logBufferLength + src_len) >= LOG_BUF_SIZE)
|
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
_logFlushLogFile();
|
|
|
|
if (g_logBufferLength) return;
|
2021-03-07 23:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy string into the log buffer. */
|
|
|
|
strcpy(g_logBuffer + g_logBufferLength, src);
|
|
|
|
g_logBufferLength += src_len;
|
|
|
|
} else {
|
|
|
|
/* Flush log buffer. */
|
2021-05-18 13:32:43 +01:00
|
|
|
_logFlushLogFile();
|
|
|
|
if (g_logBufferLength) return;
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
/* Write string data until it no longer exceeds the log buffer size. */
|
|
|
|
while(src_len >= LOG_BUF_SIZE)
|
|
|
|
{
|
|
|
|
rc = fsFileWrite(&g_logFile, g_logFileOffset, src + tmp_len, LOG_BUF_SIZE, FsWriteOption_Flush);
|
2021-05-18 13:32:43 +01:00
|
|
|
if (R_FAILED(rc)) return;
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
g_logFileOffset += LOG_BUF_SIZE;
|
|
|
|
tmp_len += LOG_BUF_SIZE;
|
|
|
|
src_len -= LOG_BUF_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy any remaining data from the string into the log buffer. */
|
|
|
|
if (src_len)
|
|
|
|
{
|
|
|
|
strcpy(g_logBuffer, src + tmp_len);
|
|
|
|
g_logBufferLength = src_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if LOG_FORCE_FLUSH == 1
|
|
|
|
/* Flush log buffer. */
|
2021-05-18 13:32:43 +01:00
|
|
|
_logFlushLogFile();
|
2021-03-07 23:22:49 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
static void _logWriteFormattedStringToLogFile(bool save, const char *func_name, const char *fmt, va_list args)
|
2021-03-07 23:22:49 +00:00
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
/* Make sure we have allocated memory for the log buffer and opened the logfile. */
|
|
|
|
if (!func_name || !*func_name || !fmt || !*fmt || !logAllocateLogBuffer() || !logOpenLogFile()) return;
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
Result rc = 0;
|
2021-03-08 11:11:28 +00:00
|
|
|
|
|
|
|
int str1_len = 0, str2_len = 0;
|
|
|
|
size_t log_str_len = 0;
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
char *tmp_str = NULL;
|
|
|
|
size_t tmp_len = 0;
|
|
|
|
|
|
|
|
/* Get current time with nanosecond precision. */
|
|
|
|
struct timespec now = {0};
|
|
|
|
clock_gettime(CLOCK_REALTIME, &now);
|
|
|
|
|
|
|
|
/* Get local time. */
|
|
|
|
struct tm *ts = localtime(&(now.tv_sec));
|
|
|
|
ts->tm_year += 1900;
|
|
|
|
ts->tm_mon++;
|
|
|
|
|
|
|
|
/* Get formatted string length. */
|
|
|
|
str1_len = snprintf(NULL, 0, g_logStrFormat, ts->tm_year, ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec, now.tv_nsec, func_name);
|
2021-05-18 13:32:43 +01:00
|
|
|
if (str1_len <= 0) return;
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
str2_len = vsnprintf(NULL, 0, fmt, args);
|
2021-05-18 13:32:43 +01:00
|
|
|
if (str2_len <= 0) return;
|
2021-03-07 23:22:49 +00:00
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
log_str_len = (size_t)(str1_len + str2_len + 2);
|
2021-03-07 23:22:49 +00:00
|
|
|
|
2021-03-08 14:44:11 +00:00
|
|
|
/* Save log message to our global stack buffer (if needed). */
|
|
|
|
if (save)
|
|
|
|
{
|
|
|
|
tmp_len = (strlen(func_name) + 2);
|
|
|
|
if ((tmp_len + (size_t)str2_len) < sizeof(g_lastLogMsg))
|
|
|
|
{
|
|
|
|
sprintf(g_lastLogMsg, "%s: ", func_name);
|
|
|
|
vsprintf(g_lastLogMsg + tmp_len, fmt, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp_len = 0;
|
|
|
|
}
|
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* Check if the formatted string length is less than the log buffer size. */
|
|
|
|
if (log_str_len < LOG_BUF_SIZE)
|
|
|
|
{
|
|
|
|
/* Flush log buffer contents (if needed). */
|
|
|
|
if ((g_logBufferLength + log_str_len) >= LOG_BUF_SIZE)
|
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
_logFlushLogFile();
|
|
|
|
if (g_logBufferLength) return;
|
2021-03-07 23:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Nice and easy string formatting using the log buffer. */
|
|
|
|
sprintf(g_logBuffer + g_logBufferLength, g_logStrFormat, ts->tm_year, ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec, now.tv_nsec, func_name);
|
2021-03-08 11:11:28 +00:00
|
|
|
vsprintf(g_logBuffer + g_logBufferLength + (size_t)str1_len, fmt, args);
|
2021-07-21 16:04:18 +01:00
|
|
|
strcat(g_logBuffer, CRLF);
|
2021-03-07 23:22:49 +00:00
|
|
|
g_logBufferLength += log_str_len;
|
|
|
|
} else {
|
|
|
|
/* Flush log buffer. */
|
2021-05-18 13:32:43 +01:00
|
|
|
_logFlushLogFile();
|
|
|
|
if (g_logBufferLength) return;
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
/* Allocate memory for a temporary buffer. This will hold the formatted string. */
|
|
|
|
tmp_str = calloc(log_str_len + 1, sizeof(char));
|
2021-05-18 13:32:43 +01:00
|
|
|
if (!tmp_str) return;
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
/* Generate formatted string. */
|
|
|
|
sprintf(tmp_str, g_logStrFormat, ts->tm_year, ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec, now.tv_nsec, func_name);
|
2021-03-08 11:11:28 +00:00
|
|
|
vsprintf(tmp_str + (size_t)str1_len, fmt, args);
|
2021-07-21 16:04:18 +01:00
|
|
|
strcat(tmp_str, CRLF);
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
/* Write formatted string data until it no longer exceeds the log buffer size. */
|
|
|
|
while(log_str_len >= LOG_BUF_SIZE)
|
|
|
|
{
|
|
|
|
rc = fsFileWrite(&g_logFile, g_logFileOffset, tmp_str + tmp_len, LOG_BUF_SIZE, FsWriteOption_Flush);
|
|
|
|
if (R_FAILED(rc)) goto end;
|
|
|
|
|
|
|
|
g_logFileOffset += LOG_BUF_SIZE;
|
|
|
|
tmp_len += LOG_BUF_SIZE;
|
|
|
|
log_str_len -= LOG_BUF_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy any remaining data from the formatted string into the log buffer. */
|
|
|
|
if (log_str_len)
|
|
|
|
{
|
|
|
|
strcpy(g_logBuffer, tmp_str + tmp_len);
|
|
|
|
g_logBufferLength = log_str_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if LOG_FORCE_FLUSH == 1
|
|
|
|
/* Flush log buffer. */
|
2021-05-18 13:32:43 +01:00
|
|
|
_logFlushLogFile();
|
2021-03-07 23:22:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (tmp_str) free(tmp_str);
|
|
|
|
}
|
2021-03-08 11:11:28 +00:00
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
static void _logFlushLogFile(void)
|
2021-03-08 11:11:28 +00:00
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
if (!serviceIsActive(&(g_logFile.s)) || !g_logBuffer || !g_logBufferLength) return;
|
2021-03-08 11:11:28 +00:00
|
|
|
|
|
|
|
/* Write log buffer contents and flush the written data right away. */
|
|
|
|
Result rc = fsFileWrite(&g_logFile, g_logFileOffset, g_logBuffer, g_logBufferLength, FsWriteOption_Flush);
|
|
|
|
if (R_SUCCEEDED(rc))
|
|
|
|
{
|
|
|
|
/* Update global variables. */
|
|
|
|
g_logFileOffset += (s64)g_logBufferLength;
|
|
|
|
*g_logBuffer = '\0';
|
|
|
|
g_logBufferLength = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool logAllocateLogBuffer(void)
|
|
|
|
{
|
|
|
|
if (g_logBuffer) return true;
|
|
|
|
g_logBuffer = memalign(LOG_BUF_SIZE, LOG_BUF_SIZE);
|
|
|
|
return (g_logBuffer != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool logOpenLogFile(void)
|
|
|
|
{
|
|
|
|
if (serviceIsActive(&(g_logFile.s))) return true;
|
|
|
|
|
|
|
|
Result rc = 0;
|
2021-05-18 13:32:43 +01:00
|
|
|
bool use_root = true;
|
|
|
|
const char *launch_path = utilsGetLaunchPath();
|
2021-03-08 11:11:28 +00:00
|
|
|
char path[FS_MAX_PATH] = {0}, *ptr1 = NULL, *ptr2 = NULL;
|
|
|
|
|
|
|
|
/* Get SD card FsFileSystem object. */
|
|
|
|
FsFileSystem *sdmc_fs = utilsGetSdCardFileSystemObject();
|
|
|
|
if (!sdmc_fs) return false;
|
|
|
|
|
|
|
|
/* Generate logfile path. */
|
2021-05-18 13:32:43 +01:00
|
|
|
if (launch_path)
|
2021-03-08 11:11:28 +00:00
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
ptr1 = strchr(launch_path, '/');
|
|
|
|
ptr2 = strrchr(launch_path, '/');
|
2021-03-08 11:11:28 +00:00
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
if (ptr1 && ptr2 && ptr1 != ptr2)
|
2021-03-08 11:11:28 +00:00
|
|
|
{
|
|
|
|
/* Create logfile in the current working directory. */
|
2021-05-18 13:32:43 +01:00
|
|
|
snprintf(path, sizeof(path), "%.*s" LOG_FILE_NAME, (int)((ptr2 - ptr1) + 1), ptr1);
|
|
|
|
use_root = false;
|
2021-03-08 11:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
/* Create logfile in the SD card root directory. */
|
|
|
|
if (use_root) sprintf(path, "/" LOG_FILE_NAME);
|
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
/* Create file. This will fail if the logfile exists, so we don't check its return value. */
|
|
|
|
fsFsCreateFile(sdmc_fs, path, 0, 0);
|
|
|
|
|
|
|
|
/* Open file. */
|
|
|
|
rc = fsFsOpenFile(sdmc_fs, path, FsOpenMode_Write | FsOpenMode_Append, &g_logFile);
|
|
|
|
if (R_SUCCEEDED(rc))
|
|
|
|
{
|
|
|
|
/* Get file size. */
|
|
|
|
rc = fsFileGetSize(&g_logFile, &g_logFileOffset);
|
|
|
|
if (R_SUCCEEDED(rc))
|
|
|
|
{
|
|
|
|
/* Write UTF-8 BOM right away (if needed). */
|
|
|
|
if (!g_logFileOffset)
|
|
|
|
{
|
2021-07-21 16:04:18 +01:00
|
|
|
size_t utf8_bom_len = strlen(UTF8_BOM);
|
|
|
|
fsFileWrite(&g_logFile, g_logFileOffset, UTF8_BOM, utf8_bom_len, FsWriteOption_Flush);
|
2021-03-08 11:11:28 +00:00
|
|
|
g_logFileOffset += (s64)utf8_bom_len;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fsFileClose(&g_logFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return R_SUCCEEDED(rc);
|
|
|
|
}
|