2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2015-02-10 21:59:40 +01:00
|
|
|
//
|
|
|
|
// This file implements the callback "bridge" between Java and C++ for
|
2015-03-10 23:16:21 +01:00
|
|
|
// rocksdb::Logger.
|
2015-02-10 21:59:40 +01:00
|
|
|
|
2015-03-10 23:16:21 +01:00
|
|
|
#include "include/org_rocksdb_Logger.h"
|
2015-02-10 21:59:40 +01:00
|
|
|
|
2017-02-28 01:26:12 +01:00
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstdio>
|
2018-04-13 02:55:14 +02:00
|
|
|
#include "rocksjni/loggerjnicallback.h"
|
|
|
|
#include "rocksjni/portal.h"
|
2015-02-10 21:59:40 +01:00
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
LoggerJniCallback::LoggerJniCallback(JNIEnv* env, jobject jlogger)
|
|
|
|
: JniCallback(env, jlogger) {
|
2015-03-10 23:16:21 +01:00
|
|
|
m_jLogMethodId = LoggerJni::getLogMethodId(env);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (m_jLogMethodId == nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown: NoSuchMethodException or OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 01:54:06 +02:00
|
|
|
|
|
|
|
jobject jdebug_level = InfoLogLevelJni::DEBUG_LEVEL(env);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (jdebug_level == nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown: NoSuchFieldError, ExceptionInInitializerError
|
|
|
|
// or OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 01:54:06 +02:00
|
|
|
m_jdebug_level = env->NewGlobalRef(jdebug_level);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (m_jdebug_level == nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 01:54:06 +02:00
|
|
|
|
|
|
|
jobject jinfo_level = InfoLogLevelJni::INFO_LEVEL(env);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (jinfo_level == nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown: NoSuchFieldError, ExceptionInInitializerError
|
|
|
|
// or OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 01:54:06 +02:00
|
|
|
m_jinfo_level = env->NewGlobalRef(jinfo_level);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (m_jinfo_level == nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 01:54:06 +02:00
|
|
|
|
|
|
|
jobject jwarn_level = InfoLogLevelJni::WARN_LEVEL(env);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (jwarn_level == nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown: NoSuchFieldError, ExceptionInInitializerError
|
|
|
|
// or OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 01:54:06 +02:00
|
|
|
m_jwarn_level = env->NewGlobalRef(jwarn_level);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (m_jwarn_level == nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 01:54:06 +02:00
|
|
|
|
|
|
|
jobject jerror_level = InfoLogLevelJni::ERROR_LEVEL(env);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (jerror_level == nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown: NoSuchFieldError, ExceptionInInitializerError
|
|
|
|
// or OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 01:54:06 +02:00
|
|
|
m_jerror_level = env->NewGlobalRef(jerror_level);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (m_jerror_level == nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 01:54:06 +02:00
|
|
|
|
|
|
|
jobject jfatal_level = InfoLogLevelJni::FATAL_LEVEL(env);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (jfatal_level == nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown: NoSuchFieldError, ExceptionInInitializerError
|
|
|
|
// or OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 01:54:06 +02:00
|
|
|
m_jfatal_level = env->NewGlobalRef(jfatal_level);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (m_jfatal_level == nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 01:54:06 +02:00
|
|
|
|
|
|
|
jobject jheader_level = InfoLogLevelJni::HEADER_LEVEL(env);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (jheader_level == nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown: NoSuchFieldError, ExceptionInInitializerError
|
|
|
|
// or OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 01:54:06 +02:00
|
|
|
m_jheader_level = env->NewGlobalRef(jheader_level);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (m_jheader_level == nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2015-02-10 21:59:40 +01:00
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void LoggerJniCallback::Logv(const char* /*format*/, va_list /*ap*/) {
|
2015-02-10 21:59:40 +01:00
|
|
|
// We implement this method because it is virtual but we don't
|
|
|
|
// use it because we need to know about the log level.
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void LoggerJniCallback::Logv(const InfoLogLevel log_level, const char* format,
|
|
|
|
va_list ap) {
|
2015-02-10 21:59:40 +01:00
|
|
|
if (GetInfoLogLevel() <= log_level) {
|
2015-03-10 23:16:21 +01:00
|
|
|
// determine InfoLogLevel java enum instance
|
|
|
|
jobject jlog_level;
|
|
|
|
switch (log_level) {
|
|
|
|
case rocksdb::InfoLogLevel::DEBUG_LEVEL:
|
2016-07-29 01:54:06 +02:00
|
|
|
jlog_level = m_jdebug_level;
|
2015-03-10 23:16:21 +01:00
|
|
|
break;
|
|
|
|
case rocksdb::InfoLogLevel::INFO_LEVEL:
|
2016-07-29 01:54:06 +02:00
|
|
|
jlog_level = m_jinfo_level;
|
|
|
|
break;
|
|
|
|
case rocksdb::InfoLogLevel::WARN_LEVEL:
|
|
|
|
jlog_level = m_jwarn_level;
|
2015-03-10 23:16:21 +01:00
|
|
|
break;
|
|
|
|
case rocksdb::InfoLogLevel::ERROR_LEVEL:
|
2016-07-29 01:54:06 +02:00
|
|
|
jlog_level = m_jerror_level;
|
2015-03-10 23:16:21 +01:00
|
|
|
break;
|
|
|
|
case rocksdb::InfoLogLevel::FATAL_LEVEL:
|
2016-07-29 01:54:06 +02:00
|
|
|
jlog_level = m_jfatal_level;
|
|
|
|
break;
|
|
|
|
case rocksdb::InfoLogLevel::HEADER_LEVEL:
|
|
|
|
jlog_level = m_jheader_level;
|
2015-03-10 23:16:21 +01:00
|
|
|
break;
|
|
|
|
default:
|
2016-07-29 01:54:06 +02:00
|
|
|
jlog_level = m_jfatal_level;
|
2015-03-10 23:16:21 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-02-28 01:26:12 +01:00
|
|
|
assert(format != nullptr);
|
|
|
|
const std::unique_ptr<char[]> msg = format_str(format, ap);
|
2016-07-29 01:54:06 +02:00
|
|
|
|
2017-02-28 01:26:12 +01:00
|
|
|
// pass msg to java callback handler
|
|
|
|
jboolean attached_thread = JNI_FALSE;
|
2017-10-12 20:06:51 +02:00
|
|
|
JNIEnv* env = getJniEnv(&attached_thread);
|
2017-02-28 01:26:12 +01:00
|
|
|
assert(env != nullptr);
|
2015-02-10 21:59:40 +01:00
|
|
|
|
2017-02-28 01:26:12 +01:00
|
|
|
jstring jmsg = env->NewStringUTF(msg.get());
|
2018-04-13 02:55:14 +02:00
|
|
|
if (jmsg == nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// unable to construct string
|
2018-04-13 02:55:14 +02:00
|
|
|
if (env->ExceptionCheck()) {
|
|
|
|
env->ExceptionDescribe(); // print out exception to stderr
|
2015-02-10 21:59:40 +01:00
|
|
|
}
|
2017-10-12 20:06:51 +02:00
|
|
|
releaseJniEnv(attached_thread);
|
2017-02-28 01:26:12 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-04-13 02:55:14 +02:00
|
|
|
if (env->ExceptionCheck()) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown: OutOfMemoryError
|
2018-04-13 02:55:14 +02:00
|
|
|
env->ExceptionDescribe(); // print out exception to stderr
|
2017-02-28 01:26:12 +01:00
|
|
|
env->DeleteLocalRef(jmsg);
|
2017-10-12 20:06:51 +02:00
|
|
|
releaseJniEnv(attached_thread);
|
2017-02-28 01:26:12 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-12 20:06:51 +02:00
|
|
|
env->CallVoidMethod(m_jcallback_obj, m_jLogMethodId, jlog_level, jmsg);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (env->ExceptionCheck()) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown
|
2018-04-13 02:55:14 +02:00
|
|
|
env->ExceptionDescribe(); // print out exception to stderr
|
2017-02-28 01:26:12 +01:00
|
|
|
env->DeleteLocalRef(jmsg);
|
2017-10-12 20:06:51 +02:00
|
|
|
releaseJniEnv(attached_thread);
|
2017-02-28 01:26:12 +01:00
|
|
|
return;
|
2015-02-10 21:59:40 +01:00
|
|
|
}
|
2017-02-28 01:26:12 +01:00
|
|
|
|
|
|
|
env->DeleteLocalRef(jmsg);
|
2017-10-12 20:06:51 +02:00
|
|
|
releaseJniEnv(attached_thread);
|
2015-02-10 21:59:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
std::unique_ptr<char[]> LoggerJniCallback::format_str(const char* format,
|
|
|
|
va_list ap) const {
|
2017-02-28 01:26:12 +01:00
|
|
|
va_list ap_copy;
|
|
|
|
|
|
|
|
va_copy(ap_copy, ap);
|
2018-04-13 02:55:14 +02:00
|
|
|
const size_t required =
|
|
|
|
vsnprintf(nullptr, 0, format, ap_copy) + 1; // Extra space for '\0'
|
2017-02-28 01:26:12 +01:00
|
|
|
va_end(ap_copy);
|
|
|
|
|
|
|
|
std::unique_ptr<char[]> buf(new char[required]);
|
|
|
|
|
|
|
|
va_copy(ap_copy, ap);
|
|
|
|
vsnprintf(buf.get(), required, format, ap_copy);
|
|
|
|
va_end(ap_copy);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
2015-02-10 21:59:40 +01:00
|
|
|
LoggerJniCallback::~LoggerJniCallback() {
|
2017-02-28 01:26:12 +01:00
|
|
|
jboolean attached_thread = JNI_FALSE;
|
2017-10-12 20:06:51 +02:00
|
|
|
JNIEnv* env = getJniEnv(&attached_thread);
|
2017-02-28 01:26:12 +01:00
|
|
|
assert(env != nullptr);
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
if (m_jdebug_level != nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
env->DeleteGlobalRef(m_jdebug_level);
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
if (m_jinfo_level != nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
env->DeleteGlobalRef(m_jinfo_level);
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
if (m_jwarn_level != nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
env->DeleteGlobalRef(m_jwarn_level);
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
if (m_jerror_level != nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
env->DeleteGlobalRef(m_jerror_level);
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
if (m_jfatal_level != nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
env->DeleteGlobalRef(m_jfatal_level);
|
|
|
|
}
|
2016-07-29 01:54:06 +02:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
if (m_jheader_level != nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
env->DeleteGlobalRef(m_jheader_level);
|
|
|
|
}
|
2016-07-29 01:54:06 +02:00
|
|
|
|
2017-10-12 20:06:51 +02:00
|
|
|
releaseJniEnv(attached_thread);
|
2015-02-10 21:59:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
/*
|
2015-03-10 23:16:21 +01:00
|
|
|
* Class: org_rocksdb_Logger
|
2015-02-10 21:59:40 +01:00
|
|
|
* Method: createNewLoggerOptions
|
2016-01-20 18:05:41 +01:00
|
|
|
* Signature: (J)J
|
2015-02-10 21:59:40 +01:00
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
jlong Java_org_rocksdb_Logger_createNewLoggerOptions(JNIEnv* env, jobject jobj,
|
|
|
|
jlong joptions) {
|
2017-02-28 01:26:12 +01:00
|
|
|
auto* sptr_logger = new std::shared_ptr<rocksdb::LoggerJniCallback>(
|
|
|
|
new rocksdb::LoggerJniCallback(env, jobj));
|
|
|
|
|
2015-02-10 21:59:40 +01:00
|
|
|
// set log level
|
2017-02-28 01:26:12 +01:00
|
|
|
auto* options = reinterpret_cast<rocksdb::Options*>(joptions);
|
|
|
|
sptr_logger->get()->SetInfoLogLevel(options->info_log_level);
|
|
|
|
|
|
|
|
return reinterpret_cast<jlong>(sptr_logger);
|
2015-02-10 21:59:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-03-10 23:16:21 +01:00
|
|
|
* Class: org_rocksdb_Logger
|
2015-02-10 21:59:40 +01:00
|
|
|
* Method: createNewLoggerDbOptions
|
2016-01-20 18:05:41 +01:00
|
|
|
* Signature: (J)J
|
2015-02-10 21:59:40 +01:00
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
jlong Java_org_rocksdb_Logger_createNewLoggerDbOptions(JNIEnv* env,
|
|
|
|
jobject jobj,
|
|
|
|
jlong jdb_options) {
|
2017-02-28 01:26:12 +01:00
|
|
|
auto* sptr_logger = new std::shared_ptr<rocksdb::LoggerJniCallback>(
|
2018-04-13 02:55:14 +02:00
|
|
|
new rocksdb::LoggerJniCallback(env, jobj));
|
2017-02-28 01:26:12 +01:00
|
|
|
|
2015-02-10 21:59:40 +01:00
|
|
|
// set log level
|
2017-02-28 01:26:12 +01:00
|
|
|
auto* db_options = reinterpret_cast<rocksdb::DBOptions*>(jdb_options);
|
|
|
|
sptr_logger->get()->SetInfoLogLevel(db_options->info_log_level);
|
|
|
|
|
|
|
|
return reinterpret_cast<jlong>(sptr_logger);
|
2015-02-10 21:59:40 +01:00
|
|
|
}
|
|
|
|
|
2015-03-10 22:57:15 +01:00
|
|
|
/*
|
2015-03-10 23:16:21 +01:00
|
|
|
* Class: org_rocksdb_Logger
|
2015-03-10 22:57:15 +01:00
|
|
|
* Method: setInfoLogLevel
|
|
|
|
* Signature: (JB)V
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
void Java_org_rocksdb_Logger_setInfoLogLevel(JNIEnv* /*env*/, jobject /*jobj*/,
|
|
|
|
jlong jhandle, jbyte jlog_level) {
|
2017-02-28 01:26:12 +01:00
|
|
|
auto* handle =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<std::shared_ptr<rocksdb::LoggerJniCallback>*>(jhandle);
|
|
|
|
handle->get()->SetInfoLogLevel(
|
|
|
|
static_cast<rocksdb::InfoLogLevel>(jlog_level));
|
2015-03-10 22:57:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-03-10 23:16:21 +01:00
|
|
|
* Class: org_rocksdb_Logger
|
2015-03-10 22:57:15 +01:00
|
|
|
* Method: infoLogLevel
|
|
|
|
* Signature: (J)B
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
jbyte Java_org_rocksdb_Logger_infoLogLevel(JNIEnv* /*env*/, jobject /*jobj*/,
|
|
|
|
jlong jhandle) {
|
2017-02-28 01:26:12 +01:00
|
|
|
auto* handle =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<std::shared_ptr<rocksdb::LoggerJniCallback>*>(jhandle);
|
2017-02-28 01:26:12 +01:00
|
|
|
return static_cast<jbyte>(handle->get()->GetInfoLogLevel());
|
2015-03-10 22:57:15 +01:00
|
|
|
}
|
|
|
|
|
2015-02-10 21:59:40 +01:00
|
|
|
/*
|
2015-03-10 23:16:21 +01:00
|
|
|
* Class: org_rocksdb_Logger
|
2015-02-10 21:59:40 +01:00
|
|
|
* Method: disposeInternal
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
void Java_org_rocksdb_Logger_disposeInternal(JNIEnv* /*env*/, jobject /*jobj*/,
|
|
|
|
jlong jhandle) {
|
2017-02-28 01:26:12 +01:00
|
|
|
auto* handle =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<std::shared_ptr<rocksdb::LoggerJniCallback>*>(jhandle);
|
2017-02-28 01:26:12 +01:00
|
|
|
delete handle; // delete std::shared_ptr
|
2015-02-10 21:59:40 +01:00
|
|
|
}
|