Page 1 of 1

Mod to log circuit signals into the file

Posted: Wed Oct 23, 2019 8:46 pm
by coppercoil
I want to log several signals from circuit network into the file in every tick. Maybe someone could make a mod for this.

Why this mod:
I develop complex circuit and I have made its algorithmic model outside of Factorio to test various approaches and coefficients, visualize results and dependencies etc. I want to test that model using real input data. So, I need to transfer signals from the game to the file, I hope it is possible.

More thoughts and expectations:
  • File name and location are not important. It may be mod’s log file along with other information; I can filter specific lines with signal data.
  • I want to log three signals; more would be better. Though, I don’t think it’s worth to log all available signals. A-Z is more than enough.
  • Signal names can be predefined, e.g. A, B, C, D.
  • Log signals every tick.
  • Maybe there should be a separate control signal to on/off logging to avoid flooding.
  • I think it might be useful to log data only if signal is changed (for signal debugging), so maybe the separate control signal for this mode. I don’t need it now, though.
  • Signals may be logged in CSV form, e.g. “<SomeTimestampOrGlobalTickNumber> signal_log: 123;456;789;159186;753”.
  • Please do not log signal names “C:123;E:456;K:789”, I don’t want to parse strings. Empty fields would be much better for me: “;;123;;456;;;;;;789;;;;;;;;;;;;;;;”.
  • It would be a small bonus to have a title line before the first log line:

    Code: Select all

    signal_log: A;B;C;D;E
    signal_log: 123;456;789;159
    signal_log: 124;;790;
    signal_log: ;457;791;160
  • The logging entity needs inputs signals only, it may be a special lamp, a special chest, pole etc.
  • Maybe specific signals can be selected by using spec. const. combinator content?
  • I don’t need to have two or more logging devices at once.

Re: Mod to log circuit signals into the file

Posted: Thu Oct 24, 2019 2:46 am
by DaveMcW

Code: Select all

/c
global.signals = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
global.entity = game.player.selected
global.last_output = nil
script.on_event(defines.events.on_tick, function()
  if not global.entity or not global.entity.valid then return end
  local output = ""
  for i = 1, #global.signals do
    local value = global.entity.get_merged_signal({type="virtual", name="signal-"..global.signals:sub(i,i)}, 1)
    output = output .. value .. ";"
  end
  if global.last_output ~= output then
    game.write_file("signal_log.txt", "tick "..game.tick..": "..output.."\n", true)
    global.last_output = output
  end
end)

Re: Mod to log circuit signals into the file

Posted: Thu Oct 24, 2019 5:51 am
by coppercoil
DaveMcW wrote: Thu Oct 24, 2019 2:46 am
Thank you for this listing, and... what to do next? I've never run any code in Factorio before. Give me please further instructions or some topic to read.

Re: Mod to log circuit signals into the file

Posted: Thu Oct 24, 2019 6:24 am
by Pi-C
coppercoil wrote: Thu Oct 24, 2019 5:51 am Thank you for this listing, and... what to do next? I've never run any code in Factorio before. Give me please further instructions or some topic to read.
Go into console mode with the shortcut defined in Settings -> Control settings --> Basic interaction under "Toggle chat (and Lua console)". If you look at DaveMcW's code again, you'll notice "/c" at the top. That will (very roughly said) switch from chat to console mode, so that everything after it is interpreted as a command.

Re: Mod to log circuit signals into the file

Posted: Thu Oct 24, 2019 7:18 pm
by coppercoil
Great! It works 8-)

How to stop it? :mrgreen:

Re: Mod to log circuit signals into the file

Posted: Thu Oct 24, 2019 7:37 pm
by Pi-C
You must unregister the event handler:

Code: Select all

/c script.on_event(defines.events.on_tick, nil)

Re: Mod to log circuit signals into the file

Posted: Thu Oct 24, 2019 7:41 pm
by coppercoil
Thanks

Re: Mod to log circuit signals into the file

Posted: Thu Oct 31, 2019 9:01 pm
by coppercoil
I really like this script. I have modified it slightly:
  • Every time unique file is created to not overwrite old data.
  • Entire file content is pure CSV now.
  • Tick counter is moved to the line end, so signal names match column names if you open it with Excel.
  • If you connect nonzero “Wall” signal, logging will be suspended.
  • If you connect nonzero “Gate” signal, only changes will be logged.

Code: Select all

/c
global.signals = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
global.entity = game.player.selected
global.last_output = nil
global.start_tick = game.tick
script.on_event(defines.events.on_tick, function()
  if not global.entity or not global.entity.valid or global.entity.get_merged_signal({type="item", name="stone-wall"}, 1) ~= 0 then return end
  local output = ""
  for i = 1, #global.signals do
    local value = global.entity.get_merged_signal({type="virtual", name="signal-"..global.signals:sub(i,i)}, 1)
    output = output .. value .. ";"	
  end
  if global.last_output ~= output or global.entity.get_merged_signal({type="item", name="gate"}, 1) == 0 then
	game.write_file("signals"..global.start_tick..".csv", output..game.tick.."\n", true)
    global.last_output = output
  end  
end)