EvilChicken!
2010-07-26 22:21:02
While scripting, I accidentally came across a way of debugging which I realized might prove pretty useful for some users: not only capturing screenshots, but also recording details about them.
I have no idea why the function turned out to be that long, all it does is to capture an image of the client screen and save it to a folder which is created and marked with today's date. The image itself is given a numeric ID (which also serves as a file name), which may have a range starting from 0 to ~999 if previous file names already are taken.
Then, once the image is saved, it saves any and all given information in the imgInfo parameter to a text file, categorized under the ID/number of the captured image. This info can be anything of relevance, and however many lines desired; as new lines are created by simply adding the same character(s) as specified in the 'separator' parameter. Sort of like "/n" in C#.
(I'm sorry if I wasn't too good at explaining, it's currently 4:15AM here. Please read the actual code to fully understand this, and do take note of the usage example in the mainloop.)
This would by the way go in Debug.scar, which is why it is posted here in the /misc/ section.
I'd greatly appreciate any and all questions, comments, feedback and otherwise all other types of response. Is it useful? Should one add this to Debug.scar?
And, please, do feel free to test this.
I, ehm, haven't exactly found the time to do so myself. :p
I have no idea why the function turned out to be that long, all it does is to capture an image of the client screen and save it to a folder which is created and marked with today's date. The image itself is given a numeric ID (which also serves as a file name), which may have a range starting from 0 to ~999 if previous file names already are taken.
Then, once the image is saved, it saves any and all given information in the imgInfo parameter to a text file, categorized under the ID/number of the captured image. This info can be anything of relevance, and however many lines desired; as new lines are created by simply adding the same character(s) as specified in the 'separator' parameter. Sort of like "/n" in C#.
(I'm sorry if I wasn't too good at explaining, it's currently 4:15AM here. Please read the actual code to fully understand this, and do take note of the usage example in the mainloop.)
This would by the way go in Debug.scar, which is why it is posted here in the /misc/ section.
I'd greatly appreciate any and all questions, comments, feedback and otherwise all other types of response. Is it useful? Should one add this to Debug.scar?
{.include SRL/SRL.scar}
{.include SRL/SRL/Misc/Debug.scar}
{*******************************************************************************
function LogScreenShot(imgInfo, Path, separator: string): Boolean;
By: EvilChicken!
Description: Takes a screenshot and assigns the captured image an ID number,
which also serves as the image's name. Logs all given data in 'imgInfo' to a
.txt, explodes the given string into a TSA by given 'separator'.
Will create all required paths and files, results True if successful.
NOTE: 'Path' must simply be the image's path, NOT including the image name.
*******************************************************************************}
function LogScreenShot(imgInfo, Path, separator: string): Boolean;
var
I, logFile: Integer;
infoArr: TStringArray;
begin
Result := False;
if (Trim(Path) = '') then
begin
srl_Warn('LogScreenShot', 'You must enter a valid path where the data will be stored.',
warn_AllVersions);
Exit;
end;
if (not (DirectoryExists(Path))) then
begin
{$IFDEF Simba}
if (not CreateDirectory(Path)) then
begin
srl_Warn('LogScreenShot', 'Failed to create requested directory.', warn_AllVersions);
Exit;
end;
{$ELSE}
try
logFile := RewriteFile(Path + 'Screenshots Info.txt', False);
CloseFile(logFile);
except
srl_Warn('LogScreenShot', 'Failed to create requested directory.', warn_AllVersions);
end;
{$ENDIF}
end;
while (FileExists(Path + IntToStr(I) + '.bmp')) do
begin
Inc(I);
if (I > 999) then
begin
srl_Warn('LogScreenShot', 'Destination folder already contains 1000 images.' +
'Ceasing further screenshotting due to security measures.', warn_AllVersions);
Exit;
end;
end;
TakeScreenshot(Path + IntToStr(I) + '.bmp');
if (not FileExists(Path + 'Screenshots Info.txt')) then
begin
try
logFile := RewriteFile(Path + 'Screenshots Info.txt', False);
except
end;
end else
try
logFile := OpenFile(Path + 'Screenshots Info.txt', False);
except
end;
if (logFile < 0) then
begin
srl_Warn('LogScreenShot', 'Unable to create/open logging .txt file.',
warn_AllVersions);
Exit;
end;
try
WriteFileString(logFile, 'Screenshot #' + IntTostr(I) + ':'#13#10);
infoArr := srl_Explode(imgInfo, separator)
for I := 0 to High(infoArr) do
WriteFileString(logFile, infoArr[I] + #13#10);
WriteFileString(logFile, #13#10#13#10);
CloseFile(logFile);
Result := True;
except
srl_Warn('LogScreenShot', 'Image #' + IntToStr(I) +
': failed to add log entry to image index file!', warn_AllVersions);
end;
end;
begin
LogScreenShot('Error: ' + 'WhatEverErrorMayOccurVariableHere'+
'|Player: ' + Players[CurrentPlayer].Nick +
'|Status/Loc: ' + Players[CurrentPlayer].Loc, '|');
end.And, please, do feel free to test this.
I, ehm, haven't exactly found the time to do so myself. :p