Using TEventLog in lazarus - freepascal

I understand that EventLog is capable into writing the exception messages into a log on disk.I have managed to setup the component, but when I set the component to active and do a simple test like:
eventlog.Debug('Application has started!');
I receive the following exception :
Operation not allowed when eventlog is active.
What am I doing wrong ? I can't find the exception in the log what so ever eaven if I set RaiseExceptionOnError to true.
Please a simple sample usage would be great!

Minimal working sample: A Form with two buttons and one eventlog.
Code (change $Username):
procedure TForm1.Button1Click(Sender: TObject);
begin
EventLog1.LogType := ltFile;
EventLog1.FileName := 'C:\users\$USERNAME\Desktop\Test.log';
EventLog1.Active := True;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
EventLog1.Debug('This is a test.');
end;
Click first on Button1, then on Button2. Have fun.

If you want to auto-log exceptions, have a look at http://wiki.freepascal.org/Logging_exceptions#Handling_exceptions

Related

Why does this MySQL stored procedure not seem to execute its exit handler?

I have read the documentation on Exit Handlers and have found useful code ideas in relevant SO questions here and here amongst other places.
Nevertheless, calling the stored procedure below appears to complete OK and returns a TRUE value for the parameter success. when I know it is definitely not completing OK, not least because there was a syntax error in the body SQL (it was referring to a field that did not exist).
So the exit handler should have kicked in and returned FALSE for parameter success.
Can anyone help me to understand why a transaction that fails does not return the correct value for the parameter? (I suspect it has something to do with where I set success to true)
The actual SQL inside the transaction is not important to this question so I haven't shown it. Just assume that it might or might not successfully complete the transaction. It is the success or failure of that transaction that I want to detect through the parameter
DELIMITER $$
CREATE PROCEDURE do_thing (OUT success BOOLEAN)
DETERMINISTIC
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN SET success := FALSE; ROLLBACK; END; # rollback on any error
DECLARE EXIT HANDLER FOR SQLWARNING BEGIN SET success := FALSE; ROLLBACK; END; # rollback on any warning
START TRANSACTION;
< SQL that might cause an error >
< in my case it was referring to a field that didn't exist>
COMMIT;
SET success := TRUE;
END$$
DELIMITER ;
SEE DBFIDDLE
The first part is a copy of your code, it throws an error....
The second part is corrected, both DECLARE EXIT are moved within the block.
The third part is an example where #success will be set to false.

SQLEXCEPTION handler for wrong usage of stored procedures

Is it possible to handle errors like inexistence of stored procedure and wrong number of params?
As I see, the handler like bellow doesn’t catch such errors :(
CREATE PROCEDURE _tmp_proc(
)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
IF IFNULL(#LOG_LEVEL, 1) < 2 THEN
ROLLBACK;
END IF;
SELECT 'An error has occurred';
RESIGNAL;
END;
CALL inexisted_proc();
END
Is it possible to handle errors like inexistence of stored procedure
and wrong number of params?
Yes you can but it should be handled at the calling site and not in the procedure. With calling site, I mean your application end where you are calling the stored procedure. So if you are using PHP then use the error handling mechanism try .. catch construct and in catch block either log the exception or perform as necessary.

Delphi ADOStoredProcedure calling a procedure with OUT params and capturing them after the call

I am wanting to use an ADOStoredProcedure object to call a stored procedure from a database
The stored procedure returns 3 varchar(255)'s : Processed, record_set_size and records_remaining
I then want to capture the returned values into a variable on delphi's side
I am currently using MySQL and Delphi 7
The stored procedure works fine on its own in MySQL so theres nothing the fault on that side.
At the moment the code in delphi is:
ADOWizconSP.ProcedureName := 'proc_moveToWizconTemp';
ADOWizconSP.Parameters.Refresh;
ADOWizconSP.Parameters.ParamByName('#processed').Value := processed;
ADOWizconSP.Parameters.ParamByName('#record_set_size').Value := record_set_size;
ADOWizconSP.Parameters.ParamByName('#records_remaining').Value := records_remaining;
ADOWizconSP.ExecProc;
lb_wizconValues.Items.Add(IntToStr(ADOWizconSP.Parameters.ParamByName('#processed').Value));
lb_wizconValues.Items.Add(IntToStr(ADOWizconSP.Parameters.ParamByName('#record_set_size').Value));
lb_wizconValues.Items.Add(IntToStr(ADOWizconSP.Parameters.ParamByName('#records_remaining').Value));
I am presented with the error:
ADOWizconSP: 'Parameter #processed#not found'
Can anyone help me with what I am trying to achieve here.
Kind Regards,
Jordan :)

Returning a value from a MySQL Stored Procedure when completed

Currently I have a stored procedure which works fine without the out variable, but i wanted to put one in so that i can ensure that my code on the Delphi Application does no run until the Stored Procedure has finished.
Can someone see if I have done this right as i keep getting an error regarding out not being a variable. Is anyone able to explain this?
Please see below my code I am using.
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `proc_moveToWizconTemp`(
OUT success int)
BEGIN
//Code to do some processing and end of SP Processing
SET success = 1;
END
Regards,
Jordan

Logging with MYSQL Handlers

I'm hoping this isn't just duplicating this question.
I have read the documentation but I don't think I fully understand how to use this properly yet.
I would like to catch errors thrown in my stored procedures and log those errors in a table. This is mostly for experimental purposes. I am wondering is there a way to catch any error and then log the code and error to a table.
So far it looks like I have to declare a different handler for each error. Is this correct or is there a way to catch all errors and get the code and message.
For example in each stored procedure I'm declaring a couple of handlers
DECLARE EXIT HANDLER FOR 1062
BEGIN
SELECT 'Attempt to create a duplicate entry occurred';
END;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SELECT 'Unexpected Error Ocurred';
END;
Instead of
SELECT 'custom message';
I want to do
INSERT INTO errorLogs(code, message);
Is this possible without declaring a load of handlers and adding each code manually?
Really appreciate any help pointing me in the right direction.
It looks like this is not really possible until I can use an updated version of MYSQL with DIAGNOSTICS. I found this question which is basically the same as mine. Ah well.