Skip to content

Instantly share code, notes, and snippets.

@ljmf00
Created July 22, 2020 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljmf00/accb93a727e65e2f74eb42594d92dc2e to your computer and use it in GitHub Desktop.
Save ljmf00/accb93a727e65e2f74eb42594d92dc2e to your computer and use it in GitHub Desktop.
ADB Log Filter
#!/usr/bin/env rdmd
import std.process;
import std.stdio;
import std.concurrency;
import core.thread;
import std.format;
import std.ascii;
import core.sync.mutex;
import std.file : remove, write;
import std.algorithm;
import std.uni : asLowerCase;
// global shared
__gshared string log_id;
__gshared string[][string] filter_rules;
__gshared Mutex[string] filter_lock;
__gshared File[string] filter_write;
shared static this()
{
import std.uuid : randomUUID;
log_id = randomUUID.toString;
filter_rules["selinux-denied"] = [
"selinux",
"sepolicy",
"avc:",
"permission denied",
"access denied",
"unable to set property"
];
filter_rules["avc"] = [
"avc:"
];
filter_rules["dev_sysfs"] = [
"/dev/",
"/sys/"
];
filter_rules["nfc"] = [
"NXP_NFC_DEV_NODE",
"nq-nci",
"nxp",
"nfc"
];
filter_rules["fingerprint"] = [
"fingerprint",
"biometrics"
];
filter_rules["camera"] = [
"camera"
];
// write and assign
write(log_id ~ ".log", "");
filter_write["nofilter"] = File(log_id ~ ".log", "ab");
foreach (key, _; filter_rules)
{
write(log_id ~ "-" ~ key ~ ".log", "");
filter_write[key] = File(log_id ~ "-" ~ key ~ ".log", "ab");
}
filter_lock["nofilter"] = new Mutex();
foreach (key, _; filter_rules)
filter_lock[key] = new Mutex();
}
void write_log(string command)
{
writeln(format!"starting '%s'..."(command));
while(true)
{
auto pipes = pipeShell(command, Redirect.stdout | Redirect.stderr);
scope(exit) wait(pipes.pid);
//recreate file securely
filter_lock["nofilter"].lock();
remove(log_id ~ ".log");
write(log_id ~ ".log", "");
filter_write["nofilter"].reopen(log_id ~ ".log", "ab");
filter_lock["nofilter"].unlock();
foreach (key, _; filter_rules)
{
// recreate key-files securely
filter_lock[key].lock();
remove(log_id ~ "-" ~ key ~ ".log");
write(log_id ~ "-" ~ key ~ ".log", "");
filter_write[key].reopen(log_id ~ "-" ~ key ~ ".log", "ab");
filter_lock[key].unlock();
}
new Thread((){
foreach (line; pipes.stderr.byLine)
{
filter_lock["nofilter"].lock();
writeln(line);
filter_write["nofilter"].writeln(line);
filter_write["nofilter"].flush();
filter_write["nofilter"].sync();
filter_lock["nofilter"].unlock();
if(line == "- waiting for device -")
{
Thread.sleep( dur!("msecs")( 100 ) );
kill(pipes.pid);
break;
}
Thread.sleep( dur!("msecs")( 100 ) );
}
}).start;
new Thread((){
foreach (line; pipes.stdout.byLine)
{
filter_lock["nofilter"].lock();
writeln(line);
filter_write["nofilter"].writeln(line);
filter_write["nofilter"].flush();
filter_write["nofilter"].sync();
filter_lock["nofilter"].unlock();
foreach (key, value; filter_rules)
{
foreach(rule; value)
{
if(asLowerCase(line).canFind(rule))
{
filter_lock[key].lock();
filter_write[key].writeln(line);
filter_write[key].flush();
filter_write[key].sync();
filter_lock[key].unlock();
}
}
}
}
}).start;
}
}
int main(string[] args)
{
spawn(&write_log, "adb logcat -C");
spawn(&write_log, "adb shell dmesg -w");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment