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