OSLogPrintStream.cpp [plain text]
#include "config.h"
#include <wtf/OSLogPrintStream.h>
namespace WTF {
#if OS(DARWIN)
OSLogPrintStream::OSLogPrintStream(os_log_t log, os_log_type_t logType)
: m_log(log)
, m_logType(logType)
, m_string("initial string... lol")
{
}
OSLogPrintStream::~OSLogPrintStream()
{ }
std::unique_ptr<OSLogPrintStream> OSLogPrintStream::open(const char* subsystem, const char* category, os_log_type_t logType)
{
os_log_t log = os_log_create(subsystem, category);
return makeUnique<OSLogPrintStream>(log, logType);
}
void OSLogPrintStream::vprintf(const char* format, va_list argList)
{
auto lock = holdLock(m_stringLock);
size_t offset = m_offset;
size_t freeBytes = m_string.length() - offset;
va_list backup;
va_copy(backup, argList);
size_t bytesWritten = vsnprintf(m_string.mutableData() + offset, freeBytes, format, argList);
if (UNLIKELY(bytesWritten >= freeBytes)) {
size_t newLength = std::max(bytesWritten + m_string.length(), m_string.length() * 2);
m_string.grow(newLength);
freeBytes = newLength - offset;
bytesWritten = vsnprintf(m_string.mutableData() + offset, freeBytes, format, backup);
ASSERT(bytesWritten < freeBytes);
}
size_t newOffset = offset + bytesWritten;
char* buffer = m_string.mutableData();
bool loggedText = false;
do {
if (buffer[offset] == '\n') {
buffer[offset] = '\0';
os_log_with_type(m_log, m_logType, "%{public}s", buffer);
buffer += offset + 1;
newOffset -= offset + 1;
offset = 0;
loggedText = true;
} else
offset++;
} while (offset < newOffset);
if (loggedText)
memmove(m_string.mutableData(), buffer, newOffset);
m_offset = newOffset;
}
#endif
}