[Scummvm-git-logs] scummvm master -> 65aba0351771f3ebc2367954c75144079aa7941f

sev- noreply at scummvm.org
Sun Oct 16 19:52:25 UTC 2022


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
65aba03517 AGI: Write log() Function to File


Commit: 65aba0351771f3ebc2367954c75144079aa7941f
    https://github.com/scummvm/scummvm/commit/65aba0351771f3ebc2367954c75144079aa7941f
Author: Daniel Abramowitz (dannyross at gmail.com)
Date: 2022-10-16T21:52:21+02:00

Commit Message:
AGI: Write log() Function to File

Now the log() function writes to a file, as
the AGI engine did.

It appears that ScummVM doesn't have any "append" functionality.
As a result, I made this implementation create a new file each time
a message is logged. Note that the file is only created if the
log() function is called. The file name includes the game name
and the current time, ensuring that logs won't be overwritten.

The format is: agi.<gameid>.<currentime>.log and will be logged to
the working directory.

Last week I first implemented the log() function. In that commit
I just had it log to the console. In this commit we start
writing to a file.

Changed paths:
    engines/agi/agi.cpp
    engines/agi/agi.h
    engines/agi/op_cmd.cpp


diff --git a/engines/agi/agi.cpp b/engines/agi/agi.cpp
index 4ca1cbee797..feca9b44354 100644
--- a/engines/agi/agi.cpp
+++ b/engines/agi/agi.cpp
@@ -395,6 +395,7 @@ AgiEngine::AgiEngine(OSystem *syst, const AGIGameDescription *gameDesc) : AgiBas
 	_menu = nullptr;
 	_systemUI = nullptr;
 	_inventory = nullptr;
+	_logFile = nullptr;
 
 	_keyHoldMode = false;
 	_keyHoldModeLastKey = Common::KEYCODE_INVALID;
@@ -494,6 +495,11 @@ AgiEngine::~AgiEngine() {
 	if (_gfx) {
 		_gfx->deinitVideo();
 	}
+	if (_logFile) {
+		_logFile->finalize();
+		_logFile->close();
+	}
+	delete _logFile;
 	delete _inventory;
 	delete _systemUI;
 	delete _menu;
diff --git a/engines/agi/agi.h b/engines/agi/agi.h
index a66da31d8d7..9b033e7feeb 100644
--- a/engines/agi/agi.h
+++ b/engines/agi/agi.h
@@ -847,6 +847,7 @@ public:
 	AgiLoader *_loader; // loader
 	GfxMenu *_menu;
 	SystemUI *_systemUI;
+	Common::DumpFile *_logFile; // File used for the log() agi command.
 
 	Common::Stack<ImageStackElement> _imageStack;
 
@@ -899,6 +900,8 @@ public:
 	void processScummVMEvents();
 	void checkQuickLoad();
 
+	const Common::String getTargetName() const { return _targetName; }
+
 	// Objects
 public:
 	int showObjects();
diff --git a/engines/agi/op_cmd.cpp b/engines/agi/op_cmd.cpp
index 253f87ac240..e0f6039be74 100644
--- a/engines/agi/op_cmd.cpp
+++ b/engines/agi/op_cmd.cpp
@@ -32,8 +32,29 @@
 #include "agi/words.h"
 
 #include "common/random.h"
+#include "common/system.h"
 #include "common/textconsole.h"
 
+namespace {
+// Creates a unique log file name each time this function is called.
+// We never want to override an existing log file, so here we create one
+// based on the current game and system time.
+//
+// For example: dumps/agi.kq.20221013214511.log
+Common::String generateLogFileName(Agi::AgiGame *state, Agi::AgiEngine *vm) {
+	TimeDate date;
+	vm->_system->getTimeAndDate(date, true);
+	return Common::String::format("dumps/agi.%s.%d%02d%02d%02d%02d%02d.log",
+								  vm->getTargetName().c_str(),
+								  date.tm_year + 1900,
+								  date.tm_mon + 1,
+								  date.tm_mday,
+								  date.tm_hour,
+								  date.tm_min,
+								  date.tm_sec);
+}
+} // namespace
+
 namespace Agi {
 
 #define getFeatures() state->_vm->getFeatures()
@@ -784,19 +805,23 @@ void cmdLoadGame(AgiGame *state, AgiEngine *vm, uint8 *parameter) {
 void cmdInitDisk(AgiGame *state, AgiEngine *vm, uint8 *parameter) {             // do nothing
 }
 
-// The log command adds an entry to the game's log file. The log format is:
+// The log command logs adds an entry to the game's log file and the console.
+// Neither are enabled by default.
 //
-// Room <#>
-// Input line : <text>
-// <message>
+// * To log to the console, run ScummVM with the following arguments:
+//   * --debugflags=Scripts -d 1
+// * To log to the file, create the directory "dumps" in the current
+//   working directory.
 //
-// Note: If AGI encounters any file errors while trying to open or write to
-// the logfile, it just ignores the errors, and does not write the log file entry.
+// The log format is:
+//   Room <#>
+//   Input line : <text>
+//   <message>
 //
 // This is not yet a complete implementation of log(). There
-// are two follow-up items which are planned to be addressed.
+// is a follow-up item which is planned to be addressed.
 //
-// 1. No Log Formatting
+// == No Log Formatting ==
 // We just log the message literally without
 // processing. According to the docs, you can use variables in the message, like:
 //
@@ -810,12 +835,6 @@ void cmdInitDisk(AgiGame *state, AgiEngine *vm, uint8 *parameter) {
 // %v<number>: at this place the output will include a decimal value of variable with the given number.
 // %w<number>: the text of the player - entered word with the given index number is inserted at this place. (The index is 'one based'; i.e.first word is % w1, second is % w2, etc.)
 //
-// 2. Logs not written to a file.
-// At the moment we are just logging to the console. To see
-// the logs, use the following arguments to scummvm:
-//
-// --debugflags=Scripts -d 1
-//
 void cmdLog(AgiGame *state, AgiEngine *vm, uint8 *parameter) {
 	uint16 textNr = parameter[0];
 	if (state->_curLogic->texts != nullptr && (textNr - 1) <= state->_curLogic->numTexts) {
@@ -823,9 +842,26 @@ void cmdLog(AgiGame *state, AgiEngine *vm, uint8 *parameter) {
 		const char *inputLine = (char *)vm->_text->_promptPrevious;
 		const char *message = state->_curLogic->texts[textNr - 1];
 
-		debugC(1, kDebugLevelScripts, "Room %hhu", currentRoom);
-		debugC(1, kDebugLevelScripts, "Input line : %s", inputLine);
-		debugC(1, kDebugLevelScripts, "%s", message);
+		Common::String logMessage = Common::String::format("Room %hhu\nInput line : %s\n%s\n",
+														   currentRoom,
+														   inputLine,
+														   message);
+
+		// Logs to the console. To see, use arguments: --debugflags=Scripts -d 1
+		debugCN(1, kDebugLevelScripts, "%s", logMessage.c_str());
+
+		Common::DumpFile *&dumpFile = vm->_logFile;
+		if (!dumpFile) {
+			dumpFile = new Common::DumpFile();
+			Common::String logFileName = generateLogFileName(state, vm);
+			dumpFile->open(logFileName);
+		}
+		// The logs will only be written if the "dumps" folder has been created by
+		// the user.
+		if (dumpFile->isOpen()) {
+			dumpFile->writeString(logMessage);
+			dumpFile->flush();
+		}
 	}
 }
 




More information about the Scummvm-git-logs mailing list