Last active
January 20, 2018 07:40
-
-
Save lizhangqu/f1047a4641a547e4a37512be45d77096 to your computer and use it in GitHub Desktop.
JniDiagnostics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Diagnostics.h" | |
JNIEXPORT jint JNICALL Java_io_github_lizhangqu_test( | |
JNIEnv* env, jclass clazz, jobject diagnostics_obj) { | |
JniDiagnostics diagnostics(env, diagnostics_obj); | |
return test1(&diagnostics); | |
} | |
int test1(IDiagnostics* diagnostics) { | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DIAGNOSTICS_H | |
#define DIAGNOSTICS_H | |
#include <jni.h> | |
#include <stdio.h> | |
#if !defined(DISALLOW_COPY_AND_ASSIGN) | |
// DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. It goes in the private: | |
// declarations in a class. | |
#if __cplusplus >= 201103L | |
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ | |
TypeName(const TypeName&) = delete; \ | |
void operator=(const TypeName&) = delete | |
#else | |
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ | |
TypeName(const TypeName&); \ | |
void operator=(const TypeName&) | |
#endif // __has_feature(cxx_deleted_functions) | |
#endif // !defined(DISALLOW_COPY_AND_ASSIGN) | |
struct IDiagnostics { | |
virtual ~IDiagnostics() = default; | |
enum class Level { | |
VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT | |
}; | |
virtual void log(Level level, const char *message) = 0; | |
virtual void verbose(const char *message) { | |
log(Level::VERBOSE, message); | |
} | |
virtual void debug(const char *message) { | |
log(Level::DEBUG, message); | |
} | |
virtual void info(const char *message) { | |
log(Level::INFO, message); | |
} | |
virtual void warn(const char *message) { | |
log(Level::WARN, message); | |
} | |
virtual void error(const char *message) { | |
log(Level::ERROR, message); | |
} | |
virtual void assert(const char *message) { | |
log(Level::ASSERT, message); | |
} | |
}; | |
class JniDiagnostics : public IDiagnostics { | |
public: | |
JniDiagnostics(JNIEnv *env, jobject diagnostics_obj) | |
: env_(env), diagnostics_obj_(diagnostics_obj) { | |
mid_ = NULL; | |
} | |
void log(Level level, const char *message) override { | |
jint level_value; | |
switch (level) { | |
case Level::VERBOSE: | |
level_value = 1; | |
break; | |
case Level::DEBUG: | |
level_value = 2; | |
break; | |
case Level::INFO: | |
level_value = 3; | |
break; | |
case Level::WARN: | |
level_value = 4; | |
break; | |
case Level::ERROR: | |
level_value = 5; | |
break; | |
case Level::ASSERT: | |
level_value = 6; | |
break; | |
} | |
jstring jMessage = env_->NewStringUTF(message); | |
if (!mid_) { | |
jclass diagnostics_cls = env_->GetObjectClass(diagnostics_obj_); | |
mid_ = env_->GetMethodID(diagnostics_cls, "log", | |
"(ILjava/lang/String;)V"); | |
} | |
env_->CallVoidMethod(diagnostics_obj_, mid_, level_value, jMessage); | |
} | |
private: | |
JNIEnv *env_; | |
jobject diagnostics_obj_; | |
jmethodID mid_; | |
DISALLOW_COPY_AND_ASSIGN(JniDiagnostics); | |
}; | |
#endif /*DIAGNOSTICS_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IDiagnostics { | |
enum LogLevel { | |
VERBOSE(1), DEBUG(2), INFO(3), WARN(4), ERROR(5), ASSERT(6); | |
int level; | |
LogLevel(int level) { | |
this.level = level; | |
} | |
static LogLevel ofLevel(int level) { | |
LogLevel[] values = values(); | |
//noinspection ConstantConditions | |
if (values != null && values.length > 0) { | |
for (LogLevel logLevel : values) { | |
if (logLevel.level == level) { | |
return logLevel; | |
} | |
} | |
} | |
return INFO; | |
} | |
} | |
/** | |
* @param level the log level | |
* @param message the actual message. | |
*/ | |
void log(int level, String message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment