Getting debug logs - iPhone - android

On Android I am using the android.util.Log to log within my application and during development I am using the adb logcat or Eclipse to see the logs - I use it even more then debugging...
On device I can save the logfile from my code or use some application form Android Market to save the logs - e.g. aLogCat.
Now can I do the same on the iPhone? I can use the NSLog(#"message");, but can I easily save the log file from my application and access it? Are there any ways for that?
Regards,
STeN

This is from NSFoundation reference
NSLog:
Simply calls NSLogv, passing it a variable number of arguments.
NSLogv:
Logs an error message to the Apple System Log facility (see man 3 asl). If the STDERR_FILENO file descriptor has been redirected away from the default or is going to a tty, it will also be written there. If you want to direct output elsewhere, you need to use a custom logging facility.
Thus, it is only a matter of redirecting the file-descriptor "stderr" (2) to a custom file, and you will get everything that you print using NSLog in that file.
This seems to be exactly what you want.
Note that if you want to get logs on console when you are connected to the debugger, you can wrap your code around this to avoid redirection in this case:
if (!isatty(STDERR_FILENO)) { // Not connected to any terminal
// your redirection code
}

You can access the console log from Organizer->Device->Your device->console.
If that is not powerful enough, consider using utilities like NSLogger.

The previous answers are good; also see this if you're inclined to making system calls.

Related

Log output to file and system log at the same time

I am trying to diagnose an issue in an app I have written. The issue is a sporadic one, and occurs only under real-world conditions: in the field, away from my PC, and when I’m in the middle of something else, with no resources to spare for immediate debugging. Therefore, my best bet is collecting and analyzing log data.
Unfortunately, by the time I realize the issue has struck again and get around to debugging it, any log data has already rotated out of the Android log as I frequently have other chatty apps running at the same time. Increasing the size of the log buffer has not helped (either Android does not honor it or other apps are still too chatty) so I have abandoned this route.
For this reason, I am now considering having my app log to a separate text file in addition to the regular log.
Now I could easily double every call like
Log.i(TAG, "something happened");
adding another call that writes the same thing to the log file—but that does not seem very elegant to me.
Another option would be to replace all calls to Log with a wrapper that writes the event both to the Android log and the log file.
Question: Does the Android API provide a built-in mechanism for this, i.e. telling Log to write its data to the default log and a text file at the same time? Or do I need to code this by myself?
Edit:
Assumptions:
I know where in my code I need to generate log output (which can happen anywhere, which may or may not involve an exception) and what I want to be written to the log.
Getting log data from the device to my PC is also not a concern (one-man show, I just plug my phone into my PC and transfer the log file).
If you know the current Android API has no built-in mechanism to achieve what I want, then ”no, Android does not support this” is a perfectly acceptable answer. In that case the solution is clear—I would fall back to the wrapper function. I am specifically not looking for a different approach to the problem.
After doing some more research, it seems the Android API does not provide a standard way to do this. There are two possible workarounds:
Mirror output at the source
System.out and System.err output, which is written to the console in desktop systems, writes to the log on Android. These two can be redirected into any PrintStream of your choice, which would give you all Java console output. You can subclass PrintStream to duplicate its input, feeding it into the default stream as well as into a file of your choice.
Create a class which exposes the same methods as android.util.Log. In each method, call through to the respective android.util.Log method and additionally log the data to a file. If you call your class Log (but with a different package name, e.g. org.example.Log), then all you need to do is replace imports of android.util.Log with an import of your class, and any Log method calls will go to your class.
Caveats: This will only give you data explicitly logged by your code (i.e. for which you have the source files), as well as anything that goes to System.out or System.err. It will not include log output from JAR libraries (if you cannot modify their source code), nor any output generated by the system (such as stack traces from default exception handlers) or by other processes (some of which may be system processes and report conditions related to your process).
Read the logs from the command line
This article explains how to read the logs from within Android. In a nutshell:
Android includes a command line utility called logcat on the device, which will give you a continuous feed of log messages until stopped. (Try it by adb shelling into your device and running it. It has a bunch of command-line options to control its behavior. Not sure if it is present on all distributions, though.)
Launch this command via Runtime.getRuntime().exec("logcat"), then obtain the input stream of the process returned. This will give you an input stream of log messages.
According to the article, your app needs the android.permission.READ_LOGS permission to read logs.
I have read statements that certain versions of Android (4.2 was mentioned) do not allow this permission to be granted to non-system apps, though. According to my own tests, behavior without this permissions differ: Anbox will return the full logcat, while LineageOS (tested on 15.1) will only show log entries from the app which called it (including previous instances, presumably everything associated with the same Linux user). This can be a limitation or a welcome filter feature. YMMV.
logcat conveniently has a command line option, -f, to specify an output file. I tried
Runtime.getRuntime().exec("logcat -f " + absolutePathToLogFile);
and logcat keeps logging as long as the app’s process runs. Killing the app (by clicking the X in the title bar on Anbox) apparently also terminated the child process.
Now you can either run this code when your app starts up, or you can turn this functionality into a separate app which starts on boot and continuously collects logs for all apps.
Caveats: This may fill up your storage space quickly if you have some chatty apps running (which is why entries rotate out of the logcat so quickly in the first place). It is recommended to make log mirroring configurable (e.g. via Preferences) and/or ensure old files are deleted regularly. Also, if you keep the logcat process running until your app terminates, you will not be able to access the file over MTP as there is no easy way to run the media scanner (if you scan the file while it is still written to, it will appear truncated over MTP until another media scan runs).
You have not specified if some exception are thrown but you don't handle.
In case, take a look at this answer:
Android Handling Unhandled Exception
If you must look at a bunch of variables and objects, I'd suggest two choices:
Write a copy of your logs on a file. When your problem occurs, just ask the user to send the file to you. This is ideal during tests with self-aware users.
Obtain statistics about usage, like commercial software do. Just log user operations and send the data to your server (you would need one for this). This is the most transparent way to do remote logging.
In the case of writing log to a file, you can read and write what you want in internal memory (inside the app's sandbox) or external memory (in this case, write permission is required and explicit permission must have been granted at runtime if you are targeting Android 6 and above).

Android : How to enable logging Log.v values which are by default disabled

I am a novice to the Android Studio and working on one group project.I am trying to log some variable values to logging.
When I use Log.e values are properly logged. While when I use Log.w or Log.v nothing gets printed. Here I am switching from logcat from error to verbose and to warning as I am using Log but still can't see anything printed.I previously used Toast but heard that using it might make my app slow and using Log.e everywhere is not a good practice.So how to print logs of lower priority?
I tried referring following resources but didn't find anything that could help me enabling logging for lower priority Logs, Just got the info that for some reason lower priority logs are disabled.
https://developer.android.com/reference/android/util/Log.html
Android Log.v(), Log.d(), Log.i(), Log.w(), Log.e() - When to use each one?
Thank you in advance.
All logs are always default enabled.
I think you are viewing logs in error view.
Check
You are viewing logs in Verbose
Your filter is set to selected app.
You have selected your app not other app. (where in below image no debuggable process is written)
Android Log.v(), Log.d(), Log.i(), Log.w(), Log.e() - When to use each one?
Log.e: This is for when bad stuff happens. Use this tag in places like inside a catch statement. You know that an error has occurred and therefore you're logging an error.
Log.w: Use this when you suspect something shady is going on. You may not be completely in full on error mode, but maybe you recovered from some unexpected behavior. Basically, use this to log stuff you didn't expect to happen but isn't necessarily an error. Kind of like a "hey, this happened, and it's weird, we should look into it."
Log.i: Use this to post useful information to the log. For example: that you have successfully connected to a server. Basically use it to report successes.
Log.d: Use this for debugging purposes. If you want to print out a bunch of messages so you can log the exact flow of your program, use this. If you want to keep a log of variable values, use this.
Log.v: Use this when you want to go absolutely nuts with your logging. If for some reason you've decided to log every little thing in a particular part of your app, use the Log.v tag.
Explanation by link Kurtis Nusbaum
Update:
If above things does not work then you are facing an device setting issue. Some mobiles have set default log level to DEBUG or ERROR. Allow logging from phone setting.
You can check if log is loggable by Log.isLoggable()
Check
Settings -> Accessibility -> Developer options -> advanced logging->set "Allow all"
or
Settings->Accessibility - > Developer Options -> Performance optimization -> Advanced logging -> set "Allow all"
or for other phone search in "developers options": option "logging" and set "all".
also you can use Log.wtf when your Log.d is not working.
try restarting android studio also

LogCat won't show my logs

I've searched for similar problems, but I didn't find anything useful - I'm working with eclipse, and I can't see my Logs.
The device is connected properly, the app runs and does what it's supposed to do, but I get no logs from it.
I get other logs messages from the device, not the ones that I print, e.g. Log.d("SMS", "hello"). On the other hand, if I use the statement System.out.println("hello"), I do see it, tagged as System.out.
I've tried to disconnect and reconnect the device, restart it, close and open eclipse, choose the device from Device window. It happens both with a 'real' device and an emulator. I've also tried to remove the filtering, but nothing helps - I still don't get the logs.
Okay, I've found the problem -
Apprently there are some illegal tags, and I've used one of them.
My app is spam SMS blocker, and I've used the tag SMS. If I change it to another tag (like SMSBlocker) it suddenly appears in the LogCat.
Check if your project is using proguard.
Basically proguard will remove all the debug logs and optimize your code while creating apk.
Try adding proguard.enabled=false in your project.properties
I think you should use TAG.
When you log from your android app, the first parameter is a TAG string. So if you set it up to a unique string (like your app name) then you can later filter by it in Eclipse.
Example : Log.e(TAG, "state error");

Using Android Studio's logcat better, permanent focus on a single process?

When testing on a device in Android Studio you get an awful lot of output in the logcat.
I'm only interested in the output for the app I'm developing. I can see just this, after running, by opening the Devices section and manually selecting my apps process. Problem is, it's pretty tedious to do this every time I run my app, which seems to be the case.
Is there a way to get it to remember this setup?
How about a way to get it to stop reporting anything after I'm done with my app or it's crashed ? (otherwise my app specific stuff gets buried so quickly by output from other proccesses on my phone)
I'm open to other ways of filtering the logcat too, however I couldn't think of a way to set up filters so that I would get my tagged Log messages AND other exceptions I wasn't expecting.
Any suggestions?
Normally this is done by default, but if not,
in logcat, the green plus sign, when you click it you get a dialog, fill the byApplicationName with your package name, and also your filter name with something, now you can filter your output according to your app
with that beeing said, sometimes you don't get the filter column info (application name) in logcat at all (blank), here (and I my self don't know the cause of it) just forget it for a while and retry again

Logging inside Framework?

I am trying to develop small application for reset Logging on Phone.
Can some one throw some Lights on how to achieve logging in AndriodRunTimeInit whenever there is exception? I want to write into file whenever there is RunTime exception.
It is recommended that you use Android's built in logging via Log. You can access the log via adb logcat and don't have to worry about anything else.

Categories

Resources