Author: [log in to unmask]
Date: Fri Apr 17 14:45:28 2015
New Revision: 2741
Log:
Change print outs to use logger. HPSJAVA-484
Modified:
java/trunk/conditions/src/main/java/org/hps/conditions/cli/AddCommand.java
java/trunk/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java
java/trunk/conditions/src/main/java/org/hps/conditions/cli/LoadCommand.java
java/trunk/conditions/src/main/java/org/hps/conditions/cli/PrintCommand.java
java/trunk/conditions/src/main/java/org/hps/conditions/cli/TagCommand.java
Modified: java/trunk/conditions/src/main/java/org/hps/conditions/cli/AddCommand.java
=============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/cli/AddCommand.java (original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/cli/AddCommand.java Fri Apr 17 14:45:28 2015
@@ -1,7 +1,8 @@
package org.hps.conditions.cli;
-import java.io.PrintStream;
import java.util.Date;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
@@ -10,6 +11,7 @@
import org.hps.conditions.api.ConditionsRecord;
import org.hps.conditions.api.FieldValueMap;
import org.hps.conditions.database.DatabaseConditionsManager;
+import org.lcsim.util.log.LogUtil;
/**
* This is a command for the conditions CLI that will add a conditions record, making a conditions set with a particular
@@ -20,9 +22,9 @@
public class AddCommand extends AbstractCommand {
/**
- * For printing out messages.
+ * Setup logger.
*/
- private final PrintStream ps = System.out;
+ private static final Logger LOGGER = LogUtil.create(AddCommand.class);
/**
* Define command line options.
@@ -50,10 +52,46 @@
}
/**
+ * Create a conditions record.
+ *
+ * @param runStart the run start
+ * @param runEnd the run end
+ * @param tableName the table name
+ * @param name the key name
+ * @param collectionId the collection ID
+ * @param createdBy the user name
+ * @param tag the conditions tag
+ * @param notes the text notes about the collection
+ * @return the new conditions record
+ */
+ // FIXME: Too many method parameters (max 7 is recommended).
+ private ConditionsRecord createConditionsRecord(final int runStart, final int runEnd, final String tableName,
+ final String name, final int collectionId, final String createdBy, final String tag, final String notes) {
+ final ConditionsRecord conditionsRecord = new ConditionsRecord();
+ final FieldValueMap fieldValues = new FieldValueMap();
+ fieldValues.put("run_start", runStart);
+ fieldValues.put("run_end", runEnd);
+ fieldValues.put("table_name", tableName);
+ fieldValues.put("name", name);
+ fieldValues.put("collection_id", collectionId);
+ fieldValues.put("created_by", createdBy);
+ if (tag != null) {
+ fieldValues.put("tag", tag);
+ }
+ if (notes != null) {
+ fieldValues.put("notes", notes);
+ }
+ conditionsRecord.setFieldValues(fieldValues);
+ fieldValues.put("created", new Date());
+ return conditionsRecord;
+ }
+
+ /**
* Execute the command with the given arguments.
*
* @param arguments the command line arguments
*/
+ @Override
final void execute(final String[] arguments) {
final CommandLine commandLine = parse(arguments);
@@ -124,45 +162,10 @@
}
conditionsRecord.insert();
manager.closeConnection(createdConnection);
- } catch (ConditionsObjectException e) {
+ } catch (final ConditionsObjectException e) {
+ LOGGER.log(Level.SEVERE, "Error adding conditions record", e);
throw new RuntimeException("An error occurred while adding a conditions record.", e);
}
- ps.println("successfully added conditions record ...");
- ps.println(conditionsRecord);
- }
-
- /**
- * Create a conditions record.
- *
- * @param runStart the run start
- * @param runEnd the run end
- * @param tableName the table name
- * @param name the key name
- * @param collectionId the collection ID
- * @param createdBy the user name
- * @param tag the conditions tag
- * @param notes the text notes about the collection
- * @return the new conditions record
- */
- // FIXME: Too many method parameters (max 7 is recommended).
- private ConditionsRecord createConditionsRecord(final int runStart, final int runEnd, final String tableName,
- final String name, final int collectionId, final String createdBy, final String tag, final String notes) {
- final ConditionsRecord conditionsRecord = new ConditionsRecord();
- final FieldValueMap fieldValues = new FieldValueMap();
- fieldValues.put("run_start", runStart);
- fieldValues.put("run_end", runEnd);
- fieldValues.put("table_name", tableName);
- fieldValues.put("name", name);
- fieldValues.put("collection_id", collectionId);
- fieldValues.put("created_by", createdBy);
- if (tag != null) {
- fieldValues.put("tag", tag);
- }
- if (notes != null) {
- fieldValues.put("notes", notes);
- }
- conditionsRecord.setFieldValues(fieldValues);
- fieldValues.put("created", new Date());
- return conditionsRecord;
+ LOGGER.info("successfully added conditions record ..." + '\n' + conditionsRecord);
}
}
Modified: java/trunk/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java
=============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java (original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java Fri Apr 17 14:45:28 2015
@@ -14,7 +14,6 @@
import org.apache.commons.cli.PosixParser;
import org.hps.conditions.database.DatabaseConditionsManager;
import org.lcsim.conditions.ConditionsManager.ConditionsNotFoundException;
-import org.lcsim.util.log.DefaultLogFormatter;
import org.lcsim.util.log.LogUtil;
/**
@@ -26,18 +25,16 @@
*
* @author <a href="mailto:[log in to unmask]">Jeremy McCormick</a>
*/
-// FIXME: Print outs should use conditions manager's log settings and not boolean verbose flag.
public class CommandLineTool {
/**
* Setup logging.
*/
- private static final Logger LOGGER = LogUtil.create(CommandLineTool.class.getSimpleName(),
- new DefaultLogFormatter(), Level.WARNING);
+ private static final Logger LOGGER = LogUtil.create(CommandLineTool.class);
/**
* Create a basic instance of this class.
- *
+ *
* @return the instance of this class
*/
private static CommandLineTool create() {
@@ -112,7 +109,7 @@
/**
* Register a sub-command handler.
- *
+ *
* @param command the sub-command handler
*/
private void registerCommand(final AbstractCommand command) {
Modified: java/trunk/conditions/src/main/java/org/hps/conditions/cli/LoadCommand.java
=============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/cli/LoadCommand.java (original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/cli/LoadCommand.java Fri Apr 17 14:45:28 2015
@@ -7,12 +7,14 @@
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
+import java.util.logging.Logger;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.hps.conditions.database.DatabaseConditionsManager;
import org.hps.conditions.database.QueryBuilder;
+import org.lcsim.util.log.LogUtil;
/**
* This is a sub-command to add conditions data using an input text file. The file should be ASCII text that is tab or
@@ -21,6 +23,7 @@
* exist already in the table. Otherwise, the command will fail. By default, the next collection ID will be found by the
* conditions manager.
* <p>
+ *
* <pre>
* java -cp hps-distribution-bin.jar org.hps.conditions.cli.CommandLineTool \
* -p conditions_dev_local.properties load -t scratch_svt_gains -f ./scratch_svt_gains.txt -c 1
@@ -29,6 +32,11 @@
* @author <a href="mailto:[log in to unmask]">Jeremy McCormick</a>
*/
class LoadCommand extends AbstractCommand {
+
+ /**
+ * Setup logger.
+ */
+ private static final Logger LOGGER = LogUtil.create(LoadCommand.class);
/**
* Define command options.
@@ -62,7 +70,7 @@
if (fileName == null) {
throw new IllegalArgumentException("Missing file argument.");
}
- if (!(new File(fileName)).exists()) {
+ if (!new File(fileName).exists()) {
throw new IllegalArgumentException("Input file does not exist: " + fileName);
}
@@ -95,24 +103,24 @@
final String insertSql = QueryBuilder.buildInsert(tableName, collectionID, columnNames, rows);
if (getVerbose()) {
- System.out.println(insertSql);
+ LOGGER.info(insertSql);
}
// FIXME: This call should go through an object API like ConditionsObjectCollection.insert rather than the
// manager directly.
final List<Integer> ids = conditionsManager.updateQuery(insertSql);
- System.out.println("Inserted " + ids.size() + " new rows into table " + tableName + " with collection_id "
+ LOGGER.info("Inserted " + ids.size() + " new rows into table " + tableName + " with collection_id "
+ collectionID);
conditionsManager.closeConnection(openedConnection);
}
/**
* Parse an input text file and add column names and row data to the input lists.
+ *
* @param fileName the name of the text file
* @param columnNames the list of columns (modified by this method)
* @param rows the list of rows (modified by this method)
*/
- private final void parseFile(final String fileName, final List<String> columnNames,
- final List<List<String>> rows) {
+ private final void parseFile(final String fileName, final List<String> columnNames, final List<List<String>> rows) {
final File inputFile = new File(fileName);
BufferedReader reader = null;
try {
@@ -134,13 +142,13 @@
}
rows.add(row);
}
- } catch (Exception e) {
+ } catch (final Exception e) {
throw new RuntimeException(e);
} finally {
if (reader != null) {
try {
reader.close();
- } catch (IOException e) {
+ } catch (final IOException e) {
e.printStackTrace();
}
}
Modified: java/trunk/conditions/src/main/java/org/hps/conditions/cli/PrintCommand.java
=============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/cli/PrintCommand.java (original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/cli/PrintCommand.java Fri Apr 17 14:45:28 2015
@@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
+import java.util.logging.Logger;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
@@ -17,6 +18,7 @@
import org.hps.conditions.api.ConditionsRecord.ConditionsRecordCollection;
import org.hps.conditions.database.DatabaseConditionsManager;
import org.hps.conditions.database.TableMetaData;
+import org.lcsim.util.log.LogUtil;
/**
* This sub-command of the conditions CLI prints conditions conditions table data by run number to the console or
@@ -27,34 +29,15 @@
class PrintCommand extends AbstractCommand {
/**
- * Print stream for output.
- */
- private PrintStream ps = System.out;
-
- /**
- * Flag to print row IDs.
- */
- private boolean printIDs = false;
-
- /**
- * Flag to print out column headers.
- */
- private boolean printHeaders = true;
-
- /**
- * The field delimiter for print output.
- */
- private char fieldDelimiter = ' ';
-
- /**
- * Output file if printing to a file.
- */
- private File outputFile;
-
+ * Setup logger.
+ */
+ private static final Logger LOGGER = LogUtil.create(PrintCommand.class);
+
/**
* Defines command options.
*/
static Options options = new Options();
+
static {
options.addOption(new Option("h", false, "Show help for print command"));
options.addOption(new Option("t", true, "Set the table name"));
@@ -66,6 +49,31 @@
}
/**
+ * The field delimiter for print output.
+ */
+ private char fieldDelimiter = ' ';
+
+ /**
+ * Output file if printing to a file.
+ */
+ private File outputFile;
+
+ /**
+ * Flag to print out column headers.
+ */
+ private boolean printHeaders = true;
+
+ /**
+ * Flag to print row IDs.
+ */
+ private boolean printIDs = false;
+
+ /**
+ * This is the <code>PrintStream</code> for printing the collections to the console or a file.
+ */
+ private PrintStream ps = System.out;
+
+ /**
* Class constructor.
*/
PrintCommand() {
@@ -77,6 +85,7 @@
*
* @param arguments the command line arguments
*/
+ @Override
final void execute(final String[] arguments) {
final CommandLine commandLine = parse(arguments);
@@ -107,27 +116,27 @@
if (new File(path).exists()) {
throw new IllegalArgumentException("File already exists: " + path);
}
- outputFile = new File(path);
+ this.outputFile = new File(path);
try {
- ps = new PrintStream(new BufferedOutputStream(new FileOutputStream(outputFile, false)));
- } catch (FileNotFoundException e) {
+ this.ps = new PrintStream(new BufferedOutputStream(new FileOutputStream(this.outputFile, false)));
+ } catch (final FileNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
// Print IDs in the output.
if (commandLine.hasOption("i")) {
- printIDs = true;
+ this.printIDs = true;
}
// Print header info. Option turns this off.
if (commandLine.hasOption("h")) {
- printHeaders = false;
+ this.printHeaders = false;
}
// Use tabs instead of spaces for field delimiter.
if (commandLine.hasOption("d")) {
- fieldDelimiter = '\t';
+ this.fieldDelimiter = '\t';
}
// List of conditions records to print.
@@ -135,11 +144,11 @@
// Did the user specify a table to use?
if (userConditionsKey == null) {
- System.out.println("printing all conditions");
+ LOGGER.info("printing all conditions");
// Use all table names if there was not one specified.
conditionsRecords.addAll(conditionsManager.getConditionsRecords());
} else {
- System.out.println("printing conditions with name: " + userConditionsKey);
+ LOGGER.info("printing conditions with name: " + userConditionsKey);
// Get records only for the user specified table name.
conditionsRecords.addAll(conditionsManager.findConditionsRecords(userConditionsKey));
}
@@ -152,12 +161,77 @@
// Print the records and the data.
printConditionsRecords(conditionsKeys);
- ps.flush();
- ps.close();
-
- if (outputFile != null) {
- System.out.println("wrote collection data to file " + outputFile.getPath());
- }
+ this.ps.flush();
+ this.ps.close();
+
+ if (this.outputFile != null) {
+ LOGGER.info("wrote collection data to file " + this.outputFile.getPath());
+ }
+ }
+
+ /**
+ * Print a single collection.
+ *
+ * @param collection the collection to print
+ */
+ private void printCollection(final ConditionsObjectCollection<?> collection) {
+ final StringBuffer buffer = new StringBuffer();
+ for (final Object object : collection) {
+ for (final String columnName : collection.getTableMetaData().getFieldNames()) {
+ buffer.append(((ConditionsObject) object).getFieldValue(columnName));
+ buffer.append(this.fieldDelimiter);
+ }
+ buffer.setLength(buffer.length() - 1);
+ buffer.append('\n');
+ }
+ buffer.setLength(buffer.length() - 1);
+ this.ps.print(buffer.toString());
+ this.ps.flush();
+ }
+
+ /**
+ * Print the header for a collection. This is printed to the log rather than the <code>PrintStream</code>.
+ *
+ * @param collection the collection
+ */
+ private void printCollectionHeader(final ConditionsObjectCollection<?> collection) {
+ LOGGER.info('\n' + "--------------------------------------" + '\n' + collection.getConditionsRecord()
+ + "--------------------------------------");
+ }
+
+ /**
+ * Print the list of collections.
+ *
+ * @param collectionList the list of collections
+ */
+ private void printCollections(final List<ConditionsObjectCollection<?>> collectionList) {
+ // Loop over all the collections and print them.
+ for (final ConditionsObjectCollection<?> collection : collectionList) {
+ if (this.printHeaders) {
+ printCollectionHeader(collection);
+ }
+ printColumnNames(collection.getTableMetaData());
+ printCollection(collection);
+ this.ps.println();
+ }
+ this.ps.flush();
+ }
+
+ /**
+ * Print the column names for a table.
+ *
+ * @param tableMetaData the table meta data
+ */
+ private void printColumnNames(final TableMetaData tableMetaData) {
+ if (this.printIDs) {
+ this.ps.print("id");
+ this.ps.print(this.fieldDelimiter);
+ }
+ for (final String columnName : tableMetaData.getFieldNames()) {
+ this.ps.print(columnName);
+ this.ps.print(this.fieldDelimiter);
+ }
+ this.ps.println();
}
/**
@@ -169,13 +243,14 @@
final DatabaseConditionsManager conditionsManager = DatabaseConditionsManager.getInstance();
- System.out.print("printing conditions sets: ");
- for (String conditionsKey : conditionsKeys) {
- System.out.print(conditionsKey + " ");
- }
- System.out.println();
+ final StringBuffer sb = new StringBuffer();
+ for (final String conditionsKey : conditionsKeys) {
+ sb.append(conditionsKey + " ");
+ }
+ LOGGER.info("printing conditions sets: " + sb.toString());
+
// Loop over the conditions keys from the conditions records.
- for (String conditionsKey : conditionsKeys) {
+ for (final String conditionsKey : conditionsKeys) {
// The list of collections to print.
final List<ConditionsObjectCollection<?>> collectionList = new ArrayList<ConditionsObjectCollection<?>>();
@@ -199,68 +274,4 @@
printCollections(collectionList);
}
}
-
- /**
- * Print the list of collections.
- * @param collectionList the list of collections
- */
- private void printCollections(final List<ConditionsObjectCollection<?>> collectionList) {
- // Loop over all the collections and print them.
- for (ConditionsObjectCollection<?> collection : collectionList) {
- if (printHeaders) {
- printCollectionHeader(collection);
- }
- printColumnNames(collection.getTableMetaData());
- printCollection(collection);
- System.out.println();
- }
- ps.flush();
- }
-
- /**
- * Print a single collection.
- * @param collection the collection to print
- */
- private void printCollection(final ConditionsObjectCollection<?> collection) {
- final StringBuffer buffer = new StringBuffer();
- for (Object object : collection) {
- for (String columnName : collection.getTableMetaData().getFieldNames()) {
- buffer.append(((ConditionsObject) object).getFieldValue(columnName));
- buffer.append(fieldDelimiter);
- }
- buffer.setLength(buffer.length() - 1);
- buffer.append('\n');
- }
- buffer.setLength(buffer.length() - 1);
- ps.print(buffer.toString());
- ps.flush();
- }
-
- /**
- * Print the header for a collection.
- * @param collection the collection
- */
- private void printCollectionHeader(final ConditionsObjectCollection<?> collection) {
- System.out.println("--------------------------------------");
- System.out.print(collection.getConditionsRecord());
- System.out.println("--------------------------------------");
- System.out.println();
- System.out.flush();
- }
-
- /**
- * Print the column names for a table.
- * @param tableMetaData the table meta data
- */
- private void printColumnNames(final TableMetaData tableMetaData) {
- if (printIDs) {
- ps.print("id");
- ps.print(fieldDelimiter);
- }
- for (String columnName : tableMetaData.getFieldNames()) {
- ps.print(columnName);
- ps.print(fieldDelimiter);
- }
- ps.println();
- }
}
Modified: java/trunk/conditions/src/main/java/org/hps/conditions/cli/TagCommand.java
=============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/cli/TagCommand.java (original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/cli/TagCommand.java Fri Apr 17 14:45:28 2015
@@ -5,6 +5,7 @@
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.logging.Level;
+import java.util.logging.Logger;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
@@ -16,6 +17,7 @@
import org.hps.conditions.database.DatabaseConditionsManager;
import org.hps.conditions.database.TableMetaData;
import org.lcsim.conditions.ConditionsManager.ConditionsNotFoundException;
+import org.lcsim.util.log.LogUtil;
/**
* Create a conditions system tag.
@@ -28,6 +30,11 @@
* The default detector name (dummy detector).
*/
private static final String DETECTOR_NAME = "HPS-dummy-detector";
+
+ /**
+ * Setup logger.
+ */
+ private static final Logger LOGGER = LogUtil.create(TagCommand.class);
/**
* Defines command options.
@@ -135,15 +142,15 @@
}
// Print out all the records that were found.
- System.out.println("found ConditionsRecords for tag " + newTag + " ...");
+ LOGGER.info("found ConditionsRecords for tag " + newTag + " ...");
for (final ConditionsRecord record : tagRecords) {
- System.out.println(record.toString());
+ LOGGER.info(record.toString());
}
// Prompt user to verify with console input.
boolean makeTag = true;
if (!dontPrompt) {
- System.out.println("Create conditions tag " + newTag + " in database? (Y/N)");
+ LOGGER.info("Create conditions tag " + newTag + " in database? (Y/N)");
final String line = System.console().readLine();
if (!line.equals("Y")) {
makeTag = false;
|