Page 1 of 1

Better event logging OR time functions from os to the API

Posted: Fri Mar 24, 2017 4:45 pm
by masa
Are the chat etc. messages that were "recently" added (as mentioned in viewtopic.php?f=66&t=27197) not logged into the log file at all? I see them on the server console, but not in the factorio-current.log file.

I just spent a few hours looking into factorio modding, and I was trying to make a tiny mod that would simply log certain game events into a nice timestamped log file.
(I want these so that I can hook up factorio to my custom "game statistics" web page thingy that I have been using for Minecraft and Terraria.)
Unfortunately I hit a brick wall in that the io, and more importantly, the os module/library have been disabled in Factorio's Lua. So this means that I can't get the system time nor nicely format it :/

So would it be possible to add nicely formatted logging for "interesting game events", preferably in a separate log file that doesn't have the other verbose, and for this purpose, unnecessary noise that the server console normally has?
Interesting game events for my purposes would include:
  • server start
  • server stop
  • player join
  • player quit
  • player death
  • rocket launched
The formatting would be very close to what the chat formatting is:

Code: Select all

2017-03-24 18:15:24 +0200 [INFO] Server started
2017-03-24 18:15:24 +0200 [INFO] Player masa joined the game
2017-03-24 18:15:24 +0200 [INFO] Player masa died
2017-03-24 18:15:24 +0200 [INFO] Player masa left the game
2017-03-24 18:15:24 +0200 [INFO] A rocket was launched
2017-03-24 18:15:24 +0200 [INFO] Server stopped
Note that the current timezone has been added to the timestamp, so that daylight saving changes twice a year become unambigous too.

If this is out of scope for the game, then could I at least request that the relevant time functions from the os library be made available via the game's API?
For me it would include, at the very least, the following functions:

Code: Select all

os.time()
os.difftime()
os.date()
That might even be the better "solution", as that would allow me to develop that logging mod that I was going to make, and add a lot more functionality to it. I saw some people requesting logging of certain game events to deal with griefers etc. I could make a nice event logging mod that groups up actions, you could have configurable event types that you want to log etc. But I'd really want to have access to the system time and time formatting functions for those logging purposes.

Also, are there any relevant events I could use to log the server start and stop events? If not, could those events be added as well?

Re: Better event logging OR time functions from os to the API

Posted: Fri Mar 24, 2017 5:06 pm
by Yoyobuae

Re: Better event logging OR time functions from os to the API

Posted: Fri Mar 24, 2017 5:09 pm
by Klonan
masa wrote: If this is out of scope for the game, then could I at least request that the relevant time functions from the os library be made available via the game's API?
For me it would include, at the very least, the following functions:

Code: Select all

os.time()
os.difftime()
os.date()
Never going to happen unfortunately, as any of these OS values are not deterministic

You can write your logs using the game time in ticks, which you could format to normal time

Re: Better event logging OR time functions from os to the API

Posted: Fri Mar 24, 2017 6:15 pm
by ssilk
I wouldn't say never, cause I think it is possible to add time to Factorio. But stuff like that can be added only, if they are events!
For example a event every minute, which is sent by the server "This is tick X, my current time is Y"
A modder needs to program an event handler to catch those events. That's all.
Such mods can simply estimate the the "real" time: Last time event was at tick X, now it's 600 ticks later, so the real time now should be about Y + 10 seconds. Or much better: Y + 600 ticks. That is more than enough to see about the time, when someone says something. Or other things.

Also sub-events for the cases like server-start/stop etc. See above. But with one time-event every minute it is not really needed.

Re: Better event logging OR time functions from os to the API

Posted: Fri Mar 24, 2017 7:31 pm
by masa
Never going to happen unfortunately, as any of these OS values are not deterministic
Hmmm... what do you mean by that exactly?
And I don't quite understand how asking factorio about the current system time would be problematic?

But in any case, the method ssilk suggested about an event that periodically updates the system time, would be sufficient.
The remaining issue then would be to properly format that into a human readable time stamp (assuming the event gives out like a unix timestamp in seconds since epoch), if the os.date() function isn't available... but I guess there is code for that online.

Re: Better event logging OR time functions from os to the API

Posted: Sat Mar 25, 2017 12:09 am
by ssilk
masa wrote:
Never going to happen unfortunately, as any of these OS values are not deterministic
Hmmm... what do you mean by that exactly?
He means, that time is a non-deterministic function. https://en.wikipedia.org/wiki/Deterministic_algorithm
And I don't quite understand how asking factorio about the current system time would be problematic?
Cause the system time is on each client different.
(You need to know, that the first versions of Factorio had this function included. But then it was removed due to the determinism issues.)
See for example
viewtopic.php?f=71&t=13299 Send players current time as event to all others
viewtopic.php?f=6&t=32690 Clock needed to keep in touch with the outside world

If a mod uses that value it will calculate different results on each client and that results in a desync of the game.

And eventually the devs will add it:
viewtopic.php?f=89&t=42206
But in any case, the method ssilk suggested about an event that periodically updates the system time, would be sufficient.
The remaining issue then would be to properly format that into a human readable time stamp (assuming the event gives out like a unix timestamp in seconds since epoch), if the os.date() function isn't available... but I guess there is code for that online.
Yeah, it might be just a simple string. More is not needed.

And how I think this should be implemented:
The server can send "strings" on different channels in different intervals/on different events. One channel is of course "server time", but others could be for example "last save" or other non-deterministic values, that only can be known from the server.

Re: Better event logging OR time functions from os to the API

Posted: Sat Mar 25, 2017 11:30 am
by masa
Oh you are right... I have been simply thinking about the functions being added for my sort of purposes, like logging, where it wouldn't matter that they would be different on the clients.
But if people start using that real time for game logic, then things will break... And I assume adding it to the API and just clearly documenting that the values MAY NOT be used for anything game logic related or anything that needs to be deterministic, also wouldn't be an option?

In any case, I think I can work around this issue for my own personal use case (where I just want those events with a timestamp in a log file). I can do this by using that tiny mod I managed to scrape together yesterday, which simply prints a message for each of the events I'm interested in, without a timestamp, to the console. And then I will redirect the console output through a wrapper startup script that uses some stdbuf, awk, grep and tee magic to add the system time to the messages outside the game :) I already use such a wrapper script for my Terraria servers anyway...

Re: Better event logging OR time functions from os to the API

Posted: Sat Mar 25, 2017 5:10 pm
by ssilk
It's no option to say "and you are not allowed to use that function for that and that. If there is a function it will be used. Game sync brakes and he devs are blamed, cause they build it into the game, knowing, that it will be used wrong. :)

Re: Better event logging OR time functions from os to the API

Posted: Mon Mar 27, 2017 11:12 pm
by noliVe
im interested in this logging server sided

- user logged in and out , last login
- chat log
- rocket launched

Why?

... to make things visible on websites..
... statistics ....
... player was online on this map x hours
... search for players on servers . make even more mods and websites arround factorio ...
... auto load/restart a map if event is reached
and so on

Re: Better event logging OR time functions from os to the API

Posted: Tue Mar 28, 2017 11:33 am
by Rseding91
You don't need time related functions to log info. Just use the "log" function and it automatically adds the time information when writing to the log file.