Commit b465f530 authored by Barry Lind's avatar Barry Lind

The patch does the following:

  Allows you to set the loglevel at runtime by adding ?loglevel=X to the connection URL, where 1 = INFO and 2 = DEBUG.
  Automatically turns on logging by calling DriverManager.setPrintWriter(new PrintWriter(System.out)) if one is not already set.
Adds a Driver.info() message that prints out the version number
Adds member variables logDebug and logInfo that can be checked before making logging methods calls
Adds a build number to the version number string.  This build number will need to be manually incremented when we see fit.

----------------------------------------------------------------------
Modified Files:
 	org/postgresql/Connection.java org/postgresql/Driver.java.in
 	org/postgresql/fastpath/Fastpath.java
 	org/postgresql/jdbc1/DatabaseMetaData.java
 	org/postgresql/jdbc2/Connection.java
 	org/postgresql/jdbc2/DatabaseMetaData.java
 	org/postgresql/largeobject/LargeObjectManager.java
 	org/postgresql/util/PSQLException.java
 	org/postgresql/util/Serialize.java
----------------------------------------------------------------------
No related merge requests found
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -29,13 +29,10 @@ public class Driver implements java.sql.Driver
// make these public so they can be used in setLogLevel below
public static final int DEBUG = 0;
public static final int DEBUG = 2;
public static final int INFO = 1;
public static final int WARN = 2;
public static final int ERROR = 3;
public static final int FATAL = 4;
private static int logLevel = FATAL;
public static boolean logDebug = false;
public static boolean logInfo = false;
static
{
......@@ -46,7 +43,6 @@ public class Driver implements java.sql.Driver
// my early jdbc work did - and that was based on other examples).
// Placing it here, means that the driver is registered once only.
java.sql.DriverManager.registerDriver(new Driver());
}
catch (SQLException e)
{
......@@ -106,7 +102,12 @@ public class Driver implements java.sql.Driver
* to/from the database to unicode. If multibyte is enabled on the
* server then the character set of the database is used as the default,
* otherwise the jvm character encoding is used as the default.
* compatible - This is used to toggle
* loglevel - (optional) Enable logging of messages from the driver.
* The value is an integer from 1 to 2 where:
* INFO = 1, DEBUG = 2
* The output is sent to DriverManager.getPrintWriter() if set,
* otherwise it is sent to System.out.
* compatible - (optional) This is used to toggle
* between different functionality as it changes across different releases
* of the jdbc driver code. The values here are versions of the jdbc
* client and not server versions. For example in 7.1 get/setBytes
......@@ -140,12 +141,12 @@ public class Driver implements java.sql.Driver
{
if ((props = parseURL(url, info)) == null)
{
Driver.debug("Error in url" + url);
if (Driver.logDebug) Driver.debug("Error in url" + url);
return null;
}
try
{
Driver.debug("connect " + url);
if (Driver.logDebug) Driver.debug("connect " + url);
org.postgresql.Connection con = (org.postgresql.Connection)(Class.forName("@JDBCCONNECTCLASS@").newInstance());
con.openConnection (host(), port(), props, database(), url, this);
......@@ -153,7 +154,7 @@ public class Driver implements java.sql.Driver
}
catch (ClassNotFoundException ex)
{
Driver.debug("error", ex);
if (Driver.logDebug) Driver.debug("error", ex);
throw new PSQLException("postgresql.jvm.version", ex);
}
catch (PSQLException ex1)
......@@ -164,7 +165,7 @@ public class Driver implements java.sql.Driver
}
catch (Exception ex2)
{
Driver.debug("error", ex2);
if (Driver.logDebug) Driver.debug("error", ex2);
throw new PSQLException("postgresql.unusual", ex2);
}
}
......@@ -251,7 +252,7 @@ public class Driver implements java.sql.Driver
*/
public static String getVersion()
{
return "@VERSION@";
return "@VERSION@ jdbc driver build " + m_buildNumber;
}
/*
......@@ -293,12 +294,6 @@ public class Driver implements java.sql.Driver
{
String token = st.nextToken();
// PM June 29 1997
// Added this, to help me understand how this works.
// Unless you want each token to be processed, leave this commented out
// but don't delete it.
//DriverManager.println("wellFormedURL: state="+state+" count="+count+" token='"+token+"'");
// PM Aug 2 1997 - Modified to allow multiple backends
if (count <= 3)
{
......@@ -379,7 +374,6 @@ public class Driver implements java.sql.Driver
else if (state == -5)
{
value = token;
//DriverManager.println("put("+key+","+value+")");
urlProps.put(key, value);
state = -2;
}
......@@ -387,13 +381,6 @@ public class Driver implements java.sql.Driver
}
}
// PM June 29 1997
// This now outputs the properties only if we are logging
// PM Sep 13 1999 Commented out, as it throws a Deprecation warning
// when compiled under JDK1.2.
//if (DriverManager.getLogStream() != null)
// urlProps.list(DriverManager.getLogStream());
return urlProps;
}
......@@ -446,12 +433,13 @@ public class Driver implements java.sql.Driver
* used to turn logging on to a certain level, can be called
* by specifying fully qualified class ie org.postgresql.Driver.setLogLevel()
* @param int logLevel sets the level which logging will respond to
* FATAL being almost no messages
* INFO being almost no messages
* DEBUG most verbose
*/
public static void setLogLevel(int logLevel)
{
Driver.logLevel = logLevel;
logDebug = (logLevel >= DEBUG) ? true : false;
logInfo = (logLevel >= INFO) ? true : false;
}
/*
* logging message at the debug level
......@@ -459,7 +447,7 @@ public class Driver implements java.sql.Driver
*/
public static void debug(String msg)
{
if (logLevel <= DEBUG)
if (logDebug)
{
DriverManager.println(msg);
}
......@@ -470,7 +458,7 @@ public class Driver implements java.sql.Driver
*/
public static void debug(String msg, Exception ex)
{
if (logLevel <= DEBUG)
if (logDebug)
{
DriverManager.println(msg + ex != null ? ex.getMessage() : "null Exception");
}
......@@ -481,7 +469,7 @@ public class Driver implements java.sql.Driver
*/
public static void info(String msg)
{
if (logLevel <= INFO)
if (logInfo)
{
DriverManager.println(msg);
}
......@@ -492,75 +480,13 @@ public class Driver implements java.sql.Driver
*/
public static void info(String msg, Exception ex)
{
if (logLevel <= INFO)
{
DriverManager.println(msg + ex != null ? ex.getMessage() : "null Exception");
}
}
/*
* logging message at warn level
* messages will be printed if the logging level is less or equal to WARN
*/
public static void warn(String msg)
{
if (logLevel <= WARN)
{
DriverManager.println(msg);
}
}
/*
* logging message at warn level
* messages will be printed if the logging level is less or equal to WARN
*/
public static void warn(String msg, Exception ex)
{
if (logLevel <= WARN)
{
DriverManager.println(msg + ex != null ? ex.getMessage() : "null Exception");
}
}
/*
* logging message at error level
* messages will be printed if the logging level is less or equal to ERROR
*/
public static void error(String msg)
{
if (logLevel <= ERROR)
{
DriverManager.println(msg);
}
}
/*
* logging message at error level
* messages will be printed if the logging level is less or equal to ERROR
*/
public static void error(String msg, Exception ex)
{
if (logLevel <= ERROR)
{
DriverManager.println(msg + ex != null ? ex.getMessage() : "null Exception");
}
}
/*
* logging message at fatal level
* messages will be printed if the logging level is less or equal to FATAL
*/
public static void fatal(String msg)
{
if (logLevel <= FATAL)
{
DriverManager.println(msg);
}
}
/*
* logging message at fatal level
* messages will be printed if the logging level is less or equal to FATAL
*/
public static void fatal(String msg, Exception ex)
{
if (logLevel <= FATAL)
if (logInfo)
{
DriverManager.println(msg + ex != null ? ex.getMessage() : "null Exception");
}
}
//The build number should be incremented for every new build
private static int m_buildNumber = 100;
}
package org.postgresql.fastpath;
import org.postgresql.Driver;
import java.io.*;
import java.lang.*;
import java.net.*;
......@@ -44,7 +45,6 @@ public class Fastpath
{
this.conn = conn;
this.stream = stream;
//DriverManager.println("Fastpath initialised");
}
/*
......@@ -174,7 +174,7 @@ public class Fastpath
*/
public Object fastpath(String name, boolean resulttype, FastpathArg[] args) throws SQLException
{
//DriverManager.println("Fastpath: calling "+name);
if (Driver.logDebug) Driver.debug("Fastpath: calling "+name);
return fastpath(getID(name), resulttype, args);
}
......
......@@ -13,7 +13,7 @@ import org.postgresql.util.PSQLException;
/*
* This class provides information about the database as a whole.
*
* $Id: DatabaseMetaData.java,v 1.45 2002/06/06 14:47:52 davec Exp $
* $Id: DatabaseMetaData.java,v 1.46 2002/06/11 02:55:16 barry Exp $
*
* <p>Many of the methods here return lists of information in ResultSets. You
* can use the normal ResultSet methods such as getString and getInt to
......@@ -1731,7 +1731,7 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
String relKind;
switch (r.getBytes(3)[0])
{
case (byte) 'r':
case (byte) 'r':
if ( r.getString(1).startsWith("pg_") )
{
relKind = "SYSTEM TABLE";
......@@ -2108,7 +2108,6 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
{
byte[][] tuple = new byte[8][0];
tuple[0] = tuple[1] = "".getBytes();
DriverManager.println("relname=\"" + r.getString(1) + "\" relacl=\"" + r.getString(2) + "\"");
// For now, don't add to the result as relacl needs to be processed.
//v.addElement(tuple);
......
......@@ -17,7 +17,7 @@ import org.postgresql.largeobject.*;
import org.postgresql.util.*;
/*
* $Id: Connection.java,v 1.18 2002/03/26 05:52:50 barry Exp $
* $Id: Connection.java,v 1.19 2002/06/11 02:55:16 barry Exp $
*
* A Connection represents a session with a specific database. Within the
* context of a Connection, SQL statements are executed and results are
......@@ -318,6 +318,14 @@ public class Connection extends org.postgresql.Connection implements java.sql.Co
Types.ARRAY
};
//Because the get/setLogStream methods are deprecated in JDBC2
//we use the get/setLogWriter methods here for JDBC2 by overriding
//the base version of this method
protected void enableDriverManagerLogging() {
if (DriverManager.getLogWriter() == null) {
DriverManager.setLogWriter(new PrintWriter(System.out));
}
}
}
......
package org.postgresql.largeobject;
import org.postgresql.Driver;
import java.io.*;
import java.lang.*;
import java.net.*;
......@@ -117,7 +118,7 @@ public class LargeObjectManager
fp.addFunctions(res);
res.close();
DriverManager.println("Large Object initialised");
if (Driver.logDebug) Driver.debug("Large Object initialised");
}
/*
......
package org.postgresql.util;
import org.postgresql.Driver;
import java.io.*;
import java.lang.*;
import java.lang.reflect.*;
......@@ -128,14 +129,14 @@ public class Serialize
try
{
conn = c;
DriverManager.println("Serialize: initializing instance for type: " + type);
if (Driver.logDebug) Driver.debug("Serialize: initializing instance for type: " + type);
tableName = toPostgreSQL(type);
className = type;
ourClass = Class.forName(className);
}
catch (ClassNotFoundException cnfe)
{
DriverManager.println("Serialize: " + className + " java class not found");
if (Driver.logDebug) Driver.debug("Serialize: " + className + " java class not found");
throw new PSQLException("postgresql.serial.noclass", type);
}
......@@ -147,14 +148,14 @@ public class Serialize
if (rs.next())
{
status = true;
DriverManager.println("Serialize: " + tableName + " table found");
if (Driver.logDebug) Driver.debug("Serialize: " + tableName + " table found");
}
rs.close();
}
// This should never occur, as org.postgresql has it's own internal checks
if (!status)
{
DriverManager.println("Serialize: " + tableName + " table not found");
if (Driver.logDebug) Driver.debug("Serialize: " + tableName + " table not found");
throw new PSQLException("postgresql.serial.table", type);
}
// Finally cache the fields within the table
......@@ -186,9 +187,9 @@ public class Serialize
{
try
{
DriverManager.println("Serialize.fetch: " + "attempting to instantiate object of type: " + ourClass.getName() );
if (Driver.logDebug) Driver.debug("Serialize.fetch: " + "attempting to instantiate object of type: " + ourClass.getName() );
Object obj = ourClass.newInstance();
DriverManager.println("Serialize.fetch: " + "instantiated object of type: " + ourClass.getName() );
if (Driver.logDebug) Driver.debug("Serialize.fetch: " + "instantiated object of type: " + ourClass.getName() );
// NB: we use java.lang.reflect here to prevent confusion with
// the org.postgresql.Field
......@@ -219,7 +220,7 @@ public class Serialize
sb.append(" where oid=");
sb.append(oid);
DriverManager.println("Serialize.fetch: " + sb.toString());
if (Driver.logDebug) Driver.debug("Serialize.fetch: " + sb.toString());
ResultSet rs = conn.ExecSQL(sb.toString());
if (rs != null)
......@@ -388,7 +389,7 @@ public class Serialize
sb.append(')');
}
DriverManager.println("Serialize.store: " + sb.toString() );
if (Driver.logDebug) Driver.debug("Serialize.store: " + sb.toString() );
org.postgresql.ResultSet rs = (org.postgresql.ResultSet) conn.ExecSQL(sb.toString());
// fetch the OID for returning
......@@ -495,13 +496,13 @@ public class Serialize
ResultSet rs = con.ExecSQL("select relname from pg_class where relname = '" + tableName + "'");
if ( rs.next() )
{
DriverManager.println("Serialize.create: table " + tableName + " exists, skipping");
if (Driver.logDebug) Driver.debug("Serialize.create: table " + tableName + " exists, skipping");
rs.close();
return;
}
// else table not found, so create it
DriverManager.println("Serialize.create: table " + tableName + " not found, creating" );
if (Driver.logDebug) Driver.debug("Serialize.create: table " + tableName + " not found, creating" );
// No entries returned, so the table doesn't exist
StringBuffer sb = new StringBuffer("create table ");
......@@ -547,7 +548,7 @@ public class Serialize
sb.append(")");
// Now create the table
DriverManager.println("Serialize.create: " + sb );
if (Driver.logDebug) Driver.debug("Serialize.create: " + sb );
con.ExecSQL(sb.toString());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment