Skip to content

Commit

Permalink
Merge pull request #101 from tonystone/fixed-protected-data
Browse files Browse the repository at this point in the history
Add protected data monitoring for fixed FileWriter strategy on iOS
  • Loading branch information
tonystone authored Sep 15, 2019
2 parents 60c947b + 76e258b commit 4c050a3
Show file tree
Hide file tree
Showing 98 changed files with 623 additions and 287 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Change Log
All significant changes to this project will be documented in this file.

## [5.0.0-beta.3](https://github.com/tonystone/tracelog/tree/5.0.0-beta.3)

#### Added
- Added protected data monitoring for `FileWriter.Strategy.fixed` on iOS for use with `AsyncConcurrencyModeOption.buffer(writeInterval:strategy:)`. This allows TraceLog to be started up before protected data is available on iOS.

## [5.0.0-beta.2](https://github.com/tonystone/tracelog/tree/5.0.0-beta.2)

#### Changed
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* **TextFormat** a customizable human readable text formatter useable with any `OutputStreamWriter`.
* **JSONFormat** a customizable JSON string formatter usable with any `OutputStreamWriter`.
- [x] Create custom output formatters for any use case.
- [x] An output buffering mode to buffer output when a writer is unavailable (e.g. on iOS when protected data is not available).
- [x] Multiple **concurrency modes** for writing to Writers. Settable globally or per Writer installed.
* **direct** - straight through real-time logging.
* **sync** - blocking queued logging.
Expand Down
6 changes: 3 additions & 3 deletions Sources/TraceLog/ConcurrencyMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ public enum AsyncConcurrencyModeOption {
/// will write each log entry in the buffer (in order) until the end of the
/// buffer or the Writer becomes unavailable again.
///
/// Buffering is useful for many different use case including:
/// - Remark: Buffering is useful for many different use case including:
///
/// E.g. In an iOS application when protected data is not available to your
/// In an iOS application when protected data is not available to your
/// app but you require visibility into the apps logging even during
//// these times.
/// these times.
///
/// A network writer when the network connection is unavailable for any reason.
///
Expand Down
3 changes: 2 additions & 1 deletion Sources/TraceLog/Internal/Proxies/AsyncWriterProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ internal class AsyncWriterProxy: WriterProxy {
switch option {

/// Enable buffering
///
case ._buffer(let writeInterval, let strategy):

let writeTimer = BlockTimer(deadline: .now() + writeInterval, repeating: writeInterval, queue: self.queue)
Expand Down Expand Up @@ -189,6 +190,7 @@ private class LogEntryQueue {
}

/// Now add the new element
///
storage.append(element)
}

Expand All @@ -204,4 +206,3 @@ private class LogEntryQueue {
return storage[0]
}
}

40 changes: 0 additions & 40 deletions Sources/TraceLog/Writers & Formatters/FileStrategy+Fixed.swift

This file was deleted.

81 changes: 81 additions & 0 deletions Sources/TraceLog/Writers/FileStrategy+Fixed.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
///
/// FileStrategy+Fixed.swift
///
/// Copyright 2019 Tony Stone
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Created by Tony Stone on 1/31/19.
///
import Foundation

@available(iOSApplicationExtension, unavailable)
internal class FileStrategyFixed: FileStrategyManager {

let url: URL

private var stream: FileOutputStream
private var available: Bool

init(directory: URL, fileName: String) throws {

self.url = directory.appendingPathComponent(fileName)
self.available = isLogAvailable()
self.stream = try FileOutputStream(url: url, options: [.create])

#if os(iOS) && !targetEnvironment(simulator)
/// Note: You can create empty files with file protection in any state. You just cant write or read from them.
/// We can safely create the file and set it's protection level even if not available.
///
try FileManager.default.setAttributes([.protectionKey : FileProtectionType.completeUntilFirstUserAuthentication], ofItemAtPath: url.path)

if !available {
NotificationCenter.default.addObserver(forName: UIApplication.protectedDataDidBecomeAvailableNotification, object: nil, queue: nil) { (_) in
if !self.available {
self.available = true
}
}
}
#endif
}
deinit {
self.stream.close()
}

func write(_ bytes: [UInt8]) -> Result<Int, FailureReason> {

guard self.available
else { return .failure(.unavailable) }

return stream.write(bytes).mapError({ self.failureReason($0) })
}
}

#if os(iOS) && !targetEnvironment(simulator)
import UIKit

@available(iOSApplicationExtension, unavailable)
func isLogAvailable() -> Bool {
if !Thread.isMainThread {
return DispatchQueue.main.sync {
return UIApplication.shared.isProtectedDataAvailable
}
}
return UIApplication.shared.isProtectedDataAvailable
}
#else

func isLogAvailable() -> Bool {
return true
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import Foundation
///
/// - SeeAlso: `FileWriter.Strategy` for complete details of all strategies that can be used.
///
@available(iOSApplicationExtension, unavailable, message: "FielWriter can not be initialized in an Extension. Please initialize it in the main App.")
public class FileWriter: OutputStreamWriter {

// MARK: Initialization
Expand Down Expand Up @@ -191,6 +192,11 @@ extension FileWriter {
/// with this option, TraceLog will continue
/// to append to the file name specified.
///
/// - Note: On the iOS platform, this strategy will monitor protected data availability
/// and if you use the `ConcurrencyMode.async(options:)` mode with the
/// `AsyncConcurrencyModeOption.buffer(writeInterval:strategy:)` the FileWriter will
/// buffer when protected data is not available.
///
/// - Remark: Once Swift Evolution [SE-0155](https://github.com/apple/swift-evolution/blob/master/proposals/0155-normalize-enum-case-representation.md) is implemented
/// this will func will be changed to a case in the enum with default values. We must
/// use a func now to work around the lack of defaults on enums.
Expand Down
2 changes: 1 addition & 1 deletion TraceLog.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

Pod::Spec.new do |s|
s.name = "TraceLog"
s.version = "5.0.0-beta.2"
s.version = "5.0.0-beta.3"
s.summary = "Dead Simple: logging the way it's meant to be!"
s.description = <<-DESC
TraceLog is a configurable debug logging system. It is unique in that it's configured
Expand Down
2 changes: 1 addition & 1 deletion docs/Classes/ConsoleWriter.html
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ <h4>Declaration</h4>
</div>
<section class="footer">
<p>© 2019 <a class="link" href="https://github.com/tonystone" target="_blank" rel="external">Tony Stone</a> under the <a class="link" href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank" rel="external">Apache License, Version 2.0</a>.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.5</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.6</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/Classes/ConsoleWriter/Default.html
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ <h4>Declaration</h4>
</div>
<section class="footer">
<p>© 2019 <a class="link" href="https://github.com/tonystone" target="_blank" rel="external">Tony Stone</a> under the <a class="link" href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank" rel="external">Apache License, Version 2.0</a>.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.5</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.6</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/Classes/Environment.html
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ <h4>Declaration</h4>
</div>
<section class="footer">
<p>© 2019 <a class="link" href="https://github.com/tonystone" target="_blank" rel="external">Tony Stone</a> under the <a class="link" href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank" rel="external">Apache License, Version 2.0</a>.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.5</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.6</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/Classes/FileWriter.html
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ <h4>Declaration</h4>
</div>
<section class="footer">
<p>© 2019 <a class="link" href="https://github.com/tonystone" target="_blank" rel="external">Tony Stone</a> under the <a class="link" href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank" rel="external">Apache License, Version 2.0</a>.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.5</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.6</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/Classes/FileWriter/Default.html
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ <h4>Declaration</h4>
</div>
<section class="footer">
<p>© 2019 <a class="link" href="https://github.com/tonystone" target="_blank" rel="external">Tony Stone</a> under the <a class="link" href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank" rel="external">Apache License, Version 2.0</a>.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.5</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.6</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
</div>
Expand Down
10 changes: 9 additions & 1 deletion docs/Classes/FileWriter/Strategy.html
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ <h3 class="section-name">Strategies</h3>
with this option, TraceLog will continue
to append to the file name specified.</p>

</div>
<div class="aside aside-note">
<p class="aside-title">Note</p>
<p>On the iOS platform, this strategy will monitor protected data availability
and if you use the <code><a href="../../Enums/ConcurrencyMode.html#/s:8TraceLog15ConcurrencyModeO5async7optionsACShyAA05AsynccD6OptionOG_tFZ">ConcurrencyMode.async(options:)</a></code> mode with the
<code><a href="../../Enums/AsyncConcurrencyModeOption.html#/s:8TraceLog26AsyncConcurrencyModeOptionO6buffer13writeInterval8strategyAC8Dispatch0k4TimeI0O_AC14BufferStrategyOtFZ">AsyncConcurrencyModeOption.buffer(writeInterval:strategy:)</a></code> the FileWriter will
buffer when protected data is not available.</p>

</div>
<div class="aside aside-remark">
<p class="aside-title">Remark</p>
Expand Down Expand Up @@ -425,7 +433,7 @@ <h4>Declaration</h4>
</div>
<section class="footer">
<p>© 2019 <a class="link" href="https://github.com/tonystone" target="_blank" rel="external">Tony Stone</a> under the <a class="link" href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank" rel="external">Apache License, Version 2.0</a>.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.5</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.6</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/Classes/FileWriter/Strategy/RotationOption.html
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ <h4>Declaration</h4>
</div>
<section class="footer">
<p>© 2019 <a class="link" href="https://github.com/tonystone" target="_blank" rel="external">Tony Stone</a> under the <a class="link" href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank" rel="external">Apache License, Version 2.0</a>.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.5</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.6</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/Configuration.html
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ <h4>Parameters</h4>
</div>
<section class="footer">
<p>© 2019 <a class="link" href="https://github.com/tonystone" target="_blank" rel="external">Tony Stone</a> under the <a class="link" href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank" rel="external">Apache License, Version 2.0</a>.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.5</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.6</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
</div>
Expand Down
29 changes: 16 additions & 13 deletions docs/Enums/AsyncConcurrencyModeOption.html
Original file line number Diff line number Diff line change
Expand Up @@ -250,22 +250,25 @@ <h3 class="section-name">Available Options</h3>
<div class="pointer"></div>
<div class="abstract">
<p>Back the async mode with a buffer for when the writer is not <code>available</code> to
write to it&rsquo;s endpoint. Useful for situations where the endpoint may not
be available at all times.</p>
write to it&rsquo;s endpoint. Useful for situations where the endpoint may not
be available at all times.</p>

<p>Whenever the writer returns false for <code>available</code> TraceLog will buffer the
log entries until the endpoint is <code>available</code>. It will check the writer for
availability based on the <code>writeInterval</code> parameter. Once available, TraceLog
will write each log entry in the buffer (in order) until the end of the
buffer or the Writer becomes unavailable again.</p>
log entries until the endpoint is <code>available</code>. It will check the writer for
availability based on the <code>writeInterval</code> parameter. Once available, TraceLog
will write each log entry in the buffer (in order) until the end of the
buffer or the Writer becomes unavailable again.</p>
<div class="aside aside-remark">
<p class="aside-title">Remark</p>
<p>Buffering is useful for many different use case including:</p>

<p>Buffering is useful for many different use case including:</p>
<p>In an iOS application when protected data is not available to your
app but you require visibility into the apps logging even during
these times.</p>

<p>E.g. In an iOS application when protected data is not available to your
app but you require visibility into the apps logging even during
/ these times.</p>
<pre class="highlight swift"><code> <span class="kt">A</span> <span class="n">network</span> <span class="n">writer</span> <span class="n">when</span> <span class="n">the</span> <span class="n">network</span> <span class="n">connection</span> <span class="k">is</span> <span class="n">unavailable</span> <span class="k">for</span> <span class="n">any</span> <span class="n">reason</span><span class="o">.</span>
</code></pre>
<p>A network writer when the network connection is unavailable for any reason.</p>

</div>

</div>
<div class="declaration">
Expand Down Expand Up @@ -439,7 +442,7 @@ <h4>Declaration</h4>
</div>
<section class="footer">
<p>© 2019 <a class="link" href="https://github.com/tonystone" target="_blank" rel="external">Tony Stone</a> under the <a class="link" href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank" rel="external">Apache License, Version 2.0</a>.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.5</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.6</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/Enums/AsyncConcurrencyModeOption/BufferStrategy.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h4>Declaration</h4>
</div>
<section class="footer">
<p>© 2019 <a class="link" href="https://github.com/tonystone" target="_blank" rel="external">Tony Stone</a> under the <a class="link" href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank" rel="external">Apache License, Version 2.0</a>.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.5</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.6</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
</div>
Expand Down
Loading

0 comments on commit 4c050a3

Please sign in to comment.