Skip to content
This repository has been archived by the owner on Mar 10, 2022. It is now read-only.

Commit

Permalink
Refactor internal Log class
Browse files Browse the repository at this point in the history
* Refactored internal Log class to take LogDomain instead of String when logging; this will fix the mismatching b/w the LogDomain and tag which is string.
* Cleaned up debugging logs

#1872
  • Loading branch information
pasin committed Feb 25, 2019
1 parent 8894a73 commit 2d5e857
Show file tree
Hide file tree
Showing 32 changed files with 280 additions and 405 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import android.support.test.InstrumentationRegistry;

import com.couchbase.lite.internal.support.Log;
import com.couchbase.lite.utils.Config;

import org.junit.After;
Expand All @@ -39,7 +38,9 @@
import static org.junit.Assert.assertTrue;

public class BaseReplicatorTest extends BaseTest {
protected final static String kOtherDatabaseName = "otherdb";
private static final String kActivityNames[] = {"stopped", "offline", "connecting", "idle", "busy"};
protected static final String kOtherDatabaseName = "otherdb";

Database otherDB;
Replicator repl;
long timeout; // seconds
Expand Down Expand Up @@ -87,11 +88,11 @@ protected Replicator run(final Replicator r, final int code, final String domain
public void changed(ReplicatorChange change) {
Replicator.Status status = change.getStatus();
CouchbaseLiteException error = status.getError();
final String kActivityNames[] = {"stopped", "offline", "connecting", "idle", "busy"};
Log.i(TAG, "ReplicatorChangeListener.changed() status: %s (%d / %d), lastError = %s",
kActivityNames[status.getActivityLevel().getValue()],
status.getProgress().getCompleted(), status.getProgress().getTotal(),
error);
String activity = kActivityNames[status.getActivityLevel().getValue()];
long completed = status.getProgress().getCompleted();
long total = status.getProgress().getTotal();
log(LogLevel.INFO, "ReplicatorChangeListener.changed() status: " + activity +
"(" + completed + "/" + total + "), lastError: " + error);
if (r.getConfig().isContinuous()) {
if (status.getActivityLevel() == Replicator.ActivityLevel.IDLE &&
status.getProgress().getCompleted() == status.getProgress().getTotal()) {
Expand Down Expand Up @@ -146,12 +147,6 @@ void stopContinuousReplicator(Replicator repl) throws InterruptedException {
@Override
public void changed(ReplicatorChange change) {
Replicator.Status status = change.getStatus();
CouchbaseLiteException error = status.getError();
final String kActivityNames[] = {"stopped", "offline", "connecting", "idle", "busy"};
Log.i(TAG, "--- ReplicatorChangeListener.changed() -> status: %s (%d / %d), lastError = %s",
kActivityNames[status.getActivityLevel().getValue()],
status.getProgress().getCompleted(), status.getProgress().getTotal(),
error);
if (status.getActivityLevel() == Replicator.ActivityLevel.STOPPED) {
latch.countDown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ protected static boolean isARM() {

@Before
public void setUp() throws Exception {
Database.setLogLevel(LogDomain.ALL, LogLevel.INFO);
Log.enableLogging(TAG, Log.INFO); // NOTE: Without loading Database, this fails.

executor = Executors.newSingleThreadExecutor();

context = InstrumentationRegistry.getTargetContext();
Expand Down Expand Up @@ -345,4 +342,24 @@ protected static void expectError(String domain, int code, Execution execution)
assertEquals(code, e.getCode());
}
}

protected static void log(LogLevel level, String message) {
switch(level) {
case DEBUG:
android.util.Log.d("CouchbaseLite/" + TAG, message);
break;
case VERBOSE:
android.util.Log.v("CouchbaseLite/" + TAG, message);
break;
case INFO:
android.util.Log.i("CouchbaseLite/" + TAG, message);
break;
case WARNING:
android.util.Log.w("CouchbaseLite/" + TAG, message);
break;
case ERROR:
android.util.Log.e("CouchbaseLite/" + TAG, message);
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,9 @@ boolean updateDocs(List<String> docIds, int rounds, String tag) {
phones.setValue(0, phone);

doc.setValue("updated", new Date());

Log.i(TAG, "[%s] rounds: %d updating %s", tag, i, doc.getId());
try {
db.save(doc);
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Error in Database.save()", e);
return false;
}
}
Expand All @@ -126,7 +123,6 @@ void verifyByTagName(String tag, VerifyBlock block) throws CouchbaseLiteExceptio
SelectResult DOCID = SelectResult.expression(Meta.id);
DataSource ds = DataSource.database(db);
Query q = QueryBuilder.select(DOCID).from(ds).where(TAG_EXPR.equalTo(Expression.string(tag)));
Log.e(TAG, "query - > %s", q.explain());
ResultSet rs = q.execute();
Result result;
int n = 0;
Expand Down Expand Up @@ -175,7 +171,6 @@ public void run() {
try {
assertTrue(latchs[i].await(waitSec, TimeUnit.SECONDS));
} catch (InterruptedException e) {
Log.e(TAG, "InterruptedException", e);
fail();
}
}
Expand All @@ -198,7 +193,6 @@ public void callback(int threadIndex) {
try {
createDocs(kNDocs, tag);
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Error in createDocs()", e);
fail();
}
}
Expand Down Expand Up @@ -230,13 +224,11 @@ public void run() {
try {
createDocs(kNDocs, tag);
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Error in createDocs() kNDocs -> %d, tag1 -> %s", e, kNDocs, tag);
fail();
}
}
});
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Error in inBatch()", e);
fail();
}
}
Expand Down Expand Up @@ -333,7 +325,6 @@ public void run() {
}
});
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Error in inBatch()", e);
fail();
}
}
Expand Down Expand Up @@ -400,7 +391,6 @@ public void run() {
if (doc != null)
db.delete(doc);
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Error in Database.delete(Document)", e);
fail();
}
}
Expand All @@ -418,7 +408,6 @@ public void run() {
if (doc != null)
db.delete(doc);
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Error in Database.delete(Document)", e);
fail();
}
}
Expand Down Expand Up @@ -502,7 +491,6 @@ public void run() {
createDocs(kNDocs, tag1);
} catch (CouchbaseLiteException e) {
if (!e.getDomain().equals(CBLError.Domain.CBLErrorDomain) || e.getCode() != CBLErrorNotOpen) {
Log.e(TAG, "Error in createDocs() kNDocs -> %d, tag1 -> %s", e, kNDocs, tag1);
fail();
}
} catch (IllegalStateException ise) {
Expand All @@ -519,7 +507,6 @@ public void run() {
try {
db.close();
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Error in Database.close()", e);
fail();
}
latch2.countDown();
Expand All @@ -546,7 +533,6 @@ public void run() {
createDocs(kNDocs, tag1);
} catch (CouchbaseLiteException e) {
if (!e.getDomain().equals(CBLError.Domain.CBLErrorDomain) || e.getCode() != CBLErrorNotOpen) {
Log.e(TAG, "Error in createDocs() kNDocs -> %d, tag1 -> %s", e, kNDocs, tag1);
fail();
}
} catch (IllegalStateException ise) {
Expand All @@ -563,7 +549,6 @@ public void run() {
try {
db.delete();
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Error in Database.delete()", e);
fail();
}
latch2.countDown();
Expand All @@ -590,7 +575,6 @@ public void run() {
createDocs(kNDocs, tag1);
} catch (CouchbaseLiteException e) {
if (!e.getDomain().equals(CBLError.Domain.CBLErrorDomain) || e.getCode() != CBLErrorNotOpen) {
Log.e(TAG, "Error in createDocs() kNDocs -> %d, tag1 -> %s", e, kNDocs, tag1);
fail();
}
}
Expand All @@ -605,7 +589,6 @@ public void run() {
try {
db.compact();
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Error in Database.compact()", e);
fail();
}
latch2.countDown();
Expand Down Expand Up @@ -634,7 +617,6 @@ public void run() {
createDocs(kNDocs, tag1);
} catch (CouchbaseLiteException e) {
if (!e.getDomain().equals(CBLError.Domain.CBLErrorDomain) || e.getCode() != CBLErrorNotOpen) {
Log.e(TAG, "Error in createDocs() kNDocs -> %d, tag1 -> %s", e, kNDocs, tag1);
fail();
}
}
Expand All @@ -650,7 +632,6 @@ public void run() {
Index index = IndexBuilder.fullTextIndex(FullTextIndexItem.property("sentence"));
db.createIndex("sentence", index);
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Error in Database.createIndex()", e);
fail();
}
latch2.countDown();
Expand All @@ -672,7 +653,6 @@ public void testBlockDatabaseChange() throws InterruptedException, CouchbaseLite
db.addChangeListener(executor, new DatabaseChangeListener() {
@Override
public void changed(DatabaseChange change) {
Log.e(TAG, "changed() change -> %s", change);
latch2.countDown();
}
});
Expand Down Expand Up @@ -714,7 +694,6 @@ public void run() {
try {
db.save(new MutableDocument("doc1"));
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Error in save()", e);
fail();
}
latch1.countDown();
Expand Down Expand Up @@ -744,7 +723,7 @@ public void callback(int threadIndex) {
try {
rs = query.execute();
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Error in Query.execute()", e);
fail();
}
List<Result> results = rs.allResults();
assertEquals(100, results.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,10 @@ void validateDocs(int n) {
@Before
public void setUp() throws Exception {
super.setUp();
Log.i(TAG, "setUp");
}

@After
public void tearDown() throws Exception {
Log.i(TAG, "tearDown");
super.tearDown();
}

Expand Down Expand Up @@ -1296,11 +1294,9 @@ public void testCopy() throws CouchbaseLiteException {
if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.JELLY_BEAN
&& Build.FINGERPRINT.startsWith("generic")
&& "armv7l".equals(System.getProperty("os.arch"))) {
Log.w(TAG, "testCopy() is skipped.");
return;
}

// TODO: crash with 2048
final int NUM_DOCS = 10;

for (int i = 0; i < NUM_DOCS; i++) {
Expand Down Expand Up @@ -1386,8 +1382,6 @@ public void testCreateIndex() throws CouchbaseLiteException {
Index index4 = IndexBuilder.valueIndex(fNameItem2, lNameItem2);
db.createIndex("index4", index4);
assertEquals(4, db.getIndexes().size());

Log.i(TAG, "db.getIndexes() -> " + db.getIndexes());
}

@Test
Expand Down Expand Up @@ -1503,7 +1497,6 @@ public void run() {
try {
database2.save(doc);
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Failed in storing document " + e.getMessage(), e);
fail();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,6 @@ public void testLargeLongValue() throws CouchbaseLiteException {
doc.setValue("num2", num2);
doc.setValue("num3", num3);
doc = save(doc).toMutable();
Log.i(TAG, "num1 long -> " + doc.getLong("num1"));
Log.i(TAG, "num2 long -> " + doc.getLong("num2"));
Log.i(TAG, "num3 long -> " + doc.getLong("num3"));
assertEquals(num1, doc.getLong("num1"));
assertEquals(num2, doc.getLong("num2"));
assertEquals(num3, doc.getLong("num3"));
Expand All @@ -406,8 +403,6 @@ public void testLargeLongValue2() throws CouchbaseLiteException {
doc.setValue("num1", num1);
doc.setValue("num2", num2);
doc = save(doc).toMutable();
Log.i(TAG, "num1 long -> " + doc.getLong("num1"));
Log.i(TAG, "num2 long -> " + doc.getLong("num2"));
assertEquals(num1, doc.getLong("num1"));
assertEquals(num2, doc.getLong("num2"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ public void changed(QueryChange change) {
if (list.size() <= 0)
return;

Log.e(TAG, "%d matched. update query", list.size());

// remove current listener
query.removeChangeListener(token);
listener = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public class LoadTest extends BaseTest {

protected void logPerformanceStats(String name, long time) {
Log.e(TAG, "PerformanceStats: " + name + " -> " + time + " ms");
log(LogLevel.INFO, "PerformanceStats: " + name + " -> " + time + " ms");
}

MutableDocument createDocumentWithTag(String id, String tag) {
Expand Down Expand Up @@ -119,7 +119,6 @@ void verifyByTagName(String tag, VerifyBlock block) throws CouchbaseLiteExceptio
SelectResult DOCID = SelectResult.expression(Meta.id);
DataSource ds = DataSource.database(db);
Query q = QueryBuilder.select(DOCID).from(ds).where(TAG_EXPR.equalTo(Expression.string(tag)));
Log.v(TAG, "query - > %s", q.explain());
ResultSet rs = q.execute();
Result row;
int n = 0;
Expand Down Expand Up @@ -273,7 +272,7 @@ public void testGlobalReferenceExcceded() throws InterruptedException, Couchbase
try {
db.save(doc);
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Failed to save: %s", e, doc.getId());
log(LogLevel.ERROR, "Failed to save: " + e);
}
}

Expand Down Expand Up @@ -317,8 +316,7 @@ boolean updateMap(Map map, int i, long l) {
try {
db.save(newDoc);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
Log.e(TAG, "DB is not responding");
log(LogLevel.ERROR, "DB is not responding: " + e);
return false;
}
return true;
Expand Down
Loading

0 comments on commit 2d5e857

Please sign in to comment.