[MySQL] Error Logger

Started by vcmptr, Mar 02, 2015, 02:20 AM

Previous topic - Next topic

vcmptr

This script logging errors to MySQL DB.
function errorlog( str )
{
local hata = "\n[Error]"+str+"\n Functions:\n"
local cs = {};
local now = date();

 
 for(local i = 2; i != null; i++)
 {
 if(getstackinfos( i ))
 {
 local fnad = getstackinfos( i );
 hata = hata+"["+fnad["func"]+"()] "+fnad["src"]+" ["+fnad["line"]+"]\n";
 }
else break;
 }
 
 for(local i = 2; i != null; i++)
 {
 if(getstackinfos( i ))
 {
foreach(idx,val in getstackinfos( i ))
cs[idx+i] <- val;
 }
else break;
 }
 
hata = hata + "Locals:\n"
 for(local i = 2; i != null; i++)
 {
 if(getstackinfos( i ))
 {
 local fnad = getstackinfos( i );
 foreach(idx,val in cs["locals"+i])
 hata = hata+"["+fnad["func"]+"]"+idx+", Value="+val+"\n";
}
else break;
}
print(hata);
mysql_query( YOUR_DB, "INSERT INTO errors (`error`, `date`) VALUES ( '" + mysql_escape_string(YOUR_DB, hata) + "', '" + (now.day + "/" + (now.month.tointeger() + 1) + "/" + now.year + " " + now.hour + ":" + now.min + ":" + now.sec) + "')" );
print( mysql_insert_id(YOUR_DB)+" error saved with this number." );
}
seterrorhandler( errorlog );


DB Create Table Query:
CREATE TABLE IF NOT EXISTS `errors` (
  `error` text NOT NULL,
  `date` text NOT NULL
);
ALTER TABLE errors ADD id INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST; 
My English is not good.

Thijn

You're doing the same loop 3 times, calling getstackinfos 2 times in one loop, and compare int's with nulls.

You should be able to combine all 3 loops into one, cleanup some more variables, and combine it all into much less code like so:
function errorlog( str )
{
local stackinfo, stacktrace = "\n[Error]"+str+"\n Functions:\n", locals = "Locals:\n";
for(local i = 2; (stackinfo = getstackinfos(i)); i++)
{
stacktrace += "["+stackinfo["func"]+"()] "+stackinfo["src"]+" ["+stackinfo["line"]+"]\n";
foreach(idx,val in stackinfo["locals"])
{
locals += "["+stackinfo["func"]+"]"+idx+", Value="+val+"\n";
}
}
print( stacktrace + locals );
mysql_query( YOUR_DB, "INSERT INTO errors (`error`, `date`) VALUES ( '" + mysql_escape_string(YOUR_DB, stacktrace + locals) + "', CURDATE() );" );
print( "Error saved as ID: " + mysql_insert_id(YOUR_DB) );
}

If you're saving a date, you should use the format that's made for it. You don't save a datetime in a textfield...
CREATE TABLE IF NOT EXISTS `errors` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `error` text NOT NULL,
  `date` datetime NOT NULL,
  PRIMARY KEY (`id`)
)

vcmptr

Quote from: Thijn on Mar 02, 2015, 07:01 PMYou're doing the same loop 3 times, calling getstackinfos 2 times in one loop, and compare int's with nulls.

You should be able to combine all 3 loops into one, cleanup some more variables, and combine it all into much less code like so:
function errorlog( str )
{
local stackinfo, stacktrace = "\n[Error]"+str+"\n Functions:\n", locals = "Locals:\n";
for(local i = 2; (stackinfo = getstackinfos(i)); i++)
{
stacktrace += "["+stackinfo["func"]+"()] "+stackinfo["src"]+" ["+stackinfo["line"]+"]\n";
foreach(idx,val in stackinfo["locals"])
{
locals += "["+stackinfo["func"]+"]"+idx+", Value="+val+"\n";
}
}
print( stacktrace + locals );
mysql_query( YOUR_DB, "INSERT INTO errors (`error`, `date`) VALUES ( '" + mysql_escape_string(YOUR_DB, stacktrace + locals) + "', CURDATE() );" );
print( "Error saved as ID: " + mysql_insert_id(YOUR_DB) );
}

If you're saving a date, you should use the format that's made for it. You don't save a datetime in a textfield...
CREATE TABLE IF NOT EXISTS `errors` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `error` text NOT NULL,
  `date` datetime NOT NULL,
  PRIMARY KEY (`id`)
)
Wow only 1 loop :o. Thanks Thijn.
My English is not good.