Skip to content

Commit 7b4cc4e

Browse files
bake package_name into the android module (#11)
- update resources to no longer use WriteFiles and just compile directly with `aapt2 compile` - bake package_name into the Zig `android` package so it can always log with the correct tag name - ie. even if SDL starts your code from a new thread
1 parent 6d945e5 commit 7b4cc4e

File tree

7 files changed

+246
-75
lines changed

7 files changed

+246
-75
lines changed

build.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,12 @@ pub fn build(b: *std.Build) void {
2222
.target = target,
2323
.optimize = optimize,
2424
});
25+
26+
// Create stub of builtin options.
27+
// This is discovered and then replace in src/androidbuild/apk.zig
28+
const android_builtin_options = std.Build.addOptions(b);
29+
android_builtin_options.addOption([:0]const u8, "package_name", "");
30+
module.addImport("android_builtin", android_builtin_options.createModule());
31+
2532
module.linkSystemLibrary("log", .{});
2633
}

examples/minimal/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ adb uninstall "com.zig.minimal"
2929

3030
### View logs of application
3131

32-
Powershell
32+
Powershell (app doesn't need to be running)
3333
```sh
3434
adb logcat | Select-String com.zig.minimal:
3535
```
3636

37-
Bash
37+
Bash (app doesn't need running to be running)
38+
```sh
39+
adb logcat com.zig.minimal:D *:S
40+
```
41+
42+
Bash (app must be running, logs everything by the process including modules)
3843
```sh
3944
adb logcat --pid=`adb shell pidof -s com.zig.minimal`
4045
```

examples/sdl2/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ adb uninstall "com.zig.sdl2"
3535

3636
### View logs of application
3737

38-
Powershell
38+
Powershell (app doesn't need to be running)
3939
```sh
4040
adb logcat | Select-String com.zig.sdl2:
4141
```
4242

43-
Bash
43+
Bash (app doesn't need running to be running)
44+
```sh
45+
adb logcat com.zig.sdl2:D *:S
46+
```
47+
48+
Bash (app must be running, logs everything by the process including modules)
4449
```sh
4550
adb logcat --pid=`adb shell pidof -s com.zig.sdl2`
4651
```

examples/sdl2/src/sdl-zig-demo.zig

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ else
2323
/// This needs to be exported for Android builds
2424
export fn SDL_main() callconv(.C) void {
2525
if (builtin.abi == .android) {
26-
android.set_default_tag("com.zig.sdl2");
2726
_ = std.start.callMain();
2827
} else {
2928
@panic("SDL_main should not be called outside of Android builds");
3029
}
3130
}
3231

3332
pub fn main() !void {
33+
log.debug("started sdl-zig-demo", .{});
34+
3435
if (sdl.SDL_Init(sdl.SDL_INIT_VIDEO) < 0) {
3536
log.info("Unable to initialize SDL: {s}", .{sdl.SDL_GetError()});
3637
return error.SDLInitializationFailed;
@@ -69,7 +70,14 @@ pub fn main() !void {
6970
defer sdl.SDL_DestroyTexture(zig_texture);
7071

7172
var quit = false;
73+
var has_run_frame: FrameLog = .none;
7274
while (!quit) {
75+
if (has_run_frame == .one_frame_passed) {
76+
// NOTE(jae): 2024-10-03
77+
// Allow inspection of logs to see if a frame executed at least once
78+
log.debug("has executed one frame", .{});
79+
has_run_frame = .logged_one_frame;
80+
}
7381
var event: sdl.SDL_Event = undefined;
7482
while (sdl.SDL_PollEvent(&event) != 0) {
7583
switch (event.type) {
@@ -83,7 +91,15 @@ pub fn main() !void {
8391
_ = sdl.SDL_RenderClear(renderer);
8492
_ = sdl.SDL_RenderCopy(renderer, zig_texture, null, null);
8593
sdl.SDL_RenderPresent(renderer);
86-
8794
sdl.SDL_Delay(17);
95+
if (has_run_frame == .none) {
96+
has_run_frame = .one_frame_passed;
97+
}
8898
}
8999
}
100+
101+
const FrameLog = enum {
102+
none,
103+
one_frame_passed,
104+
logged_one_frame,
105+
};

src/android/android.zig

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
const std = @import("std");
22
const builtin = @import("builtin");
33

4-
extern "log" fn __android_log_write(prio: c_int, tag: [*c]const u8, text: [*c]const u8) c_int;
4+
// TODO(jae): 2024-10-03
5+
// Consider exposing this in the future
6+
// pub const builtin = android_builtin;
57

6-
/// Update the tag used by the Android logger, this will default to your package name unless logging occurs in a seperate
7-
/// thread.
8-
///
9-
/// ie. If using via SDL2, you'll likely want the tag set.
10-
///
11-
/// NOTE(jae): 2024-09-22
12-
/// We should look at moving to making the "tag" default your Android package name by parsing that out of your AndroidManifest.xml
13-
/// or perhaps even just make it use the "name" of your app
14-
pub fn set_default_tag(tag: [:0]const u8) void {
15-
LogWriter.tag = tag;
16-
}
8+
const android_builtin = struct {
9+
const ab = @import("android_builtin");
10+
11+
/// package name extracted from your AndroidManifest.xml file
12+
/// ie. "com.zig.sdl2"
13+
pub const package_name: [:0]const u8 = ab.package_name;
14+
};
15+
16+
extern "log" fn __android_log_write(prio: c_int, tag: [*c]const u8, text: [*c]const u8) c_int;
1717

1818
/// Alternate panic implementation that calls __android_log_write so that you can see the logging via "adb logcat"
1919
pub const panic = Panic.panic;
@@ -50,12 +50,12 @@ pub const Level = enum(u8) {
5050
/// Alternate log function implementation that calls __android_log_write so that you can see the logging via "adb logcat"
5151
pub fn logFn(
5252
comptime message_level: std.log.Level,
53-
comptime scope: if (builtin.zig_version.minor != 13)
54-
// Support Zig 0.14.0-dev
55-
@Type(.enum_literal)
56-
else
53+
comptime scope: if (builtin.zig_version.major == 0 and builtin.zig_version.minor == 13)
5754
// Support Zig 0.13.0
58-
@Type(.EnumLiteral),
55+
@Type(.EnumLiteral)
56+
else
57+
// Support Zig 0.14.0-dev
58+
@Type(.enum_literal),
5959
comptime format: []const u8,
6060
args: anytype,
6161
) void {
@@ -82,9 +82,17 @@ pub fn logFn(
8282

8383
/// LogWriter was was taken basically as is from: https://github.com/ikskuh/ZigAndroidTemplate
8484
const LogWriter = struct {
85-
/// name of the application / log scope
86-
/// if not set, it'll default to the "package" attribute defined in AndroidManifest.xml
87-
var tag: [:0]const u8 = &[0:0]u8{};
85+
/// Default to the "package" attribute defined in AndroidManifest.xml
86+
///
87+
/// If tag isn't set when calling "__android_log_write" then it *usually* defaults to the current
88+
/// package name, ie. "com.zig.minimal"
89+
///
90+
/// However if running via a seperate thread, then it seems to use that threads
91+
/// tag, which means if you log after running code through sdl_main, it won't print
92+
/// logs with the package name.
93+
///
94+
/// To workaround this, we bake the package name into the Zig binaries.
95+
var tag: [:0]const u8 = android_builtin.package_name;
8896

8997
level: Level,
9098

0 commit comments

Comments
 (0)