Logcat: make output using 'Log' class show full package name - android

I am using statements in my code in the same class that look like:
Log.i(TAG, "Writing command to initialize the FORA");
and
Log.i(TAG, "onDescriptorWrite signaled with status " + status);
yet the output from the first one is
Writing command to initialize the FORA
but the second one is
04-11 08:01:13.109 9030-9139/? I/com.lampreynetworks.ahd.transport.btle.b: onDescriptorWrite signaled with status 0
I would like to have the output with the package name in both cases. I thought that was determined by the TAG which in my case is
private static final String TAG = AndroidBtleHandler.class.getName();
I thought it might be because some of the statements were in a class within the class but that is not so. What do I need to do to get the full package name in the logcat output?

It's not dependent on the message itself. If you have directly following info logs with Log.i() from the same class in your Logcat, only the first one shows the class name. (I don't know why that is.) It's the same with Log.w().
If you use Log.d(), the class name is shown every time.

public class App {
public static String getTag() {
String tag = "";
final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
for (int i = 0; i < ste.length; i++) {
if (ste[i].getMethodName().equals("getTag")) {
tag = "("+ste[i + 1].getFileName() + ":" + ste[i + 1].getLineNumber()+")";
}
}
return tag;
}
}
And instead of Log.i(TAG, "Writing command to initialize the FORA"); use Log.i(App.getTag(), "Writing command to initialize the FORA")

Related

Check If String Exists In Array Using Array.asList(myArray) Doesnt Work

I am trying to see if FTP files exists on server before uploading them as I am using FTP4J library which uploads existing files (overwrites) but my check does not work even though the file exists in the array:
for (String ftpFolder : my_ftpFolders)
{
FTPFile[] list = ftpClient.list(ftpFolder);
for (int i = 0; i < list.length; i++)
{
String ftpFile = list[i].getName();
Log.v("LOG", "zzz_ARRAY: " +Arrays.asList(myFiles));
Log.v("LOG", "zzz_ftpFile: " +ftpFile);
if (Arrays.asList(myFiles).contains(ftpFile))
{
Log.v("LOG", "zzz_ftpFile EXIST: " +ftpFile);
}else{
Log.v("LOG", "zzz_ftpFile NOT EXIST: " +ftpFile);
}
}
}
Array Print (Arrays.asList(myFiles)):
[[/storage/emulated/0/Download/mohamed-nohassi-odxB5oIG_iA-unsplash.jpg, /storage/emulated/0/Download/atheek-mohomed-e0JOwGDsUHQ-unsplash.jpg]]
ftpFile Print:
mohamed-nohassi-odxB5oIG_iA-unsplash.jpg
As you can see, the file exists in the array but Array.asList(myFiles) returns false
That is because you are trying to seach the string mohamed-nohassi-odxB5oIG_iA-unsplash.jpg
in a list that does not contains it. The value in the list is /storage/emulated/0/Download/mohamed-nohassi-odxB5oIG_iA-unsplash.jpg, that is clearly different.
To obtain the result you're trying to achieve, you have to split the file name, and get rid of the path. You can do it with some string manipulation. First, import Collectors:
import java.util.stream.Collectors;
Then, you can use this code to test if the list contains the file:
if (Arrays.asList(myFiles).stream().map(el -> {
String[] tokenized = el.split("/");
return tokenized.length > 0 ? tokenized[tokenized.length-1] : "";
}).collect(Collectors.toList()).contains(ftpFile))
With this code, for each element in the list you're getting just the last part after the last "/", then you're checking if the ftpFile is contained in the list you'll obtain after taking only all the last parts.

My current location is not clear in Google map Android

I use Google map in my app. I try to get my current location I can show my location but the point is not clear. I don't Know what is problem .
As you can see picture below.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches {
public static void main(String args[]) {
// String to be scanned to find the pattern.
String line = "This order was placed for QT3000! OK?";
String pattern = "(.*)(\\d+)(.*)";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
if (m.find()) {
System.out.println("Found value: " + m.group(0));
System.out.println("Found value: " + m.group(1));
System.out.println("Found value: " + m.group(2));
} else {
System.out.println("NO MATCH");
}
}
}
Anyone know solution that ?
Your current location will not work correctly in emulator. Current location is not showing in my android app You should try it in actual device to get correct result. For focus in or out you may use this code:
mMap.addMarker(MarkerOptions().position(mylocation).title("My Location"))
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mylocation, 14f))
You can change 14f to what you want.

Exctract values from given text in android

