Skip to content

Commit

Permalink
vab: colored errors, warnings, notices, shorter opt.verbosity print…
Browse files Browse the repository at this point in the history
… statements (#300)
  • Loading branch information
larpon authored Sep 15, 2024
1 parent 83f31af commit 8b5c5c0
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 329 deletions.
7 changes: 7 additions & 0 deletions android/compile.v
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ pub:
min_sdk_version int = default_min_sdk_version
}

// verbose prints `msg` to STDOUT if `CompileOptions.verbosity` level is >= `verbosity_level`.
pub fn (co &CompileOptions) verbose(verbosity_level int, msg string) {
if co.verbosity >= verbosity_level {
println(msg)
}
}

// uses_gc returns true if a `-gc` flag is found among the passed v flags.
pub fn (opt CompileOptions) uses_gc() bool {
mut uses_gc := true // V default
Expand Down
58 changes: 23 additions & 35 deletions android/deploy.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module android
import os
import time
import vab.java
import vab.util as vabutil
import vab.android.env
import vab.android.util

Expand All @@ -31,6 +32,13 @@ pub enum LogMode {
raw
}

// verbose prints `msg` to STDOUT if `DeployOptions.verbosity` level is >= `verbosity_level`.
pub fn (do &DeployOptions) verbose(verbosity_level int, msg string) {
if do.verbosity >= verbosity_level {
println(msg)
}
}

fn (do DeployOptions) gen_logcat_filters() []string {
mut filters := []string{}
// Only filter output in "normal" log mode
Expand Down Expand Up @@ -87,10 +95,7 @@ pub fn deploy_apk(opt DeployOptions) ! {

// Deploy
if device_id != '' {
if opt.verbosity > 0 {
println('Deploying ${opt.format} package to "${device_id}"')
}

opt.verbose(1, 'Deploying ${opt.format} package to "${device_id}"')
if opt.kill_adb {
os.signal_opt(.int, kill_adb_on_exit) or {
// Kept for debugging return error('$error_tag: Could not set signal handler:\n$err')
Expand All @@ -105,9 +110,7 @@ pub fn deploy_apk(opt DeployOptions) ! {
]
if opt.clear_device_log || (opt.run != '' && opt.device_log) {
// Clear logs first
if opt.verbosity > 0 {
println('Clearing log buffer on device "${device_id}"')
}
opt.verbose(1, 'Clearing log buffer on device "${device_id}"...')
util.verbosity_print_cmd(adb_logcat_clear_cmd, opt.verbosity)
util.run_or_error(adb_logcat_clear_cmd)!
// Give adb/Android/connection time to settle... *sigh*
Expand All @@ -128,9 +131,7 @@ pub fn deploy_apk(opt DeployOptions) ! {
time.sleep(100 * time.millisecond)

if opt.run != '' {
if opt.verbosity > 0 {
println('Running "${opt.run}" on "${device_id}"')
}
opt.verbose(1, 'Running "${opt.run}" on "${device_id}"...')
adb_run_cmd := [
adb,
'-s "${device_id}"',
Expand All @@ -150,10 +151,8 @@ pub fn deploy_apk(opt DeployOptions) ! {

has_crash_report := adb_detect_and_report_crashes(opt, device_id)!
if has_crash_report {
$if !vab_no_notices ? {
eprintln('You can clear all logs by running:\n"' + adb_logcat_clear_cmd.join(' ') +
'"')
}
vabutil.vab_notice('You can clear all logs by running:\n"' +
adb_logcat_clear_cmd.join(' ') + '"')
}
}
}
Expand All @@ -174,9 +173,7 @@ pub fn deploy_aab(opt DeployOptions) ! {

// Deploy
if device_id != '' {
if opt.verbosity > 0 {
println('Building APKs from "${opt.deploy_file}"')
}
opt.verbose(1, 'Building APKs from "${opt.deploy_file}"...')

apks_path := os.join_path(opt.work_dir,
os.file_name(opt.deploy_file).all_before_last('.') + '.apks')
Expand All @@ -200,9 +197,7 @@ pub fn deploy_aab(opt DeployOptions) ! {
util.verbosity_print_cmd(bundletool_apks_cmd, opt.verbosity)
util.run_or_error(bundletool_apks_cmd)!

if opt.verbosity > 0 {
println('Deploying ${opt.format} package to "${device_id}"')
}
opt.verbose(1, 'Deploying ${opt.format} package to "${device_id}"...')

if opt.kill_adb {
os.signal_opt(.int, kill_adb_on_exit) or {
Expand All @@ -218,9 +213,7 @@ pub fn deploy_aab(opt DeployOptions) ! {
]
if opt.clear_device_log || (opt.run != '' && opt.device_log) {
// Clear logs first
if opt.verbosity > 0 {
println('Clearing log buffer on device "${device_id}"')
}
opt.verbose(1, 'Clearing log buffer on device "${device_id}"...')
util.verbosity_print_cmd(adb_logcat_clear_cmd, opt.verbosity)
util.run_or_error(adb_logcat_clear_cmd)!
// Give adb/Android/connection time to settle... *sigh*
Expand Down Expand Up @@ -265,10 +258,8 @@ pub fn deploy_aab(opt DeployOptions) ! {

has_crash_report := adb_detect_and_report_crashes(opt, device_id)!
if has_crash_report {
$if !vab_no_notices ? {
eprintln('You can clear all logs by running:\n"' + adb_logcat_clear_cmd.join(' ') +
'"')
}
vabutil.vab_notice('You can clear all logs by running:\n"' +
adb_logcat_clear_cmd.join(' ') + '"')
}
}
}
Expand All @@ -287,8 +278,9 @@ fn adb_detect_and_report_crashes(opt DeployOptions, device_id string) !bool {
util.verbosity_print_cmd(adb_logcat_cmd, opt.verbosity)
crash_log := util.run_or_error(adb_logcat_cmd)!
if crash_log.count('\n') > 3 {
eprintln('It looks like your app might have crashed\nDumping crash buffer:')
eprintln(crash_log)
vabutil.vab_notice('It looks like your app might have crashed. Dumping crash buffer...',
details: crash_log
)
return true
}
return false
Expand All @@ -297,9 +289,7 @@ fn adb_detect_and_report_crashes(opt DeployOptions, device_id string) !bool {
fn adb_log_step(opt DeployOptions, device_id string) ! {
adb := env.adb()
mut crash_mode := false
if opt.verbosity > 0 {
println('Showing log output from device "${device_id}"')
}
opt.verbose(1, 'Showing log output from device "${device_id}"')
println('Ctrl+C to cancel logging')
mut adb_logcat_cmd := [
adb,
Expand Down Expand Up @@ -336,9 +326,7 @@ fn adb_log_step(opt DeployOptions, device_id string) ! {

fn kill_adb_on_exit(signum os.Signal) {
uos := os.user_os()
$if !vab_no_notices ? {
eprintln('Killing adb on signal ${signum}')
}
vabutil.vab_notice('Killing adb on signal ${signum}')
if uos == 'windows' {
// os.system('Taskkill /IM adb.exe /F') // TODO Untested
} else {
Expand Down
28 changes: 13 additions & 15 deletions android/env/env.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import os
import semver
import net.http
import vab.cache
import vab.util as vabutil
import vab.android.sdk
import vab.android.ndk
import vab.android.util
Expand Down Expand Up @@ -127,6 +128,13 @@ pub struct InstallOptions {
verbosity int
}

// verbose prints `msg` to STDOUT if `InstallOptions.verbosity` level is >= `verbosity_level`.
pub fn (io &InstallOptions) verbose(verbosity_level int, msg string) {
if io.verbosity >= verbosity_level {
println(msg)
}
}

pub fn managable() bool {
sdk_is_writable := os.is_writable(sdk.root())
// sdkmanager checks
Expand Down Expand Up @@ -311,9 +319,7 @@ fn install_opt(opt InstallOptions) !bool {

item := opt.item

if opt.verbosity > 0 {
println(@MOD + '.' + @FN + ' installing ${opt.dep}: "${item}"...')
}
opt.verbose(1, 'installing ${opt.dep}: "${item}"...')

install_cmd := $if windows {
[
Expand Down Expand Up @@ -357,15 +363,11 @@ fn install_opt(opt InstallOptions) !bool {
sv_check := semver.from(version_check) or { panic(err) }
comp_sv := semver.from(ndk.min_supported_version) or { panic(err) }
if sv_check < comp_sv {
$if !vab_no_notices ? {
eprintln('Notice: Skipping install. NDK ${item} is lower than supported ${ndk.min_supported_version}...')
}
vabutil.vab_notice('Skipping install. NDK ${item} is lower than supported ${ndk.min_supported_version}...')
return true
}
}
if opt.verbosity > 0 {
println('Installing NDK (Side-by-side) "${item}"...')
}
opt.verbose(1, 'Installing NDK (Side-by-side) "${item}"...')

util.verbosity_print_cmd(install_cmd, opt.verbosity)
cmd_res := $if windows {
Expand All @@ -384,9 +386,7 @@ fn install_opt(opt InstallOptions) !bool {
sv_check := semver.from(version_check) or { panic(err) }
comp_sv := semver.from(sdk.min_supported_build_tools_version) or { panic(err) }
if sv_check < comp_sv {
$if !vab_no_notices ? {
eprintln('Notice: Skipping install. build-tools "${item}" is lower than supported ${sdk.min_supported_build_tools_version}...')
}
vabutil.vab_notice('Skipping install. build-tools "${item}" is lower than supported ${sdk.min_supported_build_tools_version}...')
return true
}
}
Expand All @@ -404,9 +404,7 @@ fn install_opt(opt InstallOptions) !bool {
.platforms {
api_level := item.all_after('-')
if api_level.i16() < sdk.min_supported_api_level.i16() {
$if !vab_no_notices ? {
eprintln('Notice: Skipping install. platform ${item} is lower than supported android-${sdk.min_supported_api_level}...')
}
vabutil.vab_notice('Skipping install. platform ${item} is lower than supported android-${sdk.min_supported_api_level}...')
return true
}
util.verbosity_print_cmd(install_cmd, opt.verbosity)
Expand Down
Loading

0 comments on commit 8b5c5c0

Please sign in to comment.