Introduced helper class TraceOutput that represents an output buffer

and added AbstractTraceEntry::AddDump(TraceOutput&) method.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23622 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold 2008-01-18 22:15:30 +00:00
parent 2f1836da5c
commit f1047a1c3e
2 changed files with 76 additions and 11 deletions

View File

@ -20,6 +20,19 @@ struct trace_entry {
#include <new> #include <new>
class TraceOutput {
public:
TraceOutput(char* buffer, size_t bufferSize);
void Print(const char* format,...);
bool IsFull() const { return fSize >= fCapacity; }
private:
char* fBuffer;
size_t fCapacity;
size_t fSize;
};
class TraceEntry : trace_entry { class TraceEntry : trace_entry {
public: public:
TraceEntry(); TraceEntry();
@ -44,16 +57,12 @@ class AbstractTraceEntry : public TraceEntry {
{ {
} }
virtual void Dump(char* buffer, size_t bufferSize) virtual ~AbstractTraceEntry();
{
int length = snprintf(buffer, bufferSize, "[%6ld] %Ld: ",
fThread, fTime);
AddDump(buffer + length, bufferSize - length);
}
virtual void AddDump(char* buffer, size_t bufferSize) virtual void Dump(char* buffer, size_t bufferSize);
{
} virtual void AddDump(char* buffer, size_t bufferSize);
virtual void AddDump(TraceOutput& out);
thread_id Thread() const { return fThread; } thread_id Thread() const { return fThread; }
bigtime_t Time() const { return fTime; } bigtime_t Time() const { return fTime; }

View File

@ -7,6 +7,7 @@
#include <tracing.h> #include <tracing.h>
#include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <debug.h> #include <debug.h>
@ -143,6 +144,30 @@ allocate_entry(size_t size, uint16 flags)
// #pragma mark - // #pragma mark -
TraceOutput::TraceOutput(char* buffer, size_t bufferSize)
: fBuffer(buffer),
fCapacity(bufferSize),
fSize(0)
{
}
void
TraceOutput::Print(const char* format,...)
{
if (IsFull())
return;
va_list args;
va_start(args, format);
fSize += vsnprintf(fBuffer + fSize, fCapacity - fSize, format, args);
va_end(args);
}
// #pragma mark -
TraceEntry::TraceEntry() TraceEntry::TraceEntry()
{ {
} }
@ -157,8 +182,8 @@ void
TraceEntry::Dump(char* buffer, size_t bufferSize) TraceEntry::Dump(char* buffer, size_t bufferSize)
{ {
#if ENABLE_TRACING #if ENABLE_TRACING
// to be overloaded by subclasses // to be overridden by subclasses
snprintf(buffer, bufferSize, "ENTRY %p", this); kprintf(buffer, bufferSize, "ENTRY %p", this);
#endif #endif
} }
@ -187,6 +212,37 @@ TraceEntry::operator new(size_t size, const std::nothrow_t&) throw()
// #pragma mark - // #pragma mark -
AbstractTraceEntry::~AbstractTraceEntry()
{
}
void
AbstractTraceEntry::Dump(char* buffer, size_t bufferSize)
{
int length = snprintf(buffer, bufferSize, "[%6ld] %Ld: ",
fThread, fTime);
AddDump(buffer + length, bufferSize - length);
}
void
AbstractTraceEntry::AddDump(char* buffer, size_t bufferSize)
{
TraceOutput out(buffer, bufferSize);
AddDump(out);
}
void
AbstractTraceEntry::AddDump(TraceOutput& out)
{
}
// #pragma mark -
#if ENABLE_TRACING #if ENABLE_TRACING