001: /*
002:
003: Derby - Class org.apache.derby.impl.tools.dblook.Logs
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.tools.dblook;
023:
024: import java.io.PrintWriter;
025: import java.io.FileOutputStream;
026: import java.io.IOException;
027:
028: import java.sql.SQLException;
029: import org.apache.derby.tools.dblook;
030:
031: public class Logs {
032:
033: // Log file (for errors/warnings).
034: private static PrintWriter logFile = null;
035:
036: // User-specified output file.
037: private static PrintWriter ddlFile = null;
038:
039: // Statement delimiter.
040: private static String stmtEnd;
041:
042: // Verbose mode?
043: private static boolean verbose;
044:
045: // Did we write at least one message to the dblook file?
046: private static boolean atLeastOneDebug;
047:
048: /* **********************************************
049: * initLogs:
050: * Prepare output streams and initialize state for
051: * handling dblook output.
052: * @param logFileName File for errors/warnings.
053: * @param ddlFileName File for generated DDL.
054: * @param appendLogs Whether or not to append to existing
055: * log and ddl files.
056: * @param doVerbose verbose mode
057: * @param endOfStmt Statement delimiter.
058: * @return true if all state is initialized successfully;
059: * false otherwise.
060: ****/
061:
062: public static boolean initLogs(String logFileName,
063: String ddlFileName, boolean appendLogs, boolean doVerbose,
064: String endOfStmt) {
065: try {
066:
067: logFile = new PrintWriter(new FileOutputStream(logFileName,
068: appendLogs));
069: ddlFile = (ddlFileName == null) ? null : new PrintWriter(
070: new FileOutputStream(ddlFileName, appendLogs));
071: verbose = doVerbose;
072: stmtEnd = endOfStmt;
073: atLeastOneDebug = false;
074: } catch (IOException ioe) {
075: System.out
076: .println("Error initializing log file(s): " + ioe);
077: return false;
078: }
079:
080: return true;
081:
082: }
083:
084: /* **********************************************
085: * Method to report status info to the end-user.
086: * This information will be printed as SQL script
087: * comments, which means the messages must be
088: * preceded by a "--". If the user specified a
089: * DDL file, then the message will be printed to
090: * that file; otherwise, it will be printed to
091: * the console.
092: * @param msg the information to print out.
093: ****/
094:
095: public static void report(String msg) {
096:
097: if (ddlFile == null)
098: System.out.println("-- " + msg);
099: else
100: ddlFile.println("-- " + msg);
101:
102: return;
103:
104: }
105:
106: /* **********************************************
107: * Report a specific string to output.
108: * @param str The string to report.
109: ****/
110:
111: public static void reportString(String str) {
112: report(str);
113: }
114:
115: /* **********************************************
116: * Report a localized message to output.
117: * @param key Key for the message to report.
118: ****/
119:
120: public static void reportMessage(String key) {
121: reportMessage(key, (String[]) null);
122: }
123:
124: /* **********************************************
125: * Report a localized message to output,
126: * substituting the received value where
127: * appropriate.
128: * @param key Key for the message to report.
129: * @param value Value to be inserted into the
130: * message at the {0} marker.
131: ****/
132:
133: public static void reportMessage(String key, String value) {
134: reportMessage(key, new String[] { value });
135: }
136:
137: /* **********************************************
138: * Report a localized message to output,
139: * substituting the received values where
140: * appropriate.
141: * @param key Key for the message to report.
142: * @param values Array of Value to be inserted
143: * into the message at the {0}, {1}, etc markers.
144: ****/
145:
146: public static void reportMessage(String key, String[] values) {
147:
148: String msg = dblook.lookupMessage(key, values);
149: report(msg);
150:
151: }
152:
153: /* **********************************************
154: * Prints the received exception to the log
155: * file and, if the use has specified "verbose",
156: * the screen as well.
157: * @param e The exception to be printed.
158: ****/
159:
160: public static void debug(Exception e) {
161:
162: e.printStackTrace(logFile);
163: if (verbose)
164: e.printStackTrace(System.err);
165: atLeastOneDebug = true;
166:
167: }
168:
169: /* **********************************************
170: * Prints the message for the received key to the log
171: * log file and, if the use has specified "verbose",
172: * the screen as well.
173: * @param key Key for the message to be printed.
174: * @param value Value to be substituted into the
175: * message.
176: ****/
177:
178: public static void debug(String key, String value) {
179:
180: String msg = key;
181: if (value != null) {
182: msg = dblook.lookupMessage(key, new String[] { value });
183: }
184:
185: logFile.println("-- **--> DEBUG: " + msg);
186: if (verbose)
187: System.err.println("-- **--> DEBUG: " + msg);
188: atLeastOneDebug = true;
189:
190: }
191:
192: /* **********************************************
193: * Prints the message for the received key to the log
194: * log file and, if the use has specified "verbose",
195: * the screen as well.
196: * @param key Key for the message to be printed.
197: * @param value Value to be substituted into the
198: * message.
199: ****/
200:
201: public static void debug(String key, String[] values) {
202:
203: String msg = key;
204: if (values != null) {
205: msg = dblook.lookupMessage(key, values);
206: }
207:
208: logFile.println("-- **--> DEBUG: " + msg);
209: if (verbose)
210: System.err.println("-- **--> DEBUG: " + msg);
211: atLeastOneDebug = true;
212:
213: }
214:
215: /* **********************************************
216: * Recursive method to unroll a chains of SQL exceptions.
217: * @param sqlE The SQL exception to unroll.
218: * @return A string representing the unrolled exception
219: * is returned.
220: ****/
221:
222: public static String unRollExceptions(SQLException sqlE) {
223:
224: String rv = sqlE.getMessage() + "\n";
225: if (sqlE.getNextException() != null)
226: return rv + unRollExceptions(sqlE.getNextException());
227: else
228: return rv;
229:
230: }
231:
232: /* **********************************************
233: * Write a string (piece of an SQL command) to
234: * the output DDL file.
235: * @param sql The string to write.
236: ****/
237:
238: public static void writeToNewDDL(String sql) {
239:
240: if (ddlFile == null)
241: System.out.print(sql);
242: else
243: ddlFile.print(sql);
244:
245: }
246:
247: /* **********************************************
248: * Write the user-given statement delimiter to
249: * the output DDL file, followed by a newline.
250: ****/
251:
252: public static void writeStmtEndToNewDDL() {
253:
254: if (ddlFile == null)
255: System.out.println(stmtEnd);
256: else
257: ddlFile.println(stmtEnd);
258:
259: }
260:
261: /* **********************************************
262: * Write a newline character to the output DDL
263: * file, followed by a newline.
264: ****/
265:
266: public static void writeNewlineToNewDDL() {
267:
268: if (ddlFile == null)
269: System.out.println();
270: else
271: ddlFile.println();
272: }
273:
274: /* **********************************************
275: * Close output streams and, if at least one
276: * message was printed to the log file, let
277: * the user know.
278: * @return true if all streams were closed
279: * successfully; false otherwise.
280: ****/
281:
282: public static boolean cleanup() {
283:
284: try {
285: if (atLeastOneDebug)
286: dblook.writeVerboseOutput("DBLOOK_AtLeastOneDebug",
287: null);
288: logFile.close();
289: if (ddlFile != null)
290: ddlFile.close();
291:
292: } catch (Exception e) {
293: System.out.println("Error releasing resources: " + e);
294: return false;
295: }
296:
297: return true;
298:
299: }
300:
301: }
|