func logPostPrint()

in PlaygroundLogger/PlaygroundLogger/LoggerEntrypoints.swift [98:141]


func logPostPrint(startLine: Int,
                  endLine: Int,
                  startColumn: Int,
                  endColumn: Int) {
    guard !PGLGetThreadIsLogging() else { return }
    PGLSetThreadIsLogging(true)
    defer { PGLSetThreadIsLogging(false) }
    
    guard let printedString = Thread.current.threadDictionary[printedStringThreadDictionaryKey] as! String? else {
        return
    }
    
    Thread.current.threadDictionary.removeObject(forKey: printedStringThreadDictionaryKey)
    
    let packet = LogPacket(printedString: printedString, startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn)
    
    let data: Data
    do {
        data = try packet.encode()
    }
    catch LoggingError.failedToGenerateOpaqueRepresentation {
        fatalError("Failures to generate opaque representations should not occur during encoding")
    }
    catch let LoggingError.encodingFailure(reason) {
        let errorPacket = LogPacket(errorWithReason: reason, startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)

        // Encoding an error packet should not fail under any circumstances.
        data = try! errorPacket.encode()
    }
    catch let LoggingError.otherFailure(reason) {
        let errorPacket = LogPacket(errorWithReason: reason, startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)

        // Encoding an error packet should not fail under any circumstances.
        data = try! errorPacket.encode()
    }
    catch {
        let errorPacket = LogPacket(errorWithReason: "Unknown failure encoding log packet", startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)

        // Encoding an error packet should not fail under any circumstances.
        data = try! errorPacket.encode()
    }
    
    sendData(data as NSData)
}