Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Elixxir dApps SDK Swift
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mobile
iOS
Elixxir dApps SDK Swift
Commits
8da55d33
Commit
8da55d33
authored
2 years ago
by
Dariusz Rybicki
Browse files
Options
Downloads
Patches
Plain Diff
Add MessengerLogger
parent
6bb4946a
No related branches found
No related tags found
2 merge requests
!117
Logging improvements
,
!102
Release 1.0.0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Sources/XXMessengerClient/Utils/MessengerLogger.swift
+90
-0
90 additions, 0 deletions
Sources/XXMessengerClient/Utils/MessengerLogger.swift
Tests/XXMessengerClientTests/Utils/MessengerLoggerTests.swift
+71
-0
71 additions, 0 deletions
...s/XXMessengerClientTests/Utils/MessengerLoggerTests.swift
with
161 additions
and
0 deletions
Sources/XXMessengerClient/Utils/MessengerLogger.swift
0 → 100644
+
90
−
0
View file @
8da55d33
import
Foundation
import
Logging
import
XCTestDynamicOverlay
public
struct
MessengerLogger
{
public
struct
Log
:
Equatable
{
public
init
(
level
:
Logger
.
Level
,
message
:
String
)
{
self
.
level
=
level
self
.
message
=
message
}
public
var
level
:
Logger
.
Level
public
var
message
:
String
}
public
var
run
:
(
Log
,
String
,
String
,
UInt
)
->
Void
public
func
callAsFunction
(
_
item
:
Log
,
file
:
String
=
#fileID
,
function
:
String
=
#function
,
line
:
UInt
=
#line
)
{
run
(
item
,
file
,
function
,
line
)
}
}
extension
MessengerLogger
{
public
static
func
live
(
logger
:
Logger
=
Logger
(
label
:
"xx.network.MessengerClient"
)
)
->
MessengerLogger
{
MessengerLogger
{
item
,
file
,
function
,
line
in
logger
.
log
(
level
:
item
.
level
,
.
init
(
stringLiteral
:
item
.
message
),
file
:
file
,
function
:
function
,
line
:
line
)
}
}
}
extension
MessengerLogger
{
public
static
let
unimplemented
=
MessengerLogger
(
run
:
XCTUnimplemented
(
"
\(
Self
.
self
)
"
)
)
}
extension
MessengerLogger
.
Log
{
static
func
parse
(
_
string
:
String
)
->
MessengerLogger
.
Log
{
let
level
:
Logger
.
Level
let
message
:
String
let
pattern
=
#
"^([A-Z]+)(
\
d{4}/
\
d{2}/
\
d{2})?(
\
d{1,2}:
\
d{2}:
\
d{2}
\
.
\
d+)? (.*)"
#
let
regex
=
try!
NSRegularExpression
(
pattern
:
pattern
,
options
:
.
dotMatchesLineSeparators
)
let
stringRange
=
NSRange
(
location
:
0
,
length
:
string
.
utf16
.
count
)
if
let
match
=
regex
.
firstMatch
(
in
:
string
,
range
:
stringRange
)
{
var
groups
:
[
Int
:
String
]
=
[:]
for
rangeIndex
in
1
..<
match
.
numberOfRanges
{
let
nsRange
=
match
.
range
(
at
:
rangeIndex
)
if
!
NSEqualRanges
(
nsRange
,
NSMakeRange
(
NSNotFound
,
0
))
{
let
group
=
(
string
as
NSString
)
.
substring
(
with
:
nsRange
)
groups
[
rangeIndex
]
=
group
}
}
level
=
MessengerLogger
.
Log
.
level
(
form
:
groups
[
1
])
message
=
groups
[
4
]
??
string
}
else
{
level
=
.
notice
message
=
string
}
return
MessengerLogger
.
Log
(
level
:
level
,
message
:
message
)
}
static
func
level
(
form
string
:
String
?)
->
Logger
.
Level
{
switch
string
{
case
"TRACE"
:
return
.
trace
case
"DEBUG"
:
return
.
debug
case
"INFO"
:
return
.
info
case
"WARN"
:
return
.
warning
case
"ERROR"
:
return
.
error
case
"CRITICAL"
:
return
.
critical
case
"FATAL"
:
return
.
critical
default
:
return
.
notice
}
}
}
This diff is collapsed.
Click to expand it.
Tests/XXMessengerClientTests/Utils/MessengerLoggerTests.swift
0 → 100644
+
71
−
0
View file @
8da55d33
import
CustomDump
import
XCTest
@testable
import
XXMessengerClient
final
class
MessengerLoggerTests
:
XCTestCase
{
func
testParsingLog
()
{
XCTAssertNoDifference
(
MessengerLogger
.
Log
.
parse
(
"TRACE Tracing..."
),
MessengerLogger
.
Log
(
level
:
.
trace
,
message
:
"Tracing..."
)
)
XCTAssertNoDifference
(
MessengerLogger
.
Log
.
parse
(
"DEBUG Debugging..."
),
MessengerLogger
.
Log
(
level
:
.
debug
,
message
:
"Debugging..."
)
)
XCTAssertNoDifference
(
MessengerLogger
.
Log
.
parse
(
"INFO Informing..."
),
MessengerLogger
.
Log
(
level
:
.
info
,
message
:
"Informing..."
)
)
XCTAssertNoDifference
(
MessengerLogger
.
Log
.
parse
(
"WARN Warning!"
),
MessengerLogger
.
Log
(
level
:
.
warning
,
message
:
"Warning!"
)
)
XCTAssertNoDifference
(
MessengerLogger
.
Log
.
parse
(
"ERROR Failure!"
),
MessengerLogger
.
Log
(
level
:
.
error
,
message
:
"Failure!"
)
)
XCTAssertNoDifference
(
MessengerLogger
.
Log
.
parse
(
"CRITICAL Critical failure!"
),
MessengerLogger
.
Log
(
level
:
.
critical
,
message
:
"Critical failure!"
)
)
XCTAssertNoDifference
(
MessengerLogger
.
Log
.
parse
(
"FATAL Fatal failure!"
),
MessengerLogger
.
Log
(
level
:
.
critical
,
message
:
"Fatal failure!"
)
)
}
func
testParsingFallbacks
()
{
XCTAssertNoDifference
(
MessengerLogger
.
Log
.
parse
(
"1234 Wrongly formatted"
),
MessengerLogger
.
Log
(
level
:
.
notice
,
message
:
"1234 Wrongly formatted"
)
)
}
func
testParsingStripsDateTime
()
{
XCTAssertNoDifference
(
MessengerLogger
.
Log
.
parse
(
"INFO 2022/10/04 Informing..."
),
MessengerLogger
.
Log
(
level
:
.
info
,
message
:
"Informing..."
)
)
XCTAssertNoDifference
(
MessengerLogger
.
Log
.
parse
(
"INFO 23:36:55.755390 Informing..."
),
MessengerLogger
.
Log
(
level
:
.
info
,
message
:
"Informing..."
)
)
XCTAssertNoDifference
(
MessengerLogger
.
Log
.
parse
(
"INFO 2022/10/04 23:36:55.755390 Informing..."
),
MessengerLogger
.
Log
(
level
:
.
info
,
message
:
"Informing..."
)
)
}
func
testParsingMultilineMessage
()
{
XCTAssertNoDifference
(
MessengerLogger
.
Log
.
parse
(
"""
ERROR 2022/10/04 23:51:15.021658 First line
Second line
"""
),
MessengerLogger
.
Log
(
level
:
.
error
,
message
:
"""
First line
Second line
"""
)
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment