Merge "MTP: replace printfs with logcat"
diff --git a/media/mtp/MtpClient.cpp b/media/mtp/MtpClient.cpp
index 31874e9..fbf1c77 100644
--- a/media/mtp/MtpClient.cpp
+++ b/media/mtp/MtpClient.cpp
@@ -15,7 +15,10 @@
  */
 
 #define LOG_TAG "MtpClient"
-#include "utils/Log.h"
+
+#include "MtpDebug.h"
+#include "MtpClient.h"
+#include "MtpDevice.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -33,10 +36,6 @@
 #include <linux/usb_ch9.h>
 #endif
 
-#include "MtpClient.h"
-#include "MtpDevice.h"
-#include "MtpDebug.h"
-
 namespace android {
 
 MtpClient::MtpClient()
diff --git a/media/mtp/MtpCursor.cpp b/media/mtp/MtpCursor.cpp
index d63a5bf..5b6672a 100644
--- a/media/mtp/MtpCursor.cpp
+++ b/media/mtp/MtpCursor.cpp
@@ -15,8 +15,8 @@
  */
 
 #define LOG_TAG "MtpCursor"
-#include "utils/Log.h"
 
+#include "MtpDebug.h"
 #include "MtpClient.h"
 #include "MtpCursor.h"
 #include "MtpDevice.h"
@@ -24,6 +24,7 @@
 #include "MtpObjectInfo.h"
 #include "MtpStorageInfo.h"
 
+
 #include "binder/CursorWindow.h"
 
 namespace android {
diff --git a/media/mtp/MtpDataPacket.cpp b/media/mtp/MtpDataPacket.cpp
index d12425a..a7e975c 100644
--- a/media/mtp/MtpDataPacket.cpp
+++ b/media/mtp/MtpDataPacket.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "MtpDataPacket"
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <fcntl.h>
@@ -312,15 +314,12 @@
 int MtpDataPacket::read(int fd) {
     // first read the header
     int ret = ::read(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE);
-printf("MtpDataPacket::read 1 returned %d\n", ret);
     if (ret != MTP_CONTAINER_HEADER_SIZE)
         return -1;
     // then the following data
     int total = MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET);
     int remaining = total - MTP_CONTAINER_HEADER_SIZE;
-printf("total: %d, remaining: %d\n", total, remaining);
     ret = ::read(fd, &mBuffer[0] + MTP_CONTAINER_HEADER_SIZE, remaining);
-printf("MtpDataPacket::read 2 returned %d\n", ret);
     if (ret != remaining)
         return -1;
 
diff --git a/media/mtp/MtpDatabase.cpp b/media/mtp/MtpDatabase.cpp
index d7b21d0..0f9b898 100644
--- a/media/mtp/MtpDatabase.cpp
+++ b/media/mtp/MtpDatabase.cpp
@@ -14,6 +14,9 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "MtpDatabase"
+
+#include "MtpDebug.h"
 #include "MtpDatabase.h"
 #include "MtpDataPacket.h"
 #include "MtpUtils.h"
@@ -140,64 +143,64 @@
 
     // create tables and indices if necessary
     if (!exec(FILE_TABLE_CREATE)) {
-        fprintf(stderr, "could not create file table\n");
+        LOGE("could not create file table");
         return false;
     }
     if (!exec(PATH_INDEX_CREATE)) {
-        fprintf(stderr, "could not path index on file table\n");
+        LOGE("could not path index on file table");
         return false;
     }
     if (!exec(AUDIO_TABLE_CREATE)) {
-        fprintf(stderr, "could not create file table\n");
+        LOGE("could not create file table");
         return false;
     }
 
     if (!mFileIdQuery) {
         mFileIdQuery = new SqliteStatement(this);
         if (!mFileIdQuery->prepare(FILE_ID_QUERY)) {
-            fprintf(stderr, "could not compile FILE_ID_QUERY\n");
+            LOGE("could not compile FILE_ID_QUERY");
             exit(-1);
         }
     }
     if (!mFilePathQuery) {
         mFilePathQuery = new SqliteStatement(this);
         if (!mFilePathQuery->prepare(FILE_PATH_QUERY)) {
-            fprintf(stderr, "could not compile FILE_PATH_QUERY\n");
+            LOGE("could not compile FILE_PATH_QUERY");
             exit(-1);
         }
     }
     if (!mObjectInfoQuery) {
         mObjectInfoQuery = new SqliteStatement(this);
         if (!mObjectInfoQuery->prepare(GET_OBJECT_INFO_QUERY)) {
-            fprintf(stderr, "could not compile GET_OBJECT_INFO_QUERY\n");
+            LOGE("could not compile GET_OBJECT_INFO_QUERY");
             exit(-1);
         }
     }
     if (!mFileInserter) {
         mFileInserter = new SqliteStatement(this);
         if (!mFileInserter->prepare(FILE_INSERT)) {
-            fprintf(stderr, "could not compile FILE_INSERT\n");
+            LOGE("could not compile FILE_INSERT\n");
             exit(-1);
         }
     }
     if (!mFileDeleter) {
         mFileDeleter = new SqliteStatement(this);
         if (!mFileDeleter->prepare(FILE_DELETE)) {
-            fprintf(stderr, "could not compile FILE_DELETE\n");
+            LOGE("could not compile FILE_DELETE\n");
             exit(-1);
         }
     }
     if (!mAudioInserter) {
         mAudioInserter = new SqliteStatement(this);
         if (!mAudioInserter->prepare(AUDIO_INSERT)) {
-            fprintf(stderr, "could not compile AUDIO_INSERT\n");
+            LOGE("could not compile AUDIO_INSERT\n");
             exit(-1);
         }
     }
     if (!mAudioDeleter) {
         mAudioDeleter = new SqliteStatement(this);
         if (!mAudioDeleter->prepare(AUDIO_DELETE)) {
-            fprintf(stderr, "could not compile AUDIO_DELETE\n");
+            LOGE("could not compile AUDIO_DELETE\n");
             exit(-1);
         }
     }
@@ -364,14 +367,14 @@
     query += ";";
 
     SqliteStatement stmt(this);
-    printf("%s\n", (const char *)query);
+    LOGV("%s", (const char *)query);
     stmt.prepare(query);
 
     MtpObjectHandleList* list = new MtpObjectHandleList();
     while (!stmt.isDone()) {
         if (stmt.step()) {
             int index = stmt.getColumnInt(0);
-            printf("stmt.getColumnInt returned %d\n", index);
+            LOGV("stmt.getColumnInt returned %d", index);
             if (index > 0) {
                 MtpObjectFormat format = stmt.getColumnInt(1);
                 index |= getTableForFile(format);
@@ -379,7 +382,7 @@
             }
         }
     }
-    printf("list size: %d\n", list->size());
+    LOGV("list size: %d", list->size());
     return list;
 }
 
@@ -405,7 +408,7 @@
     query += ";";
 
     SqliteStatement stmt(this);
-    printf("%s\n", (const char *)query);
+    LOGV("%s", (const char *)query);
     stmt.prepare(query);
 
     if (!stmt.step())
@@ -440,7 +443,7 @@
             packet.putString(stmt.getColumnString(0));
             break;
         default:
-            fprintf(stderr, "unsupported object type\n");
+            LOGE("unsupported object type\n");
             return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
     }
     return MTP_RESPONSE_OK;
