package edu.vt.marian.uip; import java.io.*; import java.net.*; import java.util.*; import java.text.DateFormat; import edu.vt.marian.common.*; /** class name: uip_log_manager class description: this class is responsible for the uip_log management uses the services of class(es): debug designer(s): Jianxin Zhao (jxzhao@csgrad.cs.vt.edu) implementator(s): Xuelei Sun (xusun@csgrad.cs.vt.edu) finished time: December 8, 1998 known bugs: JDK version: 1.1.5 side effects: */ public class uip_log_manager { public final static int OK = 0; public final static int WRITE_LOG_ERROR = -1; public final static int NO_WRITE = 1; public final static int NULL_FUNCTION = -2; /** this is for debug */ Debug debug; /** this is the log level flag 0: null 1: common log message 2: function name 3: function parameters */ private int logging_level = 0; /** this is the output stream */ private PrintWriter pw = null; /** this is the message separator string */ private String message_separator = "================================================================================="; /** these two variables are used to reduce the disk writing times */ private int writing_count = 0; private int writing_threhold = 20; /** method description: this constructor create the uip_log_manager uses the services of class(es): debug input parameter(s): String dir, directory name of the uip_log_manager Debug debug, for debugging output parameter(s): none return value: none synchronization consideration: none */ public uip_log_manager(String dir, Debug debug) { //set debug this.debug = debug; //check the dir parameter if (dir == null) { debug.dumpTrace("class uip_log_manager, Method: constructor, null or empty directory named"); return; } //open and read the config file BufferedReader in_config_file = null; try { //variables for the config file reading String line = null; StringTokenizer token_line = null; String stoken = null; String s1 = null; //variables for temporary info read from the config file in_config_file = new BufferedReader(new FileReader(dir + "config")); line = in_config_file.readLine(); while (line != null) { token_line = new StringTokenizer(line," ",false); if (token_line.countTokens() > 1) //in case an empty line is encountered { //ignore empty lines and lines with only one token stoken = token_line.nextToken(); if (!stoken.startsWith("#")) //ignore comments(begin with "#") { //for message seperator if (stoken.equals("message_separator")) { s1 = token_line.nextToken(); message_separator = s1; } //for logging level else if (stoken.equals("logging_level")) { s1 = token_line.nextToken(); try { logging_level = Integer.parseInt(s1); if (logging_level < 0) { debug.dumpTrace("Class: uip_log_manger, Method constructor, invalid logging level" + Integer.toString(logging_level)); logging_level = 0; } } catch (Exception e) { debug.dumpTrace("Class: uip_log_manger, Method: constructor, logging level is not an integer"); logging_level = 0; } }//end logging_level }//end if not comments line } //end if token count > 1 line = in_config_file.readLine(); } //end while not null line in_config_file.close(); }//end try open file catch (Exception e) { debug.dumpTrace("Class: uip_log_manager, Method:constructor, error read config file."); try { in_config_file.close(); } catch (Exception e1) { return; } return; } //open the log file /*** Replaced by RKF 26May00. Can this Calendar stuff be for real?? Date current_time = new Date(); String file_name = dir + Integer.toString(current_time.getYear() + 1900) + "_" + Integer.toString(current_time.getMonth() + 1) + "_" + Integer.toString(current_time.getDate() ) + "_" + Integer.toString(current_time.getHours()) + "_" + Integer.toString(current_time.getMinutes()) + "_" + Integer.toString(current_time.getSeconds()); */ Calendar current_time = Calendar.getInstance(); String file_name = dir + Integer.toString(current_time.get(Calendar.YEAR)) + "_" + Integer.toString(current_time.get(Calendar.MONTH) + 1) + "_" + Integer.toString(current_time.get(Calendar.DAY_OF_MONTH) ) + "_" + Integer.toString(current_time.get(Calendar.HOUR_OF_DAY)) + "_" + Integer.toString(current_time.get(Calendar.MINUTE)) + "_" + Integer.toString(current_time.get(Calendar.SECOND)); try { pw = new PrintWriter(new FileWriter(file_name)); } catch (IOException e0) { debug.dumpTrace("class uip_log_manager, Method: constructor, create log file failed"); try { pw.close(); } catch (Exception e1) { ; } return; } //write the first creation message DateFormat df = DateFormat.getInstance(); pw.println(message_separator); pw.println("time: " + df.format(current_time.getTime())); pw.println("from: uip_log_manger"); pw.println("message: start logging, logging level " + Integer.toString(logging_level)); pw.flush(); } //end of constructor /** method description: this method provides the smooth exit uses the services of class(es): debug input parameter(s): String condition output parameter(s): none return value: OK WRITE_LOG_ERROR synchronization consideration: synchronized */ public synchronized int exit(String condition) { Date current_time = new Date(); pw.println(message_separator); pw.println("time: " + current_time.toString()); pw.println("from: uip_log_manger"); pw.println("message: exit under condition: " + condition); pw.flush(); pw.close(); return OK; }//end exit /** method description: this method write the common log message uses the services of class(es): debug input parameter(s): String caller_desc, caller description string String message, log message string output parameter(s): none return value: OK NO_WRITE WRITE_LOG_ERROR synchronization consideration: synchronized */ public synchronized int write_log(String caller_desc, String message) { //check the logging level and parameters if (logging_level == 0) { return NO_WRITE; } if ( (caller_desc == null) || (message == null) ) { debug.dumpTrace("class: uip_log_manager, Method: write_log, null caller description or message"); } //write the common message Date current_time = new Date(); pw.println(message_separator); pw.println("time: " + current_time.toString()); pw.println("from: " + caller_desc); pw.println("message: " + message); // not write disk every time writing_count++; if (writing_count > writing_threhold) { writing_count = 0; pw.flush(); } return OK; }//end write log /** method description: this method write the function passed log message uses the services of class(es): rpc_function debug input parameter(s): String caller_desc, caller description string String message, log message string rpc_function rpc output parameter(s): none return value: OK NO_WRITE WRITE_LOG_ERROR synchronization consideration: synchronized */ public synchronized int log_function_passed (String caller_desc, String message, rpc_function rf) { //check the logging level and parameters if (logging_level < 1) { return NO_WRITE; } if ( rf == null ) { debug.dumpTrace("class: uip_log_manager, Method: log_function passed, null rpc function"); return NULL_FUNCTION; } if ( (caller_desc == null) || (message == null) ) { debug.dumpTrace("class: uip_log_manager, Method: write_log, null caller description or message"); } //write the common message Date current_time = new Date(); pw.println(message_separator); pw.println("time: " + current_time.toString()); pw.println("from: " + caller_desc); pw.println("message: " + message); //write the function if ( rf.to_stream(logging_level, pw) != rf.OK ) { debug.dumpTrace("class uip_log_manger, Method: exit, error write log message"); try { pw.close(); } catch (Exception e1) { ; } return WRITE_LOG_ERROR; } // not write disk every time writing_count++; if (writing_count > writing_threhold) { writing_count = 0; pw.flush(); } return OK; }//end log function passed }