OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #if defined(WEBRTC_IOS) | 11 #if defined(WEBRTC_IOS) |
12 | 12 |
13 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
14 #error "This file requires ARC support." | |
15 #endif | |
16 | |
17 #import <Foundation/Foundation.h> | 13 #import <Foundation/Foundation.h> |
18 #include <string.h> | 14 #include <pthread.h> |
19 | 15 |
20 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
21 #include "webrtc/typedefs.h" | 17 #include "webrtc/modules/utility/interface/helpers_ios.h" |
22 | 18 |
23 namespace webrtc { | 19 namespace webrtc { |
24 namespace test { | 20 namespace ios { |
25 | 21 |
26 // TODO(henrika): move to shared location. | 22 // TODO(henrika): move to shared location. |
27 // See https://code.google.com/p/webrtc/issues/detail?id=4773 for details. | 23 // See https://code.google.com/p/webrtc/issues/detail?id=4773 for details. |
28 NSString* NSStringFromStdString(const std::string& stdString) { | 24 NSString* NSStringFromStdString(const std::string& stdString) { |
29 // std::string may contain null termination character so we construct | 25 // std::string may contain null termination character so we construct |
30 // using length. | 26 // using length. |
31 return [[NSString alloc] initWithBytes:stdString.data() | 27 return [[NSString alloc] initWithBytes:stdString.data() |
32 length:stdString.length() | 28 length:stdString.length() |
33 encoding:NSUTF8StringEncoding]; | 29 encoding:NSUTF8StringEncoding]; |
34 } | 30 } |
35 | 31 |
36 std::string StdStringFromNSString(NSString* nsString) { | 32 std::string StdStringFromNSString(NSString* nsString) { |
37 NSData* charData = [nsString dataUsingEncoding:NSUTF8StringEncoding]; | 33 NSData* charData = [nsString dataUsingEncoding:NSUTF8StringEncoding]; |
38 return std::string(reinterpret_cast<const char*>([charData bytes]), | 34 return std::string(reinterpret_cast<const char*>([charData bytes]), |
39 [charData length]); | 35 [charData length]); |
40 } | 36 } |
41 | 37 |
42 // For iOS, resource files are added to the application bundle in the root | 38 void CheckAndLogError(BOOL success, NSError* error) { |
43 // and not in separate folders as is the case for other platforms. This method | 39 if (!success) { |
44 // therefore removes any prepended folders and uses only the actual file name. | 40 NSLog(@"Error: %ld, %@", (long)error.code, error.localizedDescription); |
45 std::string IOSResourcePath(std::string name, std::string extension) { | |
46 @autoreleasepool { | |
47 NSString* path = NSStringFromStdString(name); | |
48 NSString* fileName = path.lastPathComponent; | |
49 NSString* fileType = NSStringFromStdString(extension); | |
50 // Get full pathname for the resource identified by the name and extension. | |
51 NSString* pathString = [[NSBundle mainBundle] pathForResource:fileName | |
52 ofType:fileType]; | |
53 return StdStringFromNSString(pathString); | |
54 } | 41 } |
55 } | 42 } |
56 | 43 |
57 } // namespace test | 44 std::string GetCurrentThreadDescription() { |
| 45 NSString* name = [NSString stringWithFormat:@"%@", [NSThread currentThread]]; |
| 46 return StdStringFromNSString(name); |
| 47 } |
| 48 |
| 49 std::string GetThreadId() { |
| 50 char buf[21]; // Big enough to hold a kuint64max plus terminating NULL. |
| 51 pid_t thread_id = pthread_mach_thread_np(pthread_self()); |
| 52 CHECK_LT(snprintf(buf, sizeof(buf), "%i", thread_id), |
| 53 static_cast<int>(sizeof(buf))) |
| 54 << "Thread id is bigger than uint64??"; |
| 55 return std::string(buf); |
| 56 } |
| 57 |
| 58 std::string GetThreadInfo() { |
| 59 return "@[tid=" + GetThreadId() + "]"; |
| 60 } |
| 61 |
| 62 } // namespace ios |
58 } // namespace webrtc | 63 } // namespace webrtc |
59 | 64 |
60 #endif // defined(WEBRTC_IOS) | 65 #endif // defined(WEBRTC_IOS) |
OLD | NEW |