Java tutorial
/* * Appspresso * * Copyright (c) 2011 KT Hitel Corp. * * Appspresso SDK may be freely distributed under the MIT license. */ package com.appspresso.api; import org.apache.commons.logging.Log; import android.text.TextUtils; import com.appspresso.internal.AxConfig; /** * ?? ? ?. * <p> * ? ? ? ({@link android.util.Log})? ? {@link org.apache.commons.logging.Log} * , {@link org.apache.commons.logging.LogFactory} . * <p> * ? . * <p> * : * * <pre> * public class MyClass { * private static final AxLog LOG = AxLog.getLog(MyClass.class); * * ... * * private void someMethod() { * ... * * if(LOG.isDebugEnabled()) { LOG.debug("blah blah blah..."); } * * ... * } * * ... * } * </pre> * * @see org.apache.commons.logging.Log * @see android.util.Log * @version 1.0 */ public class AxLog implements Log { private static final String DEF_TAG = "Appspresso"; private static final int DEF_LEVEL = AxConfig.getAttributeAsBoolean("app.devel", true) ? android.util.Log.VERBOSE : android.util.Log.INFO; private final String tag; private final int level; /** * ? ? ?. * * @param tag * @param level . {@link android.util.Log}? ? ? */ private AxLog(String tag, int level) { this.tag = tag; this.level = level; } /** * ? . * * @return true, false. * @see android.util.Log#VERBOSE */ @Override public boolean isTraceEnabled() { return level <= android.util.Log.VERBOSE; } /** * . * * @return true, false. * @see android.util.Log#DEBUG */ @Override public boolean isDebugEnabled() { return level <= android.util.Log.DEBUG; } /** * . * * @return true, false. * @see android.util.Log#INFO */ @Override public boolean isInfoEnabled() { return level <= android.util.Log.INFO; } /** * . * * @return true, false. * @see android.util.Log#WARN */ @Override public boolean isWarnEnabled() { return level <= android.util.Log.WARN; } /** * ? . * * @return true, false. * @see android.util.Log#ERROR */ @Override public boolean isErrorEnabled() { return level <= android.util.Log.ERROR; } /** * ?? ? . * * @return true, false. * @see android.util.Log#ASSERT */ @Override public boolean isFatalEnabled() { return level <= android.util.Log.ASSERT; } /** * ? . * * @param o * @see android.util.Log#v(String, String) */ @Override public void trace(Object o) { if (level <= android.util.Log.VERBOSE) { android.util.Log.v(tag, o.toString()); } } /** * ? . * * @param o * @param t * @see android.util.Log#v(String, String, Throwable) */ @Override public void trace(Object o, Throwable t) { if (level <= android.util.Log.VERBOSE) { android.util.Log.v(tag, o.toString(), t); } } /** * . * * @param o * @see android.util.Log#d(String, String) */ @Override public void debug(Object o) { if (level <= android.util.Log.DEBUG) { android.util.Log.d(tag, o.toString()); } } /** * . * * @param o * @param t * @see android.util.Log#d(String, String, Throwable) */ @Override public void debug(Object o, Throwable t) { if (level <= android.util.Log.DEBUG) { android.util.Log.d(tag, o.toString(), t); } } /** * . * * @param o * @see android.util.Log#i(String, String) */ @Override public void info(Object o) { if (level <= android.util.Log.INFO) { android.util.Log.i(tag, o.toString()); } } /** * . * * @param o * @param t * @see android.util.Log#i(String, String, Throwable) */ @Override public void info(Object o, Throwable t) { if (level <= android.util.Log.INFO) { android.util.Log.i(tag, o.toString(), t); } } /** * . * * @param o * @see android.util.Log#w(String, String) */ @Override public void warn(Object o) { if (level <= android.util.Log.WARN) { android.util.Log.w(tag, o.toString()); } } /** * . * * @param o * @param t * @see android.util.Log#w(String, String, Throwable) */ @Override public void warn(Object o, Throwable t) { if (level <= android.util.Log.WARN) { android.util.Log.w(tag, o.toString(), t); } } /** * ?(error) . * * @param o * @see android.util.Log#e(String, String) */ @Override public void error(Object o) { if (level <= android.util.Log.ERROR) { android.util.Log.e(tag, o.toString()); } } /** * ? . * * @param o * @param t * @see android.util.Log#e(String, String, Throwable) */ @Override public void error(Object o, Throwable t) { if (level <= android.util.Log.ERROR) { android.util.Log.e(tag, o.toString(), t); } } /** * ?? ? . * * @param o * @see android.util.Log#wtf(String, String) */ @Override public void fatal(Object o) { if (level <= android.util.Log.ASSERT) { android.util.Log.println(android.util.Log.ASSERT, tag, o.toString()); } } /** * ?? ? . * * @param o * @param t * @see android.util.Log#wtf(String, String, Throwable) */ @Override public void fatal(Object o, Throwable t) { if (level <= android.util.Log.ASSERT) { // api8+: android.util.Log.wtf(tag, o.toString(), t); android.util.Log.println(android.util.Log.ASSERT, tag, o.toString() + '\n' + android.util.Log.getStackTraceString(t)); } } // // log factory methods // private static final int MAX_TAG_LEN = 20; // 23; private static String getTagName(Class<?> klass) { if (klass == null) { return DEF_TAG; } StringBuilder result = new StringBuilder(MAX_TAG_LEN); String className = klass.getSimpleName(); result.append(className); String packageName = klass.getPackage().getName(); for (String token : packageName.split(".")) { if (className.length() > MAX_TAG_LEN) { result.insert(0, ".."); break; } result.insert(0, token.charAt(0)).insert(0, '.'); } return result.toString(); } // @Override /** * ? ?(FQCN)? ? ?. * * @return AxLog ? */ public static Log getLog(Class<?> klass) { return new AxLog(getTagName(klass), DEF_LEVEL); } // @Override /** * ? ?. * * @return AxLog ? */ public static Log getLog(String name) { return new AxLog(TextUtils.isEmpty(name) ? DEF_TAG : name, DEF_LEVEL); } }