Skip to content

Commit b8d55d4

Browse files
committed
Prepared for future log fragment usage
1 parent df9df82 commit b8d55d4

File tree

9 files changed

+342
-43
lines changed

9 files changed

+342
-43
lines changed

log/build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22

33
def versionMajor = 1
44
def versionMinor = 4
5-
def versionPatch = 8
5+
def versionPatch = 9
66

77
version = "${versionMajor}.${versionMinor}.${versionPatch}"
88

@@ -30,11 +30,12 @@ android {
3030
}
3131
}
3232

33-
final SUPPORT_LIBRARY_VERSION = '27.1.0'
33+
final SUPPORT_LIBRARY_VERSION = '27.1.1'
3434

3535
dependencies {
3636
implementation "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION"
3737
implementation "com.android.support:support-v4:$SUPPORT_LIBRARY_VERSION"
38+
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
3839
}
3940

40-
apply from: 'publish.gradle'
41+
apply from: 'publish.gradle'

log/src/main/java/ua/at/tsvetkov/util/AbstractLog.java

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package ua.at.tsvetkov.util;
22

3+
import java.util.HashMap;
34
import java.util.HashSet;
45

56
import ua.at.tsvetkov.util.interceptor.LogCatInterceptor;
67
import ua.at.tsvetkov.util.interceptor.LogInterceptor;
78

