/*
 * SIPRPLogger.java
 *
 * Created on 15 de Marco de 2005, 15:31
 */

package siprp;

import com.evolute.utils.*;
import com.evolute.utils.db.*;
import com.evolute.utils.error.*;
import com.evolute.utils.sql.*;

/**
 *
 * @author  lflores

 * MySQL
CREATE TABLE errors
(
    id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(  id ),
    date TIMESTAMP NOT NULL DEFAULT 'now()',
    type VARCHAR(20),
    environment VARCHAR( 255 ),
    description TEXT
);
 *
 *PostgreSQL
CREATE TABLE errors
(
    id SERIAL,
    PRIMARY KEY(  id ),
    date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    type VARCHAR(20),
    environment VARCHAR( 255 ),
    description VARCHAR( 256000 )
)

 */
public class SHSTLogger implements Logger
{
	private final String java = System.getProperty( "java.version" ) 
			+ "-" + System.getProperty( "java.vm.version" );
	private final String os = System.getProperty( "os.name" ) + " - " +
					System.getProperty( "os.version" );
	private final String user = ( String )Singleton.getInstance( Singleton.USERNAME );
	private final String ENV = "Java: " + java + " \nOS: " + os + " \nUser: " + user 
			+ "\n Software: " + ( String )Singleton.getInstance( SingletonConstants.SOFTWARE_NAME )
			+ "\n Version: " + Main.SHST_VERSION;
	private final Executer executer;
	
	/** Creates a new instance of SIPRPLogger */
	public SHSTLogger()
		throws Exception
	{
		DBManager dbm = ( DBManager )Singleton.getInstance( Singleton.DEFAULT_DBMANAGER );
		executer = dbm.getExclusiveExecuter( this );
	}
	
	public void log( String str )
	{
		String mem = "(" + Runtime.getRuntime().freeMemory() + "/" + 
				Runtime.getRuntime().maxMemory() + ")";
		if( str.length() > 254000 )
		{
			str = str.substring( 0, 254000 ) + " Message too big (" + str.length() + "), truncated.";
		}
		try
		{
			executer.executeQuery( new Insert( "errors", new Assignment[] {
						new Assignment( new Field( "type" ), "LOG" ),
						new Assignment( new Field( "environment" ), ENV + mem ),
						new Assignment( new Field( "description" ), str )
					} ), null );
		}
		catch( Exception ex )
		{
			logException( ex );
		}
	}
	
	public void logException( Throwable ex )
	{
		StackTraceElement ste[] = ex.getStackTrace();
		StringBuffer sb = new StringBuffer();
		for( int i = 0; i < ste.length; ++i )
		{
			sb.append( ste[ i ].toString() );
		}
		String str = "Exception Message: " + ex.getMessage() + "\nStack Trace: " + sb.toString();
		String mem = "(" + Runtime.getRuntime().freeMemory() + "/" + 
				Runtime.getRuntime().maxMemory() + ")";
		if( str.length() > 254000 )
		{
			str = str.substring( 0, 254000 ) + " Message too big (" + str.length() + "), truncated.";
		}
		try
		{
			executer.executeQuery( new Insert( "errors", new Assignment[] {
						new Assignment( new Field( "type" ), "ERROR" ),
						new Assignment( new Field( "environment" ), ENV + mem ),
						new Assignment( new Field( "description" ), str )
					} ), new SQLRetriever() );
		}
		catch( Exception ex1 )
		{
			ex.printStackTrace();
		}
	}
	
}
