1/*
2 * Copyright (C) 2020 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include <wtf/OSLogPrintStream.h>
28
29#include <wtf/StringPrintStream.h>
30
31namespace WTF {
32
33#if OS(DARWIN)
34
35OSLogPrintStream::OSLogPrintStream(os_log_t log, os_log_type_t logType)
36 : m_log(log)
37 , m_logType(logType)
38 , m_string("initial string... lol")
39{
40}
41
42OSLogPrintStream::~OSLogPrintStream()
43{ }
44
45std::unique_ptr<OSLogPrintStream> OSLogPrintStream::open(const char* subsystem, const char* category, os_log_type_t logType)
46{
47 os_log_t log = os_log_create(subsystem, category);
48 return makeUnique<OSLogPrintStream>(log, logType);
49}
50
51void OSLogPrintStream::vprintf(const char* format, va_list argList)
52{
53 auto lock = holdLock(m_stringLock);
54 size_t offset = m_offset;
55 size_t freeBytes = m_string.length() - offset;
56 va_list backup;
57 va_copy(backup, argList);
58 size_t bytesWritten = vsnprintf(m_string.mutableData() + offset, freeBytes, format, argList);
59 if (UNLIKELY(bytesWritten >= freeBytes)) {
60 size_t newLength = std::max(bytesWritten + m_string.length(), m_string.length() * 2);
61 m_string.grow(newLength);
62 freeBytes = newLength - offset;
63 bytesWritten = vsnprintf(m_string.mutableData() + offset, freeBytes, format, backup);
64 ASSERT(bytesWritten < freeBytes);
65 }
66
67 size_t newOffset = offset + bytesWritten;
68 char* buffer = m_string.mutableData();
69 bool loggedText = false;
70 do {
71 if (buffer[offset] == '\n') {
72 // Set the new line to a null character so os_log stops copying there.
73 buffer[offset] = '\0';
74 os_log_with_type(m_log, m_logType, "%{public}s", buffer);
75 buffer += offset + 1;
76 newOffset -= offset + 1;
77 offset = 0;
78 loggedText = true;
79 } else
80 offset++;
81 } while (offset < newOffset);
82
83 if (loggedText)
84 memmove(m_string.mutableData(), buffer, newOffset);
85 m_offset = newOffset;
86}
87
88#endif
89
90} // namespace WTF
91