Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,12 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
});

// Create stub of builtin options.
// This is discovered and then replace in src/androidbuild/apk.zig
const android_builtin_options = std.Build.addOptions(b);
android_builtin_options.addOption([:0]const u8, "package_name", "");
module.addImport("android_builtin", android_builtin_options.createModule());

module.linkSystemLibrary("log", .{});
}
9 changes: 7 additions & 2 deletions examples/minimal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ adb uninstall "com.zig.minimal"

### View logs of application

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

Bash
Bash (app doesn't need running to be running)
```sh
adb logcat com.zig.minimal:D *:S
```

Bash (app must be running, logs everything by the process including modules)
```sh
adb logcat --pid=`adb shell pidof -s com.zig.minimal`
```
9 changes: 7 additions & 2 deletions examples/sdl2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ adb uninstall "com.zig.sdl2"

### View logs of application

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

Bash
Bash (app doesn't need running to be running)
```sh
adb logcat com.zig.sdl2:D *:S
```

Bash (app must be running, logs everything by the process including modules)
```sh
adb logcat --pid=`adb shell pidof -s com.zig.sdl2`
```
20 changes: 18 additions & 2 deletions examples/sdl2/src/sdl-zig-demo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ else
/// This needs to be exported for Android builds
export fn SDL_main() callconv(.C) void {
if (builtin.abi == .android) {
android.set_default_tag("com.zig.sdl2");
_ = std.start.callMain();
} else {
@panic("SDL_main should not be called outside of Android builds");
}
}

pub fn main() !void {
log.debug("started sdl-zig-demo", .{});

if (sdl.SDL_Init(sdl.SDL_INIT_VIDEO) < 0) {
log.info("Unable to initialize SDL: {s}", .{sdl.SDL_GetError()});
return error.SDLInitializationFailed;
Expand Down Expand Up @@ -69,7 +70,14 @@ pub fn main() !void {
defer sdl.SDL_DestroyTexture(zig_texture);

var quit = false;
var has_run_frame: FrameLog = .none;
while (!quit) {
if (has_run_frame == .one_frame_passed) {
// NOTE(jae): 2024-10-03
// Allow inspection of logs to see if a frame executed at least once
log.debug("has executed one frame", .{});
has_run_frame = .logged_one_frame;
}
var event: sdl.SDL_Event = undefined;
while (sdl.SDL_PollEvent(&event) != 0) {
switch (event.type) {
Expand All @@ -83,7 +91,15 @@ pub fn main() !void {
_ = sdl.SDL_RenderClear(renderer);
_ = sdl.SDL_RenderCopy(renderer, zig_texture, null, null);
sdl.SDL_RenderPresent(renderer);

sdl.SDL_Delay(17);
if (has_run_frame == .none) {
has_run_frame = .one_frame_passed;
}
}
}

const FrameLog = enum {
none,
one_frame_passed,
logged_one_frame,
};
48 changes: 28 additions & 20 deletions src/android/android.zig
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const std = @import("std");
const builtin = @import("builtin");

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

/// Update the tag used by the Android logger, this will default to your package name unless logging occurs in a seperate
/// thread.
///
/// ie. If using via SDL2, you'll likely want the tag set.
///
/// NOTE(jae): 2024-09-22
/// We should look at moving to making the "tag" default your Android package name by parsing that out of your AndroidManifest.xml
/// or perhaps even just make it use the "name" of your app
pub fn set_default_tag(tag: [:0]const u8) void {
LogWriter.tag = tag;
}
const android_builtin = struct {
const ab = @import("android_builtin");

/// package name extracted from your AndroidManifest.xml file
/// ie. "com.zig.sdl2"
pub const package_name: [:0]const u8 = ab.package_name;
};

extern "log" fn __android_log_write(prio: c_int, tag: [*c]const u8, text: [*c]const u8) c_int;

/// Alternate panic implementation that calls __android_log_write so that you can see the logging via "adb logcat"
pub const panic = Panic.panic;
Expand Down Expand Up @@ -50,12 +50,12 @@ pub const Level = enum(u8) {
/// Alternate log function implementation that calls __android_log_write so that you can see the logging via "adb logcat"
pub fn logFn(
comptime message_level: std.log.Level,
comptime scope: if (builtin.zig_version.minor != 13)
// Support Zig 0.14.0-dev
@Type(.enum_literal)
else
comptime scope: if (builtin.zig_version.major == 0 and builtin.zig_version.minor == 13)
// Support Zig 0.13.0
@Type(.EnumLiteral),
@Type(.EnumLiteral)
else
// Support Zig 0.14.0-dev
@Type(.enum_literal),
comptime format: []const u8,
args: anytype,
) void {
Expand All @@ -82,9 +82,17 @@ pub fn logFn(

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

level: Level,

Expand Down
Loading