How to write Logs in File Android - android

I am using java logger for java projects.But now I want logs in Android.So how to carry out it.Alos I need to maiantain logs for warnings ,errors,informations in a file with serial logs.

Use android.util.Log and use methods like:
Log.v(), Log.d(), Log.i(), Log.w() and Log.e()
There are five levels of logs you can use in your application, for the most verbose to the least verbose :
Verbose:For extra information messages that are only compiled in a debug application, but never included in a release application.
Debug: For debug log messages that are always compiled, but stripped at runtime in a release application.
Info: For an information in the logs that will be written in debug and release.
Warning: For a warning in the logs that will be written in debug and release.
Error: For an error in the logs that will be written in debug and release.
A log message includes a tag identifying a group of messages and the message. By default the log level of all tags is Info, which means than messages that are of level Debug and Verbose should never shown unless the setprop command is used to change the log level. So, to write a verbose message to the log, you should call the isLoggable method to check if the message can be logged, and call the logging method :
if (!Log.isLoggable(logMessageTag, Log.Verbose))
Log.v("MyApplicationTag", logMessage);
And to show the Debug and Verbose log message for a specific tag, run the setprop command while your device is plugged in. If you reboot the device you will have to run the command again.
adb shell setprop log.tag.MyApplicationTag VERBOSE
Hope this solved your question.
For more reference :http://www.codelearn.org/android-tutorial/android-log
Upvote if you found it helpful.

Related

Can't see ATrace logs in logcat

I used the tracing mechanism for C++ described here by google. I'm checking for ATrace_isEnabled and while I'm getting true for ATrace_isEnabled, I'm unable to see any logs in the logcat.
I also went through with this answer, but to get enabled, I have to run systrace.py --app="my binary name". The setting of flags do not help
Is there something I'm missing?
I'm on Android 6.0.1
The setting of flags do not help, because in there source code, logic is such that it gets changed to 0 again. You need to specify appname wise like
adb shell "setprop debug.atrace.app_number 1"
adb shell "setprop debug.atrace.app_0 appname"
and for getting logs in logcat, use this __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "ATrace_isEnabled = %s", ATrace_isEnabled1()?"true":"false");. Define LOG_TAG for this.
for more help, see this ATrace_isEnabled() return false

Add native service to aosp

