Skip to content

Commit

Permalink
Make Intent and Bundle formatters android-only
Browse files Browse the repository at this point in the history
  • Loading branch information
elvishew committed Oct 29, 2020
1 parent ace60f4 commit 446c092
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@

package com.elvishew.xlog.internal;

import android.content.Intent;
import android.os.Bundle;

import com.elvishew.xlog.flattener.DefaultFlattener;
import com.elvishew.xlog.flattener.Flattener;
import com.elvishew.xlog.flattener.Flattener2;
import com.elvishew.xlog.formatter.border.BorderFormatter;
import com.elvishew.xlog.formatter.border.DefaultBorderFormatter;
import com.elvishew.xlog.formatter.message.json.DefaultJsonFormatter;
import com.elvishew.xlog.formatter.message.json.JsonFormatter;
import com.elvishew.xlog.formatter.message.object.BundleFormatter;
import com.elvishew.xlog.formatter.message.object.IntentFormatter;
import com.elvishew.xlog.formatter.message.object.ObjectFormatter;
import com.elvishew.xlog.formatter.message.throwable.DefaultThrowableFormatter;
import com.elvishew.xlog.formatter.message.throwable.ThrowableFormatter;
Expand All @@ -46,8 +41,6 @@
import com.elvishew.xlog.printer.file.naming.ChangelessFileNameGenerator;
import com.elvishew.xlog.printer.file.naming.FileNameGenerator;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -59,15 +52,6 @@ public class DefaultsFactory {

private static final long DEFAULT_LOG_FILE_MAX_SIZE = 1024 * 1024; // 1M bytes;

private static final Map<Class<?>, ObjectFormatter<?>> BUILTIN_OBJECT_FORMATTERS;

static {
Map<Class<?>, ObjectFormatter<?>> objectFormatters = new HashMap<>();
objectFormatters.put(Bundle.class, new BundleFormatter());
objectFormatters.put(Intent.class, new IntentFormatter());
BUILTIN_OBJECT_FORMATTERS = Collections.unmodifiableMap(objectFormatters);
}

/**
* Create the default JSON formatter.
*/
Expand Down Expand Up @@ -158,6 +142,6 @@ public static CleanStrategy createCleanStrategy() {
* @return the builtin object formatters
*/
public static Map<Class<?>, ObjectFormatter<?>> builtinObjectFormatters() {
return BUILTIN_OBJECT_FORMATTERS;
return Platform.get().builtinObjectFormatters();
}
}
28 changes: 28 additions & 0 deletions library/src/main/java/com/elvishew/xlog/internal/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@
package com.elvishew.xlog.internal;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import com.elvishew.xlog.formatter.message.object.BundleFormatter;
import com.elvishew.xlog.formatter.message.object.IntentFormatter;
import com.elvishew.xlog.formatter.message.object.ObjectFormatter;
import com.elvishew.xlog.printer.AndroidPrinter;
import com.elvishew.xlog.printer.ConsolePrinter;
import com.elvishew.xlog.printer.Printer;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Platform {

private static final Platform PLATFORM = findPlatform();
Expand All @@ -40,6 +49,10 @@ Printer defaultPrinter() {
return new ConsolePrinter();
}

Map<Class<?>, ObjectFormatter<?>> builtinObjectFormatters() {
return Collections.emptyMap();
}

public void warn(String msg) {
System.out.println(msg);
}
Expand All @@ -56,6 +69,16 @@ private static Platform findPlatform() {
}

static class Android extends Platform {

private static final Map<Class<?>, ObjectFormatter<?>> BUILTIN_OBJECT_FORMATTERS;

static {
Map<Class<?>, ObjectFormatter<?>> objectFormatters = new HashMap<>();
objectFormatters.put(Bundle.class, new BundleFormatter());
objectFormatters.put(Intent.class, new IntentFormatter());
BUILTIN_OBJECT_FORMATTERS = Collections.unmodifiableMap(objectFormatters);
}

@Override
String lineSeparator() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
Expand All @@ -69,6 +92,11 @@ Printer defaultPrinter() {
return new AndroidPrinter();
}

@Override
Map<Class<?>, ObjectFormatter<?>> builtinObjectFormatters() {
return BUILTIN_OBJECT_FORMATTERS;
}

@Override
public void warn(String msg) {
android.util.Log.w("XLog", msg);
Expand Down

0 comments on commit 446c092

Please sign in to comment.