Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Logging Strings with newline character or <br>

It seems that if you call

String text = "String<br>String";
Log.d(TAG, text);

it automatically parses the String to take two lines. The same goes for new line (\n) characters. That makes debugging more complicated. Is there a way to tell the logger to give me the exact String?

like image 723
Christian Avatar asked Sep 08 '14 15:09

Christian


Video Answer


2 Answers

The arguments to the methods in Log class are Java strings, so escaping special characters is just like in Java. For example,

String text = "String\nString";
Log.d("TEST!!", text);

Will give you:

D/TEST!!﹕ String
    String

while:

String text = "String\\nString";
Log.d("TEST!!", text);

will give you:

D/TEST!!﹕ String\nString

in the logcat.

As far as <BR>, I'm not seeing the same effect as you. Specifically,

String text = "String<br>String";
Log.d("TEST!!", text);

Produces:

D/TEST!!﹕ String<br>String

So I am unable to reproduce your actual problem. However, in general special characters in the Log strings are escaped just like any other Java strings. The logger is dumb and there's no settings to automatically escape special characters; you'll have to do this yourself for arbitrary strings. The Log methods just turn around and call println_native.

like image 58
Paul Ratazzi Avatar answered Sep 16 '22 15:09

Paul Ratazzi


I use System.getProperty("line.separator")

ArrayList<String> txts = new ArrayList<String>();
txts.add("aoeuaeou");
txts.add("snhsnthsnth");
String msg = TextUtils.join(System.getProperty("line.separator"),txts);
Log.d(TAG, "Bla bla bla: "+ msg );

show in the log like

Bla bla bla: aoeuaeou
snhsnthsnth
like image 35
imaginabit Avatar answered Sep 20 '22 15:09

imaginabit