diff --git a/whisper_ros/include/whisper_utils/logs.hpp b/whisper_ros/include/whisper_utils/logs.hpp index b2b6b98..b50c83c 100644 --- a/whisper_ros/include/whisper_utils/logs.hpp +++ b/whisper_ros/include/whisper_utils/logs.hpp @@ -45,6 +45,37 @@ extern LogFunction log_warn; ///< Pointer to the warning logging function extern LogFunction log_info; ///< Pointer to the info logging function extern LogFunction log_debug; ///< Pointer to the debug logging function +/** + * @brief Enum representing different log levels for controlling log verbosity. + * + * This enum defines the severity levels of logs that can be used to control + * which log messages should be displayed. The levels are ordered from most + * severe to least severe. Only logs at or above the current log level will be + * shown. + */ +enum LogLevel { + /// Log level for error messages. Only critical errors should be logged. + ERROR = 0, + /// Log level for warning messages. Indicate potential issues that are not + /// critical. + WARN, + /// Log level for informational messages. General runtime information about + /// the system's state. + INFO, + /// Log level for debug messages. Used for detailed information, mainly for + /// developers. + DEBUG +}; + +/** + * @brief The current log level for the application. + * + * This global variable holds the current log level, which determines the + * verbosity of the logs. Logs at or above this level will be displayed. The + * default level is set to INFO. + */ +extern LogLevel log_level; + /** * @brief Extracts the filename from a given file path. * @@ -61,20 +92,36 @@ inline const char *extract_filename(const char *path) { return filename ? filename + 1 : path; } -// Macros for logging with automatic file and function information #define WHISPER_LOG_ERROR(text, ...) \ + if (whisper_utils::log_level >= whisper_utils::ERROR) \ whisper_utils::log_error(whisper_utils::extract_filename(__FILE__), \ __FUNCTION__, __LINE__, text, ##__VA_ARGS__) + #define WHISPER_LOG_WARN(text, ...) \ + if (whisper_utils::log_level >= whisper_utils::WARN) \ whisper_utils::log_warn(whisper_utils::extract_filename(__FILE__), \ __FUNCTION__, __LINE__, text, ##__VA_ARGS__) + #define WHISPER_LOG_INFO(text, ...) \ + if (whisper_utils::log_level >= whisper_utils::INFO) \ whisper_utils::log_info(whisper_utils::extract_filename(__FILE__), \ __FUNCTION__, __LINE__, text, ##__VA_ARGS__) + #define WHISPER_LOG_DEBUG(text, ...) \ + if (whisper_utils::log_level >= whisper_utils::DEBUG) \ whisper_utils::log_debug(whisper_utils::extract_filename(__FILE__), \ __FUNCTION__, __LINE__, text, ##__VA_ARGS__) +/** + * @brief Sets the log level for the logs. + * + * This function allows the user to specify the log level error, warning, info, + * or debug. + * + * @param log_level Log level. + */ +void set_log_level(LogLevel log_level); + } // namespace whisper_utils #endif // WHISPER_UTILS__LOGS_HPP \ No newline at end of file diff --git a/whisper_ros/src/silero_vad/vad_iterator.cpp b/whisper_ros/src/silero_vad/vad_iterator.cpp index 1499bea..f850986 100644 --- a/whisper_ros/src/silero_vad/vad_iterator.cpp +++ b/whisper_ros/src/silero_vad/vad_iterator.cpp @@ -81,7 +81,7 @@ void VadIterator::reset_states() { Timestamp VadIterator::predict(const std::vector &data) { - WHISPER_LOG_INFO("Processing audio data"); + WHISPER_LOG_DEBUG("Processing audio data"); // Pre-fill input with context this->input.clear(); diff --git a/whisper_ros/src/whisper_utils/logs.cpp b/whisper_ros/src/whisper_utils/logs.cpp index de68752..1b59752 100644 --- a/whisper_ros/src/whisper_utils/logs.cpp +++ b/whisper_ros/src/whisper_utils/logs.cpp @@ -115,4 +115,9 @@ LogFunction log_warn = default_log_warn; LogFunction log_info = default_log_info; LogFunction log_debug = default_log_debug; +// Initialize the log level to INFO +LogLevel log_level = INFO; + +void set_log_level(LogLevel log_level) { log_level = log_level; } + } // namespace whisper_utils \ No newline at end of file