Skip to content

Commit 5fcee82

Browse files
committed
Project layout updates
1 parent 57f36ee commit 5fcee82

File tree

9 files changed

+63
-38
lines changed

9 files changed

+63
-38
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: swift-android-oslog ci
1+
name: swift-android-native ci
22
on:
33
push:
44
branches: [ main ]

Package.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import PackageDescription
33

44
let package = Package(
5-
name: "swift-android-oslog",
5+
name: "swift-android-native",
66
products: [
7-
.library(name: "AndroidOSLog", targets: ["AndroidOSLog"])
7+
.library(name: "AndroidLogging", targets: ["AndroidLogging"])
88
],
99
targets: [
10-
.systemLibrary(name: "CAndroidLog"),
11-
.target(name: "AndroidOSLog", dependencies: [.target(name: "CAndroidLog", condition: .when(platforms: [.android]))]),
12-
.testTarget(name: "AndroidOSLogTests", dependencies: [.target(name: "AndroidOSLog")])
10+
.systemLibrary(name: "AndroidNDK"),
11+
.target(name: "AndroidLogging", dependencies: [.target(name: "AndroidNDK", condition: .when(platforms: [.android]))]),
12+
.testTarget(name: "AndroidLoggingTests", dependencies: [.target(name: "AndroidLogging")])
1313
]
1414
)

README.md

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,63 @@
1-
# OSLog for Android
1+
# Android Native
22

