Skip to content

Commit fe396da

Browse files
committed
results go to debugger if attached
1 parent 7b31b0d commit fe396da

File tree

4 files changed

+68
-44
lines changed

4 files changed

+68
-44
lines changed

include/mrdox/Debug.hpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
#if ! defined(NDEBUG)
1515
#include <llvm/Support/ErrorHandling.h>
1616
#endif
17+
#include <llvm/Support/raw_ostream.h>
1718

1819
// Some nice odds and ends such as leak checking
1920
// and redirection to the Visual Studio output window.
2021

2122
namespace clang {
2223
namespace mrdox {
2324

24-
/** Enable output window redirection for standard streams.
25-
26-
This will only take effect if a debugger
27-
is attached at the time of the call.
25+
/** Return a stream which writes output to the debugger.
2826
*/
29-
void
30-
debugEnableRedirecton();
27+
/** @{ */
28+
llvm::raw_ostream& debug_outs();
29+
llvm::raw_ostream& debug_errs();
30+
/** @} */
3131

3232
/** Enable debug heap checking.
3333
*/

source/lib/Debug.cpp

+50-29
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
//
1010

1111
#include <mrdox/Debug.hpp>
12+
#include <atomic>
13+
#include <memory>
1214

1315
#if defined(_MSC_VER) && ! defined(NDEBUG)
1416

15-
#include <iostream>
16-
#include <sstream>
17-
1817
#define WIN32_LEAN_AND_MEAN
1918
#include <windows.h>
2019

@@ -23,51 +22,67 @@ namespace mrdox {
2322

2423
namespace {
2524

26-
class debug_streambuf
27-
: public std::stringbuf
25+
static bool const isDebuggerPresent = ::IsDebuggerPresent();
26+
27+
class debug_ostream
28+
: public llvm::raw_ostream
2829
{
29-
bool dbg_;
30-
std::ostream& os_;
31-
std::streambuf* sb_;
30+
static constexpr std::size_t BufferSize = 4096;
3231

33-
void
34-
write(char const* s)
32+
llvm::raw_ostream& os_;
33+
std::string buf_;
34+
35+
void write_impl(const char * Ptr, size_t Size) override
3536
{
36-
if(dbg_)
37-
::OutputDebugStringA(s);
38-
sb_->sputn(s, std::strlen(s));
37+
os_.write(Ptr, Size);
38+
39+
if(isDebuggerPresent)
40+
{
41+
// Windows expects a null terminated string
42+
buf_[Size] = '\0';
43+
::OutputDebugStringA(buf_.data());
44+
}
3945
}
4046

4147
public:
42-
explicit
43-
debug_streambuf(
44-
std::ostream& os)
45-
: dbg_(::IsDebuggerPresent() != 0)
46-
, os_(os)
47-
, sb_(dbg_ ? os.rdbuf(this) : os.rdbuf())
48+
debug_ostream(
49+
llvm::raw_ostream& os)
50+
: os_(os)
4851
{
52+
buf_.resize(BufferSize + 1);
53+
SetBuffer(buf_.data(), BufferSize);
54+
os_.tie(this);
4955
}
5056

51-
~debug_streambuf()
57+
~debug_ostream()
5258
{
53-
sync();
59+
os_.tie(nullptr);
60+
flush();
5461
}
5562

56-
int sync() override
63+
std::size_t preferred_buffer_size() const override
64+
{
65+
return BufferSize;
66+
}
67+
68+
std::uint64_t current_pos() const override
5769
{
58-
write(this->str().c_str());
59-
this->str("");
6070
return 0;
6171
}
6272
};
6373

6474
} // (anon)
6575

66-
void
67-
debugEnableRedirecton()
76+
llvm::raw_ostream& debug_outs()
6877
{
69-
static debug_streambuf out(std::cout);
70-
static debug_streambuf err(std::cerr);
78+
static debug_ostream stream(llvm::outs());
79+
return stream;
80+
}
81+
82+
llvm::raw_ostream& debug_errs()
83+
{
84+
static debug_ostream stream(llvm::errs());
85+
return stream;
7186
}
7287

7388
void
@@ -91,8 +106,14 @@ debugEnableHeapChecking()
91106
namespace clang {
92107
namespace mrdox {
93108

94-
void debugEnableRedirecton()
109+
llvm::raw_ostream& debug_outs()
110+
{
111+
return llvm::outs();
112+
}
113+
114+
llvm::raw_ostream& debug_errs()
95115
{
116+
return llvm::errs();
96117
}
97118

98119
void debugEnableHeapChecking()

source/tests/TestMain.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,10 @@ main(int argc, const char** argv)
398398
{
399399
using namespace clang::mrdox;
400400

401-
debugEnableRedirecton();
401+
// VFALCO This is crashing now on exit...
402+
//llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
403+
402404
debugEnableHeapChecking();
403-
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
404405

405406
Reporter R;
406407

@@ -424,7 +425,7 @@ main(int argc, const char** argv)
424425
break;
425426
}
426427

427-
auto& os = std::cerr;
428+
auto& os = debug_outs();
428429
if(results.numberofFilesWritten > 0)
429430
os <<
430431
results.numberofFilesWritten << " files written\n";
@@ -454,13 +455,15 @@ main(int argc, const char** argv)
454455
}
455456
}
456457
auto milli = results.elapsedMilliseconds();
457-
if(milli < 1000)
458+
if(milli < 10000)
458459
os <<
459460
" in " << milli << " milliseconds\n";
461+
#if 0
460462
else if(milli < 10000)
461463
os <<
462464
" in " << std::setprecision(1) <<
463465
double(milli) / 1000 << " seconds\n";
466+
#endif
464467
else
465468
os <<
466469
" in " << ((milli + 500) / 1000) <<

source/tool/ToolMain.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ toolMain(
160160

161161
int main(int argc, char const** argv)
162162
{
163-
clang::mrdox::debugEnableRedirecton();
164-
clang::mrdox::debugEnableHeapChecking();
163+
using namespace clang::mrdox;
164+
165+
debugEnableHeapChecking();
165166
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
166167

167-
clang::mrdox::Reporter R;
168-
clang::mrdox::toolMain(argc, argv, R);
169-
168+
Reporter R;
169+
toolMain(argc, argv, R);
170170
return R.getExitCode();
171171
}

0 commit comments

Comments
 (0)