From fe513951d322dbe600b6c77a45a04d4a0624c2e0 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Mon, 27 Jan 2020 15:09:56 +0100 Subject: [PATCH] Use a macro to print error log. This allow use to compile the JNI wrapper not for android. --- src/android/filter.cpp | 1 - src/android/kiwixreader.cpp | 91 ++++++++++++++++++------------------- src/android/kiwixserver.cpp | 7 ++- src/android/library.cpp | 6 +-- src/android/manager.cpp | 21 ++++----- src/android/utils.h | 8 ++++ 6 files changed, 68 insertions(+), 66 deletions(-) diff --git a/src/android/filter.cpp b/src/android/filter.cpp index 935b903b0..09c07343d 100644 --- a/src/android/filter.cpp +++ b/src/android/filter.cpp @@ -19,7 +19,6 @@ #include -#include #include "org_kiwix_kiwixlib_Filter.h" #include "library.h" diff --git a/src/android/kiwixreader.cpp b/src/android/kiwixreader.cpp index 5b7257d45..7bc61ebd8 100644 --- a/src/android/kiwixreader.cpp +++ b/src/android/kiwixreader.cpp @@ -21,7 +21,6 @@ #include #include -#include #include "org_kiwix_kiwixlib_JNIKiwixReader.h" #include "tools/base64.h" @@ -34,14 +33,14 @@ JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getNativeReader( { std::string cPath = jni2c(filename, env); - __android_log_print(ANDROID_LOG_INFO, "kiwix", "Attempting to create reader with: %s", cPath.c_str()); + LOG("Attempting to create reader with: %s", cPath.c_str()); Lock l; try { kiwix::Reader* reader = new kiwix::Reader(cPath); return reinterpret_cast(new Handle(reader)); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_WARN, "kiwix", "Error opening ZIM file"); - __android_log_print(ANDROID_LOG_WARN, "kiwix", e.what()); + LOG("Error opening ZIM file"); + LOG(e.what()); return 0; } } @@ -64,8 +63,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getMainPage(JNIEnv* env, jobject obj) std::string cUrl = READER->getMainPage().getPath(); url = c2jni(cUrl, env); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM main page"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get ZIM main page"); + LOG(e.what()); url = NULL; } return url; @@ -80,8 +79,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getId(JNIEnv* env, jobject obj) std::string cId = READER->getId(); id = c2jni(cId, env); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM id"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get ZIM id"); + LOG(e.what()); id = NULL; } @@ -97,8 +96,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getFileSize(JNIEnv* env, jobject obj) int cSize = READER->getFileSize(); size = c2jni(cSize, env); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM file size"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get ZIM file size"); + LOG(e.what()); } return size; @@ -113,8 +112,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getCreator(JNIEnv* env, jobject obj) std::string cCreator = READER->getCreator(); creator = c2jni(cCreator, env); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM creator"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get ZIM creator"); + LOG(e.what()); creator = NULL; } @@ -130,8 +129,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getPublisher(JNIEnv* env, jobject obj) std::string cPublisher = READER->getPublisher(); publisher = c2jni(cPublisher, env); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM publish"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get ZIM publish"); + LOG(e.what()); publisher = NULL; } return publisher; @@ -146,8 +145,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getName(JNIEnv* env, jobject obj) std::string cName = READER->getName(); name = c2jni(cName, env); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM name"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get ZIM name"); + LOG(e.what()); name = NULL; } return name; @@ -166,8 +165,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getFavicon(JNIEnv* env, jobject obj) base64_encode(cContent), env); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM favicon"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get ZIM favicon"); + LOG(e.what()); favicon = NULL; } return favicon; @@ -182,8 +181,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getDate(JNIEnv* env, jobject obj) std::string cDate = READER->getDate(); date = c2jni(cDate, env); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM date"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get ZIM date"); + LOG(e.what()); date = NULL; } return date; @@ -198,8 +197,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getLanguage(JNIEnv* env, jobject obj) std::string cLanguage = READER->getLanguage(); language = c2jni(cLanguage, env); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM language"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get ZIM language"); + LOG(e.what()); language = NULL; } @@ -217,8 +216,8 @@ JNIEXPORT jstring JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getMimeType( auto cMimeType = entry.getMimetype(); mimeType = c2jni(cMimeType, env); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get mime-type for url: %s", cUrl.c_str()); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get mime-type for url: %s", cUrl.c_str()); + LOG(e.what()); mimeType = NULL; } return mimeType; @@ -268,8 +267,8 @@ JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContent( data, 0, cSize, reinterpret_cast(entry.getBlob().data())); } } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get content for url: %s", cUrl.c_str()); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get content for url: %s", cUrl.c_str()); + LOG(e.what()); } return data; @@ -300,8 +299,8 @@ JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContentPa setIntObjValue(cLen, sizeObj, env); } } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get partial content for url: %s (%u : %u)", cUrl.c_str(), cOffset, cLen); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get partial content for url: %s (%u : %u)", cUrl.c_str(), cOffset, cLen); + LOG(e.what()); } return data; } @@ -322,8 +321,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getDirectAccessInformation( auto part_info = entry.getDirectAccessInfo(); setPairObjValue(part_info.first, part_info.second, pair, env); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get direct access info for url: %s", cUrl.c_str()); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get direct access info for url: %s", cUrl.c_str()); + LOG(e.what()); } return pair; } @@ -343,8 +342,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_searchSuggestions(JNIEnv* env, retVal = JNI_TRUE; } } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_WARN, "kiwix", "Unable to get search results for pattern: %s", cPrefix.c_str()); - __android_log_print(ANDROID_LOG_WARN, "kiwix", e.what()); + LOG("Unable to get search results for pattern: %s", cPrefix.c_str()); + LOG(e.what()); } return retVal; @@ -367,8 +366,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getNextSuggestion(JNIEnv* env, retVal = JNI_TRUE; } } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_WARN, "kiwix", "Unable to get next suggestion"); - __android_log_print(ANDROID_LOG_WARN, "kiwix", e.what()); + LOG("Unable to get next suggestion"); + LOG(e.what()); } return retVal; @@ -388,8 +387,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getPageUrlFromTitle(JNIEnv* env, setStringObjValue(entry.getPath(), urlObj, env); return JNI_TRUE; } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_WARN, "kiwix", "Unable to get url for title %s: ", cTitle.c_str()); - __android_log_print(ANDROID_LOG_WARN, "kiwix", e.what()); + LOG("Unable to get url for title %s: ", cTitle.c_str()); + LOG(e.what()); } return JNI_FALSE; @@ -404,8 +403,8 @@ JNIEXPORT jstring JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getTitle( std::string cTitle = READER->getTitle(); title = c2jni(cTitle, env); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get zim title"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get zim title"); + LOG(e.what()); title = NULL; } return title; @@ -420,8 +419,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getDescription(JNIEnv* env, jobject obj) std::string cDescription = READER->getDescription(); description = c2jni(cDescription, env); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get zim description"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get zim description"); + LOG(e.what()); description = NULL; } return description; @@ -435,8 +434,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getArticleCount(JNIEnv* env, jobject obj) auto cArticleCount = READER->getArticleCount(); articleCount = c2jni(cArticleCount, env); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get article count."); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get article count."); + LOG(e.what()); } return articleCount; } @@ -449,8 +448,8 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getMediaCount(JNIEnv* env, jobject obj) auto cMediaCount = READER->getMediaCount(); mediaCount = c2jni(cMediaCount, env); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get media count."); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get media count."); + LOG(e.what()); } return mediaCount; } @@ -467,8 +466,8 @@ JNIEXPORT jboolean JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getRandomPage( setStringObjValue(cUrl, urlObj, env); retVal = JNI_TRUE; } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get random page"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get random page"); + LOG(e.what()); } return retVal; } diff --git a/src/android/kiwixserver.cpp b/src/android/kiwixserver.cpp index 9d758f8d2..7242c4cd4 100644 --- a/src/android/kiwixserver.cpp +++ b/src/android/kiwixserver.cpp @@ -21,7 +21,6 @@ #include #include -#include #include "org_kiwix_kiwixlib_JNIKiwixServer.h" #include "tools/base64.h" @@ -32,15 +31,15 @@ JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixServer_getNativeServer( JNIEnv* env, jobject obj, jobject jLibrary) { - __android_log_print(ANDROID_LOG_INFO, "kiwix", "Attempting to create server"); + LOG("Attempting to create server"); Lock l; try { auto library = getPtr(env, jLibrary); kiwix::Server* server = new kiwix::Server(library); return reinterpret_cast(new Handle(server)); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_WARN, "kiwix", "Error creating the server"); - __android_log_print(ANDROID_LOG_WARN, "kiwix", e.what()); + LOG("Error creating the server"); + LOG(e.what()); return 0; } } diff --git a/src/android/library.cpp b/src/android/library.cpp index e10279e22..f2a084646 100644 --- a/src/android/library.cpp +++ b/src/android/library.cpp @@ -19,7 +19,6 @@ #include -#include #include "org_kiwix_kiwixlib_Library.h" #include "library.h" @@ -55,9 +54,8 @@ Java_org_kiwix_kiwixlib_Library_addBook( book.update(reader); return LIBRARY->addBook(book); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to add the book"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); - } + LOG("Unable to add the book"); + LOG(e.what()); } return false; } diff --git a/src/android/manager.cpp b/src/android/manager.cpp index 7c2b29f75..b2e94d9ed 100644 --- a/src/android/manager.cpp +++ b/src/android/manager.cpp @@ -20,7 +20,6 @@ #include #include -#include #include "org_kiwix_kiwixlib_Manager.h" #include "manager.h" @@ -53,8 +52,8 @@ Java_org_kiwix_kiwixlib_Manager_readFile( try { return MANAGER->readFile(cPath); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get readFile"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get readFile"); + LOG(e.what()); } return false; } @@ -69,8 +68,8 @@ Java_org_kiwix_kiwixlib_Manager_readXml( try { return MANAGER->readXml(cContent, false, cPath); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM id"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get ZIM id"); + LOG(e.what()); } return false; @@ -86,8 +85,8 @@ Java_org_kiwix_kiwixlib_Manager_readOpds( try { return MANAGER->readOpds(cContent, cUrl); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM id"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get ZIM id"); + LOG(e.what()); } return false; @@ -102,8 +101,8 @@ Java_org_kiwix_kiwixlib_Manager_readBookmarkFile( try { return MANAGER->readBookmarkFile(cPath); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM id"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get ZIM id"); + LOG(e.what()); } return false; @@ -125,8 +124,8 @@ Java_org_kiwix_kiwixlib_Manager_addBookFromPath( id = c2jni(cId, env); } } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM file size"); - __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); + LOG("Unable to get ZIM file size"); + LOG(e.what()); } return id; diff --git a/src/android/utils.h b/src/android/utils.h index 7f51c299e..7fa32a161 100644 --- a/src/android/utils.h +++ b/src/android/utils.h @@ -27,6 +27,14 @@ #include #include #include +#include + +#if __ANDROID__ + #include + #define LOG(...) __android_log_print(ANDROID_LOG_ERROR, "kiwix", __VA_ARGS__) +#else + #define LOG(...) +#endif extern pthread_mutex_t globalLock;