Skip to content

Commit

Permalink
new whisper logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mgonzs13 committed Dec 27, 2024
1 parent 520de60 commit 6c608e3
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 10 deletions.
2 changes: 2 additions & 0 deletions whisper_ros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_executable(whisper_node
src/whisper_ros/whisper_node.cpp
src/whisper_ros/whisper_base_node.cpp
src/whisper_ros/whisper.cpp
src/whisper_ros/logs.cpp
)
target_link_libraries(whisper_node
whisper_cpp_vendor::grammar
Expand All @@ -49,6 +50,7 @@ add_executable(whisper_server_node
src/whisper_ros/whisper_server_node.cpp
src/whisper_ros/whisper_base_node.cpp
src/whisper_ros/whisper.cpp
src/whisper_ros/logs.cpp
)
target_link_libraries(whisper_server_node
whisper_cpp_vendor::grammar
Expand Down
80 changes: 80 additions & 0 deletions whisper_ros/include/whisper_ros/logs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (C) 2024 Miguel Ángel González Santamarta
//
// This program 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.
//
// This program 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/>.

#ifndef WHISPER_ROS__LOGS_HPP
#define WHISPER_ROS__LOGS_HPP

#include <cstdarg>
#include <cstdio>
#include <cstring>

namespace whisper_ros {

/**
* @brief Type definition for a logging function.
*
* This type represents a function pointer that takes a file name,
* function name, line number, log message, and a variable number of
* additional arguments for formatting the log message.
*
* @param file The name of the source file where the log function is called.
* @param function The name of the function where the log function is called.
* @param line The line number in the source file where the log function is
* called.
* @param text The format string for the log message, similar to printf.
* @param ... Additional arguments for the format string.
*/
typedef void (*LogFunction)(const char *file, const char *function, int line,
const char *text, ...);

// Declare function pointers for logging at different severity levels
extern LogFunction log_error; ///< Pointer to the error logging function
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 Extracts the filename from a given file path.
*
* This function takes a full path to a file and returns just the file name.
*
* @param path The full path to the file.
* @return A pointer to the extracted filename.
*/
inline const char *extract_filename(const char *path) {
const char *filename = std::strrchr(path, '/');
if (!filename) {
filename = std::strrchr(path, '\\'); // handle Windows-style paths
}
return filename ? filename + 1 : path;
}

// Macros for logging with automatic file and function information
#define WHISPER_LOG_ERROR(text, ...) \
whisper_ros::log_error(extract_filename(__FILE__), __FUNCTION__, __LINE__, \
text, ##__VA_ARGS__)
#define WHISPER_LOG_WARN(text, ...) \
whisper_ros::log_warn(extract_filename(__FILE__), __FUNCTION__, __LINE__, \
text, ##__VA_ARGS__)
#define WHISPER_LOG_INFO(text, ...) \
whisper_ros::log_info(extract_filename(__FILE__), __FUNCTION__, __LINE__, \
text, ##__VA_ARGS__)
#define WHISPER_LOG_DEBUG(text, ...) \
whisper_ros::log_debug(extract_filename(__FILE__), __FUNCTION__, __LINE__, \
text, ##__VA_ARGS__)

} // namespace whisper_ros

#endif // WHISPER_ROS__LOGS_HPP
8 changes: 0 additions & 8 deletions whisper_ros/include/whisper_ros/whisper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@
#include "grammar-parser.h"
#include "whisper.h"

// whisper logs
#define WHISPER_LOG_ERROR(text, ...) \
fprintf(stderr, "[ERROR] " text "\n", ##__VA_ARGS__)
#define WHISPER_LOG_WARN(text, ...) \
fprintf(stderr, "[WARN] " text "\n", ##__VA_ARGS__)
#define WHISPER_LOG_INFO(text, ...) \
fprintf(stderr, "[INFO] " text "\n", ##__VA_ARGS__)

struct TranscriptionOutput {
std::string text;
float prob;
Expand Down
118 changes: 118 additions & 0 deletions whisper_ros/src/whisper_ros/logs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (C) 2024 Miguel Ángel González Santamarta
//
// This program 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.
//
// This program 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/>.

#include "whisper_ros/logs.hpp"

namespace whisper_ros {

/**
* @brief Default error logging function.
*
* This function logs an error message to stderr with the format:
* [ERROR] [file:function:line] message.
*
* @param file The name of the source file where the log function is called.
* @param function The name of the function where the log function is called.
* @param line The line number in the source file where the log function is
* called.
* @param text The format string for the log message.
* @param ... Additional arguments for the format string.
*/
void default_log_error(const char *file, const char *function, int line,
const char *text, ...) {
va_list args;
va_start(args, text);
fprintf(stderr, "[ERROR] [%s:%s:%d] ", file, function, line);
vfprintf(stderr, text, args);
fprintf(stderr, "\n");
va_end(args);
}

/**
* @brief Default warning logging function.
*
* This function logs a warning message to stderr with the format:
* [WARN] [file:function:line] message.
*
* @param file The name of the source file where the log function is called.
* @param function The name of the function where the log function is called.
* @param line The line number in the source file where the log function is
* called.
* @param text The format string for the log message.
* @param ... Additional arguments for the format string.
*/
void default_log_warn(const char *file, const char *function, int line,
const char *text, ...) {
va_list args;
va_start(args, text);
fprintf(stderr, "[WARN] [%s:%s:%d] ", file, function, line);
vfprintf(stderr, text, args);
fprintf(stderr, "\n");
va_end(args);
}

/**
* @brief Default info logging function.
*
* This function logs an informational message to stderr with the format:
* [INFO] [file:function:line] message.
*
* @param file The name of the source file where the log function is called.
* @param function The name of the function where the log function is called.
* @param line The line number in the source file where the log function is
* called.
* @param text The format string for the log message.
* @param ... Additional arguments for the format string.
*/
void default_log_info(const char *file, const char *function, int line,
const char *text, ...) {
va_list args;
va_start(args, text);
fprintf(stderr, "[INFO] [%s:%s:%d] ", file, function, line);
vfprintf(stderr, text, args);
fprintf(stderr, "\n");
va_end(args);
}

/**
* @brief Default debug logging function.
*
* This function logs a debug message to stderr with the format:
* [DEBUG] [file:function:line] message.
*
* @param file The name of the source file where the log function is called.
* @param function The name of the function where the log function is called.
* @param line The line number in the source file where the log function is
* called.
* @param text The format string for the log message.
* @param ... Additional arguments for the format string.
*/
void default_log_debug(const char *file, const char *function, int line,
const char *text, ...) {
va_list args;
va_start(args, text);
fprintf(stderr, "[DEBUG] [%s:%s:%d] ", file, function, line);
vfprintf(stderr, text, args);
fprintf(stderr, "\n");
va_end(args);
}

// Initialize the function pointers with default log functions
LogFunction log_error = default_log_error;
LogFunction log_warn = default_log_warn;
LogFunction log_info = default_log_info;
LogFunction log_debug = default_log_debug;

} // namespace whisper_ros
14 changes: 12 additions & 2 deletions whisper_ros/src/whisper_ros/whisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <thread>

#include "grammar-parser.h"
#include "whisper_ros/logs.hpp"
#include "whisper_ros/whisper.hpp"

using namespace whisper_ros;
Expand All @@ -42,7 +43,7 @@ Whisper::Whisper(const std::string &model,
this->ctx = whisper_init_from_file_with_params(model.c_str(), cparams);

if (this->ctx == nullptr) {
WHISPER_LOG_ERROR("failed to initialize whisper context\n");
WHISPER_LOG_ERROR("Failed to initialize whisper context\n");
}

if (!whisper_is_multilingual(this->ctx)) {
Expand Down Expand Up @@ -75,6 +76,8 @@ Whisper::~Whisper() { whisper_free(this->ctx); }
struct TranscriptionOutput
Whisper::transcribe(const std::vector<float> &pcmf32) {

WHISPER_LOG_DEBUG("Starting transcription");

int prob_n = 0;
struct TranscriptionOutput result;
result.text = "";
Expand Down Expand Up @@ -137,9 +140,11 @@ std::string Whisper::timestamp_to_str(int64_t t, bool comma) {
bool Whisper::set_grammar(const std::string grammar,
const std::string start_rule, float grammar_penalty) {

WHISPER_LOG_DEBUG("Setting new grammar");
this->grammar_parsed = grammar_parser::parse(grammar.c_str());

if (this->grammar_parsed.rules.empty()) {
WHISPER_LOG_ERROR("Error setting the grammar");
return false;
}

Expand All @@ -155,14 +160,19 @@ bool Whisper::set_grammar(const std::string grammar,
}

void Whisper::reset_grammar() {
WHISPER_LOG_DEBUG("Resetting grammar");
this->wparams.grammar_rules = nullptr;
this->wparams.n_grammar_rules = 0;
this->wparams.i_start_rule = 0;
this->wparams.grammar_penalty = 100.0f;
}

void Whisper::set_init_prompt(const std::string prompt) {
WHISPER_LOG_DEBUG("Resetting initial prompt");
this->wparams.initial_prompt = prompt.c_str();
}

void Whisper::reset_init_prompt() { this->wparams.initial_prompt = ""; }
void Whisper::reset_init_prompt() {
WHISPER_LOG_DEBUG("Resetting initial prompt");
this->wparams.initial_prompt = "";
}

0 comments on commit 6c608e3

Please sign in to comment.