1 package com.quikj.server.framework; 2 3 import java.io.*; 4 import java.net.*; 5 import java.util.*; 6 7 import javax.xml.parsers.*; 9 import org.xml.sax.*; 10 import org.w3c.dom.*; 11 12 13 public class AceLogger extends AceThread implements AceLoggerInterface 14 { 15 public static final int MAX_LIST_SIZE = 100; 16 17 public static final int TRACE = 0; 19 public static final int INFORMATIONAL = 1; 20 public static final int WARNING = 2; 21 public static final int ERROR = 3; 22 public static final int FATAL = 4; 23 public static final int NUM_MSG_TYPES = 5; 24 25 public static final String [] SEVERITY_S = 27 { 28 "TRACE", 29 "INFORMATIONAL", 30 "WARNING", 31 "ERROR", 32 "FATAL" 33 }; 34 35 public static final String [] COLOR_S = 37 { 38 "\033[25m\033[39m", 39 "\033[25m\033[32m", 40 "\033[25m\033[33m", 41 "\033[25m\033[31m", 42 "\033[5m\033[31m" 43 }; 44 45 public static final int MAX_SEVERITY_LENGTH = SEVERITY_S[INFORMATIONAL].length(); 47 48 public static final int LOG_NONE = 0; 49 public static final int SYSTEM_LOG = 1; 50 public static final int USER_LOG = 2; 51 public static final int SYSTEM_REPORT = 4; 52 53 public static final int MAX_CONTROL_MSG_SIZE = 10000; 54 public static final int MAX_LOG_MSG_SIZE = 10000; 55 56 public static final String EL_LOGS = "logs"; 57 58 public AceLogger(String tx_host, 59 int tx_port, 60 String process_name, 61 int process_instance, 62 int process_group) 63 throws IOException, UnknownHostException, ParserConfigurationException, AceException 64 { 65 super("AceLogger"); 66 67 try 68 { 69 hostName = InetAddress.getLocalHost().getHostName(); 70 txHost = tx_host; 71 txPort = tx_port; 72 processName = process_name; 73 processInstance = process_instance; 74 processGroup = process_group; 75 76 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 77 dbf.setValidating(false); 79 dbf.setIgnoringComments(true); 80 dbf.setIgnoringElementContentWhitespace(true); 81 dbf.setCoalescing(true); 82 dBuilder = dbf.newDocumentBuilder(); 83 } 84 85 catch (UnknownHostException ex2) 86 { 87 dispose(); 88 89 throw ex2; 90 } 91 catch (ParserConfigurationException ex4) 92 { 93 dispose(); 94 95 throw ex4; 96 } 97 98 instance = this; 99 } 100 101 public static AceLogger Instance() 102 { 103 return instance; 104 } 105 106 public boolean log(int severity, int msg_type, String message, int msg_id) 107 { 108 AceLogMessage log_msg = new AceLogMessage(processGroup, 110 hostName, 111 processName, 112 processInstance, 113 severity, 114 msg_type, 115 message, 116 msg_id); 117 118 return sendMessage(new LogMessageEvent(log_msg)); 119 } 120 121 public boolean log(int severity, int msg_type, String message) 122 { 123 return log(severity, msg_type, message, -1); 124 } 125 126 public boolean isTraceEnabled(int trace_id, String [] constraints) 127 { 128 boolean [] found_array = new boolean[constraints.length]; 129 int found_count = 0; 130 131 for (int j = 0; j < found_array.length; j++) 132 { 133 found_array[j] = false; 134 } 135 136 String [] constraint_list = null; 137 synchronized (controlInfoSynch) 138 { 139 constraint_list = (String [])controlInfo.get(new Integer (trace_id)); 140 } 141 142 if (constraint_list == null) { 144 return false; 145 } 146 147 for (int i = 0; i < constraint_list.length; i++) 148 { 149 for (int k = 0; k < constraints.length; k++) 150 { 151 if (found_array[k] == true) { 153 continue; 154 } 155 156 if (constraints[k].equals(constraint_list[i]) == true) 157 { 158 found_array[k] = true; 159 found_count++; 160 break; 161 } 162 } 163 164 if (found_count >= constraints.length) 165 { 166 return true; 167 } 168 } 169 170 return false; 171 } 172 173 public boolean isTraceEnabled(int trace_id, String constraint) 174 { 175 String [] constraints = null; 176 synchronized (controlInfoSynch) 177 { 178 constraints = (String [])controlInfo.get(new Integer (trace_id)); 179 } 180 181 if (constraints == null) { 183 return false; 184 } 185 186 for (int i = 0; i < constraints.length; i++) 187 { 188 if (constraint.equals(constraints[i]) == true) 189 { 190 return true; 191 } 192 } 193 194 return false; 195 } 196 197 public boolean isTraceEnabled(int trace_id) 198 { 199 synchronized (controlInfoSynch) 200 { 201 return controlInfo.contains(new Integer (trace_id)); 202 } 203 } 204 205 public boolean trace(String message) 206 { 207 return log(TRACE, LOG_NONE, message); 208 } 209 210 public void dispose() 211 { 212 if (communicationUp == true) 213 { 214 if (readerThread != null) 215 { 216 readerThread.dispose(); 217 readerThread = null; 218 } 219 } 220 221 if (timerId != -1) 222 { 223 try 224 { 225 AceTimer.Instance().cancelTimer(timerId); 226 } 227 catch (IOException ex) 228 { 229 ; 230 } 231 timerId = -1; 232 } 233 234 interruptWait(0, "disposed"); 235 super.dispose(); 236 } 237 238 private void establishCommunications() 239 throws IOException, UnknownHostException, AceException 240 { 241 if (createTxSocket() == true) 242 { 243 Iterator iterator = savedLogList.iterator(); 245 while (iterator.hasNext() == true) 246 { 247 AceLogMessage message = (AceLogMessage)iterator.next(); 248 sendMessage(message.getFormattedMessage()); 249 } 250 251 savedLogList.clear(); 252 listSize = 0; 253 } 254 255 if (communicationUp == false) 256 { 257 try 258 { 259 timerId = AceTimer.Instance().startTimer(60 * 1000L, 0L); 261 if (timerId == -1) 262 { 263 System.err.println("AceLogger.establishCommunications() -- The timer could not be restarted"); 264 } 265 } 266 catch (IOException ex) 267 { 268 ; 269 } 270 } 271 272 } 273 274 public void run() 275 { 276 try 277 { 278 establishCommunications(); 279 } 280 catch (Exception ex) 281 { 282 dispose(); 283 return; 284 } 285 286 while (true) 287 { 288 AceMessageInterface message = waitMessage(); 289 if (message == null) 290 { 291 log(AceLogger.ERROR, AceLogger.SYSTEM_LOG, 293 getName() 294 + " - AceLogger.run() -- A null message was received while waiting for a message - " 295 + getErrorMessage()); 296 297 dispose(); 298 break; 299 } 300 301 if ((message instanceof AceSignalMessage) == true) 302 { 303 305 log(AceLogger.INFORMATIONAL, AceLogger.SYSTEM_LOG, 307 getName() 308 + " - AceLoggger.run() -- A signal " 309 + ((AceSignalMessage)message).getSignalId() 310 + " is received : " 311 + ((AceSignalMessage)message).getMessage()); 312 313 break; 314 } 315 else if ((message instanceof AceLogger.LogMessageEvent) == true) 316 { 317 AceLogMessage log = ((AceLogger.LogMessageEvent)message).getMessage(); 318 319 if (communicationUp == true) 320 { 321 sendMessage(log.getFormattedMessage()); 323 } 324 else 325 { 326 savedLogList.addLast(log); 327 listSize++; 328 329 if (listSize > MAX_LIST_SIZE) 330 { 331 System.err.println("AceLogger.run() -- There are too many log message queued up, removing some"); 332 savedLogList.removeFirst(); 334 listSize--; 335 } 336 } 337 } 338 else if ((message instanceof AceTimerMessage) == true) 339 { 340 timerId = -1; 341 342 try 343 { 344 establishCommunications(); 346 } 347 catch (Exception ex) 348 { 349 System.err.println("AceLogger.run() -- Exception " + ex.getClass().getName() 350 + " occured while establishing communications. " 351 + ex.getMessage()); 352 } 353 } 354 else if ((message instanceof AceInputSocketStreamMessage) == true) 355 { 356 AceInputSocketStreamMessage socket_message = (AceInputSocketStreamMessage)message; 357 int status = socket_message.getStatus(); 358 switch (status) 359 { 360 case AceInputSocketStreamMessage.EOF_REACHED: 361 362 communicationUp = false; 363 364 if (readerThread != null) 365 { 366 readerThread.dispose(); 367 readerThread = null; 368 } 369 370 try 372 { 373 establishCommunications(); 374 } 375 catch (Exception ex) 376 { 377 System.err.println("AceLogger.run() -- Exception " + ex.getClass().getName() 378 + " occured while establishing communications. " 379 + ex.getMessage()); 380 } 381 break; 382 383 case AceInputSocketStreamMessage.READ_COMPLETED: 384 try 385 { 386 Document doc = null; 387 try 388 { 389 InputSource is = new InputSource(new StringReader(socket_message.getLines())); 390 doc = dBuilder.parse(is); 391 } 392 catch (IOException ex) 393 { 394 log(WARNING, SYSTEM_LOG, 395 "AceLogger.run() -- Could not parse received log message, ignoring: " + 396 ex.getMessage()); 397 continue; 398 } 399 400 try 402 { 403 AceLogMessageParser parser = new AceLogMessageParser(processGroup, doc); 404 405 AceLogMessageInterface msg = parser.getMessageElement(); 407 408 if ((msg instanceof AceTraceInfoMessage) == false) 410 { 411 log(WARNING, SYSTEM_LOG, 412 "AceLogger.run() -- A message other than trace_info_message is received, ignoring"); 413 continue; 414 } 415 416 synchronized (controlInfoSynch) 417 { 418 controlInfo = ((AceTraceInfoMessage)msg).getTraceInformation(); 419 } 420 421 } 422 catch (AceException ex) 423 { 424 log(WARNING, SYSTEM_LOG, 425 "AceLogger.run() -- Error parsing received message" 426 + '\n' 427 + ex.getMessage()); 428 continue; 429 } 430 } 431 catch (SAXException ex1) 432 { 433 435 log(WARNING, SYSTEM_LOG, 437 "AceLogger.run() -- Error parsing trace information message" 438 + '\n' 439 + ex1.getMessage()); 440 continue; 441 } 442 break; 443 } 444 } 445 } 447 } 448 449 private boolean createTxSocket() 450 throws IOException, UnknownHostException, AceException 451 { 452 if (communicationUp == false) 453 { 454 Socket tx_socket = null; 456 try 457 { 458 tx_socket = new Socket(txHost, txPort); 459 } 460 catch (IOException ex) 461 { 462 return false; 463 } 464 465 tx_socket.setKeepAlive(true); 466 tx_socket.setTcpNoDelay(true); 467 468 txWriter = new BufferedWriter(new OutputStreamWriter(tx_socket.getOutputStream())); 469 470 readerThread = new AceInputSocketStream(0L, "AceLogger", tx_socket, true); 471 readerThread.start(); 472 communicationUp = true; 473 474 if (sendTraceInfoRequest() == false) 475 { 476 System.err.println( 478 "AceLogger.AceLogger() -- Error sending request for trace information from the log processor" 479 + '\n' 480 + "tracing is disabled" 481 + '\n' 482 + getErrorMessage()); 483 } 484 485 } 486 return true; 487 } 488 489 490 private boolean sendTraceInfoRequest() 491 { 492 AceTraceReqMessage req = new AceTraceReqMessage(processGroup); 493 return sendMessage(req.getFormattedMessage()); 494 } 495 496 private String formatMessage(String message) 497 { 498 StringBuffer buffer = new StringBuffer (); 499 StringTokenizer tokens = new StringTokenizer(message, "\n"); 500 int count = tokens.countTokens(); 501 502 for (int i = 0; i < count; i++) 503 { 504 String line = tokens.nextToken(); 505 if (line.startsWith(".") == true) 506 { 507 buffer.append(" ." + line + '\n'); 508 } 509 else 510 { 511 buffer.append(line + '\n'); 512 } 513 } 514 515 buffer.append(".\n"); 516 return buffer.toString(); 517 } 518 519 private synchronized boolean sendMessage(String message) 520 { 521 try 522 { 523 txWriter.write(formatMessage(message)); 524 txWriter.flush(); 525 } 526 catch (IOException ex) 527 { 528 writeErrorMessage(ex.getMessage()); 529 return false; 530 } 531 return true; 532 } 533 534 class LogMessageEvent implements AceMessageInterface 535 { 536 public LogMessageEvent(AceLogMessage message) 537 { 538 this.message = message; 539 } 540 541 public String messageType() 542 { 543 return "AceLogMessage"; 544 } 545 546 public AceLogMessage getMessage() 547 { 548 return message; 549 } 550 551 private AceLogMessage message; 552 } 553 554 private String txHost; 555 private int txPort; 556 private BufferedWriter txWriter; 557 private AceInputSocketStream readerThread; 558 private boolean communicationUp = false; 559 private LinkedList savedLogList = new LinkedList(); 560 private int listSize = 0; 561 562 private int timerId = -1; 563 564 private String hostName = ""; 565 private String processName; 566 private int processInstance; 567 private int processGroup; 568 569 private DocumentBuilder dBuilder = null; 570 571 private Hashtable controlInfo = new Hashtable(); 572 private Object controlInfoSynch = new Object (); 573 574 private static AceLogger instance = null; 575 576 577 public static void main(String [] args) 579 { 580 class MyAceThread extends AceThread 581 { 582 public MyAceThread() 583 throws IOException, ParserConfigurationException, AceException 584 { 585 super("MyAceThread", true); 586 587 AceTimer.Instance().start(); new AceLogger("localhost", 589 8088, 590 "Test Process", 591 0, 592 1); 593 AceLogger.Instance().start(); 594 } 595 596 public void dispose() 597 { 598 AceLogger.Instance().dispose(); 599 super.dispose(); 600 } 601 602 public void run() 603 { 604 String message = "This is a message with level = "; 605 606 int severity = AceLogger.INFORMATIONAL; 607 try 608 { 609 int num = 0; 610 while (true) 611 { 612 AceLogger.Instance().log(severity, AceLogger.SYSTEM_LOG, 613 message + num++); 614 sleep(1000); 615 616 switch (severity) 617 { 618 case AceLogger.INFORMATIONAL: 619 severity = AceLogger.WARNING; 620 break; 621 622 case AceLogger.WARNING: 623 severity = AceLogger.ERROR; 624 break; 625 626 case AceLogger.ERROR: 627 severity = AceLogger.FATAL; 628 break; 629 630 case AceLogger.FATAL: 631 severity = AceLogger.INFORMATIONAL; 632 break; 633 } 634 } 635 } 636 catch (InterruptedException ex) 637 { 638 System.err.println("Interrupted Exception : " 639 + ex.getMessage()); 640 } 641 } 642 } 644 645 try 646 { 647 MyAceThread thread = new MyAceThread(); 648 thread.start(); 649 650 thread.join(); 651 } 652 catch (Exception ex) 653 { 654 System.err.println(ex.getClass().getName() + " occured : " 655 + ex.getMessage()); 656 System.exit(1); 657 } 658 System.exit(0); 659 } 660 } 661 662 663 664 665 666 667 668 669 670 671 672 673 | Popular Tags |