From 4b7cde9304fc6008fc5863276f2a0e8a64ca1098 Mon Sep 17 00:00:00 2001 From: Riningan Date: Sun, 6 Jan 2019 21:11:33 +0500 Subject: [PATCH] version 1.0 --- logger/build.gradle | 6 +- .../src/main/java/com/riningan/util/Log.java | 285 ++++++++---------- .../main/java/com/riningan/util/Logger.java | 42 +-- .../java/com/riningan/util/LoggerConfig.java | 10 + .../java/com/riningan/logger/SampleApp.java | 1 + 5 files changed, 153 insertions(+), 191 deletions(-) diff --git a/logger/build.gradle b/logger/build.gradle index 1d03815..8db7cea 100644 --- a/logger/build.gradle +++ b/logger/build.gradle @@ -5,8 +5,8 @@ android { defaultConfig { minSdkVersion 16 targetSdkVersion 28 - versionCode 1 - versionName "0.1" + versionCode 2 + versionName "1.0" } } @@ -23,7 +23,7 @@ ext { siteUrl = 'https://github.com/Riningan/Logger' gitUrl = 'https://github.com/Riningan/Logger.git' - libraryVersion = '0.1' + libraryVersion = '1.0' developerId = 'Riningan' developerName = 'Vadim Akhmarov' diff --git a/logger/src/main/java/com/riningan/util/Log.java b/logger/src/main/java/com/riningan/util/Log.java index 0aebc5e..beff6d2 100644 --- a/logger/src/main/java/com/riningan/util/Log.java +++ b/logger/src/main/java/com/riningan/util/Log.java @@ -8,205 +8,153 @@ public class Log { - private LoggerConfig config; - private Object mThis = null; + private final LoggerConfig mConfig; private final Semaphore mSemaphore = new Semaphore(1, true); private final LinkedList> mLogQueue = new LinkedList<>(); + private Object mThis = null; Log(LoggerConfig config) { - this.config = config; + mConfig = config; + } + + + void forThis(Object object) { + try { + mSemaphore.acquire(); + } catch (InterruptedException ignored) { + } + mThis = object; } public void debug() { - String msg = getClassAndMethodOfThis(); - addDebug(msg); - mThis = null; - mSemaphore.release(); + if (mConfig.isEnabled()) { + String msg = getClassAndMethod(); + addDebug(msg); + mThis = null; + mSemaphore.release(); + } } public void debug(String message) { - String msg = getClassAndMethodOfThis() + " - " + message; - addDebug(msg); - mThis = null; - mSemaphore.release(); + if (mConfig.isEnabled()) { + String msg = getClassAndMethod() + " - " + message; + addDebug(msg); + mThis = null; + mSemaphore.release(); + } } public void debug(String param, String value) { - String msg = getClassAndMethodOfThis() + "(" + param + " = " + value + ")"; - addDebug(msg); - mThis = null; - mSemaphore.release(); + if (mConfig.isEnabled()) { + String msg = getClassAndMethod() + "(" + param + " = " + value + ")"; + addDebug(msg); + mThis = null; + mSemaphore.release(); + } } - public void debug(String param, int value) { - String msg = getClassAndMethodOfThis() + "(" + param + " = " + value + ")"; - addDebug(msg); - mThis = null; - mSemaphore.release(); + public void debug(String param, boolean value) { + if (mConfig.isEnabled()) { + String msg = getClassAndMethod() + "(" + param + " = " + value + ")"; + addDebug(msg); + mThis = null; + mSemaphore.release(); + } } - public void debug(String param, boolean value) { - String msg = getClassAndMethodOfThis() + "(" + param + " = " + value + ")"; - addDebug(msg); - mThis = null; - mSemaphore.release(); + public void debug(String param, int value) { + if (mConfig.isEnabled()) { + String msg = getClassAndMethod() + "(" + param + " = " + value + ")"; + addDebug(msg); + mThis = null; + mSemaphore.release(); + } } public void debug(String param, long value) { - String msg = getClassAndMethodOfThis() + "(" + param + " = " + value + ")"; - addDebug(msg); - mThis = null; - mSemaphore.release(); + if (mConfig.isEnabled()) { + String msg = getClassAndMethod() + "(" + param + " = " + value + ")"; + addDebug(msg); + mThis = null; + mSemaphore.release(); + } } - public void debug(String param, double value) { - String msg = getClassAndMethodOfThis() + "(" + param + " = " + value + ")"; - addDebug(msg); - mThis = null; - mSemaphore.release(); + public void debug(String param, float value) { + if (mConfig.isEnabled()) { + String msg = getClassAndMethod() + "(" + param + " = " + value + ")"; + addDebug(msg); + mThis = null; + mSemaphore.release(); + } } - public void debug(String param, float value) { - String msg = getClassAndMethodOfThis() + "(" + param + " = " + value + ")"; - addDebug(msg); - mThis = null; - mSemaphore.release(); + public void debug(String param, double value) { + if (mConfig.isEnabled()) { + String msg = getClassAndMethod() + "(" + param + " = " + value + ")"; + addDebug(msg); + mThis = null; + mSemaphore.release(); + } } public void debug(String param, String value, String... params) { - StringBuilder msg = new StringBuilder(getClassAndMethodOfThis() + "(" + param + " = " + value); - for (int i = 0; i < params.length - 1; i += 2) { - msg.append(", ").append(params[i]).append(" = ").append(params[i + 1]); - } - if (params.length % 2 == 0) { - msg.append(")"); - } else { - msg.append(") - ").append(params[params.length - 1]); + if (mConfig.isEnabled()) { + StringBuilder msg = new StringBuilder(getClassAndMethod() + "(" + param + " = " + value); + for (int i = 0; i < params.length - 1; i += 2) { + msg.append(", ").append(params[i]).append(" = ").append(params[i + 1]); + } + if (params.length % 2 == 0) { + msg.append(")"); + } else { + msg.append(") - ").append(params[params.length - 1]); + } + addDebug(msg.toString()); + mThis = null; + mSemaphore.release(); } - addDebug(msg.toString()); - mThis = null; - mSemaphore.release(); } public void error(String message) { - int line = Thread.currentThread().getStackTrace()[4].getLineNumber(); - String msg = getClassAndMethodOfThis() + "(" + Integer.toString(line) + ") - " + message; - addError(msg); - mThis = null; - mSemaphore.release(); + if (mConfig.isEnabled()) { + int line = Thread.currentThread().getStackTrace()[4].getLineNumber(); + String msg = getClassAndMethod() + "(" + Integer.toString(line) + ") - " + message; + addError(msg); + mThis = null; + mSemaphore.release(); + } } public void error(Throwable throwable) { - int line = Thread.currentThread().getStackTrace()[4].getLineNumber(); - String msg = getClassAndMethodOfThis() + "(" + Integer.toString(line) + ") - " + throwable.getMessage(); - addError(msg); - mThis = null; - mSemaphore.release(); + if (mConfig.isEnabled()) { + int line = Thread.currentThread().getStackTrace()[4].getLineNumber(); + String msg = getClassAndMethod() + "(" + Integer.toString(line) + ") - " + throwable.getMessage(); + addError(msg); + mThis = null; + mSemaphore.release(); + } } public void info() { - String msg = getClassAndMethodOfThis(); - addInfo(msg); - mThis = null; - mSemaphore.release(); - } - - public void info(String message) { - String msg = getClassAndMethodOfThis() + " - " + message; - addInfo(msg); - mThis = null; - mSemaphore.release(); - } - - - void forThis(Object object) { - try { - mSemaphore.acquire(); - } catch (InterruptedException ignored) { + if (mConfig.isEnabled()) { + String msg = getClassAndMethod(); + addInfo(msg); + mThis = null; + mSemaphore.release(); } - mThis = object; - } - - - void debugThread() { - String msg = getClassAndMethod(); - addDebug(msg); - } - - void debugThread(String message) { - String msg = getClassAndMethod() + " - " + message; - addDebug(msg); - } - - void debugThread(String param, String value) { - String msg = getClassAndMethod() + "(" + param + " = " + value + ")"; - addDebug(msg); - } - - void debugThread(String param, int value) { - String msg = getClassAndMethod() + "(" + param + " = " + value + ")"; - addDebug(msg); - } - - void debugThread(String param, boolean value) { - String msg = getClassAndMethod() + "(" + param + " = " + value + ")"; - addDebug(msg); - } - - void debugThread(String param, long value) { - String msg = getClassAndMethod() + "(" + param + " = " + value + ")"; - addDebug(msg); - } - - void debugThread(String param, double value) { - String msg = getClassAndMethod() + "(" + param + " = " + value + ")"; - addDebug(msg); } - void debugThread(String param, float value) { - String msg = getClassAndMethod() + "(" + param + " = " + value + ")"; - addDebug(msg); - } - - void debugThread(String param, String value, String... params) { - StringBuilder msg = new StringBuilder(getClassAndMethod() + "(" + param + " = " + value); - for (int i = 0; i < params.length - 1; i += 2) { - msg.append(", ").append(params[i]).append(" = ").append(params[i + 1]); - } - if (params.length % 2 == 0) { - msg.append(")"); - } else { - msg.append(") - ").append(params[params.length - 1]); + public void info(String message) { + if (mConfig.isEnabled()) { + String msg = getClassAndMethod() + " - " + message; + addInfo(msg); + mThis = null; + mSemaphore.release(); } - addDebug(msg.toString()); - } - - - void errorThread(String message) { - int line = Thread.currentThread().getStackTrace()[5].getLineNumber(); - String msg = getClassAndMethod() + "(" + Integer.toString(line) + ") - " + message; - addError(msg); - } - - void errorThread(Throwable throwable) { - int line = Thread.currentThread().getStackTrace()[5].getLineNumber(); - String msg = getClassAndMethod() + "(" + Integer.toString(line) + ") - " + throwable.getMessage(); - addError(msg); - } - - - void infoThread() { - String msg = getClassAndMethod(); - addInfo(msg); - } - - void infoThread(String message) { - String msg = getClassAndMethod() + " - " + message; - addInfo(msg); } @@ -214,40 +162,43 @@ private synchronized void addDebug(String message) { String fullMessage = getPrefix() + ": " + message; android.util.Log.d(MessageType.DEBUG.name(), fullMessage); addToQueue(MessageType.DEBUG, fullMessage); - config.onNewMessage(MessageType.DEBUG, fullMessage); + mConfig.onNewMessage(MessageType.DEBUG, fullMessage); } private synchronized void addError(String message) { String fullMessage = getPrefix() + ": " + message; android.util.Log.e(MessageType.ERROR.name(), fullMessage); addToQueue(MessageType.ERROR, fullMessage); - config.onNewMessage(MessageType.ERROR, fullMessage); + mConfig.onNewMessage(MessageType.ERROR, fullMessage); } private synchronized void addInfo(String message) { String fullMessage = getPrefix() + ": " + message; android.util.Log.i(MessageType.INFO.name(), fullMessage); addToQueue(MessageType.INFO, fullMessage); - config.onNewMessage(MessageType.INFO, fullMessage); + mConfig.onNewMessage(MessageType.INFO, fullMessage); } private String getClassAndMethod() { - String className = Thread.currentThread().getStackTrace()[5].getClassName(); - String methodName = Thread.currentThread().getStackTrace()[5].getMethodName(); - return className.substring(config.getApplicationIdLength()) + "." + methodName; + String className; + String methodName; + if (mThis == null) { + className = Thread.currentThread().getStackTrace()[5].getClassName(); + methodName = Thread.currentThread().getStackTrace()[5].getMethodName(); + } else { + className = mThis.getClass().getName(); + methodName = Thread.currentThread().getStackTrace()[4].getMethodName(); + } + return className.substring(mConfig.getApplicationIdLength()) + "." + methodName; } - private String getClassAndMethodOfThis() { - String className = mThis.getClass().getName(); - String methodName = Thread.currentThread().getStackTrace()[4].getMethodName(); - return className.substring(config.getApplicationIdLength()) + "." + methodName; - } private String getPrefix() { - return config.getPreffix() + "time(" + config.getDateTime() + "): process(" + Process.myPid() + "): thread(" + Thread.currentThread().getId() + ")"; + return mConfig.getPreffix() + "time(" + mConfig.getDateTime() + "): process(" + Process.myPid() + "): thread(" + Thread.currentThread().getId() + ")"; } + private void addToQueue(MessageType type, String msg) { synchronized (mLogQueue) { if (mLogQueue.size() > 100) { @@ -256,4 +207,4 @@ private void addToQueue(MessageType type, String msg) { mLogQueue.add(new Pair<>(type, msg)); } } -} +} \ No newline at end of file diff --git a/logger/src/main/java/com/riningan/util/Logger.java b/logger/src/main/java/com/riningan/util/Logger.java index 727c4ba..7acf544 100644 --- a/logger/src/main/java/com/riningan/util/Logger.java +++ b/logger/src/main/java/com/riningan/util/Logger.java @@ -3,7 +3,7 @@ public class Logger { public static final LoggerConfig Config = new LoggerConfig(); - private static final Log log = new Log(Config); + private static final Log mLog = new Log(Config); private Logger() { @@ -11,62 +11,62 @@ private Logger() { public static synchronized Log forThis(Object object) { - log.forThis(object); - return log; + mLog.forThis(object); + return mLog; } public static synchronized void debug() { - log.debugThread(); + forThis(null).debug(); } public static synchronized void debug(String message) { - log.debugThread(message); + forThis(null).debug(message); } public static synchronized void debug(String param, String value) { - log.debugThread(param, value); + forThis(null).debug(param, value); } - public static synchronized void debug(String param, int value) { - log.debugThread(param, value); + public static synchronized void debug(String param, boolean value) { + forThis(null).debug(param, value); } - public static synchronized void debug(String param, boolean value) { - log.debugThread(param, value); + public static synchronized void debug(String param, int value) { + forThis(null).debug(param, value); } public static synchronized void debug(String param, long value) { - log.debugThread(param, value); + forThis(null).debug(param, value); } - public static synchronized void debug(String param, double value) { - log.debugThread(param, value); + public static synchronized void debug(String param, float value) { + forThis(null).debug(param, value); } - public static synchronized void debug(String param, float value) { - log.debugThread(param, value); + public static synchronized void debug(String param, double value) { + forThis(null).debug(param, value); } public static synchronized void debug(String param, String value, String... params) { - log.debugThread(param, value, params); + forThis(null).debug(param, value, params); } public static synchronized void error(String message) { - log.errorThread(message); + forThis(null).error(message); } public static synchronized void error(Throwable throwable) { - log.errorThread(throwable); + forThis(null).error(throwable); } public static synchronized void info() { - log.infoThread(); + forThis(null).info(); } public static synchronized void info(String message) { - log.infoThread(message); + forThis(null).info(message); } -} +} \ No newline at end of file diff --git a/logger/src/main/java/com/riningan/util/LoggerConfig.java b/logger/src/main/java/com/riningan/util/LoggerConfig.java index 87d1160..0f812a4 100644 --- a/logger/src/main/java/com/riningan/util/LoggerConfig.java +++ b/logger/src/main/java/com/riningan/util/LoggerConfig.java @@ -9,6 +9,7 @@ public class LoggerConfig { private int mApplicationIdLength = 0; private String mPreffix = ""; + private Boolean mIsEnabled = true; @SuppressLint("SimpleDateFormat") private SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd: HH:mm:ss"); private OnMessageListener mListener = null; @@ -34,6 +35,11 @@ public LoggerConfig addPreffix(String preffix) { return this; } + public LoggerConfig setEnabled(boolean isEnabled) { + mIsEnabled = isEnabled; + return this; + } + public LoggerConfig setOnMessageListener(OnMessageListener listener) { mListener = listener; return this; @@ -52,6 +58,10 @@ String getPreffix() { return mPreffix; } + boolean isEnabled() { + return mIsEnabled; + } + void onNewMessage(MessageType type, String message) { if (mListener != null) { mListener.onNewMessage(type, message); diff --git a/sample/src/main/java/com/riningan/logger/SampleApp.java b/sample/src/main/java/com/riningan/logger/SampleApp.java index 106de3a..7ce6c11 100644 --- a/sample/src/main/java/com/riningan/logger/SampleApp.java +++ b/sample/src/main/java/com/riningan/logger/SampleApp.java @@ -13,6 +13,7 @@ public class SampleApp extends Application { public void onCreate() { super.onCreate(); Logger.Config + .setEnabled(false) .removeApplicationId(BuildConfig.APPLICATION_ID) .addPreffix(BuildConfig.VERSION_NAME) .setOnMessageListener(new OnMessageListener() {