i understand where you are coming from furious programming so you do not really have to elaborate on your decisions for every answer, although it provides a little more insight on what it is you are looking for exactly.
However, this means that if I run a build that does not create a console from an external console, the data will not be logged in the console, but in a file. A solution would be useful to check how the game was launched (from the disc or from the console) and if the console is available, the data should be logged to it.
understood.
I need the automatically created console only for debugging, but it would be good if the player could specify where the data will be logged. This will not only come in handy on Windows, but also on other platforms.
The tricky part lies in the automatic creation because what console (read terminal) to launch depends on so many variables.
But let's try to keep it simple and not complicate matters (that was actually a note to myself)
One thing you can be sure about is that on startup of your game you are unable to determine whether a terminal/console is available or not (please do not read "was launched from" but rather /is available anywhere/).
However, you could determine if your game was launched from a terminal or not. isatty comes to mind (should also be available on windows but windows has some other convenient API's such as GetConsoleMode etc that are able to determine the same status').
Initially I made some test that depended on locating the parent process and see what executable was responsible for that process. That brought some tears into my eyes because I used about a dozen different launch methods and got exactly the same amount of different answers in return.
In short it is unreliable unless you wish to account for every program launcher that is available for Linux.
So, instead I had to use come up with another approach (see last part of my message)
Ultimately, as far as the final product is concerned, the game should not use the console at all, unless the player deliberately launches it from the console so that they can see what is being logged. That would be an extra feature.
And that's why it would be nice if the game could detect from the code level whether the console is available, and on this basis determine whether to create a log file or not. If anyone knows how to check this (for Windows and other platforms), I'd be grateful.
See my earlier remark about isatty.
But there is a catch as the user can route/pipe the output in which case the isatty check alone will not be helpful enough. But things could be refined if you so wish to do so.
Edit: I found this — http://kriscode.blogspot.com/2018/02/console-vs-gui-application-in-op.html — I will check it later.
Yeah, I've read that.
I have no idea what the writer means with the buzz around his discovery because all that the writer does is check for the presence of a commandline argument and in case it is present then the writer mistakenly use that as a fact that either the program was launched from either a desktop or from a terminal/console and uses that as a check to either start the GUI part of the program or only the non-gui bits.
Besides the fact that I can start a application from a desktop environment /and/ provide it with parameters without using a console/terminal there also is no secret or unwritten behaviour that can be relied on (other then the presence of a commandline argument)
However, the path I would like to suggest is the following:
- use api calls like isatty to determine if your game was launched from a terminal/console
- in case not launched form a terminal/console /and/ user requests (commandline argument ?) debug output
then either:
a) use a pre-game-launcher executable that launches the rest of the game using TProcess and that uses bash/sh/cmd.exe to launch the main executable.
b) do not use a pre-launcher executable but use the same logic to execute the same executable again but in a detached manner (using the same methodology as described above).
(you could for example use the presence (or not by removing it from second launch) of the argument to determine in which launch 'phase' your game/executable resides.
That way you have control over whether or not /which/ terminal/console needs to be launched and when exactly.
Could something like that be an option for you ?