2021-03-07 23:22:49 +00:00
|
|
|
/*
|
2021-03-26 04:35:14 +00:00
|
|
|
* nxdt_log.h
|
2021-03-07 23:22:49 +00:00
|
|
|
*
|
2022-03-17 12:58:40 +00:00
|
|
|
* Copyright (c) 2020-2022, 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
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-03-26 04:35:14 +00:00
|
|
|
#ifndef __NXDT_LOG_H__
|
|
|
|
#define __NXDT_LOG_H__
|
2021-03-24 17:25:19 +00:00
|
|
|
|
2021-03-23 14:06:52 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2022-07-12 01:27:03 +01:00
|
|
|
/// 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.
|
2022-07-12 04:27:30 +01:00
|
|
|
#define LOG_LEVEL LOG_LEVEL_DEBUG /* TODO: change before release (warning?). */
|
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
|
|
|
/// Helper macros.
|
2022-07-12 01:27:03 +01:00
|
|
|
|
|
|
|
#define LOG_MSG_GENERIC(level, fmt, ...) logWriteFormattedStringToLogFile(level, __FILENAME__, __LINE__, __func__, fmt, ##__VA_ARGS__)
|
|
|
|
#define LOG_MSG_BUF_GENERIC(dst, dst_size, level, fmt, ...) logWriteFormattedStringToBuffer(dst, dst_size, level, __FILENAME__, __LINE__, __func__, fmt, ##__VA_ARGS__)
|
|
|
|
#define LOG_DATA_GENERIC(data, data_size, level, fmt, ...) logWriteBinaryDataToLogFile(data, data_size, level, __FILENAME__, __LINE__, __func__, fmt, ##__VA_ARGS__)
|
|
|
|
|
2022-07-12 04:27:30 +01:00
|
|
|
#if LOG_LEVEL == LOG_LEVEL_DEBUG
|
2022-07-12 01:27:03 +01:00
|
|
|
#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__)
|
2022-07-12 04:27:30 +01:00
|
|
|
#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 */
|
2022-07-12 01:27:03 +01:00
|
|
|
|
2022-07-12 04:27:30 +01:00
|
|
|
#if LOG_LEVEL <= LOG_LEVEL_INFO
|
2022-07-12 01:27:03 +01:00
|
|
|
#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)
|
2022-07-12 04:27:30 +01:00
|
|
|
#endif /* LOG_LEVEL <= LOG_LEVEL_INFO */
|
2022-07-12 01:27:03 +01:00
|
|
|
|
2022-07-12 04:27:30 +01:00
|
|
|
#if LOG_LEVEL <= LOG_LEVEL_WARNING
|
2022-07-12 01:27:03 +01:00
|
|
|
#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)
|
2022-07-12 04:27:30 +01:00
|
|
|
#endif /* LOG_LEVEL <= LOG_LEVEL_WARNING */
|
2022-07-12 01:27:03 +01:00
|
|
|
|
2022-07-12 04:27:30 +01:00
|
|
|
#if LOG_LEVEL <= LOG_LEVEL_ERROR
|
2022-07-12 01:27:03 +01:00
|
|
|
#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)
|
2022-07-12 04:27:30 +01:00
|
|
|
#endif /* LOG_LEVEL <= LOG_LEVEL_ERROR */
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
/// Writes the provided string to the logfile.
|
2022-07-12 01:27:03 +01:00
|
|
|
/// If the logfile hasn't been created and/or opened, this function takes care of it.
|
2021-03-07 23:22:49 +00:00
|
|
|
void logWriteStringToLogFile(const char *src);
|
|
|
|
|
|
|
|
/// Writes a formatted log string to the logfile.
|
2022-07-12 01:27:03 +01:00
|
|
|
/// 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, ...);
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
/// 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.
|
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
|
|
|
|
|
|
|
/// Writes a formatted log string + a hex string representation of the provided binary data to the logfile.
|
2022-07-12 01:27:03 +01:00
|
|
|
/// 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, ...);
|
2021-03-07 23:22:49 +00:00
|
|
|
|
|
|
|
/// Forces a flush operation on the logfile.
|
|
|
|
void logFlushLogFile(void);
|
|
|
|
|
2022-07-12 01:27:03 +01:00
|
|
|
/// Write any pending data to the logfile, flushes it and then closes it.
|
2021-03-07 23:22:49 +00:00
|
|
|
void logCloseLogFile(void);
|
|
|
|
|
2021-03-08 14:44:11 +00:00
|
|
|
/// Stores the last log message in the provided buffer.
|
|
|
|
void logGetLastMessage(char *dst, size_t dst_size);
|
|
|
|
|
2021-03-08 11:11:28 +00:00
|
|
|
/// (Un)locks the log mutex. Can be used to block other threads and prevent them from writing data to the logfile.
|
|
|
|
/// Use with caution.
|
2021-03-07 23:22:49 +00:00
|
|
|
void logControlMutex(bool lock);
|
|
|
|
|
2022-07-12 01:27:03 +01:00
|
|
|
#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
|
|
|
|
}
|
2021-03-24 17:25:19 +00:00
|
|
|
#endif
|
|
|
|
|
2021-03-26 04:35:14 +00:00
|
|
|
#endif /* __NXDT_LOG_H__ */
|