SQL logging fixes

* Added user mapping on the "remote" logging database for the
administrative user. This allows calls to sys.write_sql_log() to succeed
when they are executed by code with administrative privileges.

* Added test suites for both the link to the database and the function
itself.

* Replaced RAISE NOTICE with actual logging in the universe generator
This commit is contained in:
Emmanuel BENOîT 2012-01-07 11:14:17 +01:00
parent e50775ec76
commit 3e109b13bc
9 changed files with 177 additions and 64 deletions
legacyworlds-server-data/db-structure/parts/040-functions

View file

@ -124,21 +124,21 @@ $$ LANGUAGE plpgsql;
GRANT EXECUTE ON FUNCTION sys.write_log( TEXT , log_level , TEXT ) TO :dbuser;
--
-- Remotely append a system log entry
--
-- This function is called from within transactions in order to write to the
-- system log. Unlike sys.write_log(), entries added through this function
-- will not be lost if the transaction is rolled back.
--
-- Since the function is meant to be called from SQL code, it does not return
-- anything as the identifier of the new entry is not required.
--
-- Parameters:
-- _component The component that is appending to the log
-- _level The log level
-- _message The message to write
--
/*
* Remotely append a system log entry
*
* This function is called from within transactions in order to write to the
* system log. Unlike sys.write_log(), entries added through this function
* will not be lost if the transaction is rolled back.
*
* Since the function is meant to be called from SQL code, it does not return
* anything as the identifier of the new entry is not required.
*
* Parameters:
* _component The component that is appending to the log
* _level The log level
* _message The message to write
*/
CREATE OR REPLACE FUNCTION sys.write_sql_log( _component TEXT , _level log_level , _message TEXT )
RETURNS VOID
@ -146,14 +146,19 @@ CREATE OR REPLACE FUNCTION sys.write_sql_log( _component TEXT , _level log_level
SECURITY INVOKER
AS $write_sql_log$
BEGIN
PERFORM dblink_connect( 'cn_logging' , 'srv_logging' );
BEGIN
PERFORM dblink_connect( 'cn_logging' , 'srv_logging' );
EXCEPTION
WHEN duplicate_object THEN
-- Ignore the error, assume we're connected
END;
PERFORM * FROM dblink( 'cn_logging' ,
'SELECT * FROM sys.write_log( '
|| quote_literal( _component ) || ' , '''
|| _level::text || '''::log_level , '
|| quote_literal( _message ) || ' )'
) AS ( entry_id bigint );
PERFORM dblink_disconnect( 'cn_logging' );
END;
$write_sql_log$ LANGUAGE plpgsql;

View file

@ -250,9 +250,10 @@ BEGIN
_recovery := verse.get_random_part( _tot_recovery , _providers_left ,
_data.recovery_avg , _data.recovery_dev );
RAISE NOTICE 'Resource #% planet #%: quantity: % difficulty: % recovery: %',
_data.resource_name_id , _planet ,
_quantity , _difficulty , _recovery;
PERFORM sys.write_sql_log( 'UniverseGenerator' , 'TRACE'::log_level ,
'Resource #' || _data.resource_name_id || ', planet #' || _planet
|| ': quantity ' || _quantity || ' , difficulty '
|| _difficulty || ' , recovery ' || _recovery );
INSERT INTO verse.resource_providers (
planet_id , resource_name_id , resprov_quantity_max ,
@ -326,11 +327,13 @@ BEGIN
_tot_recovery := verse.compute_rpp_delta( _data.providers , _create ,
_data.recovery , _data.recovery_avg , _data.recovery_dev );
RAISE NOTICE 'Resource #%: % new provider(s), quantity: % (avg. %) , difficulty: % (avg. %), recovery: % (avg. %)',
_data.resource_name_id , _create ,
_tot_quantity , _tot_quantity / _create ,
_tot_difficulty , _tot_difficulty / _create ,
_tot_recovery , _tot_recovery / _create;
PERFORM sys.write_sql_log( 'UniverseGenerator' , 'TRACE'::log_level ,
'Resource #' || _data.resource_name_id || ': ' || _create
|| ' new provider(s), quantity: ' || _tot_quantity || ' (avg. '
|| ( _tot_quantity / _create ) || ') , difficulty: '
|| _tot_difficulty || ' (avg. ' || ( _tot_difficulty / _create )
|| '), recovery: ' || _tot_recovery || ' (avg. '
|| _tot_recovery / _create || ')' );
-- Select random planets to add resource providers to
FOR _planet IN SELECT * FROM verse.list_random_planets_in( _area , _create )