89
class AbstractLog {
910

10-
static final HashSet<LogInterceptor> interceptors = new HashSet<>();
11+
protected static final HashMap<Integer, LogInterceptor> interceptors = new HashMap<>();
1112

12-
private static final LogCatInterceptor logCatInterceptor = new LogCatInterceptor();
13+
protected static final LogCatInterceptor logCatInterceptor = new LogCatInterceptor();
1314

1415
static {
15-
interceptors.add(logCatInterceptor);
16+
interceptors.put(logCatInterceptor.hashCode(), logCatInterceptor);
1617
}
1718

1819
/**
@@ -49,7 +50,7 @@ public static void setEnabled() {
4950

5051
public static void addInterceptor(LogInterceptor interceptor) {
5152
synchronized (interceptors) {
52-
interceptors.add(interceptor);
53+
interceptors.put(interceptor.hashCode(), interceptor);
5354
}
5455
}
5556

@@ -73,26 +74,48 @@ public static void removeLogCatInterceptor() {
7374

7475
public static void addLogCatInterceptor() {
7576
synchronized (interceptors) {
76-
interceptors.add(logCatInterceptor);
77+
interceptors.put(logCatInterceptor.hashCode(), logCatInterceptor);
7778
}
7879
}
7980

80-
public static HashSet<LogInterceptor> getInterceptors() {
81+
public static HashMap<Integer, LogInterceptor> getInterceptors() {
8182
return interceptors;
8283
}
8384

85+
public static LogInterceptor getInterceptor(int id) {
86+
synchronized (interceptors) {
87+
return interceptors.get(id);
88+
}
89+
}
90+
91+
public static boolean hasInterceptor(int id) {
92+
synchronized (interceptors) {
93+
return interceptors.containsKey(id);
94+
}
95+
}
96+
97+
public static boolean hasInterceptor(LogInterceptor interceptor) {
98+
synchronized (interceptors) {
99+
return interceptors.containsValue(interceptor);
100+
}
101+
}
102+
84103
protected static void logToAll(LogInterceptor.Level level, String tag, String message) {
85-
for (LogInterceptor interceptor : interceptors) {
86-
if (interceptor.isEnabled()) {
87-
interceptor.log(level, tag, message, null);
104+
synchronized (interceptors) {
105+
for (LogInterceptor interceptor : interceptors.values()) {
106+
if (interceptor.isEnabled()) {
107+
interceptor.log(level, tag, message, null);
108+
}
88109
}
89110
}
90111
}
91112

92113
protected static void logToAll(LogInterceptor.Level level, String tag, String message, Throwable throwable) {
93-
for (LogInterceptor interceptor : interceptors) {
94-
if (interceptor.isEnabled()) {
95-
interceptor.log(level, tag, message, throwable);
114+
synchronized (interceptors) {
115+
for (LogInterceptor interceptor : interceptors.values()) {
116+
if (interceptor.isEnabled()) {
117+
interceptor.log(level, tag, message, throwable);
118+
}
96119
}
97120
}
98121
}

log/src/main/java/ua/at/tsvetkov/util/interceptor/LogCatInterceptor.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,58 @@
11
package ua.at.tsvetkov.util.interceptor;
22

33
import android.support.annotation.Nullable;
4+
import android.util.Log;
45

56
public class LogCatInterceptor extends LogInterceptor {
67

78
@Override
89
public void log(Level level, String tag, String msg, @Nullable Throwable th) {
9-
switch (level) {
10+
switch (level) {
1011
case VERBOSE: {
1112
if (th == null) {
12-
android.util.Log.v(tag, msg);
13+
Log.v(tag, msg);
1314
} else {
14-
android.util.Log.v(tag, msg, th);
15+
Log.v(tag, msg, th);
1516
}
1617
break;
1718
}
1819
case INFO: {
1920
if (th == null) {
20-
android.util.Log.i(tag, msg);
21+
Log.i(tag, msg);
2122
} else {
22-
android.util.Log.i(tag, msg, th);
23+
Log.i(tag, msg, th);
2324
}
2425
break;
2526
}
2627
case DEBUG: {
2728
if (th == null) {
28-
android.util.Log.d(tag, msg);
29+
Log.d(tag, msg);
2930
} else {
30-
android.util.Log.d(tag, msg, th);
31+
Log.d(tag, msg, th);
3132
}
3233
break;
3334
}
3435
case WARNING: {
3536
if (th == null) {
36-
android.util.Log.w(tag, msg);
37+
Log.w(tag, msg);
3738
} else {
38-
android.util.Log.w(tag, msg, th);
39+
Log.w(tag, msg, th);
3940
}
4041
break;
4142
}
4243
case ERROR: {
4344
if (th == null) {
44-
android.util.Log.e(tag, msg);
45+
Log.e(tag, msg);
4546
} else {
46-
android.util.Log.e(tag, msg, th);
47+
Log.e(tag, msg, th);
4748
}
4849
break;
4950
}
5051
case WTF: {
5152
if (th == null) {
52-
android.util.Log.wtf(tag, msg);
53+
Log.wtf(tag, msg);
5354
} else {
54-
android.util.Log.wtf(tag, msg, th);
55+
Log.wtf(tag, msg, th);
5556
}
5657
break;
5758
}

log/src/main/java/ua/at/tsvetkov/util/interceptor/LogInterceptor.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,50 @@
22

33
public abstract class LogInterceptor {
44

5+
private String tag;
6+
57
private boolean isDisabled = false;
68

79
public enum Level {
810
VERBOSE, INFO, DEBUG, WARNING, ERROR, WTF;
911

1012
public char getShortName() {
11-
switch (this){
12-
case VERBOSE:{
13+
switch (this) {
14+
case VERBOSE: {
1315
return 'V';
1416
}
15-
case INFO:{
17+
case INFO: {
1618
return 'I';
1719
}
18-
case DEBUG:{
20+
case DEBUG: {
1921
return 'D';
2022
}
21-
case WARNING:{
23+
case WARNING: {
2224
return 'W';
2325
}
24-
case ERROR:{
26+
case ERROR: {
2527
return 'E';
2628
}
27-
case WTF:{
29+
case WTF: {
2830
return 'F';
2931
}
3032
}
3133
return '?';
3234
}
3335
}
3436

37+
LogInterceptor() {
38+
tag = this.getClass().getName();
39+
}
40+
41+
public String getTag() {
42+
return tag;
43+
}
44+
45+
public void setTag(String tag) {
46+
this.tag = tag;
47+
}
48+
3549
/**
3650
* Intercepted log
3751
*
@@ -54,6 +68,9 @@ public boolean isEnabled() {
5468
return !isDisabled;
5569
}
5670

71+
public int getId() {
72+
return hashCode();
73+
}
5774

5875
public boolean isDisabled() {
5976
return isDisabled;

0 commit comments

Comments
 (0)