Skip to content

Commit

Permalink
reduced verbosity by default with function to adjust
Browse files Browse the repository at this point in the history
  • Loading branch information
plammer committed Oct 9, 2019
1 parent 569d383 commit 3863ee7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
69 changes: 54 additions & 15 deletions library/src/main/java/com/pixplicity/sharp/Sharp.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import android.os.Build;
import android.os.Looper;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand Down Expand Up @@ -94,6 +95,14 @@ public abstract class Sharp {

static final String TAG = Sharp.class.getSimpleName();

public static final int LOG_LEVEL_ERROR = 1;
public static final int LOG_LEVEL_WARN = 2;
public static final int LOG_LEVEL_INFO = 3;
static int LOG_LEVEL = LOG_LEVEL_ERROR;

@IntDef({LOG_LEVEL_ERROR, LOG_LEVEL_WARN, LOG_LEVEL_INFO})
public @interface LogLevel {}

private static String sAssumedUnit;
private static HashMap<String, String> sTextDynamic = null;

Expand Down Expand Up @@ -130,6 +139,10 @@ public static Unit matches(String value) {
}
}

public static void setLogLevel(@LogLevel int logLevel) {
LOG_LEVEL = logLevel;
}

@SuppressWarnings("unused")
public static void prepareTexts(HashMap<String, String> texts) {
sTextDynamic = texts;
Expand Down Expand Up @@ -1302,7 +1315,9 @@ public void read(InputStream in) {
int magicInt = (magic[0] + (((int) magic[1]) << 8)) & 0xffff;
in.reset();
if (r == 2 && magicInt == GZIPInputStream.GZIP_MAGIC) {
Log.d(TAG, "SVG is gzipped");
if (LOG_LEVEL >= LOG_LEVEL_INFO) {
Log.d(TAG, "SVG is gzipped");
}
in = new GZIPInputStream(in);
}
}
Expand All @@ -1315,7 +1330,9 @@ public void read(InputStream in) {
sTextDynamic.clear();
sTextDynamic = null;
}
Log.v(TAG, "Parsing complete in " + (System.currentTimeMillis() - start) + " ms.");
if (LOG_LEVEL >= LOG_LEVEL_INFO) {
Log.v(TAG, "Parsing complete in " + (System.currentTimeMillis() - start) + " ms.");
}
} catch (IOException | SAXException | ParserConfigurationException e) {
Log.e(TAG, "Failed parsing SVG", e);
throw new SvgParseException(e);
Expand Down Expand Up @@ -1373,7 +1390,7 @@ private boolean doFill(Properties atts, RectF boundingBox) {
}
return true;
} else {
Log.d(TAG, "Didn't find shader, using black: " + id);
//Log.d(TAG, "Didn't find shader, using black: " + id);
mFillPaint.setShader(null);
doColor(atts, Color.BLACK, true, mFillPaint);
return true;
Expand All @@ -1390,7 +1407,9 @@ private boolean doFill(Properties atts, RectF boundingBox) {
doColor(atts, color, true, mFillPaint);
return true;
} else {
Log.d(TAG, "Unrecognized fill color, using black: " + fillString);
if (LOG_LEVEL >= LOG_LEVEL_WARN) {
Log.w(TAG, "Unrecognized fill color, using black: " + fillString);
}
doColor(atts, Color.BLACK, true, mFillPaint);
return true;
}
Expand Down Expand Up @@ -1503,7 +1522,9 @@ private boolean doStroke(Properties atts, RectF boundingBox) {
}
return true;
} else {
Log.d(TAG, "Didn't find shader, using black: " + id);
if (LOG_LEVEL >= LOG_LEVEL_WARN) {
Log.w(TAG, "Didn't find shader, using black: " + id);
}
mStrokePaint.setShader(null);
doColor(atts, Color.BLACK, true, mStrokePaint);
return true;
Expand All @@ -1514,7 +1535,9 @@ private boolean doStroke(Properties atts, RectF boundingBox) {
doColor(atts, color, false, mStrokePaint);
return true;
} else {
Log.d(TAG, "Unrecognized stroke color, using black: " + strokeString);
if (LOG_LEVEL >= LOG_LEVEL_WARN) {
Log.w(TAG, "Unrecognized stroke color, using black: " + strokeString);
}
doColor(atts, Color.BLACK, true, mStrokePaint);
return true;
}
Expand Down Expand Up @@ -1594,7 +1617,9 @@ private void finishGradients() {
positions[i] = gradient.mPositions.get(i);
}
if (colors.length == 0) {
Log.w(TAG, "bad gradient, id=" + gradient.mId);
if (LOG_LEVEL >= LOG_LEVEL_WARN) {
Log.w(TAG, "Failed to parse gradient for id " + gradient.mId);
}
}
if (gradient.mIsLinear) {
gradient.mShader = new LinearGradient(gradient.mX1, gradient.mY1, gradient.mX2, gradient.mY2, colors, positions, gradient.mTileMode);
Expand Down Expand Up @@ -1734,7 +1759,9 @@ public void startElement(String namespaceURI, String localName, String qName, At
if (width < 0 || height < 0) {
width = 100;
height = 100;
Log.w(TAG, "element '" + localName + "' does not provide its dimensions; using " + width + "x" + height);
if (LOG_LEVEL >= LOG_LEVEL_WARN) {
Log.w(TAG, "Element '" + localName + "' does not provide its dimensions; using " + width + "x" + height);
}
}
mBounds = new RectF(x, y, x + width, y + height);
//Log.d(TAG, "svg boundaries: " + mBounds);
Expand Down Expand Up @@ -2003,7 +2030,9 @@ public void startElement(String namespaceURI, String localName, String qName, At
mReadIgnoreStack.push(localName);
break;
default:
Log.w(TAG, "Unrecognized SVG command: " + localName);
if (LOG_LEVEL >= LOG_LEVEL_WARN) {
Log.w(TAG, "Unrecognized SVG command: " + localName);
}
break;
}
}
Expand Down Expand Up @@ -2353,7 +2382,9 @@ private Typeface setTypeface(Attributes atts, Properties props, AssetManager ass
String typefaceFile = "fonts/" + family + ".ttf";
try {
plain = Typeface.createFromAsset(assetManager, typefaceFile);
Log.d(TAG, "loaded typeface from assets: " + typefaceFile);
if (LOG_LEVEL >= LOG_LEVEL_INFO) {
Log.d(TAG, "Loaded typeface from assets: " + typefaceFile);
}
} catch (RuntimeException e) {
boolean found = true;
try {
Expand All @@ -2365,12 +2396,18 @@ private Typeface setTypeface(Attributes atts, Properties props, AssetManager ass
}
}
} catch (IOException e1) {
Log.e(TAG, "Failed listing assets directory for /fonts", e);
if (LOG_LEVEL >= LOG_LEVEL_ERROR) {
Log.e(TAG, "Failed listing assets directory for /fonts", e);
}
}
if (!found) {
Log.e(TAG, "Typeface is missing from assets: " + typefaceFile);
if (LOG_LEVEL >= LOG_LEVEL_ERROR) {
Log.e(TAG, "Typeface is missing from assets: " + typefaceFile);
}
} else {
Log.e(TAG, "Failed to create typeface from assets: " + typefaceFile, e);
if (LOG_LEVEL >= LOG_LEVEL_ERROR) {
Log.e(TAG, "Failed to create typeface from assets: " + typefaceFile, e);
}
}
plain = null;
}
Expand All @@ -2379,8 +2416,10 @@ private Typeface setTypeface(Attributes atts, Properties props, AssetManager ass
return Typeface.create(plain, styleParam);
}
} else {
Log.e(TAG, "Typefaces can only be loaded if assets are provided; " +
"invoke " + Sharp.class.getSimpleName() + " with .withAssets()");
if (LOG_LEVEL >= LOG_LEVEL_ERROR) {
Log.e(TAG, "Typefaces can only be loaded if assets are provided; " +
"invoke " + Sharp.class.getSimpleName() + " with .withAssets()");
}
}
}
if (defaultTypeface == null) {
Expand Down
8 changes: 4 additions & 4 deletions library/src/main/java/com/pixplicity/sharp/SharpDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void draw(Canvas parentCanvas) {
// creates a new mutable bitmap
int w = (int) (bounds.width() * mCacheScale);
int h = (int) (bounds.height() * mCacheScale);
Log.v(TAG, "cache bitmap " + w + "x" + h);
//Log.v(TAG, "cache bitmap " + w + "x" + h);
mCacheBitmap = Bitmap.createBitmap(
w,
h,
Expand All @@ -132,8 +132,6 @@ public void draw(Canvas parentCanvas) {
if (canvas != null) {
save(canvas);
canvas.clipRect(bounds);
Log.v(TAG, "canvas " + canvas.getWidth() + "x" + canvas.getHeight());
Log.v(TAG, "bounds " + bounds.toString());
canvas.translate(bounds.left, bounds.top);
onBeforeScaleAndDraw(canvas, picture, bounds);
canvas.scale(mScaleX, mScaleY, 0, 0);
Expand All @@ -150,7 +148,9 @@ public void draw(Canvas parentCanvas) {
parentCanvas.restore();
}
}
Log.v(TAG, "Drawing " + hashCode() + " complete in " + (System.currentTimeMillis() - start) + " ms.");
if (Sharp.LOG_LEVEL >= Sharp.LOG_LEVEL_INFO) {
Log.v(TAG, "Drawing " + hashCode() + " complete in " + (System.currentTimeMillis() - start) + " ms.");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ protected void onCreate(Bundle savedInstanceState) {
mImageView = findViewById(R.id.iv_image);
mButton = findViewById(R.id.bt_button);

Sharp.setLogLevel(Sharp.LOG_LEVEL_INFO);

mSvg = Sharp.loadResource(getResources(), R.raw.cartman);
// If you want to load typefaces from assets:
// .withAssets(getAssets());
Expand Down

0 comments on commit 3863ee7

Please sign in to comment.