1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
|
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2001 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
************************************************************************/
/*___INFO__MARK_END__*/
#include <ctype.h>
#include <string.h>
#include "jni.h"
#include "uti/rmon.h"
#include "cull/cull.h"
#include "cull/cull_list.h"
#include "sge_all_listsL.h"
#include "sgeobj/sge_answer.h"
#include "basis_types.h"
#include "jgdi_logging.h"
struct level_str {
const char* name;
jobject object;
jmethodID mid;
const char* log_method_name;
};
static struct level_str LEVEL_CACHE [] = {
{ "SEVERE", NULL, NULL, "severe" },
{ "WARNING", NULL, NULL, "warning" },
{ "INFO", NULL, NULL, "info" },
{ "CONFIG", NULL, NULL, "config" },
{ "FINE", NULL, NULL, "fine" },
{ "FINER", NULL, NULL, "finer" },
{ "FINEST", NULL, NULL, "finest" }
};
typedef struct {
JNIEnv *env;
jobject logger;
} jgdi_rmon_ctx_t;
static jclass Level_find_class(JNIEnv *env);
static jclass find_logger_class(JNIEnv *env);
static jobject get_level(JNIEnv *env, log_level_t level);
static jobject load_level(JNIEnv *env, const char* level);
static void jgdi_log_entering(JNIEnv *env, jobject logger, const char* classname, const char *func);
static void jgdi_log_exiting(JNIEnv *env, jobject logger, const char* classname, const char *func);
static int jgdi_rmon_is_loggable(rmon_ctx_t* ctx, int layer, int debug_class);
static void jgdi_rmon_menter(rmon_ctx_t* ctx, const char* func);
static void jgdi_rmon_mexit(rmon_ctx_t* ctx, const char* func, const char *file, int line);
static void jgdi_rmon_mtrace(rmon_ctx_t* ctx, const char *func, const char *file, int line);
static void jgdi_rmon_mprintf(rmon_ctx_t* ctx, int debug_class, const char* fmt, va_list args);
static jclass find_logger_class(JNIEnv *env)
{
static jclass clazz = NULL;
if( clazz == NULL) {
jclass tmpclazz = (*env)->FindClass(env, "java/util/logging/Logger");
if (tmpclazz == NULL) {
abort();
}
/* aquire a global ref for the class object */
clazz = (jclass)(*env)->NewGlobalRef(env, tmpclazz);
if (clazz == NULL) {
abort();
}
}
return clazz;
}
/*-------------------------------------------------------------------------*
* NAME
* jgdi_get_logger - get a java logger object
* PARAMETER
* env - JNI environment
* name - name of the logger
*
* RETURN
*
* EXTERNAL
*
* DESCRIPTION
*-------------------------------------------------------------------------*/
jobject jgdi_get_logger(JNIEnv *env , const char* name) {
jmethodID mid = NULL;
jclass clazz = NULL;
jstring name_obj = NULL;
jobject temp = NULL;
clazz = find_logger_class(env);
if (clazz == NULL) {
return NULL;
}
if (mid == NULL) {
mid = (*env)->GetStaticMethodID(env, clazz, "getLogger", "(Ljava/lang/String;)Ljava/util/logging/Logger;");
if (mid == NULL) {
(*env)->ExceptionClear(env);
return NULL;
}
}
name_obj = (*env)->NewStringUTF(env, name );
temp = (*env)->CallStaticObjectMethod(env, clazz, mid , name_obj );
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
return NULL;
} else {
return temp;
}
}
static jclass Level_find_class(JNIEnv *env)
{
static jclass clazz = NULL;
if (clazz == NULL) {
clazz = (*env)->FindClass(env, "java/util/logging/Level");
if(clazz == NULL) {
abort();
}
clazz = (jclass)(*env)->NewGlobalRef(env, clazz);
}
return clazz;
}
static jobject load_level(JNIEnv *env, const char* level)
{
jclass clazz = Level_find_class(env);
jobject ret = NULL;
jfieldID mid = (*env)->GetStaticFieldID(env, clazz, level, "Ljava/util/logging/Level;");
ret = (*env)->GetStaticObjectField(env, clazz, mid);
if( (*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
abort();
}
return ret;
}
/*-------------------------------------------------------------------------*
* NAME
* get_level - Get the level jobject for a log level
*
* PARAMETER
* env - JNI environment
* level - the log level
*
* RETURN
*
* the log level object
*
* EXTERNAL
*
* DESCRIPTION
*-------------------------------------------------------------------------*/
static jobject get_level(JNIEnv *env, log_level_t level)
{
if (LEVEL_CACHE[level].object == NULL) {
jobject level_obj = load_level(env, LEVEL_CACHE[level].name);
LEVEL_CACHE[level].object = (jclass)(*env)->NewGlobalRef(env, level_obj);
}
return LEVEL_CACHE[level].object;
}
/*-------------------------------------------------------------------------*
* NAME
* jgdi_is_loggable - Determine where a log level is loggable in a logger
* PARAMETER
* env - JNI env
* logger - logger object
* level - the log level
*
* RETURN
* true - if the level is loggable in the logger
* false - if the level is not loggable in the logger or if
* in the jni env an exception has occurred
*
* EXTERNAL
*
* DESCRIPTION
*-------------------------------------------------------------------------*/
jboolean jgdi_is_loggable(JNIEnv *env, jobject logger, log_level_t level)
{
jobject level_obj = NULL;
static jmethodID mid = NULL;
jboolean ret = false;
/* We can not write a log message of a exception has occureed */
if((*env)->ExceptionOccurred(env)) {
return false;
}
if (logger == NULL) {
return false;
}
level_obj = get_level(env, level);
if (level_obj == NULL) {
abort();
}
if (mid == NULL) {
jclass clazz = (*env)->FindClass(env, "java/util/logging/Logger");
mid = (*env)->GetMethodID(env, clazz, "isLoggable", "(Ljava/util/logging/Level;)Z");
if (mid == NULL) {
abort();
}
}
ret = (*env)->CallBooleanMethod(env, logger, mid , level_obj );
if( (*env)->ExceptionOccurred(env)) {
(*env)->ExceptionClear(env);
ret = false;
}
return ret;
}
static void jgdi_log_entering(JNIEnv *env, jobject logger, const char* classname, const char *func)
{
static jmethodID mid = NULL;
jclass clazz = NULL;
jobject classname_obj = NULL;
jobject func_obj = NULL;
clazz = (*env)->GetObjectClass(env, logger);
if(clazz == NULL) {
abort();
}
if (mid == NULL ) {
mid = (*env)->GetMethodID(env, clazz, "entering", "(Ljava/lang/String;Ljava/lang/String;)V");
if(mid == NULL) {
return;
}
}
classname_obj = (*env)->NewStringUTF(env, classname);
func_obj = (*env)->NewStringUTF(env, func);
(*env)->CallVoidMethod(env, logger, mid , classname_obj, func_obj );
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionClear(env);
}
}
static void jgdi_log_exiting(JNIEnv *env, jobject logger, const char* classname, const char *func)
{
static jmethodID mid = NULL;
jclass clazz = NULL;
jobject classname_obj = NULL;
jobject func_obj = NULL;
/* We can not write a log message of a exception has occureed */
if((*env)->ExceptionOccurred(env)) {
return;
}
clazz = (*env)->GetObjectClass(env, logger);
if(clazz == NULL) {
abort();
}
if (mid == NULL ) {
mid = (*env)->GetMethodID(env, clazz, "exiting", "(Ljava/lang/String;Ljava/lang/String;)V");
if(mid == NULL) {
return;
}
}
classname_obj = (*env)->NewStringUTF(env, classname);
func_obj = (*env)->NewStringUTF(env, func);
(*env)->CallVoidMethod(env, logger, mid , classname_obj, func_obj );
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionClear(env);
}
}
/*-------------------------------------------------------------------------*
* NAME
* jgdi_log - write a log message into a logger
* PARAMETER
* env - JNI environment
* logger - the logger object
* level - the log level
* msg - the log message
*
* EXTERNAL
*
* DESCRIPTION
*-------------------------------------------------------------------------*/
void jgdi_log(JNIEnv *env, jobject logger, log_level_t level, const char* msg)
{
jmethodID mid = NULL;
jclass clazz = NULL;
jstring msg_obj = NULL;
clazz = (*env)->GetObjectClass(env, logger);
if(clazz == NULL) {
abort();
}
if (level >= LOG_LEVEL_COUNT || level < 0) {
abort();
}
if (LEVEL_CACHE[level].mid == NULL ) {
LEVEL_CACHE[level].mid = (*env)->GetMethodID(env, clazz, LEVEL_CACHE[level].log_method_name, "(Ljava/lang/String;)V");
}
mid = LEVEL_CACHE[level].mid;
if (mid == NULL) {
return;
}
msg_obj = (*env)->NewStringUTF(env, msg);
(*env)->CallVoidMethod(env, logger, mid , msg_obj );
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionClear(env);
}
}
/*-------------------------------------------------------------------------*
* NAME
* jgdi_log_printf - write a log message with printf into java logger
* PARAMETER
* env - JNI enviromnent
* logger - the logger object
* level - the log level
* fmt - the log format
*
* RETURN
*
* EXTERNAL
*
* DESCRIPTION
*-------------------------------------------------------------------------*/
void jgdi_log_printf(JNIEnv *env, const char* logger, log_level_t level, const char* fmt, ...)
{
jobject logger_obj = NULL;
logger_obj = jgdi_get_logger(env, logger);
if(logger_obj == NULL) {
return;
}
if (jgdi_is_loggable(env, logger_obj, level)) {
dstring ds = DSTRING_INIT;
va_list ap;
va_start(ap, fmt);
sge_dstring_vsprintf(&ds, fmt, ap);
va_end(ap);
jgdi_log(env, logger_obj, FINE, sge_dstring_get_string(&ds));
sge_dstring_free(&ds);
}
}
/*-------------------------------------------------------------------------*
* NAME
* jgdi_log_list - write a cull list into a java logger
* PARAMETER
* env - the java environment
* logger - the java logger
* level - the log level
* list - the cull list
*
* RETURN
*
* EXTERNAL
*
* DESCRIPTION
*-------------------------------------------------------------------------*/
void jgdi_log_list(JNIEnv *env, const char* logger, log_level_t level, lList *list)
{
jobject logger_obj = NULL;
logger_obj = jgdi_get_logger(env, logger);
if(logger_obj == NULL) {
return;
}
if (jgdi_is_loggable(env, logger_obj, level)) {
dstring ds = DSTRING_INIT;
lInit(nmv);
lWriteListToStr(list, &ds);
jgdi_log(env, logger_obj, FINE, sge_dstring_get_string(&ds));
sge_dstring_free(&ds);
}
}
void jgdi_log_listelem(JNIEnv *env, const char* logger, log_level_t level, lListElem *elem)
{
jobject logger_obj = NULL;
logger_obj = jgdi_get_logger(env, logger);
if(logger_obj == NULL) {
return;
}
if (jgdi_is_loggable(env, logger_obj, level)) {
dstring ds = DSTRING_INIT;
lInit(nmv);
lWriteElemToStr(elem, &ds);
jgdi_log(env, logger_obj, FINE, sge_dstring_get_string(&ds));
sge_dstring_free(&ds);
}
}
/*-------------------------------------------------------------------------*
* NAME
* jgdi_log_answer_list - write a answer list into a java logger
*
* PARAMETER
* env - JNI environment
* logger - the logger object
* alp - the answer list
*
* RETURN
*
* EXTERNAL
*
* DESCRIPTION
*
* Writes the content of an answer_list into a logger. The message of the
* log record is the content of AN_text. The level of the log message depends
* on the AN_quality field.
* ANSWER_QUALITY_ERROR -> SEVERE
* ANSWER_QUALITY_WARNING -> WARINING
* ANSWER_QUALITY_INFO -> INFO
*
*-------------------------------------------------------------------------*/
void jgdi_log_answer_list(JNIEnv *env, const char* logger, lList *alp)
{
if (alp != NULL) {
jobject logger_obj = NULL;
lListElem *answer; /* AN_Type */
logger_obj = jgdi_get_logger(env, logger);
if(logger_obj == NULL) {
return;
}
for_each(answer, alp) {
switch(lGetUlong(answer, AN_quality)) {
case ANSWER_QUALITY_ERROR:
jgdi_log(env, logger_obj, SEVERE, lGetString(answer, AN_text));
break;
case ANSWER_QUALITY_WARNING:
jgdi_log(env, logger_obj, WARNING, lGetString(answer, AN_text));
break;
case ANSWER_QUALITY_INFO:
jgdi_log(env, logger_obj, INFO, lGetString(answer, AN_text));
break;
}
}
}
}
/*-------------------------------------------------------------------------*
* NAME
* jgdi_init_rmon_ctx - setup a rmon_ctx which write log messages into a java logger
* PARAMETER
*
* env - JNI environment
* logger - the name of the java logger
* ctx - the rmon context
*
* RETURN
*
* 0
*
* EXTERNAL
*
* DESCRIPTION
*-------------------------------------------------------------------------*/
int jgdi_init_rmon_ctx(JNIEnv *env, const char* logger, rmon_ctx_t *ctx)
{
jgdi_rmon_ctx_t *myctx = NULL;
myctx = (jgdi_rmon_ctx_t*)sge_malloc(sizeof(jgdi_rmon_ctx_t));
ctx->ctx = myctx;
ctx->is_loggable = jgdi_rmon_is_loggable;
ctx->menter = jgdi_rmon_menter;
ctx->mexit = jgdi_rmon_mexit;
ctx->mtrace = jgdi_rmon_mtrace;
ctx->mprintf = jgdi_rmon_mprintf;
myctx->env = env;
myctx->logger = jgdi_get_logger(env, logger);
return 0;
}
static log_level_t get_rmon_log_level(int layer, int debug_class)
{
switch(layer) {
case TOP_LAYER:
return FINE;
default:
return FINER;
}
}
static int jgdi_rmon_is_loggable(rmon_ctx_t* ctx, int layer, int debug_class)
{
jgdi_rmon_ctx_t *myctx = (jgdi_rmon_ctx_t*)ctx->ctx;
return jgdi_is_loggable(myctx->env, myctx->logger, get_rmon_log_level(layer, debug_class));
}
static void jgdi_rmon_menter(rmon_ctx_t* ctx, const char* func)
{
jgdi_rmon_ctx_t *myctx = (jgdi_rmon_ctx_t*)ctx->ctx;
jgdi_log_entering(myctx->env, myctx->logger, "native", func);
}
static void jgdi_rmon_mexit(rmon_ctx_t* ctx, const char* func, const char *file, int line)
{
jgdi_rmon_ctx_t *myctx = (jgdi_rmon_ctx_t*)ctx->ctx;
jgdi_log_exiting(myctx->env, myctx->logger, "native", func);
}
static void jgdi_rmon_mtrace(rmon_ctx_t* ctx, const char *func, const char *file, int line)
{
/* dummy func */
}
static void jgdi_rmon_mprintf(rmon_ctx_t* ctx, int debug_class, const char* fmt, va_list args)
{
jgdi_rmon_ctx_t *myctx = (jgdi_rmon_ctx_t*)ctx->ctx;
log_level_t level = get_rmon_log_level(TOP_LAYER, debug_class);
if (jgdi_is_loggable(myctx->env, myctx->logger, level)) {
dstring ds = DSTRING_INIT;
sge_dstring_vsprintf(&ds, fmt, args);
jgdi_log(myctx->env, myctx->logger, level, sge_dstring_get_string(&ds));
sge_dstring_free(&ds);
}
}
/*-------------------------------------------------------------------------*
* NAME
* jgdi_destroy_rmon_ctx - destroy a rmon context
* PARAMETER
* rmon_ctx - the rmon context
*
* RETURN
*
* EXTERNAL
*
* DESCRIPTION
*
* Destroy a rmon context which has been initialized with the method jgdi_init_rmon_ctx
*-------------------------------------------------------------------------*/
void jgdi_destroy_rmon_ctx(rmon_ctx_t *rmon_ctx)
{
if (rmon_ctx->ctx) {
sge_free(&(rmon_ctx->ctx));
rmon_ctx->ctx = NULL;
}
}
|