@@ -472,7 +475,7 @@
                             MTP_ASSOCIATION_TYPE_GENERIC_FOLDER :
                             MTP_ASSOCIATION_TYPE_UNDEFINED);
 
-    printf("storageID: %d, format: %d, parent: %d\n", storageID, format, parent);
+    LOGV("storageID: %d, format: %d, parent: %d", storageID, format, parent);
 
     packet.putUInt32(storageID);
     packet.putUInt16(format);
@@ -549,7 +552,7 @@
 
         for (int i = 0; i < count; i++) {
             if (!stmt2.step()) {
-                printf("getFileList ended early\n");
+                LOGW("getFileList ended early");
                 count = i;
                 break;
             }
diff --git a/media/mtp/MtpDebug.h b/media/mtp/MtpDebug.h
index 3cbc209..86f601a 100644
--- a/media/mtp/MtpDebug.h
+++ b/media/mtp/MtpDebug.h
@@ -17,6 +17,9 @@
 #ifndef _MTP_DEBUG_H
 #define _MTP_DEBUG_H
 
+#define LOG_NDEBUG 0
+#include <utils/Log.h>
+
 #include "MtpTypes.h"
 
 namespace android {
diff --git a/media/mtp/MtpDevice.cpp b/media/mtp/MtpDevice.cpp
index 3aa8a35..5612387 100644
--- a/media/mtp/MtpDevice.cpp
+++ b/media/mtp/MtpDevice.cpp
@@ -15,7 +15,14 @@
  */
 
 #define LOG_TAG "MtpDevice"
-#include "utils/Log.h"
+
+#include "MtpDebug.h"
+#include "MtpDevice.h"
+#include "MtpDeviceInfo.h"
+#include "MtpObjectInfo.h"
+#include "MtpProperty.h"
+#include "MtpStorageInfo.h"
+#include "MtpStringBuffer.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -27,14 +34,6 @@
 
 #include <usbhost/usbhost.h>
 
-#include "MtpDevice.h"
-#include "MtpDebug.h"
-#include "MtpDeviceInfo.h"
-#include "MtpObjectInfo.h"
-#include "MtpProperty.h"
-#include "MtpStorageInfo.h"
-#include "MtpStringBuffer.h"
-
 namespace android {
 
 MtpDevice::MtpDevice(struct usb_device* device, int interface,
diff --git a/media/mtp/MtpDevice.h b/media/mtp/MtpDevice.h
index 9b05197..e41a872 100644
--- a/media/mtp/MtpDevice.h
+++ b/media/mtp/MtpDevice.h
@@ -22,6 +22,8 @@
 #include "MtpResponsePacket.h"
 #include "MtpTypes.h"
 
+struct usb_device;
+
 namespace android {
 
 class MtpDeviceInfo;
diff --git a/media/mtp/MtpDeviceInfo.cpp b/media/mtp/MtpDeviceInfo.cpp
index eb25fb3..025b8eb 100644
--- a/media/mtp/MtpDeviceInfo.cpp
+++ b/media/mtp/MtpDeviceInfo.cpp
@@ -15,8 +15,8 @@
  */
 
 #define LOG_TAG "MtpDeviceInfo"
-#include "utils/Log.h"
 
+#include "MtpDebug.h"
 #include "MtpDataPacket.h"
 #include "MtpDeviceInfo.h"
 #include "MtpStringBuffer.h"
diff --git a/media/mtp/MtpMediaScanner.cpp b/media/mtp/MtpMediaScanner.cpp
index 1db1b9d..ac4c0cf 100644
--- a/media/mtp/MtpMediaScanner.cpp
+++ b/media/mtp/MtpMediaScanner.cpp
@@ -14,6 +14,9 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "MtpMediaScanner"
+
+#include "MtpDebug.h"
 #include "MtpDatabase.h"
 #include "MtpMediaScanner.h"
 #include "mtp.h"
@@ -48,7 +51,7 @@
     // returns true if it succeeded, false if an exception occured in the Java code
     virtual bool scanFile(const char* path, long long lastModified, long long fileSize)
     {
-        printf("scanFile %s\n", path);
+        LOGV("scanFile %s", path);
         return true;
     }
 
@@ -88,7 +91,7 @@
             if (sscanf(value, "%d", &temp) == 1)
                 mDuration = temp;
         } else {
-            printf("handleStringTag %s : %s\n", name, value);
+            LOGV("handleStringTag %s : %s", name, value);
         }
         return true;
     }
@@ -104,7 +107,7 @@
     // returns true if it succeeded, false if an exception occured in the Java code
     virtual bool addNoMediaFolder(const char* path)
     {
-        printf("addNoMediaFolder %s\n", path);
+        LOGV("addNoMediaFolder %s", path);
         return true;
     }
 
@@ -180,7 +183,7 @@
     for (int i = 0; i < mFileCount; i++) {
         MtpObjectHandle test = mFileList[i];
         if (! (test & kObjectHandleMarkBit)) {
-            printf("delete missing file %08X\n", test);
+            LOGV("delete missing file %08X", test);
             mDatabase->deleteFile(test);
         }
     }
@@ -260,12 +263,12 @@
 
     unsigned length = strlen(path);
     if (length > sizeof(buffer) + 2) {
-        fprintf(stderr, "path too long: %s\n", path);
+        LOGE("path too long: %s", path);
     }
 
     DIR* dir = opendir(path);
     if (!dir) {
-        fprintf(stderr, "opendir %s failed, errno: %d", path, errno);
+        LOGE("opendir %s failed, errno: %d", path, errno);
         return -1;
     }
 
@@ -285,7 +288,7 @@
             continue;
         }
         if (strlen(name) + 1 > fileNameLength) {
-            fprintf(stderr, "path too long for %s\n", name);
+            LOGE("path too long for %s", name);
             continue;
         }
         strcpy(fileStart, name);
@@ -327,7 +330,7 @@
         handle = mDatabase->addFile(path, format, parent, mStorageID,
                 statbuf.st_size, statbuf.st_mtime);
         if (handle <= 0) {
-            fprintf(stderr, "addFile failed in MtpMediaScanner::scanFile()\n");
+            LOGE("addFile failed in MtpMediaScanner::scanFile()");
             mDatabase->rollbackTransaction();
             return;
         }
@@ -371,7 +374,7 @@
                 return;
             }
         }
