00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "fennel/common/CommonPreamble.h"
00025 #include "fennel/farrago/JavaTraceTarget.h"
00026
00027 #include "boost/lexical_cast.hpp"
00028
00029 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/farrago/JavaTraceTarget.cpp#17 $");
00030
00031 #define JAVATRACETARGET_TYPE_STR ("JavaTraceTarget")
00032
00033 JavaTraceTarget::JavaTraceTarget()
00034 {
00035 JniEnvAutoRef pEnv;
00036 jclass classNativeTrace = pEnv->FindClass(
00037 "net/sf/farrago/util/NativeTrace");
00038
00039 jmethodID methInstance =
00040 pEnv->GetStaticMethodID(
00041 classNativeTrace, "instance",
00042 "()Lnet/sf/farrago/util/NativeTrace;");
00043
00044 jobject javaTraceInit =
00045 pEnv->CallStaticObjectMethod(classNativeTrace, methInstance);
00046
00047 JniUtil::incrementHandleCount(JAVATRACETARGET_TYPE_STR, this);
00048 javaTrace = pEnv->NewGlobalRef(javaTraceInit);
00049
00050
00051 assert(javaTrace);
00052
00053 methTrace = pEnv->GetMethodID(
00054 classNativeTrace,"trace",
00055 "(Ljava/lang/String;ILjava/lang/String;)V");
00056 methGetSourceTraceLevel = pEnv->GetMethodID(
00057 classNativeTrace,"getSourceTraceLevel",
00058 "(Ljava/lang/String;)I");
00059 }
00060
00061 JavaTraceTarget::JavaTraceTarget(
00062 jobject javaTraceInit, jmethodID methTraceInit,
00063 jmethodID methGetSourceTraceLevelInit)
00064 {
00065 JniEnvAutoRef pEnv;
00066
00067 JniUtil::incrementHandleCount(JAVATRACETARGET_TYPE_STR, this);
00068 javaTrace = pEnv->NewGlobalRef(javaTraceInit);
00069
00070
00071 assert(javaTrace);
00072
00073 methTrace = methTraceInit;
00074 methGetSourceTraceLevel = methGetSourceTraceLevelInit;
00075 }
00076
00077 JavaTraceTarget::~JavaTraceTarget()
00078 {
00079 JniEnvAutoRef pEnv;
00080
00081 pEnv->DeleteGlobalRef(javaTrace);
00082 JniUtil::decrementHandleCount(JAVATRACETARGET_TYPE_STR, this);
00083
00084 javaTrace = NULL;
00085 }
00086
00087 void JavaTraceTarget::notifyTrace(
00088 std::string source,TraceLevel level,std::string message)
00089 {
00090 JniEnvAutoRef pEnv;
00091
00092
00093
00094
00095 jstring javaSource = pEnv->NewStringUTF(source.c_str());
00096 JniLocalRefReaper javaSourceReaper(pEnv, javaSource);
00097 jstring javaMessage = pEnv->NewStringUTF(message.c_str());
00098 JniLocalRefReaper javaMessageReaper(pEnv, javaMessage);
00099 pEnv->CallVoidMethod(javaTrace,methTrace,javaSource,level,javaMessage);
00100 }
00101
00102 TraceLevel JavaTraceTarget::getSourceTraceLevel(std::string source)
00103 {
00104 JniEnvAutoRef pEnv;
00105 jstring javaSource = pEnv->NewStringUTF(source.c_str());
00106 int level = pEnv->CallIntMethod(
00107 javaTrace,methGetSourceTraceLevel,javaSource);
00108 return static_cast<TraceLevel>(level);
00109 }
00110
00111 void JavaTraceTarget::beginSnapshot()
00112 {
00113 notifyTrace(
00114 "", TRACE_PERFCOUNTER_BEGIN_SNAPSHOT, "");
00115 }
00116
00117 void JavaTraceTarget::endSnapshot()
00118 {
00119 notifyTrace(
00120 "", TRACE_PERFCOUNTER_END_SNAPSHOT, "");
00121 }
00122
00123 void JavaTraceTarget::writeCounter(std::string name, int64_t value)
00124 {
00125 std::string s = boost::lexical_cast<std::string>(value);
00126 notifyTrace(
00127 name, TRACE_PERFCOUNTER_UPDATE, s);
00128 }
00129
00130 void JavaTraceTarget::onThreadStart()
00131 {
00132 JniEnvAutoRef pEnv;
00133
00134
00135
00136
00137 pEnv.suppressDetach();
00138 }
00139
00140 void JavaTraceTarget::onThreadEnd()
00141 {
00142 JniUtil::detachJavaEnv();
00143 }
00144
00145 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/farrago/JavaTraceTarget.cpp#17 $");
00146
00147