You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
3.1 KiB
93 lines
3.1 KiB
11 years ago
|
/*
|
||
|
* Copyright 2010-2013 Eric Kok et al.
|
||
|
*
|
||
|
* Transdroid is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation, either version 3 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* Transdroid is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
11 years ago
|
package org.transdroid.core.gui.log;
|
||
|
|
||
|
import java.sql.SQLException;
|
||
|
import java.util.Date;
|
||
|
|
||
11 years ago
|
import org.androidannotations.annotations.Bean;
|
||
11 years ago
|
import org.androidannotations.annotations.EBean;
|
||
|
import org.androidannotations.annotations.EBean.Scope;
|
||
|
import org.androidannotations.annotations.OrmLiteDao;
|
||
11 years ago
|
import org.transdroid.core.gui.navigation.NavigationHelper;
|
||
11 years ago
|
import org.transdroid.daemon.util.ITLogger;
|
||
11 years ago
|
|
||
|
import android.content.Context;
|
||
|
|
||
|
import com.j256.ormlite.dao.Dao;
|
||
|
import com.j256.ormlite.stmt.DeleteBuilder;
|
||
|
|
||
|
/**
|
||
|
* Application-wide logging class that registers entries in the database (for a certain time).
|
||
|
* @author Eric Kok
|
||
|
*/
|
||
|
@EBean(scope = Scope.Singleton)
|
||
11 years ago
|
public class Log implements ITLogger {
|
||
11 years ago
|
|
||
|
public static final String LOG_NAME = "Transdroid";
|
||
|
private static final long MAX_LOG_AGE = 15 * 60 * 1000; // 15 minutes
|
||
|
|
||
11 years ago
|
// Access to resources and database in local singleton instance
|
||
|
private Context context;
|
||
11 years ago
|
@OrmLiteDao(helper = DatabaseHelper.class, model = ErrorLogEntry.class)
|
||
|
Dao<ErrorLogEntry, Integer> errorLogDao;
|
||
11 years ago
|
@Bean
|
||
|
protected NavigationHelper navigationHelper;
|
||
11 years ago
|
|
||
11 years ago
|
protected Log(Context context) {
|
||
|
this.context = context;
|
||
|
}
|
||
|
|
||
11 years ago
|
protected void log(String logName, int priority, String message) {
|
||
11 years ago
|
if (navigationHelper.inDebugMode())
|
||
11 years ago
|
android.util.Log.println(priority, LOG_NAME, message);
|
||
11 years ago
|
try {
|
||
|
// Store this log message to the database
|
||
|
errorLogDao.create(new ErrorLogEntry(priority, logName, message));
|
||
|
// Truncate the error log
|
||
|
DeleteBuilder<ErrorLogEntry, Integer> db = errorLogDao.deleteBuilder();
|
||
|
db.setWhere(db.where().le(ErrorLogEntry.DATEANDTIME, new Date(new Date().getTime() - MAX_LOG_AGE)));
|
||
|
errorLogDao.delete(db.prepare());
|
||
|
} catch (SQLException e) {
|
||
|
android.util.Log.e(LOG_NAME, "Cannot write log message to database: " + e.toString());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void e(Context caller, String message) {
|
||
|
Log_.getInstance_(caller).log(caller.getClass().toString(), android.util.Log.ERROR, message);
|
||
|
}
|
||
|
|
||
|
public static void i(Context caller, String message) {
|
||
|
Log_.getInstance_(caller).log(caller.getClass().toString(), android.util.Log.INFO, message);
|
||
|
}
|
||
|
|
||
|
public static void d(Context caller, String message) {
|
||
|
Log_.getInstance_(caller).log(caller.getClass().toString(), android.util.Log.DEBUG, message);
|
||
|
}
|
||
|
|
||
11 years ago
|
@Override
|
||
|
public void d(String self, String msg) {
|
||
|
Log.d(context, msg);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void e(String self, String msg) {
|
||
|
Log.e(context, msg);
|
||
|
}
|
||
|
|
||
11 years ago
|
}
|