Skip to content

Commit da01edd

Browse files
committed
initial
0 parents  commit da01edd

17 files changed

+471
-0
lines changed

.github/workflows/run-checks.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Run checks
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
10+
run-checks:
11+
# runs-on: macOS-latest
12+
runs-on: self-hosted
13+
steps:
14+
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 1
19+
20+
- name: Install Dependencies
21+
run: |
22+
brew install mint
23+
mint install NickLockwood/[email protected] --no-link
24+
25+
- name: run script
26+
run: ./scripts/run-checks.sh

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc

.swift-format

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"version": 1,
3+
"lineLength": 80,
4+
"maximumBlankLines": 1,
5+
"fileScopedDeclarationPrivacy": {
6+
"accessLevel": "private"
7+
},
8+
"tabWidth": 4,
9+
"indentation": {
10+
"spaces": 4
11+
},
12+
"indentConditionalCompilationBlocks": false,
13+
"indentSwitchCaseLabels": false,
14+
"lineBreakAroundMultilineExpressionChainComponents": true,
15+
"lineBreakBeforeControlFlowKeywords": true,
16+
"lineBreakBeforeEachArgument": true,
17+
"lineBreakBeforeEachGenericRequirement": true,
18+
"prioritizeKeepingFunctionOutputTogether": false,
19+
"respectsExistingLineBreaks": true,
20+
"spacesAroundRangeFormationOperators": false,
21+
"multiElementCollectionTrailingCommas": true,
22+
"rules": {
23+
"AllPublicDeclarationsHaveDocumentation": false,
24+
"AlwaysUseLiteralForEmptyCollectionInit": true,
25+
"AlwaysUseLowerCamelCase": true,
26+
"AmbiguousTrailingClosureOverload": true,
27+
"BeginDocumentationCommentWithOneLineSummary": true,
28+
"DoNotUseSemicolons": true,
29+
"DontRepeatTypeInStaticProperties": true,
30+
"FileScopedDeclarationPrivacy": true,
31+
"FullyIndirectEnum": true,
32+
"GroupNumericLiterals": true,
33+
"IdentifiersMustBeASCII": true,
34+
"NeverForceUnwrap": false,
35+
"NeverUseForceTry": true,
36+
"NeverUseImplicitlyUnwrappedOptionals": true,
37+
"NoAccessLevelOnExtensionDeclaration": true,
38+
"NoAssignmentInExpressions": true,
39+
"NoBlockComments": true,
40+
"NoCasesWithOnlyFallthrough": true,
41+
"NoEmptyTrailingClosureParentheses": true,
42+
"NoLabelsInCasePatterns": true,
43+
"NoLeadingUnderscores": true,
44+
"NoParensAroundConditions": true,
45+
"NoPlaygroundLiterals": true,
46+
"NoVoidReturnOnFunctionSignature": true,
47+
"OmitExplicitReturns": true,
48+
"OneCasePerLine": true,
49+
"OneVariableDeclarationPerLine": true,
50+
"OnlyOneTrailingClosureArgument": true,
51+
"OrderedImports": true,
52+
"ReplaceForEachWithForLoop": true,
53+
"ReturnVoidInsteadOfEmptyTuple": true,
54+
"TypeNamesShouldBeCapitalized": true,
55+
"UseEarlyExits": true,
56+
"UseLetInEveryBoundCaseVariable": true,
57+
"UseShorthandTypeNames": true,
58+
"UseSingleLinePropertyGetter": true,
59+
"UseSynthesizedInitializer": true,
60+
"UseTripleSlashForDocumentationComments": true,
61+
"UseWhereClausesInForLoops": true,
62+
"ValidateDocumentationComments": true
63+
}
64+
}

LICENSE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Binary Birds Ltd.
4+
5+
Permission is hereby granted, free of charge, to any person
6+
obtaining a copy of this software and associated documentation
7+
files (the "Software"), to deal in the Software without
8+
restriction, including without limitation the rights to use,
9+
copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the
11+
Software is furnished to do so, subject to the following
12+
conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24+
OTHER DEALINGS IN THE SOFTWARE.

Makefile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
SHELL=/bin/bash
2+
3+
build:
4+
swift build
5+
6+
release:
7+
swift build -c release
8+
9+
test:
10+
swift test --parallel
11+
12+
test-with-coverage:
13+
swift test --parallel --enable-code-coverage
14+
15+
clean:
16+
rm -rf .build
17+
18+
check:
19+
./scripts/run-checks.sh
20+
21+
format:
22+
./scripts/run-swift-format.sh --fix

