Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import org.wordpress.android.ui.sitecreation.services.NewSiteCreationServiceStat
import org.wordpress.android.util.AppLog
import org.wordpress.android.util.AppLog.T
import org.wordpress.android.util.AutoForeground
import org.wordpress.android.util.CrashlyticsUtils
import org.wordpress.android.util.LocaleManager
import java.util.HashMap
import javax.inject.Inject
Expand Down Expand Up @@ -89,19 +88,6 @@ class NewSiteCreationService : AutoForeground<NewSiteCreationServiceState>(NewSi
trackStateUpdate(props)
}

override fun logError(message: String) {
AppLog.e(T.NUX, message)
CrashlyticsUtils.log(message)
}

override fun logInfo(message: String) {
AppLog.i(T.NUX, message)
}

override fun logWarning(message: String) {
AppLog.w(T.NUX, message)
}

companion object {
private const val ARG_RESUME_PHASE = "ARG_RESUME_PHASE"
private const val ARG_DATA = "ARG_DATA"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import org.wordpress.android.ui.sitecreation.services.NewSiteCreationServiceStat
import org.wordpress.android.ui.sitecreation.services.NewSiteCreationServiceState.NewSiteCreationStep.IDLE
import org.wordpress.android.ui.sitecreation.services.NewSiteCreationServiceState.NewSiteCreationStep.SUCCESS
import org.wordpress.android.ui.sitecreation.usecases.CreateSiteUseCase
import org.wordpress.android.util.AppLog
import org.wordpress.android.util.AppLog.T
import javax.inject.Inject
import javax.inject.Named
import kotlin.coroutines.CoroutineContext
Expand Down Expand Up @@ -55,7 +57,7 @@ class NewSiteCreationServiceManager @Inject constructor(
}

if (NewSiteCreationServiceState(phaseToExecute).isTerminal) {
this.serviceListener.logError("IllegalState: NewSiteCreationService can't resume a terminal step!")
AppLog.e(T.SITE_CREATION, "IllegalState: NewSiteCreationService can't resume a terminal step!")
} else {
executePhase(phaseToExecute)
}
Expand All @@ -79,7 +81,7 @@ class NewSiteCreationServiceManager @Inject constructor(
SUCCESS -> updateServiceState(SUCCESS, newSiteRemoteId)
FAILURE -> {
val currentState = serviceListener.getCurrentState()
serviceListener.logError(
AppLog.e(T.SITE_CREATION,
"NewSiteCreationService entered state FAILURE while on step: ${currentState?.step?.name}"
)
updateServiceState(FAILURE, currentState)
Expand All @@ -89,25 +91,26 @@ class NewSiteCreationServiceManager @Inject constructor(

private fun createSite() {
launch {
serviceListener.logInfo(
AppLog.i(
T.SITE_CREATION,
"Dispatching Create Site Action, title: ${siteData.siteTitle}, SiteName: ${siteData.domain}"
)
val createSiteEvent: OnNewSiteCreated
try {
createSiteEvent = createSiteUseCase.createSite(siteData, languageId)
} catch (e: IllegalStateException) {
serviceListener.logError(e.message ?: "Unexpected error.")
AppLog.e(T.SITE_CREATION, e.message ?: "Unexpected error.")
executePhase(FAILURE)
return@launch
}

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

fun updateState(state: NewSiteCreationServiceState)

// TODO replace with an injectable AppLog
fun logError(message: String)

// TODO replace with an injectable AppLog
fun logWarning(message: String)

// TODO replace with an injectable AppLog
fun logInfo(message: String)
}
}
178 changes: 178 additions & 0 deletions WordPress/src/test/java/org/wordpress/android/util/AppLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package org.wordpress.android.util;

import android.support.annotation.NonNull;

/**
* simple wrapper for Android log calls, enables recording and displaying log
*
* Testing version:
* - replaces Log calls with System.out, since the unit tests don't have access to Android Framework classes.
* - removes methods with Context parameter
*/
public class AppLog {
// T for Tag
public enum T {
READER,
EDITOR,
MEDIA,
NUX,
API,
STATS,
UTILS,
NOTIFS,
DB,
POSTS,
PAGES,
COMMENTS,
THEMES,
TESTS,
PROFILING,
SIMPERIUM,
SUGGESTION,
MAIN,
SETTINGS,
PLANS,
PEOPLE,
SHARING,
PLUGINS,
ACTIVITY_LOG,
JETPACK_REMOTE_INSTALL,
SUPPORT,
SITE_CREATION
}

public static final String TAG = "WordPress";

private AppLog() {
throw new AssertionError();
}

/**
* Capture log so it can be displayed by AppLogViewerActivity
*
* @param enable A boolean flag to capture log. Default is false, pass true to enable recording
*/
public static void enableRecording(boolean enable) {
}

public static void addListener(@NonNull AppLogListener listener) {
}

public static void removeListeners() {
}

public interface AppLogListener {
void onLog(T tag, LogLevel logLevel, String message);
}

/**
* Sends a VERBOSE log message
*
* @param tag Used to identify the source of a log message.
* It usually identifies the class or activity where the log call occurs.
* @param message The message you would like logged.
*/
public static void v(T tag, String message) {
message = StringUtils.notNullStr(message);
System.out.println("v - " + TAG + " - " + tag.toString() + " - " + message);
}

/**
* Sends a DEBUG log message
*
* @param tag Used to identify the source of a log message.
* It usually identifies the class or activity where the log call occurs.
* @param message The message you would like logged.
*/
public static void d(T tag, String message) {
message = StringUtils.notNullStr(message);
System.out.println("d - " + TAG + " - " + tag.toString() + " - " + message);
}

/**
* Sends a INFO log message
*
* @param tag Used to identify the source of a log message.
* It usually identifies the class or activity where the log call occurs.
* @param message The message you would like logged.
*/
public static void i(T tag, String message) {
message = StringUtils.notNullStr(message);
System.out.println("i - " + TAG + " - " + tag.toString() + " - " + message);
}

/**
* Sends a WARN log message
*
* @param tag Used to identify the source of a log message.
* It usually identifies the class or activity where the log call occurs.
* @param message The message you would like logged.
*/
public static void w(T tag, String message) {
message = StringUtils.notNullStr(message);
System.out.println("w - " + TAG + " - " + tag.toString() + " - " + message);
}

/**
* Sends a ERROR log message
*
* @param tag Used to identify the source of a log message.
* It usually identifies the class or activity where the log call occurs.
* @param message The message you would like logged.
*/
public static void e(T tag, String message) {
message = StringUtils.notNullStr(message);
System.out.println("e - " + TAG + " - " + tag.toString() + " - " + message);
}

/**
* Send a ERROR log message and log the exception.
*
* @param tag Used to identify the source of a log message.
* It usually identifies the class or activity where the log call occurs.
* @param message The message you would like logged.
* @param tr An exception to log
*/
public static void e(T tag, String message, Throwable tr) {
message = StringUtils.notNullStr(message);
System.out.println("e - " + TAG + " - " + tag.toString() + " - " + message);
}

/**
* Sends a ERROR log message and the exception with StackTrace
*
* @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
* log call occurs.
* @param tr An exception to log to get StackTrace
*/
public static void e(T tag, Throwable tr) {
System.out.println("e - " + TAG + " - " + tag.toString() + " - " + tr.getMessage());
}

/**
* Sends a ERROR log message
*
* @param tag Used to identify the source of a log message. It usually identifies the class or
* activity where the
* log call occurs.
*/
public static void e(T tag, String volleyErrorMsg, int statusCode) {
if (volleyErrorMsg == null || "".equals(volleyErrorMsg)) {
return;
}
String logText;
if (statusCode == -1) {
logText = volleyErrorMsg;
} else {
logText = volleyErrorMsg + ", status " + statusCode;
}
System.out.println("e - " + TAG + " - " + tag.toString() + " - " + logText);
}

// --------------------------------------------------------------------------------------------------------


public enum LogLevel {
v, d, i, w, e
}
}