Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
May 10, 2014 Temporary silence output (stdout) | ||||
---|---|---|---|---|
| ||||
Hi! I sometimes got a useless messages in stdout from SDL_Image library, and I want to temporary silence it. How do I do? |
May 10, 2014 Re: Temporary silence output (stdout) | ||||
---|---|---|---|---|
| ||||
Posted in reply to MarisaLovesUsAll | On Saturday, 10 May 2014 at 20:24:50 UTC, MarisaLovesUsAll wrote:
> Hi!
> I sometimes got a useless messages in stdout from SDL_Image
> library, and I want to temporary silence it. How do I do?
Consider using either version or debug statements.
If you want the messages to be opt-in, debug statements are quite
useful:
debug(myModule) writeln("Hello world!");
Which will only print when you compile with -debug=myModule
If you want more power than that, version statements can be
useful. First declare (or don't) a specific version (or several):
version = MyVersion;
Then conditionally compile code based on that:
version(MyVersion) {
writeln("Hello World");
}
Note that the compiler declares some versions automatically, such
as version(unittest) when compiling with --unittest.
|
May 10, 2014 Re: Temporary silence output (stdout) | ||||
---|---|---|---|---|
| ||||
Posted in reply to MarisaLovesUsAll | On 05/10/14 22:24, MarisaLovesUsAll via Digitalmars-d-learn wrote:
> I sometimes got a useless messages in stdout from SDL_Image library, and I want to temporary silence it. How do I do?
One way would be something like:
import std.stdio;
void writeOutput () {
static c = 1;
printf("%d\n", c++);
}
void main() {
writeOutput();
{
auto ex = PushFD!1("/dev/null".ptr);
writeOutput();
}
writeOutput();
}
struct PushFD(int fd) {
import core.sys.posix.fcntl, core.sys.posix.unistd;
int old;
this(const char* fn) { //
old = dup(fd);
auto nfd = open(fn, O_RDWR);
dup2(nfd, fd);
close(nfd);
}
~this() { dup2(old, fd); close(old); }
}
// In real code you'll want to check for errors from dup/dup2/open/close.
artur
|
May 11, 2014 Re: Temporary silence output (stdout) | ||||
---|---|---|---|---|
| ||||
Posted in reply to MarisaLovesUsAll | On Saturday, 10 May 2014 at 20:24:50 UTC, MarisaLovesUsAll wrote: > Hi! > I sometimes got a useless messages in stdout from SDL_Image > library, and I want to temporary silence it. How do I do? You can temporary redirect output to file. Example (on C): #include <stdio.h> #include <unistd.h> #include <fcntl.h> int main () { int stdout_copy = dup(STDOUT_FILENO); close (STDOUT_FILENO); int stdout_file = creat("myfile.txt", O_RDWR); //it obtains the lowest free descriptor - 1, i.e. stdout. printf("Hello file\n"); fflush(stdout); //don't forget to flush close(stdout_file); dup2(stdout_copy, STDOUT_FILENO); printf("Hello console\n"); return 0; } But it's not very portable, you probably will need to write something other for Windows system. And there are no checks for errors in this example. Also in SDL specific case it seems like it should redirect by default - http://sdl.beuc.net/sdl.wiki/FAQ_Console but I don't know whether it's true or not for SDL_Image. |
May 11, 2014 Re: Temporary silence output (stdout) | ||||
---|---|---|---|---|
| ||||
Posted in reply to MarisaLovesUsAll | On Saturday, 10 May 2014 at 20:24:50 UTC, MarisaLovesUsAll wrote:
> Hi!
> I sometimes got a useless messages in stdout from SDL_Image
> library, and I want to temporary silence it. How do I do?
Are you sure it's stdout, not stderr? For the latter, you would need to redirect FD 2, not FD 1:
...
auto ex = PushFD!2("/dev/null".ptr);
...
|
Copyright © 1999-2021 by the D Language Foundation