-        fprintf(stderr, "file %d not found in mFileList\n", handle);
+        LOGE("file %d not found in mFileList", handle);
     }
 }
 
diff --git a/media/mtp/MtpObjectInfo.cpp b/media/mtp/MtpObjectInfo.cpp
index de0f54a..dd4304e 100644
--- a/media/mtp/MtpObjectInfo.cpp
+++ b/media/mtp/MtpObjectInfo.cpp
@@ -15,8 +15,8 @@
  */
 
 #define LOG_TAG "MtpObjectInfo"
-#include "utils/Log.h"
 
+#include "MtpDebug.h"
 #include "MtpDataPacket.h"
 #include "MtpObjectInfo.h"
 #include "MtpStringBuffer.h"
diff --git a/media/mtp/MtpPacket.cpp b/media/mtp/MtpPacket.cpp
index 3db6abb..42bf8ba 100644
--- a/media/mtp/MtpPacket.cpp
+++ b/media/mtp/MtpPacket.cpp
@@ -14,15 +14,18 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "MtpPacket"
+
+#include "MtpDebug.h"
+#include "MtpPacket.h"
+#include "mtp.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdio.h>
 
 #include <usbhost/usbhost.h>
 
-#include "MtpPacket.h"
-#include "mtp.h"
-
 namespace android {
 
 MtpPacket::MtpPacket(int bufferSize)
@@ -33,7 +36,7 @@
 {
     mBuffer = (uint8_t *)malloc(bufferSize);
     if (!mBuffer) {
-        fprintf(stderr, "out of memory!\n");
+        LOGE("out of memory!");
         abort();
     }
 }
@@ -54,7 +57,7 @@
         int newLength = length + mAllocationIncrement;
         mBuffer = (uint8_t *)realloc(mBuffer, newLength);
         if (!mBuffer) {
-            fprintf(stderr, "out of memory!\n");
+            LOGE("out of memory!");
             abort();
         }
         mBufferSize = newLength;
@@ -62,12 +65,23 @@
 }
 
 void MtpPacket::dump() {
+#define DUMP_BYTES_PER_ROW  16
+    char buffer[500];
+    char* bufptr = buffer;
+
     for (int i = 0; i < mPacketSize; i++) {
-        printf("%02X ", mBuffer[i]);
-        if (i % 16 == 15)
-            printf("\n");
+        sprintf(bufptr, "%02X ", mBuffer[i]);
+        bufptr += strlen(bufptr);
+        if (i % DUMP_BYTES_PER_ROW == (DUMP_BYTES_PER_ROW - 1)) {
+            LOGV("%s", buffer);
+            bufptr = buffer;
+        }
     }
-    printf("\n\n");
+    if (bufptr != buffer) {
+        // print last line
+        LOGV("%s", buffer);
+    }
+    LOGV("\n");
 }
 
 uint16_t MtpPacket::getUInt16(int offset) const {
@@ -109,7 +123,7 @@
 
 uint32_t MtpPacket::getParameter(int index) const {
     if (index < 1 || index > 5) {
-        fprintf(stderr, "index %d out of range in MtpRequestPacket::getParameter\n", index);
+        LOGE("index %d out of range in MtpRequestPacket::getParameter", index);
         return 0;
     }
     return getUInt32(MTP_CONTAINER_PARAMETER_OFFSET + (index - 1) * sizeof(uint32_t));
@@ -117,7 +131,7 @@
 
 void MtpPacket::setParameter(int index, uint32_t value) {
     if (index < 1 || index > 5) {
-        fprintf(stderr, "index %d out of range in MtpResponsePacket::setParameter\n", index);
+        LOGE("index %d out of range in MtpResponsePacket::setParameter", index);
         return;
     }
     int offset = MTP_CONTAINER_PARAMETER_OFFSET + (index - 1) * sizeof(uint32_t);
@@ -129,7 +143,7 @@
 #ifdef MTP_HOST
 int MtpPacket::transfer(struct usb_endpoint *ep, void* buffer, int length) {
     if (usb_endpoint_queue(ep, buffer, length)) {
-        printf("usb_endpoint_queue failed, errno: %d\n", errno);
+        LOGE("usb_endpoint_queue failed, errno: %d", errno);
         return -1;
     }
     int ep_num;
diff --git a/media/mtp/MtpProperty.cpp b/media/mtp/MtpProperty.cpp
index 65ce4d4..a114e83 100644
--- a/media/mtp/MtpProperty.cpp
+++ b/media/mtp/MtpProperty.cpp
@@ -15,7 +15,6 @@
  */
 
 #define LOG_TAG "MtpProperty"
-#include "utils/Log.h"
 
 #include "MtpDataPacket.h"
 #include "MtpProperty.h"
@@ -144,7 +143,7 @@
             packet.getUInt128(value.u128);
             break;
         default:
-            fprintf(stderr, "unknown type %d in MtpProperty::readValue\n", mType);
+            LOGE("unknown type %d in MtpProperty::readValue", mType);
     }
 }
 
diff --git a/media/mtp/MtpRequestPacket.cpp b/media/mtp/MtpRequestPacket.cpp
index e3a720c..8ece580 100644
--- a/media/mtp/MtpRequestPacket.cpp
+++ b/media/mtp/MtpRequestPacket.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "MtpRequestPacket"
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <fcntl.h>
diff --git a/media/mtp/MtpResponsePacket.cpp b/media/mtp/MtpResponsePacket.cpp
index a1979d7..3ef714e 100644
--- a/media/mtp/MtpResponsePacket.cpp
+++ b/media/mtp/MtpResponsePacket.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "MtpResponsePacket"
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <fcntl.h>
diff --git a/media/mtp/MtpServer.cpp b/media/mtp/MtpServer.cpp
index a9b4ca6..048d1dc 100644
--- a/media/mtp/MtpServer.cpp
+++ b/media/mtp/MtpServer.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "MtpServer"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
@@ -29,6 +31,7 @@
 #include "MtpStorage.h"
 #include "MtpStringBuffer.h"
 #include "MtpDatabase.h"
+#include "MtpDebug.h"
 
 #include "f_mtp.h"
 
@@ -154,12 +157,12 @@
 void MtpServer::run() {
     int fd = mFD;
 
-    printf("MtpServer::run fd: %d\n", fd);
+    LOGD("MtpServer::run fd: %d", fd);
 
     while (1) {
         int ret = mRequest.read(fd);
         if (ret < 0) {
-            fprintf(stderr, "request read returned %d, errno: %d\n", ret, errno);
+            LOGE("request read returned %d, errno: %d", ret, errno);
             if (errno == ECANCELED) {
                 // return to top of loop and wait for next command
                 continue;
@@ -169,7 +172,7 @@
         MtpOperationCode operation = mRequest.getOperationCode();
         MtpTransactionID transaction = mRequest.getTransactionID();
 
-        printf("operation: %s\n", MtpDebug::getOperationCodeName(operation));
+        LOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
         mRequest.dump();
 
         // FIXME need to generalize this
@@ -177,14 +180,14 @@
         if (dataIn) {
             int ret = mData.read(fd);
             if (ret < 0) {
-                fprintf(stderr, "data read returned %d, errno: %d\n", ret, errno);
+                LOGE("data read returned %d, errno: %d", ret, errno);
                 if (errno == ECANCELED) {
                     // return to top of loop and wait for next command
                     continue;
                 }
                 break;
             }
-            printf("received data:\n");
+            LOGV("received data:");
             mData.dump();
         } else {
             mData.reset();
@@ -194,11 +197,11 @@
             if (!dataIn && mData.hasData()) {
                 mData.setOperationCode(operation);
                 mData.setTransactionID(transaction);
-                printf("sending data:\n");
+                LOGV("sending data:");
                 mData.dump();
                 ret = mData.write(fd);
                 if (ret < 0) {
-                    fprintf(stderr, "request write returned %d, errno: %d\n", ret, errno);
+                    LOGE("request write returned %d, errno: %d", ret, errno);
                     if (errno == ECANCELED) {
                         // return to top of loop and wait for next command
                         continue;
@@ -208,10 +211,10 @@
             }
 
             mResponse.setTransactionID(transaction);
-            printf("sending response %04X\n", mResponse.getResponseCode());
+            LOGV("sending response %04X", mResponse.getResponseCode());
             ret = mResponse.write(fd);
             if (ret < 0) {
-                fprintf(stderr, "request write returned %d, errno: %d\n", ret, errno);
+                LOGE("request write returned %d, errno: %d", ret, errno);
                 if (errno == ECANCELED) {
                     // return to top of loop and wait for next command
                     continue;
@@ -219,7 +222,7 @@
                 break;
             }
         } else {
-            printf("skipping response\n");
+            LOGV("skipping response");
         }
     }
 }
@@ -232,7 +235,7 @@
 
     if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
         // FIXME - need to delete mSendObjectHandle from the database
-        fprintf(stderr, "expected SendObject after SendObjectInfo\n");
+        LOGE("expected SendObject after SendObjectInfo");
         mSendObjectHandle = kInvalidObjectHandle;
     }
 
@@ -486,8 +489,9 @@
     time_t modifiedTime;
     if (!parseDateTime(modified, modifiedTime))
         modifiedTime = 0;
-printf("SendObjectInfo format: %04X size: %d name: %s, created: %s, modified: %s\n",
-format, mSendObjectFileSize, (const char*)name, (const char*)created, (const char*)modified);
+    LOGV("SendObjectInfo format: %04X size: %d name: %s, created: %s, modified: %s",
+            format, mSendObjectFileSize, (const char*)name, (const char*)created,
+            (const char*)modified);
 
     if (path[path.size() - 1] != '/')
         path += "/";
@@ -526,7 +530,7 @@
 
 MtpResponseCode MtpServer::doSendObject() {
     if (mSendObjectHandle == kInvalidObjectHandle) {
-        fprintf(stderr, "Expected SendObjectInfo before SendObject\n");
+        LOGE("Expected SendObjectInfo before SendObject");
         return MTP_RESPONSE_NO_VALID_OBJECT_INFO;
     }
 
@@ -549,7 +553,7 @@
     ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr);
     close(mfr.fd);
     // FIXME - we need to delete mSendObjectHandle from the database if this fails.
-    printf("MTP_RECEIVE_FILE returned %d\n", ret);
+    LOGV("MTP_RECEIVE_FILE returned %d", ret);
     mSendObjectHandle = kInvalidObjectHandle;
 
     if (ret < 0) {
@@ -574,7 +578,7 @@
     if (!mDatabase->getObjectFilePath(handle, filePath, fileLength))
         return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
 
-printf("deleting %s\n", (const char *)filePath);
+    LOGV("deleting %s", (const char *)filePath);
     // one of these should work
     rmdir((const char *)filePath);
     unlink((const char *)filePath);
diff --git a/media/mtp/MtpStorage.cpp b/media/mtp/MtpStorage.cpp
index ed2376e..7e89a90 100644
--- a/media/mtp/MtpStorage.cpp
+++ b/media/mtp/MtpStorage.cpp
@@ -14,6 +14,9 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "MtpStorage"
+
+#include "MtpDebug.h"
 #include "MtpDatabase.h"
 #include "MtpStorage.h"
 #include "MtpMediaScanner.h"
@@ -36,7 +39,7 @@
         mDatabase(db),
         mMaxCapacity(0)
 {
-    printf("MtpStorage id: %d path: %s\n", id, filePath);
+    LOGD("MtpStorage id: %d path: %s\n", id, filePath);
 }
 
 MtpStorage::~MtpStorage() {
diff --git a/media/mtp/MtpStorageInfo.cpp b/media/mtp/MtpStorageInfo.cpp
index 5a5306b..ca64ac0 100644
--- a/media/mtp/MtpStorageInfo.cpp
+++ b/media/mtp/MtpStorageInfo.cpp
@@ -15,8 +15,8 @@
  */
 
 #define LOG_TAG "MtpStorageInfo"
-#include "utils/Log.h"
 
+#include "MtpDebug.h"
 #include "MtpDataPacket.h"
 #include "MtpStorageInfo.h"
 #include "MtpStringBuffer.h"
diff --git a/media/mtp/MtpStringBuffer.cpp b/media/mtp/MtpStringBuffer.cpp
index 8694575..2d3cf69 100644
--- a/media/mtp/MtpStringBuffer.cpp
+++ b/media/mtp/MtpStringBuffer.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "MtpStringBuffer"
+
 #include <string.h>
 
 #include "MtpDataPacket.h"
diff --git a/media/mtp/MtpUtils.cpp b/media/mtp/MtpUtils.cpp
index 10ca166..ab01ef5 100644
--- a/media/mtp/MtpUtils.cpp
+++ b/media/mtp/MtpUtils.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "MtpUtils"
+
 #include <stdio.h>
 #include <time.h>
 
diff --git a/media/mtp/SqliteDatabase.cpp b/media/mtp/SqliteDatabase.cpp
index e115630..1de3a41 100644
--- a/media/mtp/SqliteDatabase.cpp
+++ b/media/mtp/SqliteDatabase.cpp
@@ -14,6 +14,9 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "SqliteDatabase"
+
+#include "MtpDebug.h"
 #include "SqliteDatabase.h"
 #include "SqliteStatement.h"
 
@@ -37,7 +40,7 @@
     // SQLITE_OPEN_NOMUTEX?
     int ret = sqlite3_open_v2(path, &mDatabaseHandle, flags, NULL);
     if (ret) {
-        fprintf(stderr, "could not open database\n");
+        LOGE("could not open database\n");
         return false;
     }
     return true;
diff --git a/media/mtp/SqliteStatement.cpp b/media/mtp/SqliteStatement.cpp
index e1300b6..adef7ae 100644
--- a/media/mtp/SqliteStatement.cpp
+++ b/media/mtp/SqliteStatement.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "SqliteStatement"
+
 #include "SqliteStatement.h"
 #include "SqliteDatabase.h"
 
diff --git a/media/mtp/mtptest.cpp b/media/mtp/mtptest.cpp
index 767cf2e..9062494 100644
--- a/media/mtp/mtptest.cpp
+++ b/media/mtp/mtptest.cpp
@@ -15,7 +15,8 @@
  */
 
 #define LOG_TAG "mtp_usb"
-#include "cutils/log.h"
+
+#include "MtpDebug.h"
 
 #include <stdio.h>
 #include <stdlib.h>