3-
This package provides a Logger API for native Swift on Android compatible with
4-
the [OSLog Logger](https://developer.apple.com/documentation/os/logger)
5-
for Darwin platforms.
3+
This package provides a Swift interface to various
4+
Android [NDK APIs](https://developer.android.com/ndk/reference).
65

7-
# Requirements
6+
## Requirements
87

9-
- Swift 5.9
10-
- [Swift Android SDK](https://github.com/finagolfin/swift-android-sdk)
8+
- Swift 5.9
9+
- [Swift Android SDK](https://github.com/finagolfin/swift-android-sdk)
1110

12-
# Installation
11+
## Installation
1312

14-
## Swift Package Manager
13+
### Swift Package Manager
1514

1615
Add the package to your `Package.swift` file:
1716

1817
```swift
1918
dependencies: [
20-
.package(url: "https://github.com/skiptools/swift-android-oslog.git", from: "1.0.0")
19+
.package(url: "https://github.com/skiptools/swift-android-logging.git", from: "1.0.0")
2120
]
2221
```
2322

24-
Then add it as a conditional dependency for any targets that need it:
23+
# AndroidLogging
24+
25+
This module provides a Logger API for native Swift on Android compatible with
26+
the [OSLog Logger](https://developer.apple.com/documentation/os/logger)
27+
for Darwin platforms.
28+
29+
## Installation
30+
31+
### Swift Package Manager
32+
33+
Add the `AndroidLogging` module as a conditional dependency for any targets that need it:
2534

2635
```swift
2736
.target(name: "MyTarget", dependencies: [
28-
.product(name: "AndroidOSLog", package: "swift-android-oslog", condition: .when(platforms: [.android]))
37+
.product(name: "AndroidLogging", package: "swift-android-logging", condition: .when(platforms: [.android]))
2938
])
3039
```
3140

32-
# Usage
41+
## Usage
3342

34-
## Example
43+
### Example
3544

36-
This example will use the system `OSLog` on Darwin platforms and `AndroidOSLog` on Android
45+
This example will use the system `OSLog` on Darwin platforms and `AndroidLogging` on Android
3746
to provide common logging functionality across operating systems:
3847

3948
```swift
4049
#if canImport(Darwin)
4150
import OSLog
4251
#elseif os(Android)
43-
import AndroidOSLog
52+
import AndroidLogging
4453
#endif
4554

4655
let logger = Logger(subsystem: "Subsystem", category: "Category")
4756

4857
logger.info("Hello Android logcat!")
4958
```
5059

51-
## Viewing Logs
60+
### Viewing Logs
5261

5362
Android log messages for connected devices and emulators
5463
can be viewed from the Terminal using the
@@ -64,8 +73,21 @@ $ adb logcat '*:S' 'Subsystem/Category:I'
6473
[Android Studio](https://developer.android.com/studio/debug/logcat) provides the ability to
6574
graphically view and filter log messages, as do most other Android IDEs.
6675

67-
# License
76+
## Internals
77+
78+
### Implementation details
79+
80+
The `Logger` functions will forward messages to the NDK
81+
[__android_log_write](https://developer.android.com/ndk/reference/group/logging#group___logging_1ga32a7173b092ec978b50490bd12ee523b)
82+
function.
83+
84+
### Limitations
85+
86+
- `OSLogMessage` is simply a typealias to `Swift.String`, and does not implement any of the [redaction features](https://developer.apple.com/documentation/os/logging/generating_log_messages_from_your_code#3665948) of the Darwin version.
87+
-
88+
89+
## License
6890

69-
AndroidOSLog is licensed under the Apache 2.0 license with a runtime library exception,
91+
Licensed under the Apache 2.0 license with a runtime library exception,
7092
meaning you do not need to attribute the project in your application.
7193
See the [LICENSE](LICENSE) file for details.

Sources/AndroidOSLog/OSLog.swift renamed to Sources/AndroidLogging/AndroidLogging.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#if os(Android)
22
import Android
3-
import CAndroidLog
3+
import AndroidNDK
44

55
public typealias OSLogMessage = String
66

@@ -69,10 +69,13 @@ public struct Logger : @unchecked Sendable {
6969
androidLog(priority: priority, message: message)
7070
}
7171

72+
private var logTag: String {
73+
subsystem.isEmpty && category.isEmpty ? "" : (subsystem + "/" + category)
74+
}
75+
7276
private func androidLog(priority: android_LogPriority, message: OSLogMessage) {
73-
let tag = subsystem.isEmpty && category.isEmpty ? "" : (subsystem + "/" + category)
74-
//swift_android_log(priority, tagPtr, messagePtr)
75-
__android_log_write(Int32(priority.rawValue), tag, message)
77+
//swift_android_log(priority, logTag, messagePtr)
78+
__android_log_write(Int32(priority.rawValue), logTag, message)
7679
}
7780
}
7881

@@ -101,7 +104,7 @@ extension OSLogType {
101104
public static let fault: OSLogType = OSLogType(0x11)
102105
}
103106

104-
#elseif canImport(OSLog)
107+
#elseif canImport(os)
105108
@_exported import OSLog
106109
#endif
107110

Sources/AndroidNDK/module.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module AndroidNDK [system] {
2+
link "log"
3+
export *
4+
}

Sources/CAndroidLog/coslog.c renamed to Sources/AndroidNDK/ndk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#if defined(__ANDROID__)
2-
#include "coslog.h"
2+
#include "ndk.h"
33

44
// https://android.googlesource.com/platform/system/core/+/jb-dev/include/android/log.h
55
void swift_android_log(android_LogPriority level, const char *tag, const char *msg) {

Sources/CAndroidLog/module.modulemap

Lines changed: 0 additions & 4 deletions
This file was deleted.

Tests/AndroidOSLogTests/AndroidOSLogTests.swift renamed to Tests/AndroidLoggingTests/AndroidLoggingTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import XCTest
2-
import AndroidOSLog // note: on non-android platforms, this will just export the system OSLog
2+
import AndroidLogging // note: on non-android platforms, this will just export the system OSLog
33

44
@available(iOS 14.0, *)
5-
class AndroidOSLogTests : XCTestCase {
5+
class AndroidLoggingTests : XCTestCase {
66
public func testOSLogAPI() {
77
let emptyLogger = Logger()
88
emptyLogger.info("Android logger test: empty message")
99

10-
let logger = Logger(subsystem: "AndroidOSLog", category: "test")
10+
let logger = Logger(subsystem: "AndroidLoggingTests", category: "test")
1111

1212
logger.log("Android logger test: LOG message")
1313

0 commit comments

Comments
 (0)