You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by ea...@apache.org on 2007/09/17 16:07:10 UTC
svn commit: r576447 - in /incubator/uima/uimacpp/trunk/src:
framework/log.cpp framework/resmgr.cpp framework/uima/log.hpp
framework/uima/resmgr.hpp jni/jni.cpp jni/uima/jni.hpp
test/src/test_engine.cpp
Author: eae
Date: Mon Sep 17 07:07:09 2007
New Revision: 576447
URL: http://svn.apache.org/viewvc?rev=576447&view=rev
Log:
UIMA-567 Bhavani's reimplementation of the Logger interface
Modified:
incubator/uima/uimacpp/trunk/src/framework/log.cpp
incubator/uima/uimacpp/trunk/src/framework/resmgr.cpp
incubator/uima/uimacpp/trunk/src/framework/uima/log.hpp
incubator/uima/uimacpp/trunk/src/framework/uima/resmgr.hpp
incubator/uima/uimacpp/trunk/src/jni/jni.cpp
incubator/uima/uimacpp/trunk/src/jni/uima/jni.hpp
incubator/uima/uimacpp/trunk/src/test/src/test_engine.cpp
Modified: incubator/uima/uimacpp/trunk/src/framework/log.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/log.cpp?rev=576447&r1=576446&r2=576447&view=diff
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/log.cpp (original)
+++ incubator/uima/uimacpp/trunk/src/framework/log.cpp Mon Sep 17 07:07:09 2007
@@ -1,5 +1,5 @@
/** @name log.cpp
------------------------------------------------------------------------------
+------------------------------------------------------------------F-----------
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -62,6 +62,70 @@
/* Private Implementation */
/* ----------------------------------------------------------------------- */
namespace uima {
+ FileLogger::FileLogger(string filename) : iv_logfile(0) {
+ iv_logfile = fopen(filename.c_str(),"a");
+ if (iv_logfile == NULL) { //Need to handle this better
+ //cerr << "Could not open the log file " << cpszLogFile << endl;
+ string str = "Could not open the log file ";
+ str += filename;
+ UIMA_EXC_THROW_NEW(Uima_runtime_error,
+ UIMA_MSG_ID_LITERAL_STRING,
+ UIMA_MSG_ID_LITERAL_STRING,
+ ErrorMessage(UIMA_MSG_ID_LITERAL_STRING, str.c_str()),
+ ErrorInfo::unrecoverable);
+ }
+ }
+
+ void FileLogger::log(LogStream::EnEntryType enType,
+ string cpszClass,
+ string cpszMethod,
+ string cpszMsg,
+ long lUserCode)
+ /* ----------------------------------------------------------------------- */
+ {
+ //write to local log file if one is available
+ string message = cpszClass + " ";
+ if (cpszMethod.length() >0) {
+ message += cpszMethod + " ";
+ }
+ message += cpszMsg;
+ message = format(enType, message, lUserCode);
+ fwrite(message.c_str(), 1, message.size(), iv_logfile);
+ fflush(iv_logfile);
+ }
+
+ std::string FileLogger::format(LogStream::EnEntryType enType,
+ const string & cpszMsg,
+ long lUserCode) {
+
+ time_t rawtime;
+ time ( &rawtime );
+ string currts = ctime(&rawtime);
+
+ stringstream str;
+ str << currts.substr(0,currts.length()-1);
+
+ //map enType to string
+ switch (enType) {
+ case LogStream::EnWarning :
+ str << " WARNING: ";
+ str << " RC=" << lUserCode << " ";
+ break;
+ case LogStream::EnError :
+ str << " SEVERE: ";
+ str << " RC=" << lUserCode << " ";
+ break;
+ default:
+ str << " INFO: ";
+ if (lUserCode !=0) {
+ str << " RC=" << lUserCode << " ";
+ }
+ }
+
+ str << cpszMsg << endl;
+
+ return str.str();
+ }
TyMessageId LogFacility::getTypeAsMessageId(LogStream::EnEntryType enType) const
/* ----------------------------------------------------------------------- */
@@ -74,7 +138,7 @@
case LogStream::EnError :
return(UIMA_MSG_ID_LOG_ERROR);
default:
- assertWithMsg(false, _TEXT("Unknow EnLogEntryType")); //lint !e506: Constant value Boolean
+ assertWithMsg(false, _TEXT("Unknown EnLogEntryType")); //lint !e506: Constant value Boolean
}
return(0); /* shutup compiler */
}
@@ -82,66 +146,16 @@
void LogFacility::doLog(LogStream::EnEntryType enType, const TCHAR * cpszMsg, long lUserCode) const
/* ----------------------------------------------------------------------- */
{
+ string method;
if (isLoggable(enType)) {
- if (ResourceManager::hasInstance() ) {
- ResourceManager & resmgr = ResourceManager::getInstance();
- if ( resmgr.isJavaLoggingEnabled()) {
- //route to java logger if one is available
- resmgr.writeToJavaLogger(enType, iv_strOrigin,
- cpszMsg);
- } else if (iv_logFile != NULL) {
- //write to local log file if one is available
- string message = format(enType, cpszMsg, lUserCode);
- fwrite(message.c_str(), 1, message.size(), iv_logFile);
- fflush(iv_logFile);
- }
+ for (int i=0; i < vecLoggers.size(); i++) {
+ vecLoggers.at(i)->log(enType,this->iv_strOrigin,
+ method,cpszMsg,lUserCode);
}
}
}
- std::string LogFacility::format(LogStream::EnEntryType enType, const TCHAR * cpszMsg, long lUserCode) const {
-
- //if (isLoggable(enType)) {
- /***char dateStr [9];
- char timeStr [9];
- _strdate( dateStr);
- _strtime( timeStr );
-
- message += dateStr;
- message += " ";
- message += timeStr;
- message += " ";
- message += iv_strOrigin.c_str();
- message += " ";
- ***/
-
- time_t rawtime;
- time ( &rawtime );
- string currts = ctime(&rawtime);
- string message = currts.substr(0,currts.length()-1);
-
- //map enType to Java levels
- switch (enType) {
- case LogStream::EnMessage :
- message += (" INFO: ");
- break;
- case LogStream::EnWarning :
- message += " WARNING: ";
- break;
- case LogStream::EnError :
- message += " SEVERE: ";
- break;
- default:
- message += " INFO: ";
- }
-
- message += cpszMsg;
- message += "\n";
- //}
- return message;
- }
-
bool LogFacility::isLoggable(LogStream::EnEntryType enType) const {
///LogStream::EnEntryType minLogLevel = ResourceManager::getInstance().getLoggingLevel();
if (enType < iv_logLevel) {
@@ -226,23 +240,22 @@
LogFacility::LogFacility(icu::UnicodeString const & crEngineName):
iv_lLastUserCode(0),
iv_logStream(*this, LogStream::EnMessage),
- iv_logFile(NULL),
- iv_logLevel(LogStream::EnMessage) {
+ vecLoggers(ResourceManager::getInstance().getLoggers()),
+ iv_logLevel(ResourceManager::getInstance().getLoggingLevel()) {
UnicodeStringRef ref(crEngineName);
ref.extract(iv_strOrigin, CCSID::getDefaultName() );
- iv_logFile = uima::ResourceManager::getInstance().getLogFile();
- iv_logLevel = uima::ResourceManager::getInstance().getLoggingLevel();
+
}
LogFacility::LogFacility(icu::UnicodeString const & crEngineName,
- FILE * crpLogFile,
LogStream::EnEntryType crLoggingLevel):
iv_lLastUserCode(0),
- iv_logStream(*this, LogStream::EnMessage),
- iv_logFile(crpLogFile),
+ iv_logStream(*this, crLoggingLevel),
+ vecLoggers(ResourceManager::getInstance().getLoggers()),
iv_logLevel(crLoggingLevel) {
UnicodeStringRef ref(crEngineName);
ref.extract(iv_strOrigin, CCSID::getDefaultName() );
+
}
LogFacility::~LogFacility() {
@@ -305,4 +318,5 @@
} //namespace uima
/* <EOF> */
+
Modified: incubator/uima/uimacpp/trunk/src/framework/resmgr.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/resmgr.cpp?rev=576447&r1=576446&r2=576447&view=diff
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/resmgr.cpp (original)
+++ incubator/uima/uimacpp/trunk/src/framework/resmgr.cpp Mon Sep 17 07:07:09 2007
@@ -127,12 +127,6 @@
ResourceManager * ResourceManager::cv_pclSingletonInstance = 0;
TyProcedure iv_traceProc;
- //used for java logging
- JNIEnv * cv_env = 0; //handle to Java environment
- jclass cv_clazz = 0; //proxy class on java side
- jmethodID cv_logMethod = 0; //log method
-
-
/* see docu for class ResourceManagerAutomaticInstanceDestructor above */
static ResourceManagerAutomaticInstanceDestructor gs_clResourceManagerAutomaticInstanceDestructor;
@@ -153,14 +147,13 @@
iv_locationWork("."),
iv_locationData("."),
iv_frameworkLogger(NULL),
- iv_logFile(NULL),
iv_logLevel(LogStream::EnMessage),
- iv_useJavaLogging(false)
+ iv_fileLogger(NULL),
+ iv_loggers()
/* ----------------------------------------------------------------------- */
{
string str;
- const TCHAR * cpszLogFile = 0;
assert(EXISTS(cpszInstance));
UIMA_TPRINT(_TEXT("instance: ") << cpszInstance);
@@ -172,39 +165,6 @@
util::Trace clTrace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_RESOURCE_MGR);
clTrace.dump(_TEXT("UIMACPP Instance"), cpszInstance);
- /* determine log file name */
- str = UIMA_ENVVAR_LOG_FILE;
- util::EnvironmentVariableQueryOnly clEnvVarLogFilePath(str.c_str());
- UIMA_TPRINT(_TEXT("querying envvar: ") << str.c_str());
- if (clEnvVarLogFilePath.hasValue()) {
- cpszLogFile = clEnvVarLogFilePath.getValue();
- UIMA_TPRINT(_TEXT("value: ") << cpszLogFile);
- /* open log file for writing */
- iv_logFile = fopen(cpszLogFile,"a");
- if (iv_logFile == NULL) { //Need to handle this better
- //effectively logging is OFF ... throw Exception ?
- //cerr << "Could not open the log file " << cpszLogFile << endl;
- str = "Could not open the log file ";
- str += cpszLogFile;
- UIMA_EXC_THROW_NEW(Uima_runtime_error,
- UIMA_MSG_ID_LITERAL_STRING,
- UIMA_MSG_ID_LITERAL_STRING,
- ErrorMessage(UIMA_MSG_ID_LITERAL_STRING, str.c_str()),
- ErrorInfo::unrecoverable);
- } else {
- str = "Logging directed to file ";
- str += cpszLogFile;
- }
- } else {
- iv_logFile = NULL; //logging is effectively OFF
- str = "Logging is OFF";
- }
-
- //instantiate framework logger
- iv_frameworkLogger = new LogFacility(UnicodeString("org.apache.uima.cpp"), iv_logFile, iv_logLevel);
-
- iv_frameworkLogger->logMessage(str);
-
/* determine xsd path ... use $UIMACPP_HOME/data instead of iv_locationData
(which defaults to $UIMACPP_DATAPATH) as the latter is for user data. */
bDoSchemaValidation=true;
@@ -417,6 +377,7 @@
deleteResourceList( *pAnnotators );
}
iv_resources.clear();
+
UIMA_TPRINT(" ...seek and destroy!");
}
@@ -538,18 +499,37 @@
iv_logLevel=level;
}
- FILE * ResourceManager::getLogFile() {
- return iv_logFile;
+ TCHAR const * ResourceManager::getSchemaInfo() {
+ return schemaInfo;
+ }
+
+ void ResourceManager::registerLogger(Logger * pLogger) {
+ iv_loggers.push_back(pLogger);
}
- bool ResourceManager::isJavaLoggingEnabled() {
- return iv_useJavaLogging;
+ vector<Logger*> & ResourceManager::getLoggers() {
+ return iv_loggers;
}
+
+ void ResourceManager::unregisterLogger(Logger * pLogger) {
+ vector<Logger*>::iterator iter;
+ for (iter=iv_loggers.begin(); iter != iv_loggers.end();iter++) {
+ if (*iter == pLogger) {
+ iv_loggers.erase(iter);
+ return;
+ }
+ }
+
+ string str = "Logger not found. Could not unregister.";
+ UIMA_EXC_THROW_NEW(Uima_runtime_error,
+ UIMA_MSG_ID_LITERAL_STRING,
+ UIMA_MSG_ID_LITERAL_STRING,
+ ErrorMessage(UIMA_MSG_ID_LITERAL_STRING, str.c_str()),
+ ErrorInfo::unrecoverable);
- TCHAR const * ResourceManager::getSchemaInfo() {
- return schemaInfo;
}
+
void ResourceManager::registerFactory(icu::UnicodeString const & crKind, ResourceFactoryABase & crFactory) {
iv_resourceFactories.insert(TyResourceFactories::value_type(crKind, &crFactory));
}
@@ -674,8 +654,38 @@
cv_pclSingletonInstance->iv_utLastErrorId = UIMA_ERR_RESMGR_COULD_NOT_INITIALIZE_XML4C;
assertWithMsg(false, "XML4C initialization failed");
}
- }
- // release mutex
+
+ //create the fileLogger if UIMACPP_LOGFILE env variable is set
+ /* determine log file name */
+ string str = UIMA_ENVVAR_LOG_FILE;
+ util::EnvironmentVariableQueryOnly clEnvVarLogFilePath(str.c_str());
+ UIMA_TPRINT(_TEXT("querying envvar: ") << str.c_str());
+ if (clEnvVarLogFilePath.hasValue()) {
+ const TCHAR * cpszLogFile = clEnvVarLogFilePath.getValue();
+ UIMA_TPRINT(_TEXT("value: ") << cpszLogFile);
+
+ /* create an instance of FileLogger and register it. */
+ cv_pclSingletonInstance->iv_fileLogger = new FileLogger(cpszLogFile);
+
+ if (cv_pclSingletonInstance->iv_fileLogger == NULL) { //Need to handle this better
+ //cerr << "Could not open the log file " << cpszLogFile << endl;
+ str = "Could not create FileLogger";
+ str += cpszLogFile;
+ UIMA_EXC_THROW_NEW(Uima_runtime_error,
+ UIMA_MSG_ID_LITERAL_STRING,
+ UIMA_MSG_ID_LITERAL_STRING,
+ ErrorMessage(UIMA_MSG_ID_LITERAL_STRING, str.c_str()),
+ ErrorInfo::unrecoverable);
+ } else {
+ cv_pclSingletonInstance->registerLogger(cv_pclSingletonInstance->iv_fileLogger);
+ }
+ }
+
+ //instantiate framework logger
+ cv_pclSingletonInstance->iv_frameworkLogger = new LogFacility(UnicodeString("org.apache.uima.cpp"), cv_pclSingletonInstance->iv_logLevel);
+
+ cv_pclSingletonInstance->iv_frameworkLogger->logMessage("ResourceManager Instance created.");
+ } // release mutex
UIMA_TPRINT("ResourceManager instance created");
return(*cv_pclSingletonInstance);
}
@@ -704,20 +714,15 @@
cv_pclSingletonInstance->iv_utLastErrorId = UIMA_ERR_RESMGR_COULD_NOT_TERMINATE_XML4C;
assertWithMsg(false, "XML4C termination failed");
}
- if (cv_pclSingletonInstance->iv_logFile != NULL) {
- fclose(cv_pclSingletonInstance->iv_logFile);
- }
-
-// if (cv_clazz != NULL && cv_env != NULL) {
-// cv_env->DeleteGlobalRef( (jobject) cv_clazz);
-// }
-//ee This is bad because cv_env is not valid when this method is called
-// With a common single UimaAnnotator calling from Java, this global reference not needed
-
+
if (cv_pclSingletonInstance->iv_frameworkLogger != NULL) {
delete cv_pclSingletonInstance->iv_frameworkLogger;
}
+ if (cv_pclSingletonInstance->iv_fileLogger != NULL) {
+ delete cv_pclSingletonInstance->iv_fileLogger;
+ }
+
assert(EXISTS(cv_pclSingletonInstance));
delete cv_pclSingletonInstance;
cv_pclSingletonInstance = 0;
@@ -786,146 +791,6 @@
return(clFilename.isExistent());
}
- void ResourceManager::setupJavaLogging(void * jniEnv) {
- try {
-
- if (iv_useJavaLogging ) {
- // Logging previously setup
- cv_env = (JNIEnv*) jniEnv;
- } else {
- UIMA_LOG_STREAM_MESSAGE(getLogger(), "Switching to Java Logging " << endl);
- cv_env = (JNIEnv*) jniEnv;
- assert( EXISTS(cv_env) );
-
- cv_clazz = cv_env->FindClass(JAVA_PROXY);
- if (cv_clazz == NULL ) {
- UIMA_LOG_STREAM_MESSAGE(getLogger(), JAVA_PROXY " class not found. Could not setup Java logging. " << endl);
- cv_env->ExceptionClear();
- return;
- }
-
- cv_clazz = (jclass) cv_env->NewGlobalRef(cv_clazz);
- if (cv_clazz == NULL) {
- UIMA_LOG_STREAM_MESSAGE(getLogger(), "Setup global reference to " JAVA_PROXY " class failed. Could not setup Java logging. " << endl);
- cout << "ResourceManager::setupJavaLogging() ERROR: CPPJEDIIEngine could not construct " << endl;
- cv_env->ExceptionClear();
- return;
- }
-
- //query the current logging level
- jmethodID iv_getLoggingLevelMethod = cv_env->GetStaticMethodID(cv_clazz,
- "getLoggingLevel",
- "()I");
- if (iv_getLoggingLevelMethod == NULL) {
- UIMA_LOG_STREAM_MESSAGE(getLogger(), JAVA_PROXY ".getLoggingLevel() not found. Could not setup Java logging. " << endl);
- cout << "ResourceManager::setupJavaLogging() ERROR: CPPJEDIIEngine.getLoggingLevel() not found " << endl;
- cv_env->ExceptionClear();
- return;
- }
-
- //log method
- cv_logMethod = cv_env->GetStaticMethodID(cv_clazz, "log", "("
- "I" // level
- "Ljava/lang/String;" // source class
- "Ljava/lang/String;" // source method
- "Ljava/lang/String;" // message
- ")V");
-
- if (cv_logMethod == NULL) {
- UIMA_LOG_STREAM_MESSAGE(getLogger(), JAVA_PROXY ".log(int,string,string,string) not found. Could not setup Java logging. " << endl);
- cout << "ResourceManager::setupJavaLogging() ERROR: CPPJEDIIEngine.log() not found " << endl;
- cv_env->ExceptionClear();
- return;
- }
-
- //get the current logging level
- jint logginglevel = cv_env->CallStaticIntMethod(cv_clazz, iv_getLoggingLevelMethod);
-
- if (logginglevel == 0) {
- UIMA_LOG_STREAM_MESSAGE(getLogger(), "Could not determine current logging level. Setup Java logging failed. " << endl);
- cout << "ResourceManager::setupJavaLogging() ERROR: calling CPPJEDIIEngine.getLoggingLevel() " << endl;
- cv_env->ExceptionClear();
- return;
- }
-
- if (logginglevel == 3) {
- iv_logLevel = LogStream::EnError;
- } else if (logginglevel == 2) {
- iv_logLevel = LogStream::EnWarning;
- } else {
- iv_logLevel = LogStream::EnMessage;
- }
-
- iv_useJavaLogging=true;
- LogStream & msgstream = iv_frameworkLogger->getLogStream(LogStream::EnMessage);
- msgstream << "ResourceManager Logger settings usejavaLogging=" << iv_useJavaLogging
- << " loglevel from jedii=" << logginglevel << " uimacpp logging level set to=" << iv_logLevel << endl;
- msgstream.flush();
- }
- } catch (...) {
- cout << "Exception in setupJavaLogging() " << endl;
- cv_env->ExceptionDescribe();
- }
- }
-
-
- void ResourceManager::writeToJavaLogger(LogStream::EnEntryType entype,
- std::string srcclass,
- std::string srcmethod,
- const TCHAR * message) {
- try {
- //cout << "ResourceManager::writeToJavaLogger() "
- // << "level=" << entype << " "
- // << "srcclass=" << srcclass << " "
- // << "srcmethod=" << srcmethod << " "
- // << "message=" << message << endl;
- UnicodeString msg(message);
-
- // Convert the std::strings to Unicode using the default converter
- UnicodeString ustrsource(srcclass.c_str(), srcclass.length());
- UnicodeString ustrmethod(srcmethod.c_str(), srcmethod.length());
-
- jstring jsrcclass = cv_env->NewString((jchar const *) ustrsource.getBuffer(), ustrsource.length());
- jstring jsrcmethod = cv_env->NewString((jchar const *) ustrmethod.getBuffer(), ustrmethod.length());
- jstring jmessage = cv_env->NewString((jchar const *) msg.getBuffer(), msg.length());
-
- jint loglevel;
- if ( entype == LogStream::EnError) {
- loglevel = 3;
- } else if ( entype == LogStream::EnWarning ) {
- loglevel = 2;
- } else {
- loglevel = 1;
- }
-
- // Call exception clear
- cv_env->ExceptionClear();
-
- cv_env->CallStaticVoidMethod(cv_clazz,
- cv_logMethod,
- loglevel,
- jsrcclass,
- jsrcmethod,
- jmessage);
-
- // Check for exceptions :
- jthrowable exc = cv_env->ExceptionOccurred();
- if (exc != NULL) {
- cv_env->ExceptionDescribe();
- cv_env->ExceptionClear();
- }
- } catch (...) {
- cout << "Exception in JavaLogging()" << endl;
- }
- }
-
- void ResourceManager::writeToJavaLogger(LogStream::EnEntryType entype,
- std::string srcclass,
- const TCHAR * message) {
- std::string srcmethod(".");
- writeToJavaLogger(entype, srcclass, srcmethod, message);
- }
-
LogFacility & ResourceManager::getLogger() {
return *iv_frameworkLogger;
}
@@ -999,6 +864,7 @@
}
/* <EOF> */
+
Modified: incubator/uima/uimacpp/trunk/src/framework/uima/log.hpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/uima/log.hpp?rev=576447&r1=576446&r2=576447&view=diff
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/uima/log.hpp (original)
+++ incubator/uima/uimacpp/trunk/src/framework/uima/log.hpp Mon Sep 17 07:07:09 2007
@@ -66,7 +66,6 @@
namespace uima {
-
/**
* The class LogStream logs user-related information which would otherwise be
* hard to concatenate into a single string.
@@ -108,9 +107,63 @@
}
; /* LogStream */
+
+ /**
+ * This defines the interface for Loggers. To use a custom logger,
+ * implement a class that implements this interface, and register
+ * it using the ResourceManager::registerLogger(Logger *) method.
+ */
+
+ class UIMA_LINK_IMPORTSPEC Logger {
+ public:
+ /*
+ * Destructor
+ *
+ */
+ virtual ~Logger() { }
+
+ virtual void log(LogStream::EnEntryType entrytype,
+ string classname,
+ string methodname,
+ string message,
+ long errorCode) =0;
+
+ };
+
+ /** This class is the built in Logger that writes log
+ * message to a file. The ResourceManager instantiates
+ * and registers a FileLogger when the UIMACPP_LOGFILE
+ * environment variable is set to the location of a
+ * log file.
+ */
+ class UIMA_LINK_IMPORTSPEC FileLogger : public Logger {
+ public:
+ FileLogger(string filename);
+ ~FileLogger() {fclose(iv_logfile);}
+
+ virtual void log(LogStream::EnEntryType entrytype,
+ string classname,
+ string methodname,
+ string message,
+ long errorCode) ;
+
+ private:
+ /** Format the log message */
+ std::string format(LogStream::EnEntryType enType,
+ const string & cpszMsg,
+ long lUserCode) ;
+
+ FILE * iv_logfile;
+
+ };
+
+
+
/**
* The class LogFacility logs user-related information
- * for debugging or error recording purposes.
+ * for debugging or error recording purposes. The log messages
+ * are written to one or more loggers registered with the
+ * ResourceManager.
* \code
if(myErrorOccured)
{
@@ -154,11 +207,14 @@
/*@{*/
/** Create a new instance of a log facility. <TT>cpszAppKey</TT> must denote the
key of the application. */
- /** this constructor gets handle to the log file
- * from the ResourceManager */
+ /** this constructor gets handle to the loggers
+ * from the ResourceManager and sets the mininum log level to INFO.
+ * This is used in the AnnotatorContext to create separate instances
+ * of the LogFacility for each annotator. */
LogFacility(icu::UnicodeString const & );
- /** this constructor logs to the specified file */
- LogFacility(icu::UnicodeString const &, FILE *, LogStream::EnEntryType level );
+ /** this constructor gets the handle to loggers from the ResourceManager.
+ This constructor is used by the framework ResourceManager */
+ LogFacility(icu::UnicodeString const &, LogStream::EnEntryType level );
~LogFacility();
/*@}*/
/** @name Properties */
@@ -196,8 +252,6 @@
void logError(const icu::UnicodeString & crclMessage, long lUserCode = 0) const;
/** Log the specified error. */
void logError(const ErrorInfo & crclErrorInfo) const;
- /** Format the log message */
- std::string format(LogStream::EnEntryType enType, const TCHAR * cpszMsg, long lUserCode) const;
/** */
/*@}*/
protected:
@@ -210,7 +264,8 @@
ErrorInfo iv_errInfo;
LogStream iv_logStream;
LogStream::EnEntryType iv_logLevel;
- FILE * iv_logFile;
+ vector<Logger*> & vecLoggers;
+
/* --- functions --- */
TyMessageId getTypeAsMessageId(LogStream::EnEntryType enType) const;
void doLog(LogStream::EnEntryType enType, const TCHAR * cpszMsg, long lUserCode = 0) const;
@@ -327,4 +382,5 @@
#endif /* UIMA_LOG_HPP */
/* <EOF> */
+
Modified: incubator/uima/uimacpp/trunk/src/framework/uima/resmgr.hpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/uima/resmgr.hpp?rev=576447&r1=576446&r2=576447&view=diff
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/uima/resmgr.hpp (original)
+++ incubator/uima/uimacpp/trunk/src/framework/uima/resmgr.hpp Mon Sep 17 07:07:09 2007
@@ -240,6 +240,18 @@
static icu::UnicodeString resolveFilename(icu::UnicodeString const & filename,
icu::UnicodeString const & lastFilename);
+
+ /**
+ * Register Logger. Caller owns the logger instance.
+ */
+ void registerLogger (Logger *);
+ /**
+ * unregister this logger. Caller owns logger instance.
+ */
+ void unregisterLogger(Logger *);
+
+ vector<Logger*> & getLoggers();
+
/*@}*/
protected:
/* --- functions --- */
@@ -268,22 +280,14 @@
bool bDoSchemaValidation;
TCHAR schemaInfo[1024];
- FILE * iv_logFile;
- LogStream::EnEntryType iv_logLevel;
- uima::LogFacility * iv_frameworkLogger;
- FILE * getLogFile();
-
- //java logging
- bool iv_useJavaLogging; //use java logging if true; else log to local file.
-
- void writeToJavaLogger (LogStream::EnEntryType entype,
- std::string srcclass,
- const TCHAR * message);
-
- void writeToJavaLogger (LogStream::EnEntryType entype,
- std::string srcclass,
- std::string srcmethod,
- const TCHAR * message);
+ //Logging
+ vector<Logger *> iv_loggers; //registered loggers
+ FileLogger * iv_fileLogger; //created if UIMACPP_LOGFILE env variable is set.
+
+ LogStream::EnEntryType iv_logLevel; // minimum level for message to be logged
+ uima::LogFacility * iv_frameworkLogger; //instance of log facility used by the
+ //framework.
+
/* --- friends --- */
friend class ResourceManagerAutomaticInstanceDestructor;
@@ -322,4 +326,5 @@
#endif /* UIMA_RESMGR_HPP */
/* <EOF> */
+
Modified: incubator/uima/uimacpp/trunk/src/jni/jni.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/jni/jni.cpp?rev=576447&r1=576446&r2=576447&view=diff
==============================================================================
--- incubator/uima/uimacpp/trunk/src/jni/jni.cpp (original)
+++ incubator/uima/uimacpp/trunk/src/jni/jni.cpp Mon Sep 17 07:07:09 2007
@@ -65,7 +65,7 @@
#define JAVA_PREFIX(FUNC) UIMA_JNI_CONCAT(Java_org_apache_uima_uimacpp_UimacppEngine_, FUNC )
#define CONST_PREFIX(CONST) UIMA_JNI_CONCAT(org_apache_uima_uimacpp_UimacppEngine_, CONST )
-
+#define JAVA_LOGGER_PROXY "org/apache/uima/uimacpp/UimacppAnalysisComponent"
/* ----------------------------------------------------------------------- */
/* Forward declarations */
@@ -83,6 +83,136 @@
/********************************************************************
+ ***** JNILogger
+ ********************************************************************/
+ JNILogger::JNILogger(JNIEnv * env) : iv_jnienv(env), cv_clazz(0), cv_logMethod(0) {
+ assert( EXISTS(iv_jnienv) );
+ try {
+ cv_clazz = iv_jnienv->FindClass(JAVA_LOGGER_PROXY);
+ if (cv_clazz == NULL ) {
+ uima::ResourceManager::getInstance().getLogger().logError(
+ JAVA_LOGGER_PROXY " class not found. Could not setup Java logging. ");
+ iv_jnienv->ExceptionClear();
+ return;
+ }
+
+ cv_clazz = (jclass) iv_jnienv->NewGlobalRef(cv_clazz);
+ if (cv_clazz == NULL) {
+ uima::ResourceManager::getInstance().getLogger().logError(
+ "Setup global reference to " JAVA_LOGGER_PROXY " class failed. Could not setup Java logging. ");
+ cerr << "JNILogger() ERROR: CPPJEDIIEngine could not construct " << endl;
+ iv_jnienv->ExceptionClear();
+ return;
+ }
+
+ //query the current logging level
+ jmethodID iv_getLoggingLevelMethod = iv_jnienv->GetStaticMethodID(cv_clazz,
+ "getLoggingLevel",
+ "()I");
+ if (iv_getLoggingLevelMethod == NULL) {
+ uima::ResourceManager::getInstance().getLogger().logError(
+ JAVA_LOGGER_PROXY ".getLoggingLevel() not found. Could not setup Java logging. " );
+ cout << "JNILogger() ERROR: CPPJEDIIEngine.getLoggingLevel() not found " << endl;
+ iv_jnienv->ExceptionClear();
+ return;
+ }
+
+ //log method
+ cv_logMethod = iv_jnienv->GetStaticMethodID(cv_clazz, "log", "("
+ "I" // level
+ "Ljava/lang/String;" // source class
+ "Ljava/lang/String;" // source method
+ "Ljava/lang/String;" // message
+ ")V");
+
+ if (cv_logMethod == NULL) {
+ uima::ResourceManager::getInstance().getLogger().logError(
+ JAVA_LOGGER_PROXY ".log(int,string,string,string) not found. Could not setup Java logging. " );
+ cout << "JNILogger() ERROR: CPPJEDIIEngine.log() not found " << endl;
+ iv_jnienv->ExceptionClear();
+ return;
+ }
+
+ //get the current logging level
+ jint logginglevel = iv_jnienv->CallStaticIntMethod(cv_clazz, iv_getLoggingLevelMethod);
+
+ if (logginglevel == 0) {
+ uima::ResourceManager::getInstance().getLogger().logError(
+ "JNILogger() Could not determine current logging level. Setup Java logging failed. " );
+ cout << "JNILogger() ERROR: calling CPPJEDIIEngine.getLoggingLevel() " << endl;
+ iv_jnienv->ExceptionClear();
+ return;
+ }
+
+ if (logginglevel == 3) {
+ uima::ResourceManager::getInstance().setLoggingLevel(uima::LogStream::EnError);
+ } else if (logginglevel == 2) {
+ uima::ResourceManager::getInstance().setLoggingLevel(uima::LogStream::EnWarning);
+ } else {
+ uima::ResourceManager::getInstance().setLoggingLevel(uima::LogStream::EnMessage);
+ }
+
+ } catch (...) {
+ cout << "Exception in JNILogger() " << endl;
+ iv_jnienv->ExceptionDescribe();
+ }
+ }
+
+
+ void JNILogger::log(uima::LogStream::EnEntryType entype,
+ string classname,
+ string methodname,
+ string message,
+ long lUserCode) {
+
+ stringstream str;
+ if (entype == uima::LogStream::EnMessage) {
+ if (lUserCode != 0) {
+ str << lUserCode << " " << message;
+ }
+ } else {
+ str << lUserCode << " " << message;
+ }
+
+ UnicodeString msg(str.str().c_str());
+ // Convert the std::strings to Unicode using the default converter
+ UnicodeString ustrsource(classname.c_str(), classname.length());
+ UnicodeString ustrmethod(methodname.c_str(), methodname.length());
+ try {
+ jstring jsrcclass = iv_jnienv->NewString((jchar const *) ustrsource.getBuffer(), ustrsource.length());
+ jstring jsrcmethod = iv_jnienv->NewString((jchar const *) ustrmethod.getBuffer(), ustrmethod.length());
+ jstring jmessage = iv_jnienv->NewString((jchar const *) msg.getBuffer(), msg.length());
+
+ jint loglevel;
+ if ( entype == uima::LogStream::EnError) {
+ loglevel = 3;
+ } else if ( entype == uima::LogStream::EnWarning ) {
+ loglevel = 2;
+ } else {
+ loglevel = 1;
+ }
+
+ // Call exception clear
+ iv_jnienv->ExceptionClear();
+
+ iv_jnienv->CallStaticVoidMethod(cv_clazz,
+ cv_logMethod,
+ loglevel,
+ jsrcclass,
+ jsrcmethod,
+ jmessage);
+
+ // Check for exceptions :
+ jthrowable exc = iv_jnienv->ExceptionOccurred();
+ if (exc != NULL) {
+ iv_jnienv->ExceptionDescribe();
+ iv_jnienv->ExceptionClear();
+ }
+ } catch (...) {
+ cout << "JNILogger::log(...) Exception in JavaLogging()" << endl;
+ }
+ }
+/********************************************************************
***** JNI Functions
********************************************************************/
@@ -176,9 +306,7 @@
rResourceManager.setNewLocationWork(workLocation);
rResourceManager.setNewLocationData(dataLocation);
-
- rResourceManager.setupJavaLogging(jeEnv);
-
+
UIMA_TPRINT("configureResourceManagerJNI finished");
} catch (uima::Exception & rException) {
UIMA_TPRINT("Exception: " << rException.asString() );
@@ -210,6 +338,10 @@
JNIUtils::throwNewInternalException(jeEnv, errInfo);
return;
}
+ //setup JNI logger
+ pInstance->iv_logger = new JNILogger(jeEnv);
+ uima::ResourceManager::getInstance().registerLogger(pInstance->iv_logger);
+
// setting engine
JNIUtils::setCppInstance(jeEnv, joJTaf, pInstance);
@@ -274,7 +406,7 @@
uima::JNIInstance* pInstance = JNIUtils::getCppInstance(jeEnv, joJTaf);
assert( EXISTS(pInstance) );
- uima::ResourceManager::getInstance().setupJavaLogging(jeEnv);
+ pInstance->iv_logger->setJNIEnv(jeEnv);
uima::ErrorInfo errInfo;
@@ -395,7 +527,7 @@
deSerializer.deserializeDefinitions( rSerCAS, casDef );
UIMA_TPRINT(" done deserializing definitions");
- uima::ResourceManager::getInstance().setupJavaLogging(jeEnv);
+ pInstance->iv_logger->setJNIEnv(jeEnv);
engineBase.reinitTypeSystem();
@@ -500,7 +632,7 @@
uima::JNIInstance* pInstance = JNIUtils::getCppInstance(jeEnv, joJTaf);
assert( EXISTS(pInstance) );
- uima::ResourceManager::getInstance().setupJavaLogging(jeEnv);
+ pInstance->iv_logger->setJNIEnv(jeEnv);
uima::AnalysisEngine * pEngine = pInstance->getEngine();
@@ -560,7 +692,7 @@
uima::JNIInstance* pInstance = JNIUtils::getCppInstance(jeEnv, joJTaf);
assert( EXISTS(pInstance) );
- uima::ResourceManager::getInstance().setupJavaLogging(jeEnv);
+ pInstance->iv_logger->setJNIEnv(jeEnv);
uima::AnalysisEngine * pEngine = pInstance->getEngine();
assert( EXISTS(pEngine) );
@@ -969,7 +1101,7 @@
uima::JNIInstance* pInstance = JNIUtils::getCppInstance(jeEnv, joJTaf);
assert( EXISTS(pInstance) );
- uima::ResourceManager::getInstance().setupJavaLogging(jeEnv);
+ pInstance->iv_logger->setJNIEnv(jeEnv);
uima::AnalysisEngine * pEngine = pInstance->getEngine();
@@ -1004,7 +1136,7 @@
uima::JNIInstance* pInstance = JNIUtils::getCppInstance(jeEnv, joJTaf);
assert( EXISTS(pInstance) );
- uima::ResourceManager::getInstance().setupJavaLogging(jeEnv);
+ pInstance->iv_logger->setJNIEnv(jeEnv);
uima::AnalysisEngine * pEngine = pInstance->getEngine();
@@ -1129,6 +1261,7 @@
return;
}
}
+
Modified: incubator/uima/uimacpp/trunk/src/jni/uima/jni.hpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/jni/uima/jni.hpp?rev=576447&r1=576446&r2=576447&view=diff
==============================================================================
--- incubator/uima/uimacpp/trunk/src/jni/uima/jni.hpp (original)
+++ incubator/uima/uimacpp/trunk/src/jni/uima/jni.hpp Mon Sep 17 07:07:09 2007
@@ -39,6 +39,7 @@
#include "uima/engine.hpp"
#include "uima/annotator_context.hpp"
#include "uima/casiterator.hpp"
+#include <jni.h>
/* ----------------------------------------------------------------------- */
/* Constants */
@@ -68,7 +69,33 @@
/* ----------------------------------------------------------------------- */
/* Types / Classes */
/* ----------------------------------------------------------------------- */
+class UIMA_LINK_IMPORTSPEC JNILogger : public uima::Logger {
+ public:
+ JNILogger(JNIEnv * env);
+
+ virtual void log(uima::LogStream::EnEntryType entrytype,
+ string classname,
+ string methodname,
+ string message,
+ long errorCode) ;
+ void setJNIEnv(JNIEnv * env) {
+ iv_jnienv=env;
+ }
+ private:
+ /** Format the log message */
+ std::string format(uima::LogStream::EnEntryType enType,
+ const string & cpszMsg,
+ long lUserCode) const;
+
+ JNIEnv * iv_jnienv; //handle to Java environment
+ jclass cv_clazz ; //proxy class on java side
+ jmethodID cv_logMethod; //log method
+ };
+
+
namespace uima {
+
+
class JNIInstance {
private:
uima::AnalysisEngine * iv_pEngine;
@@ -79,15 +106,23 @@
bool iv_hasNext;
public:
+ JNILogger * iv_logger;
JNIInstance() :
iv_pEngine(NULL),
iv_pCAS(NULL),
iv_serializedCAS(),
iv_pSegment(NULL),
iv_serializedSegment(),
- iv_hasNext(false) {}
+ iv_hasNext(false),
+ iv_logger(0) {}
- ~JNIInstance() {}
+ ~JNIInstance() {
+ if (iv_logger != NULL) {
+ uima::ResourceManager::getInstance().unregisterLogger(iv_logger);
+ delete iv_logger;
+ iv_logger=NULL;
+ }
+ }
uima::AnalysisEngine * getEngine() {
return iv_pEngine;
Modified: incubator/uima/uimacpp/trunk/src/test/src/test_engine.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/test/src/test_engine.cpp?rev=576447&r1=576446&r2=576447&view=diff
==============================================================================
--- incubator/uima/uimacpp/trunk/src/test/src/test_engine.cpp (original)
+++ incubator/uima/uimacpp/trunk/src/test/src/test_engine.cpp Mon Sep 17 07:07:09 2007
@@ -125,6 +125,23 @@
ErrorInfo::unrecoverable);
}
+class TestLogger : public Logger {
+public:
+ string message;
+ long errcode;
+ TestLogger() : message(), errcode(0) {}
+ void log(LogStream::EnEntryType entrytype,
+ string classname, string methodname, string msg, long code) {
+ this->message = msg;
+ errcode = code;
+ }
+ void reset() {
+ message.clear();
+ errcode =0;
+ }
+
+};
+
void testCallingSequence1(uima::util::ConsoleUI & rclConsole, const TCHAR * cpszConfigFilename)
/* ----------------------------------------------------------------------- */
{
@@ -237,6 +254,68 @@
rclConsole.info("testMissingResMgr finished." );
}
+void testRegisterLoggers(uima::util::ConsoleUI & rclConsole)
+/* ----------------------------------------------------------------------- */
+{
+ rclConsole.info("testRegisterLoggers start.");
+
+ failIfNotTrue( ResourceManager::hasInstance() );
+ ErrorInfo errInfo;
+ TestLogger * pLogger1 = new TestLogger();
+ TestLogger * pLogger2 = new TestLogger();
+
+
+ LogFacility & logFacility = ResourceManager::getInstance().getLogger();
+ /* register a logger */
+ ResourceManager::getInstance().registerLogger(pLogger1);
+ /* write log messages */
+ string message = "This is message 1.";
+ logFacility.logMessage(message);
+ failIfNotTrue(message.compare(pLogger1->message) == 0);
+ message = "This is an error message.";
+ logFacility.logError(message,100);
+ failIfNotTrue(message.compare(pLogger1->message) == 0);
+ failIfNotTrue(100 == pLogger1->errcode);
+
+ /* register second logger */
+ pLogger1->reset();
+ pLogger2->reset();
+ ResourceManager::getInstance().registerLogger(pLogger2);
+ /* write log messages */
+ message = "This is message 2.";
+ logFacility.logMessage(message);
+ failIfNotTrue(message.compare(pLogger1->message) == 0);
+ failIfNotTrue(message.compare(pLogger2->message) == 0);
+ message = "This is an error message 2.";
+ logFacility.logError(message,200);
+ failIfNotTrue(message.compare(pLogger1->message) == 0);
+ failIfNotTrue(message.compare(pLogger2->message) == 0);
+ failIfNotTrue(200 == pLogger1->errcode);
+ failIfNotTrue(200 == pLogger2->errcode);
+
+ /* unregister 2nd logger */
+ pLogger1->reset();
+ pLogger2->reset();
+ ResourceManager::getInstance().unregisterLogger(pLogger2);
+ /* write log messages */
+ message = "This is message 3.";
+ logFacility.logMessage(message);
+ failIfNotTrue(message.compare(pLogger1->message) == 0);
+ failIfNotTrue(pLogger2->message.length() == 0);
+ message = "This is an error message 3.";
+ logFacility.logError(message,100);
+ failIfNotTrue(message.compare(pLogger1->message) == 0);
+ failIfNotTrue(pLogger2->message.length() == 0);
+ failIfNotTrue(100 == pLogger1->errcode);
+ failIfNotTrue(0 == pLogger2->errcode);
+
+ /* cleanup - unregister the first logger */
+ ResourceManager::getInstance().unregisterLogger(pLogger1);
+ delete pLogger1;
+ delete pLogger2;
+ rclConsole.info("testRegisterLoggers finished." );
+}
+
void testProcessDocu(uima::util::ConsoleUI & rclConsole,
uima::TextAnalysisEngine & rclEngine,
const char * crclCCSID,
@@ -489,7 +568,8 @@
try {
/* create a UIMA resource */
(void) uima::ResourceManager::createInstance(MAIN_TITLE);
-
+ /* test registering user specified loggers */
+ testRegisterLoggers(clConsole);
///mainTest(clConsole, lCCSID, cpszConfigFilename, cpszLanguage, (size_t) lNumberOfIterations);
UnicodeString filename("toktest.xml");
UnicodeString fn = ResourceManager::resolveFilename(filename, filename);
@@ -522,5 +602,6 @@
}
/* <EOF> */
+