I spent a few hours on the topic and I come out with a solution working for my needs.
I want to share the outcoming since it could be useful for complete noobs as me to start somewhere. Also it looks like not many people are still active on Attila mods forum and maybe noob modders are going to more recent titles.
However...
First of all I want to thank Magnar for the old post on Rome II forum that gave me the possibility to get started. Mainly I downloaded his basic_script_logging.pack, I translated it to Attila and then taking it as an example I wrote my own script logger.
I start adding the next script to my pack
lua_scripts/mod_logger.lua
Code:
-------------------------------------
-- Trux's script logger based on
-- Magnar's Basic Script Log
-------------------------------------
-- General
-- Declare this file as an "exportable" module
module(..., package.seeall)
_G.main_env = getfenv(1) -- Probably not needed in most places
-- Includes
local scripting = require "lua_scripts.episodicscripting"
-- file name variables
local __is_start_turn = true;
local log_file_name = "mod_test.log";
local turn_number = 1;
-------------------------------------------------------
-- file names parameters
-- chenge here to have different file name behavior
-------------------------------------------------------
local __file_name_prefix = "mod_log";
local __file_name_prefix_per_turn = "_turn_";
local __file_name_suffix_start_turn = "_start";
local __file_name_suffix_end_turn = "_end";
local __file_name_suffix = ".log";
-- set to genetrate one file for each turn
local __file_per_turn = true;
-- set to generate file for each end/start turn event (if true overrides __file_per_turn)
local __file_per_start_end_turn = true;
-------------------------------------------------------
-- log Enabling
-- Change Here to enable/disable Logger fuctionalities
-------------------------------------------------------
-- If false no log is generated
Log_Enabled = true;
-- set to add to log file Campaign output() logs
Vanilla_Log_Enabled = false;
--------------------------------------
-- set actual log file name
--------------------------------------
function SetLogFileName()
local fileName = __file_name_prefix;
if __file_per_turn or __file_per_start_end_turn then
fileName = fileName .. __file_name_prefix_per_turn .. turn_number;
if __file_per_start_end_turn then
if __is_start_turn then
fileName = fileName .. __file_name_suffix_start_turn;
else
fileName = fileName .. __file_name_suffix_end_turn;
end
end
end
log_file_name = fileName .. __file_name_suffix;
end
--------------------------------------
-- log
--------------------------------------
function Log(text)
if Log_Enabled then
logText = tostring(text);
local logfile = io.open(log_file_name,"a")
logfile:write(logText .. "\n")
logfile:close()
end
end
-----------------------------------------
-- create log file on world created
-----------------------------------------
local function OnWorldCreated(context)
turn_number = cm:model():turn_number();
SetLogFileName();
Log("==============================");
Log("> Campaign loaded on turn " .. turn_number)
Log("==============================");
end
-----------------------------------------
-- create log file on turn start
-----------------------------------------
local function OnFactionTurnStart(context)
if context:faction():is_human() == true then
__is_start_turn = true;
turn_number = cm:model():turn_number();
SetLogFileName();
Log("==============================");
Log("> Turn " .. turn_number .. " start")
Log("==============================");
end
end
-----------------------------------------
-- create log file on turn end
-----------------------------------------
local function OnFactionTurnEnd(context)
if context:faction():is_human() == true then
__is_start_turn = false;
turn_number = cm:model():turn_number();
SetLogFileName();
Log("==============================");
Log("> Turn " .. turn_number .. " end")
Log("==============================");
end
end
---------------------------------------
-- Event callbacks
---------------------------------------
scripting.AddEventCallBack("WorldCreated", OnWorldCreated)
scripting.AddEventCallBack("FactionTurnStart", OnFactionTurnStart)
scripting.AddEventCallBack("FactionTurnEnd", OnFactionTurnEnd)
The script allows to
- log to file from inside scripts of mod
- create a single file for logging or one file per turn
- create a new log file when the player faction turn starts and one when the player faction turn ends (just to separate logs of human player turn and AI players ones)
- Enable/Disable logging
In order to be able to use the script from inside other scripts...
- it is necessary to append the next rows to the bottom of the campaigns/main_attila/scripting.lua scripts (or whichever campaign is edited)
Code:
-- LOGS
require "lua_scripts.mod_logger"
- add the next row somewhere at the beginning of script that should generate logs
Code:
local logger = require "lua_scripts.mod_logger"
- call the Log function wherever needed
Code:
function myfunc(index)
...
logger.Log("Index = " .. index)
...
end
This script can be used in order to log to the same files also all the contents printed by the function output() used by vanilla campaign scripts.
in order to do so it is necessary to make two small modification to the script/_lib/lib_misc_campaign.lua vanilla script in the PRINTING section of the file
- around line 271 add the next row
Code:
local logger = require "lua_scripts.mod_logger"
- around line 259 at the and of the definition of the function output(input) add the call to the logger as soon before the print(output_str) statement
Code:
if logger.Vanilla_Log_Enabled then
logger.Log(output_str)
end
print(output_str);
The last functionality can be Enabled/disabled setting the Vanilla_Log_Enabled parameter of the logger.
Hope it could be useful to someone... if not no problem I spent few our to find the solution.
Bye
Trux