1
0
Fork 0
mirror of https://github.com/DarkMatterCore/nxdumptool.git synced 2024-11-29 21:52:22 +00:00
nxdumptool/include/core/nxdt_log.h

152 lines
7.8 KiB
C
Raw Normal View History

/*
2021-03-26 04:35:14 +00:00
* nxdt_log.h
*
2024-04-12 10:47:36 +01:00
* Copyright (c) 2020-2024, DarkMatterCore <pabloacurielz@gmail.com>.
*
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
2021-03-26 04:35:14 +00:00
#ifndef __NXDT_LOG_H__
#define __NXDT_LOG_H__
2021-03-23 14:06:52 +00:00
#ifdef __cplusplus
extern "C" {
#endif
/// Used to control logfile verbosity.
#define LOG_LEVEL_DEBUG 0
#define LOG_LEVEL_INFO 1
#define LOG_LEVEL_WARNING 2
#define LOG_LEVEL_ERROR 3
#define LOG_LEVEL_NONE 4
/// Defines the log level used throughout the application.
/// Log messages with a log value lower than this one won't be compiled into the binary.
/// If a value lower than LOG_LEVEL_DEBUG or equal to/greater than LOG_LEVEL_NONE is used, logfile output will be entirely disabled.
#define LOG_LEVEL LOG_LEVEL_DEBUG /* TODO: change before release (warning?). */
#if (LOG_LEVEL >= LOG_LEVEL_DEBUG) && (LOG_LEVEL < LOG_LEVEL_NONE)
/// Helper macros.
#define LOG_MSG_GENERIC(level, fmt, ...) logWriteFormattedStringToLogFile(level, __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, ##__VA_ARGS__)
#define LOG_MSG_BUF_GENERIC(dst, dst_size, level, fmt, ...) logWriteFormattedStringToBuffer(dst, dst_size, level, __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, ##__VA_ARGS__)
#define LOG_DATA_GENERIC(data, data_size, level, fmt, ...) logWriteBinaryDataToLogFile(data, data_size, level, __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, ##__VA_ARGS__)
#if LOG_LEVEL == LOG_LEVEL_DEBUG
#define LOG_MSG_DEBUG(fmt, ...) LOG_MSG_GENERIC(LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
#define LOG_MSG_BUF_DEBUG(dst, dst_size, fmt, ...) LOG_MSG_BUF_GENERIC(dst, dst_size, LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
#define LOG_DATA_DEBUG(data, data_size, fmt, ...) LOG_DATA_GENERIC(data, data_size, LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
#else
#define LOG_MSG_DEBUG(fmt, ...) do {} while(0)
#define LOG_MSG_BUF_DEBUG(dst, dst_size, fmt, ...) do {} while(0)
#define LOG_DATA_DEBUG(data, data_size, fmt, ...) do {} while(0)
#endif /* LOG_LEVEL == LOG_LEVEL_DEBUG */
#if LOG_LEVEL <= LOG_LEVEL_INFO
#define LOG_MSG_INFO(fmt, ...) LOG_MSG_GENERIC(LOG_LEVEL_INFO, fmt, ##__VA_ARGS__)
#define LOG_MSG_BUF_INFO(dst, dst_size, fmt, ...) LOG_MSG_BUF_GENERIC(dst, dst_size, LOG_LEVEL_INFO, fmt, ##__VA_ARGS__)
#define LOG_DATA_INFO(data, data_size, fmt, ...) LOG_DATA_GENERIC(data, data_size, LOG_LEVEL_INFO, fmt, ##__VA_ARGS__)
#else
#define LOG_MSG_INFO(fmt, ...) do {} while(0)
#define LOG_MSG_BUF_INFO(dst, dst_size, fmt, ...) do {} while(0)
#define LOG_DATA_INFO(data, data_size, fmt, ...) do {} while(0)
#endif /* LOG_LEVEL <= LOG_LEVEL_INFO */
#if LOG_LEVEL <= LOG_LEVEL_WARNING
#define LOG_MSG_WARNING(fmt, ...) LOG_MSG_GENERIC(LOG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
#define LOG_MSG_BUF_WARNING(dst, dst_size, fmt, ...) LOG_MSG_BUF_GENERIC(dst, dst_size, LOG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
#define LOG_DATA_WARNING(data, data_size, fmt, ...) LOG_DATA_GENERIC(data, data_size, LOG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
#else
#define LOG_MSG_WARNING(fmt, ...) do {} while(0)
#define LOG_MSG_BUF_WARNING(dst, dst_size, fmt, ...) do {} while(0)
#define LOG_DATA_WARNING(data, data_size, fmt, ...) do {} while(0)
#endif /* LOG_LEVEL <= LOG_LEVEL_WARNING */
#if LOG_LEVEL <= LOG_LEVEL_ERROR
#define LOG_MSG_ERROR(fmt, ...) LOG_MSG_GENERIC(LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
#define LOG_MSG_BUF_ERROR(dst, dst_size, fmt, ...) LOG_MSG_BUF_GENERIC(dst, dst_size, LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
#define LOG_DATA_ERROR(data, data_size, fmt, ...) LOG_DATA_GENERIC(data, data_size, LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
#else
#define LOG_MSG_ERROR(fmt, ...) do {} while(0)
#define LOG_MSG_BUF_ERROR(dst, dst_size, fmt, ...) do {} while(0)
#define LOG_DATA_ERROR(data, data_size, fmt, ...) do {} while(0)
#endif /* LOG_LEVEL <= LOG_LEVEL_ERROR */
/// Writes the provided string to the logfile.
/// If the logfile hasn't been created and/or opened, this function takes care of it.
void logWriteStringToLogFile(const char *src);
/// Writes a formatted log string to the logfile.
/// If the logfile hasn't been created and/or opened, this function takes care of it.
__attribute__((format(printf, 5, 6))) void logWriteFormattedStringToLogFile(u8 level, const char *file_name, int line, const char *func_name, const char *fmt, ...);
/// Writes a formatted log string to the provided buffer.
/// If the buffer isn't big enough to hold both its current contents and the new formatted string, it will be resized.
__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, ...);
/// Writes a formatted log string + a hex string representation of the provided binary data to the logfile.
/// If the logfile hasn't been created and/or opened, this function takes care of it.
__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, ...);
/// Forces a flush operation on the logfile.
void logFlushLogFile(void);
/// Write any pending data to the logfile, flushes it and then closes it.
void logCloseLogFile(void);
/// Returns a pointer to a dynamically allocated buffer that holds the last error message string, or NULL if there's none.
Add DataTransferTaskFrame and GameCardImageDumpTaskFrame classes DataTransferTaskFrame is a template class that's derived from brls::AppletFrame, which automatically starts a background task using an internal object belonging to a class derived from DataTransferTask. A DataTransferProgressDisplay view is used to show progress updates. If the background task hits an error, the class takes care of switching to an ErrorFrame view with the corresponding error message. GameCardImageDumpTaskFrame is a derived class of DataTransferTaskFrame that uses a GameCardImageDumpTask object as its background task. In layman's terms, this provides a way to fully dump gamecard images using the new UI. DataTransferTaskFrame depends on the newly added is_base_template helper from goneskiing to check if the class for the provided task is derived from DataTransferTask. Other changes include: * DataTransferProgressDisplay: rename setProgress() method to SetProgress(). * DataTransferTask: move post-task-execution code into its own new private method, PostExecutionCallback(), and update both OnCancelled() and OnPostExecute() callbacks to invoke it. * DataTransferTask: update OnProgressUpdate() to allow sending a last progress update to all event subscribers even if the background task was cancelled. * DataTransferTask: update OnProgressUpdate() to allow sending a first progress update if no data has been transferred but the total transfer size is already known. * DataTransferTask: update OnProgressUpdate() to avoid calculating the ETA if the speed isn't greater than 0. * DumpOptionsFrame: remove UpdateOutputStorages() method. * DumpOptionsFrame: update class to use the cached output storage value from our RootView. * DumpOptionsFrame: add GenerateOutputStoragesVector() method, which is used to avoid setting dummy options while initializing the output storages SelectListItem. * DumpOptionsFrame: update UMS task callback to add the rest of the leftover logic from UpdateOutputStorages(). * DumpOptionsFrame: update RegisterButtonListener() to use a wrapper callback around the user-provided callback to check if the USB host was selected as the output storage but no USB host connection is available. * ErrorFrame: use const references for all input string arguments. * FileWriter: fix a localization stirng name typo. * FileWriter: fix an exception that was previously being thrown by a fmt::format() call because of a wrong format specifier. * FocusableItem: add a static assert to check if the provided ViewType is derived from brls::View. * gamecard: redefine global gamecard status variable as an atomic unsigned 8-bit integer, which fixes a "status-hopping" issue previously experienced by repeating tasks running under other threads that periodically call gamecardGetStatus(). * GameCardImageDumpOptionsFrame: define GAMECARD_TOGGLE_ITEM macro, which is used to initialize all ToggleListItem elements from the view. * GameCardImageDumpOptionsFrame: update button callback to push a GameCardImageDumpTaskFrame view onto the borealis stack. * GameCardImageDumpTask: move class into its own header and module files. * GameCardImageDumpTask: update class to also take in the checksum lookup method (not yet implemented). * GameCardImageDumpTask: update class to send its first progress update as soon as the gamecard image size is known. * GameCardImageDumpTask: update class to avoid returning a string if the task was cancelled -- DataTransferTaskFrame offers logic to display the appropiate cancel message on its own. * GameCardTab: update PopulateList() method to display the new version information available in TitleGameCardApplicationMetadataEntry elements as part of the generated TitlesTabItem objects. * i18n: add new localization strings. * OptionsTab: update background task callback logic to handle task cancellation, reflecting the changes made to DataTransferTask. * OptionsTab: reflect changes made to DataTransferProgressDisplay. * RootView: cache the currently selected output storage value at all times, which is propagated throughout different parts of the UI. Getter and setter helpers have been added to operate with this value. * RootView: add GetUsbHostSpeed() helper, which can be used by child views to retrieve the USB host speed on demand. * RootView: update UMS task callback to automatically reset the cached output storage value back to the SD card if a UMS device was previously selected. * title: define TitleGameCardApplicationMetadataEntry struct, which also holds version-specific information retrieved from the gamecard titles. * title: refactor titleGetGameCardApplicationMetadataEntries() to return a dynamically allocated array of TitleGameCardApplicationMetadataEntry elements. * usb: redefine global endpoint max packet size variable as an atomic unsigned 16-bit integer, which fixes a "status-hopping" issue previously experienced by repeating tasks running under other threads that periodically call usbIsReady(). * UsbHostTask: add GetUsbHostSpeed() method.
2024-04-29 14:26:12 +01:00
/// The allocated buffer must be freed by the caller using free().
char *logGetLastMessage(void);
/// (Un)locks the log mutex. Can be used to block other threads and prevent them from writing data to the logfile.
/// Use with caution.
void logControlMutex(bool lock);
#else /* (LOG_LEVEL >= LOG_LEVEL_DEBUG) && (LOG_LEVEL < LOG_LEVEL_NONE) */
/// Helper macros.
#define LOG_MSG_GENERIC(level, fmt, ...) do {} while(0)
#define LOG_MSG_BUF_GENERIC(dst, dst_size, level, fmt, ...) do {} while(0)
#define LOG_DATA_GENERIC(data, data_size, level, fmt, ...) do {} while(0)
#define LOG_MSG_DEBUG(fmt, ...) do {} while(0)
#define LOG_MSG_BUF_DEBUG(dst, dst_size, fmt, ...) do {} while(0)
#define LOG_DATA_DEBUG(data, data_size, fmt, ...) do {} while(0)
#define LOG_MSG_INFO(fmt, ...) do {} while(0)
#define LOG_MSG_BUF_INFO(dst, dst_size, fmt, ...) do {} while(0)
#define LOG_DATA_INFO(data, data_size, fmt, ...) do {} while(0)
#define LOG_MSG_WARNING(fmt, ...) do {} while(0)
#define LOG_MSG_BUF_WARNING(dst, dst_size, fmt, ...) do {} while(0)
#define LOG_DATA_WARNING(data, data_size, fmt, ...) do {} while(0)
#define LOG_MSG_ERROR(fmt, ...) do {} while(0)
#define LOG_MSG_BUF_ERROR(dst, dst_size, fmt, ...) do {} while(0)
#define LOG_DATA_ERROR(data, data_size, fmt, ...) do {} while(0)
#endif /* (LOG_LEVEL >= LOG_LEVEL_DEBUG) && (LOG_LEVEL < LOG_LEVEL_NONE) */
2021-03-23 14:06:52 +00:00
#ifdef __cplusplus
}
#endif
2021-03-26 04:35:14 +00:00
#endif /* __NXDT_LOG_H__ */