Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Add an os_log PrintStream
https://bugs.webkit.org/show_bug.cgi?id=207898 Reviewed by Mark Lam. Source/JavaScriptCore: Add jsc option to write dataLogs to os_log. * runtime/Options.cpp: (JSC::Options::initialize): * runtime/OptionsList.h: Source/WTF: When debugging on iOS writing to a file can be hard (thanks Sandboxing!) so logging our dataLogs to os_log would make things easier. This patch adds a new subclass of PrintStream, OSLogPrintStream, that converts our file writes to os_log. Unfortunately, os_log doesn't buffer writes so OSLogPrintStream needs to do its own buffering. e.g. if you call `dataLogLn("some text with a ", number, " and a ", string);` os_log will log that as 5 separate logs. * WTF.xcodeproj/project.pbxproj: * wtf/CMakeLists.txt: * wtf/DataLog.cpp: (WTF::setDataFile): * wtf/DataLog.h: * wtf/OSLogPrintStream.cpp: Added. (WTF::OSLogPrintStream::OSLogPrintStream): (WTF::OSLogPrintStream::~OSLogPrintStream): (WTF::OSLogPrintStream::open): (WTF::OSLogPrintStream::vprintf): * wtf/OSLogPrintStream.h: Copied from Source/WTF/wtf/DataLog.h. * wtf/PrintStream.cpp: (WTF::printExpectedCStringHelper): (WTF::printInternal): * wtf/text/CString.cpp: (WTF::CString::grow): * wtf/text/CString.h: Canonical link: https://commits.webkit.org/220906@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@256910 268f45cc-cd09-0410-ab3c-d52691b4dbfc
- Loading branch information
Showing
with
249 additions
and 8 deletions.
- +13 −0 Source/JavaScriptCore/ChangeLog
- +9 −0 Source/JavaScriptCore/runtime/Options.cpp
- +1 −0 Source/JavaScriptCore/runtime/OptionsList.h
- +34 −0 Source/WTF/ChangeLog
- +6 −0 Source/WTF/WTF.xcodeproj/project.pbxproj
- +2 −0 Source/WTF/wtf/CMakeLists.txt
- +5 −0 Source/WTF/wtf/DataLog.cpp
- +1 −0 Source/WTF/wtf/DataLog.h
- +91 −0 Source/WTF/wtf/OSLogPrintStream.cpp
- +62 −0 Source/WTF/wtf/OSLogPrintStream.h
- +13 −8 Source/WTF/wtf/PrintStream.cpp
- +9 −0 Source/WTF/wtf/text/CString.cpp
- +3 −0 Source/WTF/wtf/text/CString.h
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (C) 2020 Apple Inc. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | ||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include "config.h" | ||
#include <wtf/OSLogPrintStream.h> | ||
|
||
#include <wtf/StringPrintStream.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') { | ||
// Set the new line to a null character so os_log stops copying there. | ||
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 | ||
|
||
} // namespace WTF | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (C) 2020 Apple Inc. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | ||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#if OS(DARWIN) | ||
|
||
#include <wtf/Lock.h> | ||
#include <wtf/PrintStream.h> | ||
#include <wtf/RecursiveLockAdapter.h> | ||
#include <wtf/text/CString.h> | ||
#include <wtf/Vector.h> | ||
|
||
#include <os/log.h> | ||
|
||
namespace WTF { | ||
|
||
class WTF_EXPORT_PRIVATE OSLogPrintStream final : public PrintStream { | ||
public: | ||
OSLogPrintStream(os_log_t, os_log_type_t); | ||
virtual ~OSLogPrintStream(); | ||
|
||
static std::unique_ptr<OSLogPrintStream> open(const char* subsystem, const char* category, os_log_type_t = OS_LOG_TYPE_DEFAULT); | ||
|
||
void vprintf(const char* format, va_list) override WTF_ATTRIBUTE_PRINTF(2, 0); | ||
|
||
private: | ||
os_log_t m_log; | ||
os_log_type_t m_logType; | ||
Lock m_stringLock; | ||
// We need a buffer because os_log doesn't wait for a new line to print the characters. | ||
CString m_string; | ||
size_t m_offset { 0 }; | ||
}; | ||
|
||
} // namespace WTF | ||
|
||
using WTF::OSLogPrintStream; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters