Temporary silence output (stdout)

Artur Skawina via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 10 15:33:08 PDT 2014


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


More information about the Digitalmars-d-learn mailing list