Package.resolved

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// swift-tools-version:5.9
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "swift-log-env",
6+
platforms: [
7+
.macOS(.v14),
8+
.iOS(.v17),
9+
.tvOS(.v17),
10+
.watchOS(.v10),
11+
.visionOS(.v1),
12+
],
13+
products: [
14+
.library(name: "SwiftLogEnv", targets: ["SwiftLogEnv"])
15+
],
16+
dependencies: [
17+
.package(url: "https://github.com/apple/swift-log", from: "1.0.0")
18+
],
19+
targets: [
20+
.target(
21+
name: "SwiftLogEnv",
22+
dependencies: [
23+
.product(name: "Logging", package: "swift-log")
24+
]
25+
)
26+
]
27+
)

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SwiftLogEnv
2+
3+
Create custom loggers using a subsystem identifier and a log level.
4+
5+
## Install
6+
7+
Add the repository as a dependency:
8+
9+
```swift
10+
.package(url: "https://github.com/binarybirds/swift-log-env", from: "1.0.0"),
11+
```
12+
13+
Add `SwiftLogEnv` to the target dependencies:
14+
15+
```swift
16+
.product(name: "SwiftLogEnv", package: "swift-log-env"),
17+
```
18+
19+
Update the packages and you are ready.
20+
21+
## Usage example
22+
23+
Basic example
24+
25+
```swift
26+
import SwiftLogEnv
27+
28+
let libLogger = Logger.subsystem("my-lib", .notice)
29+
let appLogger = Logger.subsystem("my-app", .notice)
30+
31+
// LOG_LEVEL=info MY_LIB_LOG_LEVEL=trace swift run MyApp
32+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import Foundation
2+
import Logging
3+
4+
extension Logger {
5+
6+
///
7+
/// Creates a custom logger using a subsystem identifier and a log level.
8+
///
9+
/// This method allows users to specify the log level for each subsystem, baded on environment variables:
10+
/// - Set the `LOG_LEVEL` environment variable to override all subsystem log levels.
11+
/// - Set a custom `{id}_LOG_LEVEL` variable to override the log level for a given subsystem.
12+
///
13+
/// - Note: When looking for environmental variables, the provided logger id is automatically lowercased, dashes (`-`) are always replaced with underscores (`_`) and a `_LOG_LEVEL` suffix is added to the id.
14+
///
15+
/// **Example**
16+
///
17+
/// ```swift
18+
/// let libLogger = Logger.subsystem("my-lib", .notice)
19+
/// let appLogger = Logger.subsystem("my-app", .notice)
20+
/// ```
21+
/// **Environmental variables**
22+
/// ```sh
23+
/// LOG_LEVEL=info MY_LIB_LOG_LEVEL=trace swift run MyApp
24+
/// ```
25+
/// **Expected log levels**
26+
/// - libLogger = `.trace`
27+
/// - appLogger = `.info`
28+
///
29+
/// - Parameters:
30+
/// - id: The identifier of the logger subsystem, used as the label for the logger.
31+
/// - level: Custom log level for the logger (default value: `.info`).
32+
/// - Returns: A `Logger` instance
33+
public static func subsystem(
34+
_ id: String,
35+
_ level: Logger.Level = .info
36+
) -> Logger {
37+
var logger = Logger(label: id)
38+
logger.logLevel = level
39+
40+
let env = ProcessInfo.processInfo.environment
41+
if let rawLevel = env["LOG_LEVEL"]?.lowercased(),
42+
let level = Logger.Level(rawValue: rawLevel)
43+
{
44+
logger.logLevel = level
45+
}
46+
47+
let envKey =
48+
id
49+
.appending("-log-level")
50+
.replacingOccurrences(of: "-", with: "_")
51+
.uppercased()
52+
if let rawLevel = env[envKey]?.lowercased(),
53+
let level = Logger.Level(rawValue: rawLevel)
54+
{
55+
logger.logLevel = level
56+
}
57+
return logger
58+
}
59+
}

scripts/check-broken-symlinks.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
log() { printf -- "** %s\n" "$*" >&2; }
5+
error() { printf -- "** ERROR: %s\n" "$*" >&2; }
6+
fatal() { error "$@"; exit 1; }
7+
8+
CURRENT_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
9+
REPO_ROOT="$(git -C "${CURRENT_SCRIPT_DIR}" rev-parse --show-toplevel)"
10+
11+
log "Checking for broken symlinks..."
12+
NUM_BROKEN_SYMLINKS=0
13+
while read -r -d '' file; do
14+
if ! test -e "${REPO_ROOT}/${file}"; then
15+
error "Broken symlink: ${file}"
16+
((NUM_BROKEN_SYMLINKS++))
17+
fi
18+
done < <(git -C "${REPO_ROOT}" ls-files -z)
19+
20+
if [ "${NUM_BROKEN_SYMLINKS}" -gt 0 ]; then
21+
fatal "❌ Found ${NUM_BROKEN_SYMLINKS} symlinks."
22+
fi
23+
24+
log "✅ Found 0 symlinks."

0 commit comments

Comments
 (0)