Hey, I attached a couple of our java classes that perform this function. I
had to clean up a bit and remove the package names, but i'm sure that won't
slow you down :-)
here is the database table definition:
buf.append("CREATE TABLE ").append(DbMetadata.TABLE_DEBUG
).append(" (");
buf.append(DbMetadata.DEBUG_TIME).append(" INTEGER, ");
buf.append(DbMetadata.DEBUG_TYPE).append(" TEXT, ");
buf.append(DbMetadata.DEBUG_PACKAGE).append(" TEXT, ");
buf.append(DbMetadata.DEBUG_MESSAGE).append(" TEXT)");
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=enimport java.util.List;
import android.util.Log;
public class DbLogService implements ILogService
{
private static final String TAG = DbLogService.class.getName();
private static DbLogService instance = null;
private CoreDataPersister persister = CoreDataPersister.getInstance();
public static DbLogService getInstance()
{
if (instance == null)
{
instance = new DbLogService();
}
return instance;
}
private DbLogService()
{
}
public List<DebugMessage> getLogs()
{
return persister.getMessages();
}
public void debug(String label, String msg)
{
writeLog("debug", label, msg);
}
public void verbose(String label, String msg)
{
writeLog("verbose", label, msg);
}
public void info(String label, String msg)
{
writeLog("info", label, msg);
}
public void warn(String label, String msg)
{
writeLog("warn", label, msg);
}
public void error(String label, String msg)
{
writeLog("error", label, msg);
}
private void writeLog(String type, String pack, String msg)
{
if (AppMetadata.DEBUG_ENABLED)
{
DebugMessage dm = new DebugMessage(System.currentTimeMillis(), type, pack, msg);
try
{
persister.saveMessage(dm);
}
catch (Exception e)
{
// catching and swallowing any exception that occurs here.
Log.w(TAG, e);
}
}
}
}
public class DebugMessage
{
private long time;
private String type;
private String packageLabel;
private String message;
public DebugMessage(long time, String type, String pack, String msg)
{
this.time = time;
this.type = type;
this.packageLabel = pack;
this.message = msg;
}
public long getTime()
{
return time;
}
public void setTime(long time)
{
this.time = time;
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
public String getPackageLabel()
{
return packageLabel;
}
public void setPackageLabel(String packageLabel)
{
this.packageLabel = packageLabel;
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
public String toString()
{
StringBuilder buf = new StringBuilder();
buf.append("DebugMessage: time=[").append(time).append("(").append(DateFormat.format(AppMetadata.DEFAULT_DATE_FORMAT, time)).append(")]").append(AppMetadata.NEW_LINE);
// buf.append("type=[").append(type).append("], ");
buf.append("package=[").append(packageLabel).append("]").append(AppMetadata.NEW_LINE);
buf.append("msg=[").append(message).append("]").append(AppMetadata.NEW_LINE);
return buf.toString();
}
}
import java.util.ArrayList;
import java.util.List;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
public class DebugPersister
{
private static final String TAG = DebugPersister.class.getName();
private static DebugPersister instance;
public static DebugPersister getInstance()
{
if (instance == null)
{
instance = new DebugPersister();
}
return instance;
}
private DebugPersister()
{
}
public List<DebugMessage> getMessages()
{
ContentResolver cr = ctx.getContentResolver();
Uri uri = Uri.withAppendedPath(CoreDataProvider.CONTENT_URI, ProviderMetadata.CORE_URL_DEBUG);
Cursor c = null;
List<DebugMessage> messages = null;
try
{
c = cr.query(uri, null, null, null, null);
if (c == null || !c.moveToFirst())
{
return null;
}
messages = new ArrayList<DebugMessage>();
do
{
long time = c.getLong(c.getColumnIndex(DbMetadata.DEBUG_TIME));
String type = c.getString(c.getColumnIndex(DbMetadata.DEBUG_TYPE));
String pack = c.getString(c.getColumnIndex(DbMetadata.DEBUG_PACKAGE));
String content = c.getString(c.getColumnIndex(DbMetadata.DEBUG_MESSAGE));
DebugMessage msg = new DebugMessage(time, type, pack, content);
messages.add(msg);
} while (c.moveToNext());
}
finally
{
if (c != null)
{
c.close();
}
}
return messages;
}
public void saveMessage(DebugMessage msg)
{
if (msg == null)
{
Log.w(TAG, "saveCurrentLocation() called with a null event");
return;
}
ContentResolver cr = ctx.getContentResolver();
Uri uri = Uri.withAppendedPath(CoreDataProvider.CONTENT_URI, ProviderMetadata.CORE_URL_DEBUG);
ContentValues cv = new ContentValues();
cv.put(DbMetadata.DEBUG_TIME, msg.getTime());
cv.put(DbMetadata.DEBUG_TYPE, msg.getType());
cv.put(DbMetadata.DEBUG_PACKAGE, msg.getPackageLabel());
cv.put(DbMetadata.DEBUG_MESSAGE, msg.getMessage());
cr.insert(uri, cv);
}
}