package toolspack; import java.io.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import org.w3c.dom.*; /** * Fornisce i metodi per stampare su standard output e creare file XML al fine di aiutare * il debug. Classe con soli metodi statici e synchronized. * @author Stefano Ricciarelli * @version 1.2 - Date: 28-06-2002 */ public class Logger { private static DocumentBuilderFactory factory; private static DocumentBuilder builder; private static TransformerFactory tFactory; private static Document errorsXML; private static String path = new String(""); private static Transformer copiatore; private static Cronometro myTimer; //conta il tempo consumato dalla classe Tester private static boolean start = true; private static boolean debug = true; private static boolean debugXML = true; private static boolean console = true; private static long usedTime = 0; private static FileWriter fw; /** * NB: prima di invocare qualsiasi metodo statico di questa classe è necessario * avere invocato questo costruttore. * @param path il percorso su cui verranno scritti i file */ public Logger(String path) { this.path = path; myTimer = new Cronometro(); tFactory = TransformerFactory.newInstance(); factory = DocumentBuilderFactory.newInstance(); try { builder = factory.newDocumentBuilder(); copiatore = tFactory.newTransformer(); } catch (Exception ex) {print("LOGGER: errore interno nel costruttore " + ex); } //inizializzo il file _errors.xml try { errorsXML = builder.newDocument(); errorsXML.appendChild(errorsXML.createElement("errors")); copiatore.transform(new DOMSource(errorsXML), new StreamResult(new File(path + "_errors.xml")) ); } catch (Exception ex) {print("LOGGER: ERRORE " + ex); } //inizializzo il file _errors.txt try{fw = new FileWriter(path + "_errors.txt");} catch (Exception ex){ console = true; print("ERRORE settando il file _errors.txt; scrittura reindirizzata su console"); } } /** * Abilita/disabilita l'esecuzione dei metodi di questa classe relativi alla * scrittura di stringhe * @param b true se da abilitare, false altrimenti */ public static synchronized void setDebug(boolean b) { debug = b; } /** * Abilita/disabilita l'esecuzione dei metodi di questa classe relativi alla * scrittura di documenti o nodi DOM * @param b true se da abilitare, false altrimenti */ public static synchronized void setDebugXML(boolean b) { debugXML = b; } /** * Imposta la scrittura delle stringhe mediante i metodi print() su console o su * file. Di default è attiva la console * @param b true per scegliere la console, false per scegliere il file */ public static synchronized void setOutputOnConsole(boolean b) { console = b; } /** * Appende a _errors.xml un documento DOM * @param doc il nodo da appendere */ public static synchronized void appendErrorXML(Document doc) { if (!debugXML) return; myTimer.reset(); //appende la radice di doc sotto la radice di errorXML Document saveErrorsXML = builder.newDocument(); try { Document errorsXML = builder.parse(new File(path + "_errors.xml")); saveErrorsXML.appendChild( saveErrorsXML.importNode(errorsXML.getDocumentElement(), true)); errorsXML.getDocumentElement().appendChild( errorsXML.importNode(doc.getDocumentElement(),true)); copiatore.transform(new DOMSource(errorsXML), new StreamResult(new File(path + "_errors.xml")) ); } catch (Exception ex) { print("LOGGER: errore interno in appendErrorXML " + ex); //in caso di errore riscrivo il precedente _errors.xml try { copiatore.transform(new DOMSource(saveErrorsXML), new StreamResult(new File(path + "_errors.xml")) ); } catch (Exception ex1) {print("LOGGER: errore IRRECUPERABILE in appendErrorXML " + ex1);} } usedTime = usedTime + myTimer.getTotal(); } /** * Appende a _errors.xml un nodo DOM * @param node il nodo da appendere */ public static synchronized void appendErrorXML(Node node) { if (!debugXML) return; myTimer.reset(); Document doc = builder.newDocument(); doc.appendChild(doc.importNode(node, true)); appendErrorXML(doc); usedTime = usedTime + myTimer.getTotal(); } /** * Appende a _errors.xml un nodo DOM inserendolo sotto un elemento root che provvede a * creare * @param node il nodo da appendere * @param root il nome dell'elemento da creare sotto il quale inserire il nodo */ public static synchronized void appendErrorXML(Node node, String root) { if (!debugXML) return; myTimer.reset(); Document doc = builder.newDocument(); Element radice = doc.createElement(root); radice.appendChild(doc.importNode(node, true)); doc.appendChild(radice); appendErrorXML(doc); usedTime = usedTime + myTimer.getTotal(); } /** * Appende a _errors.xml un documento DOM inserendolo sotto un elemento root che provvede a * creare * @param doc1 il documento da appendere * @param root il nome dell'elemento da creare sotto il quale inserire il documento */ public static synchronized void appendErrorXML(Document doc1, String root) { if (!debugXML) return; myTimer.reset(); Document doc2 = builder.newDocument(); Element radice1 = doc1.getDocumentElement(); Element radice2 = doc2.createElement(root); radice2.appendChild(doc2.importNode(radice1, true)); doc2.appendChild(radice2); appendErrorXML(doc2); usedTime = usedTime + myTimer.getTotal(); } /** * Stampa, su console o sul file _errors.txt, la stringa s * @param s la stringa da stampare */ public static synchronized void print(String s) { if (!debug) return; if (console) { System.out.println(s); return; } try { fw.write(s + "\r\n"); fw.flush(); //altrimenti la scrittura subisce dei ritardi pazzeschi!! } catch (Exception ex) { System.out.println("ERRORE SCRIVENDO IL LOG SU FILE"); } } /** * Stampa, su console o sul file _errors.txt, la rappresentazione in stringa * dell'oggetto o seguita dalla stringa s * @param o l'oggetto da rappresentare in stringa * @param s la stringa da stampare */ public static synchronized void print(Object o, String s) { if (!debug) return; String s1 = o.toString() + " - " + s; print(s1); } /** * Scrive un documento DOM su un file di nome fileName nel percorso di log * @param dom documento da scrivere su file * @param fileName nome del file, senza percorso */ public static synchronized void DOMtoXMLfile(Document dom, String fileName) { if (!debugXML) return; myTimer.reset(); try { copiatore.transform(new DOMSource(dom), new StreamResult(new File(path + fileName)) ); } catch (Exception ex) {print("LOGGER: errore interno in DOMtoXMLfile " + ex); } usedTime = usedTime + myTimer.getTotal(); } /** * Fornisce il tempo finora impiegato per l'esecuzione dei metodi di questa classe * @return il tempo impiegato in millisecondi */ public static synchronized long getUsedTime() { return usedTime; } } //FINE CLASSE