[cmake-developers] [PATCH v4] For consoles output on Windows use our own std::streambuf

Brad King brad.king at kitware.com
Wed Jul 20 10:44:01 EDT 2016


On 07/09/2016 10:12 PM, Dāvis Mosāns wrote:
>  int main(int ac, char const* const* av)
>  {
> +#if defined(_WIN32)
> +  // Replace streambuf so we can output Unicode to console
> +  cmsys::ConsoleBuf *cbufio = CM_NULLPTR;
> +  cmsys::ConsoleBuf *cbuferr = CM_NULLPTR;
> +  std::streambuf *coutbuf = std::cout.rdbuf();
> +  std::streambuf *cerrbuf = std::cerr.rdbuf();
> +  try {
> +    cbufio = new cmsys::ConsoleBuf();
> +    coutbuf = std::cout.rdbuf(cbufio);
> +    cbuferr = new cmsys::ConsoleBuf(true);
> +    cerrbuf = std::cerr.rdbuf(cbuferr);
> +  } catch (const std::runtime_error& ex) {
> +    std::cerr << "Failed to create ConsoleBuf!" << std::endl
> +              << ex.what() << std::endl;
> +  };
> +#endif
...
> +#if defined(_WIN32)
> +  if (cbufio) {
> +    delete cbufio;
> +    std::cout.rdbuf(coutbuf);
> +  }
> +  if (cbuferr) {
> +    delete cbuferr;
> +    std::cerr.rdbuf(cerrbuf);
> +  }
> +#endif

Please add to KWSys ConsoleBuf a RAII-style helper to perform
the above steps.  Its constructor can take either std::cout
or std:cerr by reference.

Also, please add a test case for this to KWSys itself.  The
test should at least be able to compile and run.  Verification
that output looks right may need to be a manual step though.

Thanks,
-Brad



More information about the cmake-developers mailing list