javacommons/utils/src/logger.cpp
changeset 79 2f468c1958d0
parent 69 773449708c84
equal deleted inserted replaced
76:4ad59aaee882 79:2f468c1958d0
   424     return fd;
   424     return fd;
   425 }
   425 }
   426 
   426 
   427 #endif //J_LOG_USE_RLOGGER_ENABLED
   427 #endif //J_LOG_USE_RLOGGER_ENABLED
   428 
   428 
   429 
   429 /*
   430 /**
       
   431  * Class:     com_nokia_mj_impl_utils_Logger
   430  * Class:     com_nokia_mj_impl_utils_Logger
   432  * Method:    _logging
   431  * Method:    _logging
   433  * Signature: (IILjava/lang/String;)V
   432  * Signature: (IILjava/lang/String;java/lang/String;)V
   434  *
   433  *
   435  * Native static Logger._logging() method write log message to file and
   434  * Method for logging the log string. If the optional stack trace is provided
   436  * accepting three input parameters:
   435  * that will be logged as well.
   437  * component id, severity level id of emiting information and tracing information
       
   438  */
   436  */
   439 
       
   440 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_utils_Logger__1logging
   437 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_utils_Logger__1logging
   441 (JNIEnv *aEnv, jclass, jint aComponent, jint aLevel, jstring aLogString)
   438 (JNIEnv *aEnv, jclass /*aClassH*/, jint aComponent, jint aLevel, jstring aLogString,
   442 {
   439  jstring aStackTrace)
       
   440 {
       
   441     // It is excpected that Java peer checks that aLogString is not null.
       
   442 
       
   443     // Get logging string.
   443     const char* log = aEnv->GetStringUTFChars(aLogString, 0);
   444     const char* log = aEnv->GetStringUTFChars(aLogString, 0);
   444 
   445 
   445     if (aLevel == com_nokia_mj_impl_utils_Logger_EError)
   446     if (log)
   446     {
   447     {
   447         ELOG1((TComponents)aComponent, "%s", log);
   448         const char* stack; // Content of optional stack trace. Default is empty
   448     }
   449         const char* delim; // Delimiter between log and stack trace. Default is empty
   449     else if (aLevel == com_nokia_mj_impl_utils_Logger_EWarning)
   450         const char* stacktrace = 0;
   450     {
   451         if (aStackTrace)
   451         WLOG1((TComponents)aComponent, "%s", log);
   452         {
   452     }
   453             // By default this write error string if GetStringUTFChars fails.
   453     else if (aLevel == com_nokia_mj_impl_utils_Logger_EInfoPrd)
   454             stack = "Stack trace not available";
   454     {
   455             delim = ": ";
   455         PLOG1((TComponents)aComponent, "%s", log);
   456 
   456     }
   457             // Get stack trace string.
   457     else
   458             stacktrace = aEnv->GetStringUTFChars(aStackTrace, 0);
   458     {
   459         }
   459         LOG1((TComponents)aComponent, EInfo, "%s", log);
   460         else
   460     }
   461         {
   461 
   462             // aStackTrace was null which means that there is no stack trace
   462     aEnv->ReleaseStringUTFChars(aLogString, log);
   463             // available.
   463 }
   464             stack = "";
   464 
   465             delim = "";
   465 
   466         }
   466 /*
   467 
   467  * Class:     com_nokia_mj_impl_utils_Logger
   468         // If it succeeded overwrite the default one.
   468  * Method:    _loggingException
   469         if (stacktrace)
   469  * Signature: (IILjava/lang/String;Ljava/lang/Throwable;Ljava/io/ByteArrayOutputStream;Ljava/io/PrintStream;)V
   470         {
   470  *
   471             stack = stacktrace;
   471  * Method prints stack trace and Throwable info to log file
   472         }
   472  */
   473 
   473 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_utils_Logger__1loggingException
   474         if (aLevel == com_nokia_mj_impl_utils_Logger_EError)
   474 (JNIEnv *aEnv, jclass /*aClassH*/, jint aComponent, jint aLevel, jstring aLogString,
   475         {
   475  jthrowable aThrowable, jobject aByteStream, jobject aPrintStream)
   476             ELOG3((TComponents)aComponent, "%s%s%s", log, delim, stack);
   476 {
   477         }
   477     /* get logging string */
   478         else if (aLevel == com_nokia_mj_impl_utils_Logger_EWarning)
   478     const char* log = aEnv->GetStringUTFChars(aLogString, 0);
   479         {
   479 
   480             WLOG3((TComponents)aComponent, "%s%s%s", log, delim, stack);
   480     /*
   481         }
   481      * call Throwable.printStackTrace(java.io.PrintStream)
   482         else if (aLevel == com_nokia_mj_impl_utils_Logger_EInfoPrd)
   482      * this method is not part of CLDC spec, but it's supported by VM vendors
   483         {
   483      */
   484             PLOG3((TComponents)aComponent, "%s%s%s", log, delim, stack);
   484     jclass class_Throwable = aEnv->GetObjectClass(aThrowable);
   485         }
   485     jmethodID methodId = aEnv->GetMethodID(class_Throwable, "printStackTrace", "(Ljava/io/PrintStream;)V");
   486         else
   486     aEnv->CallVoidMethod(aThrowable, methodId, aPrintStream);
   487         {
   487 
   488             LOG3((TComponents)aComponent, EInfo, "%s%s%s", log, delim, stack);
   488     /* call ByteArrayOutputStream.toString() */
   489         }
   489     jclass class_ByteArrayOutputStream = aEnv->GetObjectClass(aByteStream);
   490 
   490     methodId = aEnv->GetMethodID(class_ByteArrayOutputStream, "toString", "()Ljava/lang/String;");
   491         // Free the stack trace string if it was available
   491     jstring stacktrace_jstr = (jstring) aEnv->CallObjectMethod(aByteStream, methodId);
   492         if (stacktrace)
   492     const char *stacktrace = aEnv->GetStringUTFChars(stacktrace_jstr, 0);
   493         {
   493 
   494             aEnv->ReleaseStringUTFChars(aStackTrace, stacktrace);
   494     if (aLevel == com_nokia_mj_impl_utils_Logger_EError)
   495         }
   495     {
   496 
   496         ELOG2((TComponents)aComponent, "%s: %s", log, stacktrace);
   497         // Free the log string.
   497     }
   498         aEnv->ReleaseStringUTFChars(aLogString, log);
   498     else if (aLevel == com_nokia_mj_impl_utils_Logger_EWarning)
   499     }
   499     {
   500 }
   500         WLOG2((TComponents)aComponent, "%s: %s", log, stacktrace);
       
   501     }
       
   502     else if (aLevel == com_nokia_mj_impl_utils_Logger_EInfoPrd)
       
   503     {
       
   504         PLOG2((TComponents)aComponent, "%s: %s", log, stacktrace);
       
   505     }
       
   506     else
       
   507     {
       
   508         LOG2((TComponents)aComponent, EInfo, "%s: %s", log, stacktrace);
       
   509     }
       
   510 
       
   511     aEnv->ReleaseStringUTFChars(aLogString, log);
       
   512     aEnv->ReleaseStringUTFChars(stacktrace_jstr, stacktrace);
       
   513 }