001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.message;
007:
008: import org.h2.constant.SysProperties;
009:
010: /**
011: * This class represents a trace module.
012: */
013: public class Trace {
014:
015: // TODO trace: java code generation does not always work
016:
017: private TraceSystem traceSystem;
018: private String module;
019: private String lineSeparator;
020:
021: public static final String LOCK = "lock";
022: public static final String SETTING = "setting";
023: public static final String COMMAND = "command";
024: public static final String INDEX = "index";
025: public static final String SEQUENCE = "sequence";
026: public static final String CONSTRAINT = "constraint";
027: public static final String USER = "user";
028: public static final String TRIGGER = "trigger";
029: public static final String FUNCTION = "function";
030: public static final String JDBC = "jdbc";
031: public static final String FILE_LOCK = "fileLock";
032: public static final String TABLE = "table";
033: public static final String LOG = "log";
034: public static final String SCHEMA = "schema";
035: public static final String DATABASE = "database";
036: public static final String SESSION = "session";
037: public static final String AGGREGATE = "aggregate";
038:
039: public Trace(TraceSystem traceSystem, String module) {
040: this .traceSystem = traceSystem;
041: this .module = module;
042: this .lineSeparator = SysProperties.LINE_SEPARATOR;
043: }
044:
045: public boolean info() {
046: return traceSystem.isEnabled(TraceSystem.INFO);
047: }
048:
049: public boolean debug() {
050: return traceSystem.isEnabled(TraceSystem.DEBUG);
051: }
052:
053: public void error(String s) {
054: traceSystem.write(TraceSystem.ERROR, module, s, null);
055: }
056:
057: public void error(String s, Throwable t) {
058: traceSystem.write(TraceSystem.ERROR, module, s, t);
059: }
060:
061: public void info(String s) {
062: traceSystem.write(TraceSystem.INFO, module, s, null);
063: }
064:
065: public void debugCode(String java) {
066: traceSystem.write(TraceSystem.DEBUG, module, lineSeparator
067: + "/**/" + java, null);
068: }
069:
070: public void infoCode(String java) {
071: traceSystem.write(TraceSystem.INFO, module, lineSeparator
072: + "/**/" + java, null);
073: }
074:
075: public void infoSQL(String sql) {
076: sql = replaceNewline(sql);
077: if (sql.startsWith("/*")) {
078: sql = sql.substring(sql.indexOf("*/") + 2).trim();
079: }
080: traceSystem.write(TraceSystem.INFO, module, lineSeparator
081: + "/*SQL*/" + sql, null);
082: }
083:
084: private String replaceNewline(String s) {
085: if (s.indexOf('\r') < 0 && s.indexOf('\n') < 0) {
086: return s;
087: }
088: StringBuffer buff = new StringBuffer(s.length());
089: for (int i = 0; i < s.length(); i++) {
090: char ch = s.charAt(i);
091: if (ch == '\r' || ch == '\n') {
092: ch = ' ';
093: }
094: buff.append(ch);
095: }
096: return buff.toString();
097: }
098:
099: public void debug(String s) {
100: traceSystem.write(TraceSystem.DEBUG, module, s, null);
101: }
102:
103: public void debug(String s, Throwable t) {
104: traceSystem.write(TraceSystem.DEBUG, module, s, t);
105: }
106:
107: }
|