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
2431using 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
0 commit comments