Skip to content

Commit 6c608e3

Browse files
committed
new whisper logs
1 parent 520de60 commit 6c608e3

File tree

5 files changed

+212
-10
lines changed

5 files changed

+212
-10
lines changed

whisper_ros/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ add_executable(whisper_node
2929
src/whisper_ros/whisper_node.cpp
3030
src/whisper_ros/whisper_base_node.cpp
3131
src/whisper_ros/whisper.cpp
32+
src/whisper_ros/logs.cpp
3233
)
3334
target_link_libraries(whisper_node
3435
whisper_cpp_vendor::grammar
@@ -49,6 +50,7 @@ add_executable(whisper_server_node
4950
src/whisper_ros/whisper_server_node.cpp
5051
src/whisper_ros/whisper_base_node.cpp
5152
src/whisper_ros/whisper.cpp
53+
src/whisper_ros/logs.cpp
5254
)
5355
target_link_libraries(whisper_server_node
5456
whisper_cpp_vendor::grammar
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (C) 2024 Miguel Ángel González Santamarta
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
16+
#ifndef WHISPER_ROS__LOGS_HPP
17+
#define WHISPER_ROS__LOGS_HPP
18+
19+
#include <cstdarg>
20+
#include <cstdio>
21+
#include <cstring>
22+
23+
namespace whisper_ros {
24+
25+
/**
26+
* @brief Type definition for a logging function.
27+
*
28+
* This type represents a function pointer that takes a file name,
29+
* function name, line number, log message, and a variable number of
30+
* additional arguments for formatting the log message.
31+
*
32+
* @param file The name of the source file where the log function is called.
33+
* @param function The name of the function where the log function is called.
34+
* @param line The line number in the source file where the log function is
35+
* called.
36+
* @param text The format string for the log message, similar to printf.
37+
* @param ... Additional arguments for the format string.
38+
*/
39+
typedef void (*LogFunction)(const char *file, const char *function, int line,
40+
const char *text, ...);
41+
42+
// Declare function pointers for logging at different severity levels
43+
extern LogFunction log_error; ///< Pointer to the error logging function
44+
extern LogFunction log_warn; ///< Pointer to the warning logging function
45+
extern LogFunction log_info; ///< Pointer to the info logging function
46+
extern LogFunction log_debug; ///< Pointer to the debug logging function
47+
48+
/**
49+
* @brief Extracts the filename from a given file path.
50+
*
51+
* This function takes a full path to a file and returns just the file name.
52+
*
53+
* @param path The full path to the file.
54+
* @return A pointer to the extracted filename.
55+
*/
56+
inline const char *extract_filename(const char *path) {
57+
const char *filename = std::strrchr(path, '/');
58+
if (!filename) {
59+
filename = std::strrchr(path, '\\'); // handle Windows-style paths
60+
}
61+
return filename ? filename + 1 : path;
62+
}
63+
64+
// Macros for logging with automatic file and function information
65+
#define WHISPER_LOG_ERROR(text, ...) \
66+
whisper_ros::log_error(extract_filename(__FILE__), __FUNCTION__, __LINE__, \
67+
text, ##__VA_ARGS__)
68+
#define WHISPER_LOG_WARN(text, ...) \
69+
whisper_ros::log_warn(extract_filename(__FILE__), __FUNCTION__, __LINE__, \
70+
text, ##__VA_ARGS__)
71+
#define WHISPER_LOG_INFO(text, ...) \
72+
whisper_ros::log_info(extract_filename(__FILE__), __FUNCTION__, __LINE__, \
73+
text, ##__VA_ARGS__)
74+
#define WHISPER_LOG_DEBUG(text, ...) \
75+
whisper_ros::log_debug(extract_filename(__FILE__), __FUNCTION__, __LINE__, \
76+
text, ##__VA_ARGS__)
77+
78+
} // namespace whisper_ros
79+
80+
#endif // WHISPER_ROS__LOGS_HPP

whisper_ros/include/whisper_ros/whisper.hpp

-8
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@
2929
#include "grammar-parser.h"
3030
#include "whisper.h"
3131

32-
// whisper logs
33-
#define WHISPER_LOG_ERROR(text, ...) \
34-
fprintf(stderr, "[ERROR] " text "\n", ##__VA_ARGS__)
35-
#define WHISPER_LOG_WARN(text, ...) \
36-
fprintf(stderr, "[WARN] " text "\n", ##__VA_ARGS__)
37-
#define WHISPER_LOG_INFO(text, ...) \
38-
fprintf(stderr, "[INFO] " text "\n", ##__VA_ARGS__)
39-
4032
struct TranscriptionOutput {
4133
std::string text;
4234
float prob;

whisper_ros/src/whisper_ros/logs.cpp

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright (C) 2024 Miguel Ángel González Santamarta
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
16+
#include "whisper_ros/logs.hpp"
17+
18+
namespace whisper_ros {
19+
20+
/**
21+
* @brief Default error logging function.
22+
*
23+
* This function logs an error message to stderr with the format:
24+
* [ERROR] [file:function:line] message.
25+
*
26+
* @param file The name of the source file where the log function is called.
27+
* @param function The name of the function where the log function is called.
28+
* @param line The line number in the source file where the log function is
29+
* called.
30+
* @param text The format string for the log message.
31+
* @param ... Additional arguments for the format string.
32+
*/
33+
void default_log_error(const char *file, const char *function, int line,
34+
const char *text, ...) {
35+
va_list args;
36+
va_start(args, text);
37+
fprintf(stderr, "[ERROR] [%s:%s:%d] ", file, function, line);
38+
vfprintf(stderr, text, args);
39+
fprintf(stderr, "\n");
40+
va_end(args);
41+
}
42+
43+
/**
44+
* @brief Default warning logging function.
45+
*
46+
* This function logs a warning message to stderr with the format:
47+
* [WARN] [file:function:line] message.
48+
*
49+
* @param file The name of the source file where the log function is called.
50+
* @param function The name of the function where the log function is called.
51+
* @param line The line number in the source file where the log function is
52+
* called.
53+
* @param text The format string for the log message.
54+
* @param ... Additional arguments for the format string.
55+
*/
56+
void default_log_warn(const char *file, const char *function, int line,
57+
const char *text, ...) {
58+
va_list args;
59+
va_start(args, text);
60+
fprintf(stderr, "[WARN] [%s:%s:%d] ", file, function, line);
61+
vfprintf(stderr, text, args);
62+
fprintf(stderr, "\n");
63+
va_end(args);
64+
}
65+
66+
/**
67+
* @brief Default info logging function.
68+
*
69+
* This function logs an informational message to stderr with the format:
70+
* [INFO] [file:function:line] message.
71+
*
72+
* @param file The name of the source file where the log function is called.
73+
* @param function The name of the function where the log function is called.
74+
* @param line The line number in the source file where the log function is
75+
* called.
76+
* @param text The format string for the log message.
77+
* @param ... Additional arguments for the format string.
78+
*/
79+
void default_log_info(const char *file, const char *function, int line,
80+
const char *text, ...) {
81+
va_list args;
82+
va_start(args, text);
83+
fprintf(stderr, "[INFO] [%s:%s:%d] ", file, function, line);
84+
vfprintf(stderr, text, args);
85+
fprintf(stderr, "\n");
86+
va_end(args);
87+
}
88+
89+
/**
90+
* @brief Default debug logging function.
91+
*
92+
* This function logs a debug message to stderr with the format:
93+
* [DEBUG] [file:function:line] message.
94+
*
95+
* @param file The name of the source file where the log function is called.
96+
* @param function The name of the function where the log function is called.
97+
* @param line The line number in the source file where the log function is
98+
* called.
99+
* @param text The format string for the log message.
100+
* @param ... Additional arguments for the format string.
101+
*/
102+
void default_log_debug(const char *file, const char *function, int line,
103+
const char *text, ...) {
104+
va_list args;
105+
va_start(args, text);
106+
fprintf(stderr, "[DEBUG] [%s:%s:%d] ", file, function, line);
107+
vfprintf(stderr, text, args);
108+
fprintf(stderr, "\n");
109+
va_end(args);
110+
}
111+
112+
// Initialize the function pointers with default log functions
113+
LogFunction log_error = default_log_error;
114+
LogFunction log_warn = default_log_warn;
115+
LogFunction log_info = default_log_info;
116+
LogFunction log_debug = default_log_debug;
117+
118+
} // namespace whisper_ros

whisper_ros/src/whisper_ros/whisper.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <thread>
2525

2626
#include "grammar-parser.h"
27+
#include "whisper_ros/logs.hpp"
2728
#include "whisper_ros/whisper.hpp"
2829

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

4445
if (this->ctx == nullptr) {
45-
WHISPER_LOG_ERROR("failed to initialize whisper context\n");
46+
WHISPER_LOG_ERROR("Failed to initialize whisper context\n");
4647
}
4748

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

79+
WHISPER_LOG_DEBUG("Starting transcription");
80+
7881
int prob_n = 0;
7982
struct TranscriptionOutput result;
8083
result.text = "";
@@ -137,9 +140,11 @@ std::string Whisper::timestamp_to_str(int64_t t, bool comma) {
137140
bool Whisper::set_grammar(const std::string grammar,
138141
const std::string start_rule, float grammar_penalty) {
139142

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

142146
if (this->grammar_parsed.rules.empty()) {
147+
WHISPER_LOG_ERROR("Error setting the grammar");
143148
return false;
144149
}
145150

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

157162
void Whisper::reset_grammar() {
163+
WHISPER_LOG_DEBUG("Resetting grammar");
158164
this->wparams.grammar_rules = nullptr;
159165
this->wparams.n_grammar_rules = 0;
160166
this->wparams.i_start_rule = 0;
161167
this->wparams.grammar_penalty = 100.0f;
162168
}
163169

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

168-
void Whisper::reset_init_prompt() { this->wparams.initial_prompt = ""; }
175+
void Whisper::reset_init_prompt() {
176+
WHISPER_LOG_DEBUG("Resetting initial prompt");
177+
this->wparams.initial_prompt = "";
178+
}

0 commit comments

Comments
 (0)