Skip to content

Commit 6948812

Browse files
authored
Merge pull request #530 from rpng/android
Support Android Logging
2 parents 6cf212d + 0491f6b commit 6948812

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

ov_core/src/utils/print.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
*/
2121

2222
#include "print.h"
23+
#include <cstdio>
24+
#include <cstdlib>
25+
26+
#ifdef __ANDROID__
27+
#include <android/log.h>
28+
#define LOG_TAG "OpenVINS"
29+
#endif
2330

2431
using namespace ov_core;
2532

@@ -83,6 +90,52 @@ void Printer::debugPrint(PrintLevel level, const char location[], const char lin
8390
return;
8491
}
8592

93+
#ifdef __ANDROID__
94+
// Use Android logging on Android
95+
android_LogPriority log_priority;
96+
switch (level) {
97+
case PrintLevel::ALL:
98+
case PrintLevel::DEBUG:
99+
log_priority = ANDROID_LOG_DEBUG;
100+
break;
101+
case PrintLevel::INFO:
102+
log_priority = ANDROID_LOG_INFO;
103+
break;
104+
case PrintLevel::WARNING:
105+
log_priority = ANDROID_LOG_WARN;
106+
break;
107+
case PrintLevel::ERROR:
108+
log_priority = ANDROID_LOG_ERROR;
109+
break;
110+
default:
111+
log_priority = ANDROID_LOG_INFO;
112+
break;
113+
}
114+
115+
// Build the message with location info if DEBUG level
116+
char location_str[256] = {0};
117+
if (static_cast<int>(Printer::current_print_level) <= static_cast<int>(Printer::PrintLevel::DEBUG)) {
118+
std::string path(location);
119+
std::string base_filename = path.substr(path.find_last_of("/\\") + 1);
120+
if (base_filename.size() > MAX_FILE_PATH_LEGTH) {
121+
snprintf(location_str, sizeof(location_str), "%s:%s ",
122+
base_filename.substr(base_filename.size() - MAX_FILE_PATH_LEGTH, base_filename.size()).c_str(), line);
123+
} else {
124+
snprintf(location_str, sizeof(location_str), "%s:%s ", base_filename.c_str(), line);
125+
}
126+
}
127+
128+
// Format the message
129+
va_list args;
130+
va_start(args, format);
131+
char formatted_msg[1024];
132+
vsnprintf(formatted_msg, sizeof(formatted_msg), format, args);
133+
va_end(args);
134+
135+
// Log to Android logcat
136+
__android_log_print(log_priority, LOG_TAG, "%s%s", location_str, formatted_msg);
137+
#else
138+
// Use standard printf on non-Android platforms
86139
// Print the location info first for our debug output
87140
// Truncate the filename to the max size for the filepath
88141
if (static_cast<int>(Printer::current_print_level) <= static_cast<int>(Printer::PrintLevel::DEBUG)) {
@@ -101,4 +154,14 @@ void Printer::debugPrint(PrintLevel level, const char location[], const char lin
101154
va_start(args, format);
102155
vprintf(format, args);
103156
va_end(args);
157+
#endif
158+
}
159+
160+
#ifndef __ANDROID__
161+
// Implementation of custom assert function (only for non-Android platforms)
162+
extern "C" void __assert(const char *msg, const char *file, int line) {
163+
fprintf(stderr, "Assertion failed: %s\n", msg);
164+
fprintf(stderr, " at %s:%d\n", file, line);
165+
std::abort();
104166
}
167+
#endif

ov_init/src/init/InertialInitializer.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
#include "InertialInitializer.h"
2323

24+
#ifndef __ANDROID__
2425
#include "dynamic/DynamicInitializer.h"
26+
#endif
2527
#include "static/StaticInitializer.h"
2628

2729
#include "feat/FeatureHelper.h"
@@ -43,7 +45,11 @@ InertialInitializer::InertialInitializer(InertialInitializerOptions &params_, st
4345

4446
// Create initializers
4547
init_static = std::make_shared<StaticInitializer>(params, _db, imu_data);
48+
#ifndef __ANDROID__
4649
init_dynamic = std::make_shared<DynamicInitializer>(params, _db, imu_data);
50+
#else
51+
init_dynamic = nullptr;
52+
#endif
4753
}
4854

4955
void InertialInitializer::feed_imu(const ov_core::ImuData &message, double oldest_time) {
@@ -133,10 +139,16 @@ bool InertialInitializer::initialize(double &timestamp, Eigen::MatrixXd &covaria
133139
PRINT_DEBUG(GREEN "[init]: USING STATIC INITIALIZER METHOD!\n" RESET);
134140
return init_static->initialize(timestamp, covariance, order, t_imu, wait_for_jerk);
135141
} else if (params.init_dyn_use && !is_still) {
142+
#ifndef __ANDROID__
136143
PRINT_DEBUG(GREEN "[init]: USING DYNAMIC INITIALIZER METHOD!\n" RESET);
137144
std::map<double, std::shared_ptr<ov_type::PoseJPL>> _clones_IMU;
138145
std::unordered_map<size_t, std::shared_ptr<ov_type::Landmark>> _features_SLAM;
139-
return init_dynamic->initialize(timestamp, covariance, order, t_imu, _clones_IMU, _features_SLAM);
146+
if (init_dynamic) {
147+
return init_dynamic->initialize(timestamp, covariance, order, t_imu, _clones_IMU, _features_SLAM);
148+
}
149+
#else
150+
PRINT_ERROR(RED "[init]: DYNAMIC INITIALIZER not available on Android (Ceres Solver not included)\n" RESET);
151+
#endif
140152
} else {
141153
std::string msg = (has_jerk) ? "" : "no accel jerk detected";
142154
msg += (has_jerk || is_still) ? "" : ", ";

0 commit comments

Comments
 (0)