I am trying to extract values from a given text.
Here is the text:
String 1: $.675.00 was spent on your CAPITAL ONE CREDIT Card ending 2123 on 2015-05-04:15:28:08 at Best Buy
String 2: $ 1,310.00 was spent on your Credit Card 5178XXXXXXXX6040 on MAY-04-15 at Amazon Stores.
I want to extract the following from string:
Amount after $
Credit Card text
Credit card number (in this case - 2123 or 5178XXXXXXXX6040)
at which place (in this case Best Buy or Amazon Stores).
To start I was trying extract all the numbers from the string: I tried the following:
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(string1);
Log.e("Value","from String"+m.group(0));
I am always getting the following error:
05-05 10:09:35.532: E/AndroidRuntime(13618): java.lang.IllegalStateException: No successful match so far
05-05 10:09:35.532: E/AndroidRuntime(13618): at java.util.regex.Matcher.ensureMatch(Matcher.java:471)
05-05 10:09:35.532: E/AndroidRuntime(13618): at java.util.regex.Matcher.group(Matcher.java:578)
Why isn't it matching even though the text has numbers?
Thanks!
As #justhecuke told there is no strict format for your string values to split using Patterns so I did using String functions, give a try
String crType1 = "CREDIT Card ending ";//mind the space at end
String crType2 = "Credit Card ";//mind the space at end
String rate, cardNo, at;
if (string1.contains(crType1)) {
rate = getStringValue(string1, "$", " ");
cardNo = getStringValue(string1, crType1, " ");
at = getAddress(string1);
} else {
rate = getStringValue(string1, "$", " ");
cardNo = getStringValue(string1, crType2, " ");
at = getAddress(string1);
}
System.out.println(String.format("Rate : %s Card No : %s Address : %s", rate, cardNo, at));
Methods
public static String getAddress(String string) {
return string.substring(string.lastIndexOf("at") + 2, string.length()).trim();
}
public static String getStringValue(String string, String startString, String endString) {
int startAt = string.indexOf(startString) + startString.length();
int endAt = string.indexOf(endString, startAt);
return string.substring(startAt, endAt).trim();
}
By the time you call m.group(0) you haven't actually told it to try to match stuff yet. This is why it gives you an IllegalStateException.
Try:
while(m.find()) {
Log.e("Value",String.format("From String: '%s'", m.group()));
}

Android - how to read first line of the string

i have string with \n, i would like to read first line of the string. i have tried following but its giving error.
String lines[] = plain.split("\\r?\\n");
String a = String.parse(lines[0]);
String b = String.parse(lines[1]);
in same time if someone tells me how to replace first line data with another value would be great.
my data is as follows
123456
A B C D
p o s j s u w
Thanks
I think you mean to only put \n and \r using double back slash will give you a literal \n
"\\n" = \n
"\n" = newline
I'm also not sure about your ? in the line
I think you want something like this (depending on the type of line endings):
String[] lines = plain.Split(new string[] {"\r\n", "\n", "\r" },StringSplitOptions.None);
After that you should probably step through your string array using for each or similar, not just assume that a you will have a [0] and [1] in the array.
However assuming you do have two lines accessing them will be
a = lines[0];
b = lines[1];
replacing a value would be
a = lines[0].replace("stuff","things");
Hope fully this will help yours..
import java.util.Arrays;
public class JavaApplication1 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
String data = "123456\n"
+ "\n"
+ "A B C D\n"
+ "\n"
+ "p o s j s u w\n"
+ "\n"
+ "Thanks";
String[] lines = data.split("\\n");
System.out.println("Before "+Arrays.toString(lines));
lines[0]=lines[0].replace("123456","ABCD PUUJA");
System.out.println("After "+Arrays.toString(lines));
}
}
To read a line we use:
String myline = reader.readLine()
Now in your case we can use the follwoing code to read the first line however We will not use "\n" but we will look for another character like "&", "$" or even "#" and use it as below:
String mytext = "This is my code.$ I want to read the first line here.$ Please check it out";
thetext = mytext .replace("$", System.getProperty("line.separator"));
String[] strings = TextUtils.split(mytext, "$");
String a = strings[0].trim();
String b = strings[1].trim();
String a is the first line while String b is the second line
In case you use Kotlin, you can use lines() method
val myString = "Hello \n NewLine"
myString.lines() // this will give you a List<String> type

Program seems to be ignoring lines of code

I need help with this function.
I know that the if statement recognizes my input because it affects the program elsewhere, but I'm not sure what's going on because this particular Log doesn't display anything even in adb logcat.
Other Log statements in the same class file that this function is from display just fine, and the value update does seem to be changing ("show all" blanks it for some reason but I can figure that out after I get the log to work.)
I am unsure how to search for this problem because it is very specific and I have no idea what causes it (probably something simple that I didn't think of, though.)
void command(String input)
{
//do stuff here
//update = whatever
if(input.equalsIgnoreCase("show all"))
{
update=printAllRooms();
Log.i(input, update);
}
else update=input; //just for testing, will delete later
}
the printAllRooms function:
public String printAllRooms() //for debug purposes
{
String result = "";
for (Iterator<Room> iterator = rooms.iterator(); iterator.hasNext();) {
Room current = iterator.next();
result = result + current.toString()+"\n";
Log.i("printallrooms", current.toString());
}
return result;
}
A note on using Log.
The first argument sent to Log is typically a fixed string indicating the name of the class you are in.
So at the top of your class you might define:
private static final String TAG = "MyClassName";
Then you would use TAG for your log statements in that class.
Log.i(TAG, "My input was: " + input + " Update was: " + update;
To put it mildly, your function looks quite odd. Set a breakpoint at your Log statement, run the debugger and then inspect the variable value contained in update. Most likely, printAllRooms() is not doing what you think.
If the iterator doesn't work for you, try using the For-Each loop:
for (Room r : rooms) {
result = result + r.toString()+"\n";
Log.i("printallrooms", r.toString());
}

Categories

Resources