Skip to content

Commit b3e91ab

Browse files
chore: Added DebugLoggerLevel to DebugLogger delegate (#4457)
Resolves #4382 - #4382 #skip-changelog
1 parent fe3a33b commit b3e91ab

14 files changed

+97
-48
lines changed

src/Sentry.Android.AssemblyReader/AndroidAssemblyReaderFactory.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,29 @@ public static class AndroidAssemblyReaderFactory
1717
/// <returns>The reader</returns>
1818
public static IAndroidAssemblyReader Open(string apkPath, IList<string> supportedAbis, DebugLogger? logger = null)
1919
{
20-
logger?.Invoke("Opening APK: {0}", apkPath);
20+
logger?.Invoke(DebugLoggerLevel.Debug, "Opening APK: {0}", apkPath);
2121

2222
#if NET9_0
23-
logger?.Invoke("Reading files using V2 APK layout.");
23+
logger?.Invoke(DebugLoggerLevel.Debug, "Reading files using V2 APK layout.");
2424
if (AndroidAssemblyStoreReaderV2.TryReadStore(apkPath, supportedAbis, logger, out var readerV2))
2525
{
26-
logger?.Invoke("APK uses AssemblyStore V2");
26+
logger?.Invoke(DebugLoggerLevel.Debug, "APK uses AssemblyStore V2");
2727
return readerV2;
2828
}
2929

30-
logger?.Invoke("APK doesn't use AssemblyStore");
30+
logger?.Invoke(DebugLoggerLevel.Debug, "APK doesn't use AssemblyStore");
3131
return new AndroidAssemblyDirectoryReaderV2(apkPath, supportedAbis, logger);
3232
#else
33-
logger?.Invoke("Reading files using V1 APK layout.");
33+
logger?.Invoke(DebugLoggerLevel.Debug, "Reading files using V1 APK layout.");
3434

3535
var zipArchive = ZipFile.OpenRead(apkPath);
3636
if (zipArchive.GetEntry("assemblies/assemblies.manifest") is not null)
3737
{
38-
logger?.Invoke("APK uses AssemblyStore V1");
38+
logger?.Invoke(DebugLoggerLevel.Debug, "APK uses AssemblyStore V1");
3939
return new AndroidAssemblyStoreReaderV1(zipArchive, supportedAbis, logger);
4040
}
4141

42-
logger?.Invoke("APK doesn't use AssemblyStore");
42+
logger?.Invoke(DebugLoggerLevel.Debug, "APK doesn't use AssemblyStore");
4343
return new AndroidAssemblyDirectoryReaderV1(zipArchive, supportedAbis, logger);
4444
#endif
4545
}

src/Sentry.Android.AssemblyReader/ArchiveUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal static MemoryStream Extract(this ZipArchiveEntry zipEntry)
4343
Debug.Assert(inputStream.Position == payloadOffset);
4444
var inputLength = (int)(inputStream.Length - payloadOffset);
4545

46-
logger?.Invoke("Decompressing assembly ({0} bytes uncompressed) using LZ4", decompressedLength);
46+
logger?.Invoke(DebugLoggerLevel.Debug, "Decompressing assembly ({0} bytes uncompressed) using LZ4", decompressedLength);
4747

4848
var outputStream = new MemoryStream(decompressedLength);
4949

src/Sentry.Android.AssemblyReader/DebugLogger.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace Sentry.Android.AssemblyReader;
33
/// <summary>
44
/// Writes a log message for debugging.
55
/// </summary>
6+
/// <param name="level">The debug log level.</param>
67
/// <param name="message">The message string to write.</param>
78
/// <param name="args">Arguments for the formatted message string.</param>
8-
public delegate void DebugLogger(string message, params object?[] args);
9+
public delegate void DebugLogger(DebugLoggerLevel level, string message, params object?[] args);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace Sentry.Android.AssemblyReader;
2+
3+
/// <summary>
4+
/// Represents the level of debug logging.
5+
/// </summary>
6+
public enum DebugLoggerLevel : short
7+
{
8+
/// <summary>
9+
/// Debug level logging.
10+
/// </summary>
11+
Debug,
12+
13+
/// <summary>
14+
/// Information level logging.
15+
/// </summary>
16+
Info,
17+
18+
/// <summary>
19+
/// Warning level logging.
20+
/// </summary>
21+
Warning,
22+
23+
/// <summary>
24+
/// Error level logging.
25+
/// </summary>
26+
Error,
27+
28+
/// <summary>
29+
/// Fatal level logging.
30+
/// </summary>
31+
Fatal
32+
}

src/Sentry.Android.AssemblyReader/V1/AndroidAssemblyDirectoryReaderV1.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public AndroidAssemblyDirectoryReaderV1(ZipArchive zip, IList<string> supportedA
1818
var zipEntry = FindAssembly(name);
1919
if (zipEntry is null)
2020
{
21-
Logger?.Invoke("Couldn't find assembly {0} in the APK", name);
21+
Logger?.Invoke(DebugLoggerLevel.Debug, "Couldn't find assembly {0} in the APK", name);
2222
return null;
2323
}
2424

25-
Logger?.Invoke("Resolved assembly {0} in the APK at {1}", name, zipEntry.FullName);
25+
Logger?.Invoke(DebugLoggerLevel.Debug, "Resolved assembly {0} in the APK at {1}", name, zipEntry.FullName);
2626

2727
// We need a seekable stream for the PEReader (or even to check whether the DLL is compressed), so make a copy.
2828
var memStream = zipEntry.Extract();

src/Sentry.Android.AssemblyReader/V1/AndroidAssemblyStoreReaderV1.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ public AndroidAssemblyStoreReaderV1(ZipArchive zip, IList<string> supportedAbis,
1616
var assembly = TryFindAssembly(name);
1717
if (assembly is null)
1818
{
19-
Logger?.Invoke("Couldn't find assembly {0} in the APK AssemblyStore", name);
19+
Logger?.Invoke(DebugLoggerLevel.Debug, "Couldn't find assembly {0} in the APK AssemblyStore", name);
2020
return null;
2121
}
2222

23-
Logger?.Invoke("Resolved assembly {0} in the APK {1} AssemblyStore", name, assembly.Store.Arch);
23+
Logger?.Invoke(DebugLoggerLevel.Debug, "Resolved assembly {0} in the APK {1} AssemblyStore", name, assembly.Store.Arch);
2424

2525
var stream = assembly.GetImage();
2626
if (stream is null)
2727
{
28-
Logger?.Invoke("Couldn't access assembly {0} image stream", name);
28+
Logger?.Invoke(DebugLoggerLevel.Debug, "Couldn't access assembly {0} image stream", name);
2929
return null;
3030
}
3131

@@ -119,26 +119,26 @@ private void ProcessStores()
119119
assembly.Hash64 = he.Hash;
120120
if (assembly.RuntimeIndex != he.MappingIndex)
121121
{
122-
_logger?.Invoke(
122+
_logger?.Invoke(DebugLoggerLevel.Debug,
123123
$"assembly with hashes 0x{assembly.Hash32} and 0x{assembly.Hash64} has a different 32-bit runtime index ({assembly.RuntimeIndex}) than the 64-bit runtime index({he.MappingIndex})");
124124
}
125125

126126
if (_manifest.EntriesByHash64.TryGetValue(assembly.Hash64, out var me))
127127
{
128128
if (string.IsNullOrEmpty(assembly.Name))
129129
{
130-
_logger?.Invoke(
130+
_logger?.Invoke(DebugLoggerLevel.Debug,
131131
$"32-bit hash 0x{assembly.Hash32:x} did not match any assembly name in the manifest");
132132
assembly.Name = me.Name;
133133
if (string.IsNullOrEmpty(assembly.Name))
134134
{
135-
_logger?.Invoke(
135+
_logger?.Invoke(DebugLoggerLevel.Debug,
136136
$"64-bit hash 0x{assembly.Hash64:x} did not match any assembly name in the manifest");
137137
}
138138
}
139139
else if (!string.Equals(assembly.Name, me.Name, StringComparison.Ordinal))
140140
{
141-
_logger?.Invoke(
141+
_logger?.Invoke(DebugLoggerLevel.Debug,
142142
$"32-bit hash 0x{assembly.Hash32:x} maps to assembly name '{assembly.Name}', however 64-bit hash 0x{assembly.Hash64:x} for the same entry matches assembly name '{me.Name}'");
143143
}
144144
}
@@ -176,15 +176,15 @@ void ProcessIndex(List<AssemblyStoreHashEntry> index, string bitness,
176176
{
177177
if (!Stores.TryGetValue(he.StoreId, out var storeList))
178178
{
179-
_logger?.Invoke($"store with id {he.StoreId} not part of the set");
179+
_logger?.Invoke(DebugLoggerLevel.Debug, $"store with id {he.StoreId} not part of the set");
180180
continue;
181181
}
182182

183183
foreach (var store in storeList)
184184
{
185185
if (he.LocalStoreIndex >= (uint)store.Assemblies.Count)
186186
{
187-
_logger?.Invoke(
187+
_logger?.Invoke(DebugLoggerLevel.Debug,
188188
$"{bitness}-bit index entry with hash 0x{he.Hash:x} has invalid store {store.StoreId} index {he.LocalStoreIndex} (maximum allowed is {store.Assemblies.Count})");
189189
continue;
190190
}

src/Sentry.Android.AssemblyReader/V2/AndroidAssemblyDirectoryReaderV2.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public AndroidAssemblyDirectoryReaderV2(string apkPath, IList<string> supportedA
1212
Logger = logger;
1313
foreach (var abi in supportedAbis)
1414
{
15-
logger?.Invoke("Adding {0} to supported architectures for Directory Reader", abi);
15+
logger?.Invoke(DebugLoggerLevel.Debug, "Adding {0} to supported architectures for Directory Reader", abi);
1616
SupportedArchitectures.Add(abi.AbiToDeviceArchitecture());
1717
}
1818
_archiveAssemblyHelper = new ArchiveAssemblyHelper(apkPath, logger, supportedAbis);
@@ -26,21 +26,21 @@ public AndroidAssemblyDirectoryReaderV2(string apkPath, IList<string> supportedA
2626
var stream = File.OpenRead(name);
2727
return new PEReader(stream);
2828
}
29-
Logger?.Invoke("File {0} does not exist in the APK", name);
29+
Logger?.Invoke(DebugLoggerLevel.Debug, "File {0} does not exist in the APK", name);
3030

3131
foreach (var arch in SupportedArchitectures)
3232
{
3333
if (_archiveAssemblyHelper.ReadEntry($"assemblies/{name}", arch) is not { } memStream)
3434
{
35-
Logger?.Invoke("Couldn't find entry {0} in the APK for the {1} architecture", name, arch);
35+
Logger?.Invoke(DebugLoggerLevel.Debug, "Couldn't find entry {0} in the APK for the {1} architecture", name, arch);
3636
continue;
3737
}
3838

39-
Logger?.Invoke("Resolved assembly {0} in the APK", name);
39+
Logger?.Invoke(DebugLoggerLevel.Debug, "Resolved assembly {0} in the APK", name);
4040
return ArchiveUtils.CreatePEReader(name, memStream, Logger);
4141
}
4242

43-
Logger?.Invoke("Couldn't find assembly {0} in the APK", name);
43+
Logger?.Invoke(DebugLoggerLevel.Debug, "Couldn't find assembly {0} in the APK", name);
4444
return null;
4545
}
4646

@@ -96,11 +96,11 @@ public ArchiveAssemblyHelper(string archivePath, DebugLogger? logger, IList<stri
9696
ELFPayloadError.NoPayloadSection => $"Entry '{path}' does not contain the 'payload' section",
9797
_ => $"Unknown ELF payload section error for entry '{path}': {error}"
9898
};
99-
_logger?.Invoke(message);
99+
_logger?.Invoke(DebugLoggerLevel.Debug, message);
100100
}
101101
else
102102
{
103-
_logger?.Invoke($"Extracted content from ELF image '{path}'");
103+
_logger?.Invoke(DebugLoggerLevel.Debug, $"Extracted content from ELF image '{path}'");
104104
}
105105

106106
if (elfPayloadOffset == 0)
@@ -142,14 +142,14 @@ public ArchiveAssemblyHelper(string archivePath, DebugLogger? logger, IList<stri
142142
var potentialEntries = TransformArchiveAssemblyPath(path, arch);
143143
if (potentialEntries == null || potentialEntries.Count == 0)
144144
{
145-
_logger?.Invoke("No potential entries for path '{0}' with arch '{1}'", path, arch);
145+
_logger?.Invoke(DebugLoggerLevel.Debug, "No potential entries for path '{0}' with arch '{1}'", path, arch);
146146
return null;
147147
}
148148

149149
// First we check the base.apk
150150
if (ReadEntryFromApk(_archivePath) is { } baseEntry)
151151
{
152-
_logger?.Invoke("Found entry '{0}' in base archive '{1}'", path, _archivePath);
152+
_logger?.Invoke(DebugLoggerLevel.Debug, "Found entry '{0}' in base archive '{1}'", path, _archivePath);
153153
return baseEntry;
154154
}
155155

@@ -159,7 +159,7 @@ public ArchiveAssemblyHelper(string archivePath, DebugLogger? logger, IList<stri
159159
var splitFilePath = _archivePath.GetArchivePathForAbi(supportedAbi, _logger);
160160
if (!File.Exists(splitFilePath))
161161
{
162-
_logger?.Invoke("No split config detected at: '{0}'", splitFilePath);
162+
_logger?.Invoke(DebugLoggerLevel.Debug, "No split config detected at: '{0}'", splitFilePath);
163163
}
164164
else if (ReadEntryFromApk(splitFilePath) is { } splitEntry)
165165
{
@@ -177,7 +177,7 @@ public ArchiveAssemblyHelper(string archivePath, DebugLogger? logger, IList<stri
177177
{
178178
if (zip.GetEntry(assemblyPath) is not { } entry)
179179
{
180-
_logger?.Invoke("No entry found for path '{0}' in archive '{1}'", assemblyPath, archivePath);
180+
_logger?.Invoke(DebugLoggerLevel.Debug, "No entry found for path '{0}' in archive '{1}'", assemblyPath, archivePath);
181181
continue;
182182
}
183183

src/Sentry.Android.AssemblyReader/V2/AndroidAssemblyStoreReaderV2.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public static bool TryReadStore(string inputFile, IList<string> supportedAbis, D
1919
var (explorers, errorMessage) = AssemblyStoreExplorer.Open(inputFile, logger);
2020
if (explorers is null)
2121
{
22-
logger?.Invoke("Unable to read store information for {0}: {1}", inputFile, errorMessage);
22+
logger?.Invoke(DebugLoggerLevel.Debug, "Unable to read store information for {0}: {1}", inputFile, errorMessage);
2323

2424
// Check for assembly stores in any device specific APKs
2525
foreach (var supportedAbi in supportedAbis)
2626
{
2727
var splitFilePath = inputFile.GetArchivePathForAbi(supportedAbi, logger);
2828
if (!File.Exists(splitFilePath))
2929
{
30-
logger?.Invoke("No split config detected at: '{0}'", splitFilePath);
30+
logger?.Invoke(DebugLoggerLevel.Debug, "No split config detected at: '{0}'", splitFilePath);
3131
continue;
3232
}
3333
(explorers, errorMessage) = AssemblyStoreExplorer.Open(splitFilePath, logger);
@@ -37,7 +37,7 @@ public static bool TryReadStore(string inputFile, IList<string> supportedAbis, D
3737
}
3838
else
3939
{
40-
logger?.Invoke("Unable to read store information for {0}: {1}", splitFilePath, errorMessage);
40+
logger?.Invoke(DebugLoggerLevel.Debug, "Unable to read store information for {0}: {1}", splitFilePath, errorMessage);
4141
}
4242
}
4343
}
@@ -62,7 +62,7 @@ public static bool TryReadStore(string inputFile, IList<string> supportedAbis, D
6262

6363
if (supportedExplorers.Count == 0)
6464
{
65-
logger?.Invoke("Could not find V2 AssemblyStoreExplorer for the supported ABIs: {0}", string.Join(", ", supportedAbis));
65+
logger?.Invoke(DebugLoggerLevel.Debug, "Could not find V2 AssemblyStoreExplorer for the supported ABIs: {0}", string.Join(", ", supportedAbis));
6666
reader = null;
6767
return false;
6868
}
@@ -76,17 +76,17 @@ public static bool TryReadStore(string inputFile, IList<string> supportedAbis, D
7676
var explorerAssembly = TryFindAssembly(name);
7777
if (explorerAssembly is null)
7878
{
79-
_logger?.Invoke("Couldn't find assembly {0} in the APK AssemblyStore", name);
79+
_logger?.Invoke(DebugLoggerLevel.Debug, "Couldn't find assembly {0} in the APK AssemblyStore", name);
8080
return null;
8181
}
8282

8383
var (explorer, storeItem) = explorerAssembly;
84-
_logger?.Invoke("Resolved assembly {0} in the APK {1} AssemblyStore", name, storeItem.TargetArch);
84+
_logger?.Invoke(DebugLoggerLevel.Debug, "Resolved assembly {0} in the APK {1} AssemblyStore", name, storeItem.TargetArch);
8585

8686
var stream = explorer.ReadImageData(storeItem, false);
8787
if (stream is null)
8888
{
89-
_logger?.Invoke("Couldn't access assembly {0} image stream", name);
89+
_logger?.Invoke(DebugLoggerLevel.Debug, "Couldn't access assembly {0} image stream", name);
9090
return null;
9191
}
9292

@@ -127,12 +127,12 @@ private bool FindBestAssembly(string name, out ExplorerStoreItem? explorerAssemb
127127
{
128128
if (explorer.AssembliesByName?.TryGetValue(name, out var assembly) is true)
129129
{
130-
_logger?.Invoke("Found best assembly {0} in APK AssemblyStore for target arch {1}", name, explorer.TargetArch);
130+
_logger?.Invoke(DebugLoggerLevel.Debug, "Found best assembly {0} in APK AssemblyStore for target arch {1}", name, explorer.TargetArch);
131131
explorerAssembly = new(explorer, assembly);
132132
return true;
133133
}
134134
}
135-
_logger?.Invoke("No best assembly for {0} in APK AssemblyStore", name);
135+
_logger?.Invoke(DebugLoggerLevel.Warning, "No best assembly for {0} in APK AssemblyStore", name);
136136
explorerAssembly = null;
137137
return false;
138138
}

src/Sentry.Android.AssemblyReader/V2/AssemblyStoreExplorer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private AssemblyStoreExplorer(Stream storeStream, string path, DebugLogger? logg
3333
{
3434
foreach (var item in Assemblies)
3535
{
36-
logger?.Invoke("Assembly {0} indexed from AssemblyStore {1}", item.Name, path);
36+
logger?.Invoke(DebugLoggerLevel.Debug, "Assembly {0} indexed from AssemblyStore {1}", item.Name, path);
3737
dict.Add(item.Name, item);
3838
}
3939
}

src/Sentry.Android.AssemblyReader/V2/StoreReaderV2.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ protected override bool IsSupported()
106106
ELFPayloadError.NoPayloadSection => $"Store '{StorePath}' does not contain the 'payload' section",
107107
_ => $"Unknown ELF payload section error for store '{StorePath}': {error}"
108108
};
109-
Logger?.Invoke(message);
109+
Logger?.Invoke(DebugLoggerLevel.Debug, message);
110110
// Was originally:
111111
// ```
112112
// } else if (elfOffset >= 0) {
@@ -122,14 +122,14 @@ protected override bool IsSupported()
122122

123123
if (magic != Utils.AssemblyStoreMagic)
124124
{
125-
Logger?.Invoke("Store '{0}' has invalid header magic number.", StorePath);
125+
Logger?.Invoke(DebugLoggerLevel.Debug, "Store '{0}' has invalid header magic number.", StorePath);
126126
return false;
127127
}
128128

129129
uint version = reader.ReadUInt32();
130130
if (!supportedVersions.Contains(version))
131131
{
132-
Logger?.Invoke("Store '{0}' has unsupported version 0x{1:x}", StorePath, version);
132+
Logger?.Invoke(DebugLoggerLevel.Debug, "Store '{0}' has unsupported version 0x{1:x}", StorePath, version);
133133
return false;
134134
}
135135

0 commit comments

Comments
 (0)