1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#import "WALTLogger.h"
18
19@implementation WALTLogger {
20  NSMutableString *_buffer;
21}
22
23+ (instancetype)sessionLogger {
24  static WALTLogger *sessionLogger = nil;
25  static dispatch_once_t token;
26  dispatch_once(&token, ^{
27    sessionLogger = [[self alloc] init];
28  });
29  return sessionLogger;
30}
31
32- (instancetype)init {
33  if ((self = [super init])) {
34    _buffer = [[NSMutableString alloc] init];
35  }
36  return self;
37}
38
39- (void)appendString:(NSString *)string {
40  @synchronized (_buffer) {
41    [_buffer appendString:string];
42  }
43}
44
45- (void)appendFormat:(NSString *)format, ... {
46  va_list args;
47  va_start(args, format);
48  NSString *formatted = [[NSString alloc] initWithFormat:format arguments:args];
49  [_buffer appendString:formatted];
50  va_end(args);
51}
52
53- (void)clear {
54  @synchronized (_buffer) {
55    [_buffer setString:[NSString string]];
56  }
57}
58
59- (BOOL)writeToURL:(NSURL *)url error:(NSError **)error {
60  @synchronized (_buffer) {
61    return [_buffer writeToURL:url
62                    atomically:YES
63                      encoding:NSUTF8StringEncoding
64                         error:error];
65  }
66}
67
68- (NSString *)stringValue {
69  @synchronized (_buffer) {
70    return [NSString stringWithString:_buffer];
71  }
72}
73@end
74