Skip to content

Commit

Permalink
fix: more timezone and precision fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cvandeplas committed Nov 26, 2024
1 parent 440febf commit e4a5f2c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 24 deletions.
7 changes: 5 additions & 2 deletions src/sysdiagnose/parsers/crashlogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ def parse_ips_file(path: str) -> list | dict:
lines = f.readlines()

result['report'] = CrashLogsParser.process_ips_lines(lines)

timestamp = datetime.strptime(result['timestamp'], '%Y-%m-%d %H:%M:%S.%f %z')
try:
# captureTime is more precise than the timestamp
timestamp = datetime.strptime(result['report']['captureTime'], '%Y-%m-%d %H:%M:%S.%f %z')
except Exception:
timestamp = datetime.strptime(result['timestamp'], '%Y-%m-%d %H:%M:%S.%f %z')
result['timestamp_orig'] = result['timestamp']
result['datetime'] = timestamp.isoformat(timespec='microseconds')
result['timestamp'] = timestamp.timestamp()
Expand Down
4 changes: 2 additions & 2 deletions src/sysdiagnose/parsers/lockdownd.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def execute(self) -> list | dict:
return result
return []

def extract_from_list(self, lines: list) -> list:
def extract_from_list(lines: list, tzinfo) -> list:
result = []
i = 0
while i < len(lines):
Expand All @@ -53,7 +53,7 @@ def extract_from_list(self, lines: list) -> list:
# process rebuild current_line
match = re.match(r'(^.{24}) pid=(\d+) ([^:]+): (.*)$', current_line, re.DOTALL)
timestamp = datetime.strptime(match.group(1), '%m/%d/%y %H:%M:%S.%f')
timestamp = timestamp.replace(tzinfo=self.sysdiagnose_creation_datetime.tzinfo)
timestamp = timestamp.replace(tzinfo=tzinfo)

# LATER parse the json blob that can sometimes be in the message
item = {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_parsers_containermanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_parsecontainermanager(self):
self.assertTrue('loglevel' in item)
self.assertTrue('hexID' in item)
self.assertTrue('loglevel' in item)
self.assertTrue('msg' in item)
self.assertTrue('message' in item)


if __name__ == '__main__':
Expand Down
37 changes: 20 additions & 17 deletions tests/test_parsers_lockdownd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
from tests import SysdiagnoseTestCase
import unittest
import os
from datetime import timezone, timedelta


class TestParsersLockdownd(SysdiagnoseTestCase):

tzinfo = timezone(timedelta(hours=1))

def test_parse_lockdownd(self):
for case_id, case in self.sd.cases().items():

Expand All @@ -22,61 +25,61 @@ def test_parse_lockdownd(self):

def test_parse_lockdownd_simple(self):
input = [
'05/24/23 12:55:38.410307 pid=76 mglog: libMobileGestalt utility.c:70: Could not open /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.mobilegestaltcache/Library/Caches/com.apple.MobileGestalt.plist: No such file or directory',
'05/24/23 12:55:38.453538 pid=76 data_ark_set_block_invoke: dirtied by changing -HasSiDP',
'05/24/23 12:55:45.978543 pid=76 lockstart_local: Build version: 19H349'
'05/24/23 13:55:38.410307 pid=76 mglog: libMobileGestalt utility.c:70: Could not open /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.mobilegestaltcache/Library/Caches/com.apple.MobileGestalt.plist: No such file or directory',
'05/24/23 13:55:38.453538 pid=76 data_ark_set_block_invoke: dirtied by changing -HasSiDP',
'05/24/23 13:55:45.978543 pid=76 lockstart_local: Build version: 19H349'
]
expected_result = [
{
'timestamp': 1684932938.410307,
'datetime': '2023-05-24T12:55:38.410307+00:00',
'datetime': '2023-05-24T13:55:38.410307+01:00',
'pid': 76,
'event_type': 'mglog',
'msg': 'libMobileGestalt utility.c:70: Could not open /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.mobilegestaltcache/Library/Caches/com.apple.MobileGestalt.plist: No such file or directory'
'message': 'libMobileGestalt utility.c:70: Could not open /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.mobilegestaltcache/Library/Caches/com.apple.MobileGestalt.plist: No such file or directory'
},
{
'timestamp': 1684932938.453538,
'datetime': '2023-05-24T12:55:38.453538+00:00',
'datetime': '2023-05-24T13:55:38.453538+01:00',
'pid': 76,
'event_type': 'data_ark_set_block_invoke',
'msg': 'dirtied by changing -HasSiDP'
'message': 'dirtied by changing -HasSiDP'
},
{
'timestamp': 1684932945.978543,
'datetime': '2023-05-24T12:55:45.978543+00:00',
'datetime': '2023-05-24T13:55:45.978543+01:00',
'pid': 76,
'event_type': 'lockstart_local',
'msg': 'Build version: 19H349'
'message': 'Build version: 19H349'
}
]
result = LockdowndParser.extract_from_list(input)
result = LockdowndParser.extract_from_list(input, tzinfo=self.tzinfo)
self.maxDiff = None
self.assertEqual(result, expected_result)

def test_parse_lockdownd_multiline(self):
input = [
'05/24/23 12:55:45.978543 pid=76 lockstart_local: Build version: 19H349',
'05/24/23 13:55:45.978543 pid=76 lockstart_local: Build version: 19H349',
'hello world',
'05/24/23 12:55:45.978543 pid=76 lockstart_local: Build version: 19H349'
'05/24/23 13:55:45.978543 pid=76 lockstart_local: Build version: 19H349'
]

expected_result = [
{
'timestamp': 1684932945.978543,
'datetime': '2023-05-24T12:55:45.978543+00:00',
'datetime': '2023-05-24T13:55:45.978543+01:00',
'pid': 76,
'event_type': 'lockstart_local',
'msg': 'Build version: 19H349hello world'
'message': 'Build version: 19H349hello world'
},
{
'timestamp': 1684932945.978543,
'datetime': '2023-05-24T12:55:45.978543+00:00',
'datetime': '2023-05-24T13:55:45.978543+01:00',
'pid': 76,
'event_type': 'lockstart_local',
'msg': 'Build version: 19H349'
'message': 'Build version: 19H349'
}
]
result = LockdowndParser.extract_from_list(input)
result = LockdowndParser.extract_from_list(input, tzinfo=self.tzinfo)
self.maxDiff = None
self.assertEqual(result, expected_result)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_parsers_mobileactivation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_mobileactivation(self):
self.assertTrue('build_version' in item)
self.assertTrue('internal_build' in item)
else:
self.assertTrue('msg' in item)
self.assertTrue('message' in item)
# self.assertTrue('event_type' in item) # not all logs have event_type


Expand Down
2 changes: 1 addition & 1 deletion tests/test_parsers_mobileinstallation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_mobileinstallation(self):
self.assertTrue('timestamp' in item)
self.assertTrue('loglevel' in item)
self.assertTrue('hexID' in item)
self.assertTrue('msg' in item)
self.assertTrue('message' in item)
# self.assertTrue('event_type' in item) # not all logs have event_type


Expand Down

0 comments on commit e4a5f2c

Please sign in to comment.