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
|
|
|
*
|
2024-04-12 10:47:36 +01:00
|
|
|
* Copyright (c) 2020-2024, DarkMatterCore <pabloacurielz@gmail.com>.
|
2021-03-07 23:22:49 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2024-04-30 22:01:42 +01:00
|
|
|
#include <core/nxdt_utils.h>
|
2021-03-07 23:22:49 +00:00
|
|
|
|
2022-07-12 01:27:03 +01:00
|
|
|
#if (LOG_LEVEL >= LOG_LEVEL_DEBUG) && (LOG_LEVEL < LOG_LEVEL_NONE)
|
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* Global variables. */
|
|
|
|
|
|
|
|
static Mutex g_logMutex = 0;
|
|
|
|
|
2024-01-20 22:12:55 +00:00
|
|
|
static char *g_lastLogMsg = NULL;
|
2021-03-08 14:44:11 +00:00
|
|
|
|
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;
|
|
|
|
|
2022-07-12 01:27:03 +01:00
|
|
|
static const char *g_logStrFormat = "[%d-%02d-%02d %02d:%02d:%02d.%09lu] [%s] %s:%d:%s -> ";
|
|
|
|
static const char *g_logSessionSeparator = "________________________________________________________________\r\n";
|
|
|
|
|
|
|
|
static const char *g_logLevelNames[] = {
|
|
|
|
[LOG_LEVEL_DEBUG] = "DEBUG",
|
|
|
|
[LOG_LEVEL_INFO] = "INFO",
|
|
|
|
[LOG_LEVEL_WARNING] = "WARNING",
|
|
|
|
[LOG_LEVEL_ERROR] = "ERROR"
|
|
|
|
};
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
/* Function prototypes. */
|
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
static void _logWriteStringToLogFile(const char *src);
|
2022-07-12 01:27:03 +01:00
|
|
|
static void _logWriteFormattedStringToLogFile(bool save, u8 level, const char *file_name, int line, 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
|
|
|
|
2022-07-12 01:27:03 +01:00
|
|
|
static void logWriteStringToNxLink(const char *str);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-12 01:27:03 +01:00
|
|
|
__attribute__((format(printf, 5, 6))) void logWriteFormattedStringToLogFile(u8 level, const char *file_name, int line, const char *func_name, const char *fmt, ...)
|
2021-03-07 23:22:49 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2022-07-12 01:27:03 +01:00
|
|
|
SCOPED_LOCK(&g_logMutex) _logWriteFormattedStringToLogFile(true, level, file_name, line, func_name, fmt, args);
|
2021-03-07 23:22:49 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2022-07-12 01:27:03 +01:00
|
|
|
__attribute__((format(printf, 7, 8))) void logWriteFormattedStringToBuffer(char **dst, size_t *dst_size, u8 level, const char *file_name, int line, const char *func_name, const char *fmt, ...)
|
2021-03-07 23:22:49 +00:00
|
|
|
{
|
2023-07-17 00:03:05 +01:00
|
|
|
if (!dst || !dst_size || level < LOG_LEVEL || !file_name || !*file_name || !func_name || !*func_name || !fmt || !*fmt) return;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
va_list args;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
int str1_len = 0, str2_len = 0;
|
|
|
|
size_t log_str_len = 0;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
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);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-08-07 09:42:03 +01:00
|
|
|
struct tm ts = {0};
|
|
|
|
struct timespec now = {0};
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-07-17 00:03:05 +01:00
|
|
|
/* Sanity check. */
|
|
|
|
if (dst_cur_size && dst_str_len >= dst_cur_size) return;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
va_start(args, fmt);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* Get current time with nanosecond precision. */
|
|
|
|
clock_gettime(CLOCK_REALTIME, &now);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* Get local time. */
|
2021-08-07 09:42:03 +01:00
|
|
|
localtime_r(&(now.tv_sec), &ts);
|
|
|
|
ts.tm_year += 1900;
|
|
|
|
ts.tm_mon++;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* Get formatted string length. */
|
2022-07-12 01:27:03 +01:00
|
|
|
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, g_logLevelNames[level], file_name, line, func_name);
|
2021-03-08 11:11:28 +00:00
|
|
|
if (str1_len <= 0) goto end;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
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;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
log_str_len = (size_t)(str1_len + str2_len + 3);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-07-17 00:03:05 +01:00
|
|
|
if (!dst_ptr || !dst_cur_size || log_str_len > (dst_cur_size - dst_str_len))
|
2021-03-07 23:22:49 +00:00
|
|
|
{
|
|
|
|
/* Update buffer size. */
|
|
|
|
dst_cur_size = (dst_str_len + log_str_len);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* Reallocate buffer. */
|
|
|
|
tmp_str = realloc(dst_ptr, dst_cur_size);
|
|
|
|
if (!tmp_str) goto end;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
dst_ptr = tmp_str;
|
|
|
|
tmp_str = NULL;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* Clear allocated area. */
|
|
|
|
memset(dst_ptr + dst_str_len, 0, log_str_len);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* Update pointers. */
|
|
|
|
*dst = dst_ptr;
|
|
|
|
*dst_size = dst_cur_size;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* Generate formatted string. */
|
2022-07-12 01:27:03 +01:00
|
|
|
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, g_logLevelNames[level], file_name, line, 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);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
end:
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2022-07-12 01:27:03 +01:00
|
|
|
__attribute__((format(printf, 7, 8))) void logWriteBinaryDataToLogFile(const void *data, size_t data_size, u8 level, const char *file_name, int line, const char *func_name, const char *fmt, ...)
|
2021-03-07 23:22:49 +00:00
|
|
|
{
|
2022-07-12 04:27:30 +01:00
|
|
|
if (!data || !data_size || level < LOG_LEVEL || !file_name || !*file_name || !func_name || !*func_name || !fmt || !*fmt) return;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
va_list args;
|
|
|
|
size_t data_str_size = ((data_size * 2) + 3);
|
|
|
|
char *data_str = NULL;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* 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;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* Generate hex string representation. */
|
2023-10-22 23:44:40 +01:00
|
|
|
utilsGenerateHexString(data_str, data_str_size, data, data_size, true);
|
2021-07-21 16:04:18 +01:00
|
|
|
strcat(data_str, CRLF);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
SCOPED_LOCK(&g_logMutex)
|
|
|
|
{
|
|
|
|
/* Write formatted string. */
|
|
|
|
va_start(args, fmt);
|
2022-07-12 01:27:03 +01:00
|
|
|
_logWriteFormattedStringToLogFile(false, level, file_name, line, func_name, fmt, args);
|
2021-05-18 13:32:43 +01:00
|
|
|
va_end(args);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
/* Write hex string representation. */
|
|
|
|
_logWriteStringToLogFile(data_str);
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
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();
|
2022-07-05 02:04:28 +01: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));
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
/* Commit SD card filesystem changes. */
|
|
|
|
utilsCommitSdCardFileSystemChanges();
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2024-01-20 22:12:55 +00:00
|
|
|
/* Free last message bugger. */
|
|
|
|
if (g_lastLogMsg)
|
|
|
|
{
|
|
|
|
free(g_lastLogMsg);
|
|
|
|
g_lastLogMsg = NULL;
|
|
|
|
}
|
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
/* Free log buffer. */
|
|
|
|
if (g_logBuffer)
|
|
|
|
{
|
|
|
|
free(g_logBuffer);
|
|
|
|
g_logBuffer = NULL;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
/* Reset logfile offset. */
|
|
|
|
g_logFileOffset = 0;
|
2021-03-07 23:22:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 22:12:55 +00:00
|
|
|
char *logGetLastMessage(void)
|
2021-03-08 14:44:11 +00:00
|
|
|
{
|
2024-01-20 22:12:55 +00:00
|
|
|
char *ret = NULL;
|
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
SCOPED_LOCK(&g_logMutex)
|
|
|
|
{
|
2024-01-20 22:12:55 +00:00
|
|
|
if (g_lastLogMsg) ret = strdup(g_lastLogMsg);
|
2021-05-18 13:32:43 +01:00
|
|
|
}
|
2024-01-20 22:12:55 +00:00
|
|
|
|
|
|
|
return ret;
|
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);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
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;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
Result rc = 0;
|
|
|
|
size_t src_len = strlen(src), tmp_len = 0;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* 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
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
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;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
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;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
g_logFileOffset += LOG_BUF_SIZE;
|
|
|
|
tmp_len += LOG_BUF_SIZE;
|
|
|
|
src_len -= LOG_BUF_SIZE;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* Copy any remaining data from the string into the log buffer. */
|
|
|
|
if (src_len)
|
|
|
|
{
|
|
|
|
strcpy(g_logBuffer, src + tmp_len);
|
|
|
|
g_logBufferLength = src_len;
|
|
|
|
}
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2022-07-12 01:27:03 +01:00
|
|
|
/* Write data to nxlink. */
|
|
|
|
logWriteStringToNxLink(src);
|
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
2022-07-12 01:27:03 +01:00
|
|
|
static void _logWriteFormattedStringToLogFile(bool save, u8 level, const char *file_name, int line, 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. */
|
2022-07-12 04:27:30 +01:00
|
|
|
if (level < LOG_LEVEL || !file_name || !*file_name || !func_name || !*func_name || !fmt || !*fmt || !logAllocateLogBuffer() || !logOpenLogFile()) return;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
Result rc = 0;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
int str1_len = 0, str2_len = 0;
|
|
|
|
size_t log_str_len = 0;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
char *tmp_str = NULL;
|
|
|
|
size_t tmp_len = 0;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-08-07 09:42:03 +01:00
|
|
|
struct tm ts = {0};
|
2021-03-07 23:22:49 +00:00
|
|
|
struct timespec now = {0};
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-08-07 09:42:03 +01:00
|
|
|
/* Get current time with nanosecond precision. */
|
2021-03-07 23:22:49 +00:00
|
|
|
clock_gettime(CLOCK_REALTIME, &now);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* Get local time. */
|
2021-08-07 09:42:03 +01:00
|
|
|
localtime_r(&(now.tv_sec), &ts);
|
|
|
|
ts.tm_year += 1900;
|
|
|
|
ts.tm_mon++;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* Get formatted string length. */
|
2022-07-12 01:27:03 +01:00
|
|
|
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, g_logLevelNames[level], file_name, line, func_name);
|
2021-05-18 13:32:43 +01:00
|
|
|
if (str1_len <= 0) return;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
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;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
log_str_len = (size_t)(str1_len + str2_len + 2);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2024-01-20 22:12:55 +00:00
|
|
|
/* Save log message (if needed). */
|
2021-03-08 14:44:11 +00:00
|
|
|
if (save)
|
|
|
|
{
|
2024-01-20 22:12:55 +00:00
|
|
|
if (g_lastLogMsg) free(g_lastLogMsg);
|
|
|
|
|
2021-03-08 14:44:11 +00:00
|
|
|
tmp_len = (strlen(func_name) + 2);
|
2024-01-20 22:12:55 +00:00
|
|
|
|
|
|
|
g_lastLogMsg = calloc(tmp_len + (size_t)str2_len + 1, sizeof(char));
|
|
|
|
if (g_lastLogMsg)
|
2021-03-08 14:44:11 +00:00
|
|
|
{
|
|
|
|
sprintf(g_lastLogMsg, "%s: ", func_name);
|
|
|
|
vsprintf(g_lastLogMsg + tmp_len, fmt, args);
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-08 14:44:11 +00:00
|
|
|
tmp_len = 0;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
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
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* Nice and easy string formatting using the log buffer. */
|
2022-07-12 01:27:03 +01:00
|
|
|
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, g_logLevelNames[level], file_name, line, 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);
|
2022-07-12 01:27:03 +01:00
|
|
|
|
|
|
|
/* Write data to nxlink. */
|
|
|
|
logWriteStringToNxLink(g_logBuffer + g_logBufferLength);
|
|
|
|
|
|
|
|
/* Update log buffer length. */
|
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;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
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;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* Generate formatted string. */
|
2022-07-12 01:27:03 +01:00
|
|
|
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, g_logLevelNames[level], file_name, line, 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);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2022-07-12 01:27:03 +01:00
|
|
|
/* Write data to nxlink. */
|
|
|
|
logWriteStringToNxLink(tmp_str);
|
|
|
|
|
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;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
g_logFileOffset += LOG_BUF_SIZE;
|
|
|
|
tmp_len += LOG_BUF_SIZE;
|
|
|
|
log_str_len -= LOG_BUF_SIZE;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
#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
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-07 23:22:49 +00:00
|
|
|
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;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
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;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
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;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
/* Get SD card FsFileSystem object. */
|
|
|
|
FsFileSystem *sdmc_fs = utilsGetSdCardFileSystemObject();
|
|
|
|
if (!sdmc_fs) return false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
/* 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, '/');
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
if (ptr1 && ptr2 && ptr1 != ptr2)
|
2021-03-08 11:11:28 +00:00
|
|
|
{
|
2023-05-24 20:05:34 +01:00
|
|
|
/* Create logfile in the current working directory. Strip the devoptab device name prefix while we're at it, since we won't need it. */
|
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
|
|
|
}
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-05-24 20:05:34 +01:00
|
|
|
/* Create logfile in the SD card root directory, if needed. */
|
2021-05-18 13:32:43 +01:00
|
|
|
if (use_root) sprintf(path, "/" LOG_FILE_NAME);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
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);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
/* 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))
|
|
|
|
{
|
2022-07-12 01:27:03 +01:00
|
|
|
size_t len = 0;
|
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
if (!g_logFileOffset)
|
|
|
|
{
|
2023-05-24 20:05:34 +01:00
|
|
|
/* Write UTF-8 BOM if the logfile is empty. */
|
2022-07-12 01:27:03 +01:00
|
|
|
len = strlen(UTF8_BOM);
|
2023-05-24 20:05:34 +01:00
|
|
|
rc = fsFileWrite(&g_logFile, g_logFileOffset, UTF8_BOM, len, FsWriteOption_Flush);
|
|
|
|
} else {
|
|
|
|
/* Write session separator if the logfile isn't empty. */
|
|
|
|
len = strlen(g_logSessionSeparator);
|
|
|
|
rc = fsFileWrite(&g_logFile, g_logFileOffset, g_logSessionSeparator, len, FsWriteOption_Flush);
|
2021-03-08 11:11:28 +00:00
|
|
|
}
|
2022-07-12 01:27:03 +01:00
|
|
|
|
|
|
|
if (R_SUCCEEDED(rc)) g_logFileOffset += (s64)len;
|
2021-03-08 11:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2022-07-12 01:27:03 +01:00
|
|
|
/* Close file if we successfully opened it, but an error occurred afterwards. */
|
|
|
|
if (R_FAILED(rc) && serviceIsActive(&(g_logFile.s)))
|
|
|
|
{
|
|
|
|
fsFileClose(&g_logFile);
|
|
|
|
memset(&g_logFile, 0, sizeof(FsFile));
|
|
|
|
}
|
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
return R_SUCCEEDED(rc);
|
|
|
|
}
|
2022-07-12 01:27:03 +01:00
|
|
|
|
|
|
|
static void logWriteStringToNxLink(const char *str)
|
|
|
|
{
|
|
|
|
int fd = utilsGetNxLinkFileDescriptor();
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
dprintf(fd, "%s", str);
|
|
|
|
fsync(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* (LOG_LEVEL >= LOG_LEVEL_DEBUG) && (LOG_LEVEL < LOG_LEVEL_NONE) */
|