Hello, is there any way to print some message to server console only?
I understand that there is game.write_file which I already use, but I'd like to have a way to see messages immediately, without using period-based tools like watch.
Print to server console only
Re: Print to server console only
Wouldn't "tail -f" do what you need?
The -F is useful for log files that get rotated away.-f, --follow[={name|descriptor}]
output appended data as the file grows;
an absent option argument means 'descriptor'
-F same as --follow=name --retry
…
--retry
keep trying to open a file if it is inaccessible
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Re: Print to server console only
Plain print() should do the job.
https://lua-api.factorio.com/latest/Libraries.html wrote:print()
print() outputs to the stdout. For Factorio, this means that it does not end up in the log file, so it can only be read when starting Factorio from the command line.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
Re: Print to server console only
`Tail -f`, and `watch` work in another console. Also, they don't work on Windows (yes, I am that crazy man who hosts Factorio server on dualboot system). That's why I ask a way to output something to server console.
I know that `print` gives output to every user connected. I'd like to do not show players some intermediate information from scripts.
Re: Print to server console only
The majority of users do not open the game open via the command line, so they will never see stdout.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
Re: Print to server console only
Here's the thing: you can use the native lua `print` from just *one* instance without risking a desync, because stdout is not part of the game state. Local mod state that never affects the game state is perfectly fine, in fact your entire mod state is local, it's what you're (usually) trying to keep in sync with all other instances' states so all their effects on the game state stay in sync.
Best I've been able to tell, there's five locally-discernible situations: setup (which ends when on_load completes or on_init starts), limbo(when ends when any game event other than on_load fires), server, singleplayer, multiplayer.
But the only way I've found to tell whether a multiplayer instance is the first client or the server is if the server chats before any players are connected:
Code: Select all
script.on_event(defines.events.on_console_chat,function(ev)
if #game.connected_players == 0 then mystate='server'
end end)
You could run your server with for instance `(echo hi this is Northsoft\'s server starting up; cat) | ./bin/x64/factorio` etc, then your mod would see the console chat regardless of whether the game ticks with no players connected, and so you can then have
Code: Select all
function serverprint(text) if mystate=='server' then print (type(text)=='string' and text or serpent.line(text)) end
fair warning: I have smoketested this, implemented it just now on a local server, but it hasn't been tested in the wild.
Re: Print to server console only
Looks like a duck tape, but as I can understand, this is the only way. Well, looks like the best way to do this is to use `game.write_file`. Thanks for your solution, I will keep it in mind.quyxkh wrote: ↑Fri Jun 11, 2021 6:46 pmCode: Select all
script.on_event(defines.events.on_console_chat,function(ev) if #game.connected_players == 0 then mystate='server' end end)
Code: Select all
function serverprint(text) if mystate=='server' then print (type(text)=='string' and text or serpent.line(text)) end
- eradicator
- Smart Inserter
- Posts: 5206
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Print to server console only
I've actually written a request for is_headless_server.
Depending on how LuaPlayer.connected behaves during on_load on a players machine, it might be possible to guess this without manually triggering events by storing references to each player in global savedata. This would only work if a player is treated as "connected" while still running their own on_load.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.