I am trying to add a native service written in C++ to the AOSP build.
The first thing I did was to create a native service and client to the AOSP build.
This worked as expected. I could start the service within an adb shell and call it via binder on a adb shell.
The trouble started when I wanted to start my service with init.
I added a .rc file to my build
service myp /system/bin/myp_service
class main
This did the the trick so that init tried to start it but it failed because of SELinux policies.
So I added a file_contexts to my device tree and added:
/system/bin/myp_service u:object_r:myp_exec:s0
Next I added a myp.te file and added:
type myp, domain;
type myp_exec, exec_type, file_type;
type myp_service, service_manager_type;
init_daemon_domain(myp)
net_domain(myp)
binder_use(myp)
binder_service(myp)
add_service(myp, myp_service)
binder_call(myp, binderservicedomain)
binder_call(myp, appdomain)
allow myp myp_service:service_manager add;
And finally I added a service_contexts file with:
myp u:object_r:myp_service:s0
This finally made my service successfully start at boot time.
Unfortunalty I cannot use binder against this service. When I try to connect to the service with my client the call
defaultServiceManager()->getService(String16("Demo"))
returns a null pointer.
I cannot find any hints in the dmesg.
So I assume I am still missing something for the SElinux but I have no clue what I am missing.
If I shutdown the SELinux with setenforce and restart the service then it works fine.
Can anyone give me a hint what I am missing for SELinux or where I can get more information about which policy blocked something?
You could see the denials like this:
adb logcat | grep "SELinux : avc" > /tmp/logs
Get sepolicy current file. (Can be taken from device this way adb pull sepolicy.
Using audit2allow (located in AOSP source code: external/selinux/prebuilts/bin/audit2allow or in SDK tools. Do this: cat /tmp/logs | .external/selinux/prebuilts/bin/audit2allow -p sepolicy
The audit2allow tool will tell you what permission you are missing for the logcat extracted and the current sepolicy file, watch-out because you could need to do this several times since fixing some permissions will show the next ones required.
If you have a userdebug kind of build you could get setenforce 0, logcat with it and all the denials will be in logcat even if you will be permited to do the operation desired. This will leave the audit2allow iterations required in 1.
For anyone who came across this problem, please make sure your service_contexts file is successfully merged with stock service_contexts file. If you're building your service for Android O or later, please put this file inside a folder and refer to it in your Makefile by BOARD_PLAT_PRIVATE_SEPOLICY_DIR1. And you don't need to add allow myp default_android_service:service_manager add if the build system does pick up your service_contexts.
Also, about the domain.te violation problem, you probably want to attach one of the coredomain or appdomain attribute to your domain 2 with typeattribute <your_domain> <attribute>;.
Finally, please double check the following built files to make sure you don't leave any sepolicy configurations out in the final build:
$(AOSP_ROOT)/out/target/product//obj/ETC/file_contexts.bin_intermediates/file_contexts.*
$(AOSP_ROOT)/out/target/product/potter/obj/ETC/plat_service_contexts_intermediates/service_contexts.*
$(AOSP_ROOT)/out/target/product/potter/obj/ETC/sepolicy_neverallows_intermediates/policy.conf

Anko Logging with verbose and debug isn't working

I'm trying to use Anko Commons – Logging
but for some reason the verbose and debug not showing on the logcat
however, when I use Log.d it works as expected.
when I'm trying this code
verbose("-------------verbose--------------")
debug("-------------debug--------------")
info("-------------info--------------")
warn("-------------warn--------------")
error("-------------error--------------")
wtf("-------------wtf--------------")
the log logcat is as following
I'm using Anko 0.10.0 android studio 3.0 canary-5
when I tried to search for a solution I found this one
All Log.* will log to logcat regardless of what the current log level is. However, AnkoLogger.* will only log to logcat when its log level is loggable.
but I don't know how to change the log level, and if even it's my problem is the same.
It turns out The default log level is INFO.
From the ADB shell, you can change the log level to DEBUG, VERBOSE etc. using this command:
setprop log.tag.MyApp DEBUG
so As far as know by AnkoLogger uses Log.isLoggable() under the hood which ignores VERBOSE and DEBUG log levels by default.
for more info you can read more about it here

Android Logging set verbose for all tags (logcat)

I am debugging on device, and I do not get the verbose or debug log messages, only the info, warn, error and wtf. I need to get all debug and verbose messages, without enabling with setprop for all my tags (I am using different tags for each file). So how can I set device log level to VERBOSE globally? It is also good if you show me a java code from I can set log level for a tag - so my wrapper class will set log level for every new tag to verbose. But command line not good...

How to enable assertions in Eclipse v3.7.2

I can't seem to get assertions working in my eclipse android project. I have followed the directions at this site
But I still can't get them to work. My code looks like:
assert(false) : "PROGRAM ERROR - invalid dialog call";
any ideas?
Dalvik VM ignores Java assertions by default. To enable that, one must change debug.assert property, e.g by invoking command in command line:
adb shell setprop debug.assert 1
Please note that you have to restart VM after that (force stop application: Settings->Apps->click on the app->Force stop).
Please also note that the setting is not persistent (assertions will be disabled again after phone reboot). However on rooted phones there's a way to make it persistent by putting /data/local.prop file containing line
debug.assert=1
What language are you expecting to use with the above?
I would try:
assertTrue("Expected true", true);
assertFalse("Expected false", true);
assertEquals("Expected equal", "same", "same");
Junit:
junit-docs/junit/framework/Assert.html

Categories

Resources