|
|
|
@ -1,10 +1,13 @@ |
|
|
|
|
package com.otaliastudios.cameraview; |
|
|
|
|
|
|
|
|
|
import android.support.annotation.IntDef; |
|
|
|
|
import android.support.annotation.Nullable; |
|
|
|
|
import android.util.Log; |
|
|
|
|
|
|
|
|
|
import java.lang.annotation.Retention; |
|
|
|
|
import java.lang.annotation.RetentionPolicy; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Utility class that can log traces and info. |
|
|
|
@ -12,81 +15,144 @@ import java.lang.annotation.RetentionPolicy; |
|
|
|
|
public final class CameraLogger { |
|
|
|
|
|
|
|
|
|
public final static int LEVEL_VERBOSE = 0; |
|
|
|
|
public final static int LEVEL_WARNING = 1; |
|
|
|
|
public final static int LEVEL_ERROR = 2; |
|
|
|
|
public final static int LEVEL_INFO = 1; |
|
|
|
|
public final static int LEVEL_WARNING = 2; |
|
|
|
|
public final static int LEVEL_ERROR = 3; |
|
|
|
|
|
|
|
|
|
@IntDef({LEVEL_VERBOSE, LEVEL_WARNING, LEVEL_ERROR}) |
|
|
|
|
/** |
|
|
|
|
* Interface of integers representing log levels. |
|
|
|
|
* @see #LEVEL_VERBOSE |
|
|
|
|
* @see #LEVEL_INFO |
|
|
|
|
* @see #LEVEL_WARNING |
|
|
|
|
* @see #LEVEL_ERROR |
|
|
|
|
*/ |
|
|
|
|
@IntDef({LEVEL_VERBOSE, LEVEL_INFO, LEVEL_WARNING, LEVEL_ERROR}) |
|
|
|
|
@Retention(RetentionPolicy.SOURCE) |
|
|
|
|
@interface LogLevel {} |
|
|
|
|
public @interface LogLevel {} |
|
|
|
|
|
|
|
|
|
private static int level = LEVEL_ERROR; |
|
|
|
|
/** |
|
|
|
|
* A Logger can listen to internal log events |
|
|
|
|
* and log them to different providers. |
|
|
|
|
* The default logger will simply post to logcat. |
|
|
|
|
*/ |
|
|
|
|
public interface Logger { |
|
|
|
|
|
|
|
|
|
public static void setLogLevel(int logLevel) { |
|
|
|
|
level = logLevel; |
|
|
|
|
/** |
|
|
|
|
* Notifies that an internal log event was just triggered. |
|
|
|
|
* |
|
|
|
|
* @param level the log level |
|
|
|
|
* @param tag the log tag |
|
|
|
|
* @param message the log message |
|
|
|
|
* @param throwable an optional throwable |
|
|
|
|
*/ |
|
|
|
|
void log(@LogLevel int level, String tag, String message, @Nullable Throwable throwable); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static String lastMessage; |
|
|
|
|
static String lastTag; |
|
|
|
|
|
|
|
|
|
private static int sLevel; |
|
|
|
|
private static List<Logger> sLoggers; |
|
|
|
|
|
|
|
|
|
static { |
|
|
|
|
setLogLevel(LEVEL_ERROR); |
|
|
|
|
sLoggers = new ArrayList<>(); |
|
|
|
|
sLoggers.add(new Logger() { |
|
|
|
|
@Override |
|
|
|
|
public void log(int level, String tag, String message, @Nullable Throwable throwable) { |
|
|
|
|
switch (level) { |
|
|
|
|
case LEVEL_VERBOSE: Log.v(tag, message, throwable); break; |
|
|
|
|
case LEVEL_INFO: Log.i(tag, message, throwable); break; |
|
|
|
|
case LEVEL_WARNING: Log.w(tag, message, throwable); break; |
|
|
|
|
case LEVEL_ERROR: Log.e(tag, message, throwable); break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static CameraLogger create(String tag) { |
|
|
|
|
return new CameraLogger(tag); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private String mTag; |
|
|
|
|
|
|
|
|
|
private CameraLogger(String tag) { |
|
|
|
|
mTag = tag; |
|
|
|
|
/** |
|
|
|
|
* Sets the log sLevel for logcat events. |
|
|
|
|
* |
|
|
|
|
* @see #LEVEL_VERBOSE |
|
|
|
|
* @see #LEVEL_INFO |
|
|
|
|
* @see #LEVEL_WARNING |
|
|
|
|
* @see #LEVEL_ERROR |
|
|
|
|
* @param logLevel the desired log sLevel |
|
|
|
|
*/ |
|
|
|
|
public static void setLogLevel(@LogLevel int logLevel) { |
|
|
|
|
sLevel = logLevel; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void i(String message) { |
|
|
|
|
if (should(LEVEL_VERBOSE)) { |
|
|
|
|
Log.i(mTag, message); |
|
|
|
|
lastMessage = message; |
|
|
|
|
lastTag = mTag; |
|
|
|
|
} |
|
|
|
|
/** |
|
|
|
|
* Registers an external {@link Logger} for log events. |
|
|
|
|
* Make sure to unregister using {@link #unregisterLogger(Logger)}. |
|
|
|
|
* |
|
|
|
|
* @param logger logger to add |
|
|
|
|
*/ |
|
|
|
|
public static void registerLogger(Logger logger) { |
|
|
|
|
sLoggers.add(logger); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void w(String message) { |
|
|
|
|
if (should(LEVEL_WARNING)) { |
|
|
|
|
Log.w(mTag, message); |
|
|
|
|
lastMessage = message; |
|
|
|
|
lastTag = mTag; |
|
|
|
|
} |
|
|
|
|
/** |
|
|
|
|
* Unregisters a previously registered {@link Logger} for log events. |
|
|
|
|
* This is needed in order to avoid leaks. |
|
|
|
|
* |
|
|
|
|
* @param logger logger to remove |
|
|
|
|
*/ |
|
|
|
|
public static void unregisterLogger(Logger logger) { |
|
|
|
|
sLoggers.remove(logger); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void e(String message) { |
|
|
|
|
if (should(LEVEL_ERROR)) { |
|
|
|
|
Log.w(mTag, message); |
|
|
|
|
lastMessage = message; |
|
|
|
|
lastTag = mTag; |
|
|
|
|
} |
|
|
|
|
private String mTag; |
|
|
|
|
|
|
|
|
|
private CameraLogger(String tag) { |
|
|
|
|
mTag = tag; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean should(int messageLevel) { |
|
|
|
|
return level <= messageLevel; |
|
|
|
|
return sLevel <= messageLevel && sLoggers.size() > 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private String string(int messageLevel, Object... ofData) { |
|
|
|
|
String message = ""; |
|
|
|
|
if (should(messageLevel)) { |
|
|
|
|
for (Object o : ofData) { |
|
|
|
|
message += String.valueOf(o); |
|
|
|
|
message += " "; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return message.trim(); |
|
|
|
|
void v(Object... data) { |
|
|
|
|
log(LEVEL_VERBOSE, data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void i(Object... data) { |
|
|
|
|
i(string(LEVEL_VERBOSE, data)); |
|
|
|
|
log(LEVEL_INFO, data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void w(Object... data) { |
|
|
|
|
w(string(LEVEL_WARNING, data)); |
|
|
|
|
log(LEVEL_WARNING, data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void e(Object... data) { |
|
|
|
|
e(string(LEVEL_ERROR, data)); |
|
|
|
|
log(LEVEL_ERROR, data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void log(@LogLevel int level, Object... data) { |
|
|
|
|
if (!should(level)) return; |
|
|
|
|
|
|
|
|
|
String message = ""; |
|
|
|
|
Throwable throwable = null; |
|
|
|
|
final int size = data.length; |
|
|
|
|
for (Object object : data) { |
|
|
|
|
if (object instanceof Throwable) { |
|
|
|
|
throwable = (Throwable) object; |
|
|
|
|
} |
|
|
|
|
message += String.valueOf(object); |
|
|
|
|
message += " "; |
|
|
|
|
} |
|
|
|
|
message = message.trim(); |
|
|
|
|
for (Logger logger : sLoggers) { |
|
|
|
|
logger.log(level, mTag, message, throwable); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
lastMessage = message; |
|
|
|
|
lastTag = mTag; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|