Skip to content

Commit

Permalink
feat(cpp): add custom log for easy debug in android
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Dec 9, 2023
1 parent 759bcd5 commit b3aacbb
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions android/src/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function(build_library target_name)
target_compile_options(${target_name} PRIVATE -mfpu=neon-vfpv4)
endif ()

if (${CMAKE_BUILD_TYPE} STREQUAL "Debug")
target_compile_options(${target_name} PRIVATE -DRNWHISPER_ANDROID_ENABLE_LOGGING)
endif ()

# NOTE: If you want to debug the native code, you can uncomment if and endif
# if (NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug")

Expand Down
3 changes: 2 additions & 1 deletion cpp/rn-audioutils.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "rn-audioutils.h"
#include "rn-whisper-log.h"

namespace rnaudioutils {

Expand Down Expand Up @@ -30,7 +31,7 @@ void save_wav_file(const std::vector<uint8_t>& raw, const std::string& file) {
std::ofstream output(file, std::ios::binary);

if (!output.is_open()) {
std::cerr << "Failed to open file for writing: " << file << std::endl;
RNWHISPER_LOG_ERROR("Failed to open file for writing: %s\n", file.c_str());
return;
}

Expand Down
11 changes: 11 additions & 0 deletions cpp/rn-whisper-log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#if defined(__ANDROID__) && defined(RNWHISPER_ANDROID_ENABLE_LOGGING)
#include <android/log.h>
#define RNWHISPER_ANDROID_TAG "RNWHISPER_LOG_ANDROID"
#define RNWHISPER_LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO , WHISPER_ANDROID_TAG, __VA_ARGS__)
#define RNWHISPER_LOG_WARN(...) __android_log_print(ANDROID_LOG_WARN , WHISPER_ANDROID_TAG, __VA_ARGS__)
#define RNWHISPER_LOG_ERROR(...) __android_log_print(ANDROID_LOG_ERROR, WHISPER_ANDROID_TAG, __VA_ARGS__)
#else
#define RNWHISPER_LOG_INFO(...) fprintf(stderr, __VA_ARGS__)
#define RNWHISPER_LOG_WARN(...) fprintf(stderr, __VA_ARGS__)
#define RNWHISPER_LOG_ERROR(...) fprintf(stderr, __VA_ARGS__)
#endif // __ANDROID__
4 changes: 2 additions & 2 deletions cpp/rn-whisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool vad_simple_impl(std::vector<float> & pcmf32, int sample_rate, int last_ms,
energy_last /= n_samples_last;

if (verbose) {
fprintf(stderr, "%s: energy_all: %f, energy_last: %f, vad_thold: %f, freq_thold: %f\n", __func__, energy_all, energy_last, vad_thold, freq_thold);
RNWHISPER_LOG_INFO("%s: energy_all: %f, energy_last: %f, vad_thold: %f, freq_thold: %f\n", __func__, energy_all, energy_last, vad_thold, freq_thold);
}

if (energy_last > vad_thold*energy_all) {
Expand Down Expand Up @@ -119,7 +119,7 @@ void job::abort() {
}

job::~job() {
fprintf(stderr, "%s: job_id: %d\n", __func__, job_id);
RNWHISPER_LOG_INFO("rnwhisper::job::%s: job_id: %d\n", __func__, job_id);

for (size_t i = 0; i < pcm_slices.size(); i++) {
delete[] pcm_slices[i];
Expand Down
1 change: 1 addition & 0 deletions cpp/rn-whisper.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string>
#include <vector>
#include "whisper.h"
#include "rn-whisper-log.h"
#include "rn-audioutils.h"

namespace rnwhisper {
Expand Down

0 comments on commit b3aacbb

Please sign in to comment.