|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import struct TSCBasic.ProcessResult |
| 14 | + |
| 15 | +/// The ID of a preparation or update indexstore task. This allows us to log messages from multiple concurrently running |
| 16 | +/// indexing tasks to the index log while still being able to differentiate them. |
| 17 | +public enum IndexTaskID: Sendable { |
| 18 | + case preparation(id: UInt32) |
| 19 | + case updateIndexStore(id: UInt32) |
| 20 | + |
| 21 | + private static func numberToEmojis(_ number: Int, numEmojis: Int) -> String { |
| 22 | + let emojis = ["🟥", "🟩", "🟦", "🟧", "⬜️", "🟪", "⬛️", "🟨", "🟫"] |
| 23 | + var number = abs(number) |
| 24 | + var result = "" |
| 25 | + for _ in 0..<numEmojis { |
| 26 | + let (quotient, remainder) = number.quotientAndRemainder(dividingBy: emojis.count) |
| 27 | + result += emojis[remainder] |
| 28 | + number = quotient |
| 29 | + } |
| 30 | + return result |
| 31 | + } |
| 32 | + |
| 33 | + /// Returns a two-character emoji string that allows easy differentiation between different task IDs. |
| 34 | + /// |
| 35 | + /// This marker is prepended to every line in the index log. |
| 36 | + public var emojiRepresentation: String { |
| 37 | + // Multiply by 2 and optionally add 1 to make sure preparation and update index store have distinct IDs. |
| 38 | + // Run .hashValue to make sure we semi-randomly pick new emoji markers for new tasks |
| 39 | + switch self { |
| 40 | + case .preparation(id: let id): |
| 41 | + return Self.numberToEmojis((id * 2).hashValue, numEmojis: 2) |
| 42 | + case .updateIndexStore(id: let id): |
| 43 | + return Self.numberToEmojis((id * 2 + 1).hashValue, numEmojis: 2) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments