This is a helper class to implement logging functions for other sample classes.
Note The sample code assumes you created a session and supplied a user name and password with appropriate access privileges.
The calling program passes one argument when it invokes the constructor for the logger helper class sample. The parameter is a string variable (outputFile) that names the log file to open. The constructor creates an instance of a PrintWriter object ("m_fOut") and ends by displaying the name of the output file with the following code:
m_fOut = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputFile))),
true);
System.out.println("Writing messages to file: " + outputFile);
The logger object displays descriptive information for stepElement or WorkObject objects with Logger.displayStepElementInfo(object) or Logger.displayWorkElementInfo(object), respectively, using the object to be described as an argument. In each method the logger assembles display lines with the argument's (stepElement or workObject) retrieval properties, as in the following Logger.displayStepElementInfo() code:
String svalue = vwStepElement.getWorkObjectName();
log("\t\t\t"+ "WorkObjectName" + "=" + svalue);
svalue = vwStepElement.getTag();
log("\t\t\t"+ "Tag" + "=" + svalue);
Prints a stack trace with ex.printStackTrace().
If argument "Object arg1" is NULL, the program invokes log(String text), which is described in the next method description, below. If "Object arg1" is a string, the method concatenates the two string arguments and invokes log(String text).
If argument "Object arg1" is an object or object array argument (usually an array of numerical values), the method builds a string from the object using StringBuffer() methods as follows:
Make an array for the argument parameter and create a null Stringbuffer.
Object[] args = (Object[])arg1;
StringBuffer buffer = new StringBuffer();
Append an opening format string.
buffer.append("{");
Append each argument to the StringBuffer() object.
for (int i = 0; i < args.length; i++) {
Append a format string.
if (i > 0) buffer.append(",");
buffer.append(args[i]);
}
Append a closing format string.
buffer.append("}");
Apply StringBuffer.toString() to convert elements to strings and call log(String text, Object arg1) again. Note the ultimate result of this will be to concatenate the two strings and invoke log(String text).
log(text, buffer.toString());
Logger.log(string svalue) logs whatever the "svalue" string contains. "svalue" is often a string form of a data element that belongs to an object. You may wish to concatenate explanatory and formatting information in the argument; for example:
Logger.log("\t\t\t"+ "Tag" + "="
+ svalue);
For each line passed to Logger.log(string text), the method invokes an instance of the printWriter object (m_fOut).
if (m_fOut != null)m_fOut.println(text);
else System.out.println(text);
logAndDisplay(String text) uses the same code and adds a standard system display method as follows:
System.out.println(text);