Skip to content

Commit ce97808

Browse files
authored
Merge pull request #9114 from wordpress-mobile/issue/8177-applog-testing
Add test-friendly AppLog
2 parents b8d1be6 + 2310326 commit ce97808

File tree

3 files changed

+188
-30
lines changed

3 files changed

+188
-30
lines changed

WordPress/src/main/java/org/wordpress/android/ui/sitecreation/services/NewSiteCreationService.kt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import org.wordpress.android.ui.sitecreation.services.NewSiteCreationServiceStat
1515
import org.wordpress.android.util.AppLog
1616
import org.wordpress.android.util.AppLog.T
1717
import org.wordpress.android.util.AutoForeground
18-
import org.wordpress.android.util.CrashlyticsUtils
1918
import org.wordpress.android.util.LocaleManager
2019
import java.util.HashMap
2120
import javax.inject.Inject
@@ -89,19 +88,6 @@ class NewSiteCreationService : AutoForeground<NewSiteCreationServiceState>(NewSi
8988
trackStateUpdate(props)
9089
}
9190

92-
override fun logError(message: String) {
93-
AppLog.e(T.NUX, message)
94-
CrashlyticsUtils.log(message)
95-
}
96-
97-
override fun logInfo(message: String) {
98-
AppLog.i(T.NUX, message)
99-
}
100-
101-
override fun logWarning(message: String) {
102-
AppLog.w(T.NUX, message)
103-
}
104-
10591
companion object {
10692
private const val ARG_RESUME_PHASE = "ARG_RESUME_PHASE"
10793
private const val ARG_DATA = "ARG_DATA"

WordPress/src/main/java/org/wordpress/android/ui/sitecreation/services/NewSiteCreationServiceManager.kt

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import org.wordpress.android.ui.sitecreation.services.NewSiteCreationServiceStat
1414
import org.wordpress.android.ui.sitecreation.services.NewSiteCreationServiceState.NewSiteCreationStep.IDLE
1515
import org.wordpress.android.ui.sitecreation.services.NewSiteCreationServiceState.NewSiteCreationStep.SUCCESS
1616
import org.wordpress.android.ui.sitecreation.usecases.CreateSiteUseCase
17+
import org.wordpress.android.util.AppLog
18+
import org.wordpress.android.util.AppLog.T
1719
import javax.inject.Inject
1820
import javax.inject.Named
1921
import kotlin.coroutines.CoroutineContext
@@ -55,7 +57,7 @@ class NewSiteCreationServiceManager @Inject constructor(
5557
}
5658

5759
if (NewSiteCreationServiceState(phaseToExecute).isTerminal) {
58-
this.serviceListener.logError("IllegalState: NewSiteCreationService can't resume a terminal step!")
60+
AppLog.e(T.SITE_CREATION, "IllegalState: NewSiteCreationService can't resume a terminal step!")
5961
} else {
6062
executePhase(phaseToExecute)
6163
}
@@ -79,7 +81,7 @@ class NewSiteCreationServiceManager @Inject constructor(
7981
SUCCESS -> updateServiceState(SUCCESS, newSiteRemoteId)
8082
FAILURE -> {
8183
val currentState = serviceListener.getCurrentState()
82-
serviceListener.logError(
84+
AppLog.e(T.SITE_CREATION,
8385
"NewSiteCreationService entered state FAILURE while on step: ${currentState?.step?.name}"
8486
)
8587
updateServiceState(FAILURE, currentState)
@@ -89,25 +91,26 @@ class NewSiteCreationServiceManager @Inject constructor(
8991

9092
private fun createSite() {
9193
launch {
92-
serviceListener.logInfo(
94+
AppLog.i(
95+
T.SITE_CREATION,
9396
"Dispatching Create Site Action, title: ${siteData.siteTitle}, SiteName: ${siteData.domain}"
9497
)
9598
val createSiteEvent: OnNewSiteCreated
9699
try {
97100
createSiteEvent = createSiteUseCase.createSite(siteData, languageId)
98101
} catch (e: IllegalStateException) {
99-
serviceListener.logError(e.message ?: "Unexpected error.")
102+
AppLog.e(T.SITE_CREATION, e.message ?: "Unexpected error.")
100103
executePhase(FAILURE)
101104
return@launch
102105
}
103106

104107
newSiteRemoteId = createSiteEvent.newSiteRemoteId
105-
serviceListener.logInfo(createSiteEvent.toString())
108+
AppLog.i(T.SITE_CREATION, createSiteEvent.toString())
106109
if (createSiteEvent.isError) {
107110
if (createSiteEvent.error.type == SiteStore.NewSiteErrorType.SITE_NAME_EXISTS) {
108111
if (isRetry) {
109112
// Move to the next step. The site was already created on the server by our previous attempt.
110-
serviceListener.logWarning(
113+
AppLog.w(T.SITE_CREATION,
111114
"WPCOM site already created but we are in retrying mode so, just move on."
112115
)
113116
executePhase(SUCCESS)
@@ -118,7 +121,7 @@ class NewSiteCreationServiceManager @Inject constructor(
118121
* the first got it.
119122
*/
120123
val errorMsg = "Site already exists - seems like an issue with domain suggestions endpoint"
121-
serviceListener.logError(errorMsg)
124+
AppLog.e(T.SITE_CREATION, errorMsg)
122125
throw IllegalStateException(errorMsg)
123126
}
124127
} else {
@@ -144,14 +147,5 @@ class NewSiteCreationServiceManager @Inject constructor(
144147
fun getCurrentState(): NewSiteCreationServiceState?
145148

146149
fun updateState(state: NewSiteCreationServiceState)
147-
148-
// TODO replace with an injectable AppLog
149-
fun logError(message: String)
150-
151-
// TODO replace with an injectable AppLog
152-
fun logWarning(message: String)
153-
154-
// TODO replace with an injectable AppLog
155-
fun logInfo(message: String)
156150
}
157151
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package org.wordpress.android.util;
2+
3+
import android.support.annotation.NonNull;
4+
5+
/**
6+
* simple wrapper for Android log calls, enables recording and displaying log
7+
*
8+
* Testing version:
9+
* - replaces Log calls with System.out, since the unit tests don't have access to Android Framework classes.
10+
* - removes methods with Context parameter
11+
*/
12+
public class AppLog {
13+
// T for Tag
14+
public enum T {
15+
READER,
16+
EDITOR,
17+
MEDIA,
18+
NUX,
19+
API,
20+
STATS,
21+
UTILS,
22+
NOTIFS,
23+
DB,
24+
POSTS,
25+
PAGES,
26+
COMMENTS,
27+
THEMES,
28+
TESTS,
29+
PROFILING,
30+
SIMPERIUM,
31+
SUGGESTION,
32+
MAIN,
33+
SETTINGS,
34+
PLANS,
35+
PEOPLE,
36+
SHARING,
37+
PLUGINS,
38+
ACTIVITY_LOG,
39+
JETPACK_REMOTE_INSTALL,
40+
SUPPORT,
41+
SITE_CREATION
42+
}
43+
44+
public static final String TAG = "WordPress";
45+
46+
private AppLog() {
47+
throw new AssertionError();
48+
}
49+
50+
/**
51+
* Capture log so it can be displayed by AppLogViewerActivity
52+
*
53+
* @param enable A boolean flag to capture log. Default is false, pass true to enable recording
54+
*/
55+
public static void enableRecording(boolean enable) {
56+
}
57+
58+
public static void addListener(@NonNull AppLogListener listener) {
59+
}
60+
61+
public static void removeListeners() {
62+
}
63+
64+
public interface AppLogListener {
65+
void onLog(T tag, LogLevel logLevel, String message);
66+
}
67+
68+
/**
69+
* Sends a VERBOSE log message
70+
*
71+
* @param tag Used to identify the source of a log message.
72+
* It usually identifies the class or activity where the log call occurs.
73+
* @param message The message you would like logged.
74+
*/
75+
public static void v(T tag, String message) {
76+
message = StringUtils.notNullStr(message);
77+
System.out.println("v - " + TAG + " - " + tag.toString() + " - " + message);
78+
}
79+
80+
/**
81+
* Sends a DEBUG log message
82+
*
83+
* @param tag Used to identify the source of a log message.
84+
* It usually identifies the class or activity where the log call occurs.
85+
* @param message The message you would like logged.
86+
*/
87+
public static void d(T tag, String message) {
88+
message = StringUtils.notNullStr(message);
89+
System.out.println("d - " + TAG + " - " + tag.toString() + " - " + message);
90+
}
91+
92+
/**
93+
* Sends a INFO log message
94+
*
95+
* @param tag Used to identify the source of a log message.
96+
* It usually identifies the class or activity where the log call occurs.
97+
* @param message The message you would like logged.
98+
*/
99+
public static void i(T tag, String message) {
100+
message = StringUtils.notNullStr(message);
101+
System.out.println("i - " + TAG + " - " + tag.toString() + " - " + message);
102+
}
103+
104+
/**
105+
* Sends a WARN log message
106+
*
107+
* @param tag Used to identify the source of a log message.
108+
* It usually identifies the class or activity where the log call occurs.
109+
* @param message The message you would like logged.
110+
*/
111+
public static void w(T tag, String message) {
112+
message = StringUtils.notNullStr(message);
113+
System.out.println("w - " + TAG + " - " + tag.toString() + " - " + message);
114+
}
115+
116+
/**
117+
* Sends a ERROR log message
118+
*
119+
* @param tag Used to identify the source of a log message.
120+
* It usually identifies the class or activity where the log call occurs.
121+
* @param message The message you would like logged.
122+
*/
123+
public static void e(T tag, String message) {
124+
message = StringUtils.notNullStr(message);
125+
System.out.println("e - " + TAG + " - " + tag.toString() + " - " + message);
126+
}
127+
128+
/**
129+
* Send a ERROR log message and log the exception.
130+
*
131+
* @param tag Used to identify the source of a log message.
132+
* It usually identifies the class or activity where the log call occurs.
133+
* @param message The message you would like logged.
134+
* @param tr An exception to log
135+
*/
136+
public static void e(T tag, String message, Throwable tr) {
137+
message = StringUtils.notNullStr(message);
138+
System.out.println("e - " + TAG + " - " + tag.toString() + " - " + message);
139+
}
140+
141+
/**
142+
* Sends a ERROR log message and the exception with StackTrace
143+
*
144+
* @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
145+
* log call occurs.
146+
* @param tr An exception to log to get StackTrace
147+
*/
148+
public static void e(T tag, Throwable tr) {
149+
System.out.println("e - " + TAG + " - " + tag.toString() + " - " + tr.getMessage());
150+
}
151+
152+
/**
153+
* Sends a ERROR log message
154+
*
155+
* @param tag Used to identify the source of a log message. It usually identifies the class or
156+
* activity where the
157+
* log call occurs.
158+
*/
159+
public static void e(T tag, String volleyErrorMsg, int statusCode) {
160+
if (volleyErrorMsg == null || "".equals(volleyErrorMsg)) {
161+
return;
162+
}
163+
String logText;
164+
if (statusCode == -1) {
165+
logText = volleyErrorMsg;
166+
} else {
167+
logText = volleyErrorMsg + ", status " + statusCode;
168+
}
169+
System.out.println("e - " + TAG + " - " + tag.toString() + " - " + logText);
170+
}
171+
172+
// --------------------------------------------------------------------------------------------------------
173+
174+
175+
public enum LogLevel {
176+
v, d, i, w, e
177+
}
178+
}

0 commit comments

Comments
 (0)