import java.util.Properties;
import org.apache.commons.io.FileUtils;
+import org.apache.log4j.Logger;
public class Config {
//constants
String project_version;
String build_time;
+ static Logger logger = Logger.getLogger(Config.class);
private Config() {
Properties configProp = new Properties();
} else if (value.equals("disabled")) {
c.setEnabled(false);
} else {
- System.out.println("Error in config file, unknown channel status \"" + parts.get(2) + "\", should be enabled or disabled");
+ logger.error("Error in config file, unknown channel status \"" + parts.get(2) + "\", should be enabled or disabled");
}
break;
case 2:
} else if (value.equals("disabled")) {
c.setEnabled(false);
} else {
- System.out.println("Error in config file, unknown channel status \"" + parts.get(3) + "\", should be enabled or disabled");
+ logger.error("Error in config file, unknown channel status \"" + parts.get(3) + "\", should be enabled or disabled");
}
}
result.channels.add(c);
try {
fileformat = Integer.parseInt(parts.get(1));
} catch (NumberFormatException e) {
- System.out.println("Unknown config file format " + parts.get(1));
+ logger.error("Unknown config file format " + parts.get(1));
fileformat = CURRENT_FILE_FORMAT; // may crash later
}
if (fileformat > CURRENT_FILE_FORMAT) {
- System.out.println("Unknown config file format " + parts.get(1));
+ logger.error("Unknown config file format " + parts.get(1));
fileformat = CURRENT_FILE_FORMAT;
}
} else if (key.equals("cache-file")) {
result.cacheDbUser = "SA";
result.cacheDbPassword = "";
} else {
- System.out.println("Illegal key cache-file in config file!");
+ logger.warn("Illegal key cache-file in config file with fileformat "+fileformat+"!");
}
} else if (key.equals("cache-db-handle")) {
result.cacheDbHandle = parts.get(1);
} else if (key.equals("nice-time-milliseconds")) {
result.niceMilliseconds = Integer.parseInt(parts.get(1));
} else {
- System.out.println("Unknown key " + key + " in config file!");
+ logger.error("Unknown key " + key + " in config file!");
}
}
} catch (IOException e) {
e.printStackTrace();
- System.out.println("Error reading configuration file, continuing with empty configuration");
+ logger.warn("Error reading configuration file, continuing with empty configuration");
return getDefaultConfig();
}
return result;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
+import org.apache.log4j.Logger;
public class ProgrammeCache {
private Connection db;
private final static Integer SCHEMA_VERSION=1;
private final static String SCHEMA_KEY="TV_GRAB_NL_JAVA_SCHEMA_VERSION";
+ static Logger logger = Logger.getLogger(ProgrammeCache.class);
+
public ProgrammeCache(Config config) {
this.config = config;
try {
} catch (SQLException e) {
db = null;
if (!config.quiet) {
- System.out.println("Unable to open cache database, proceeding without cache");
- if (config.logLevel>=Config.LOG_DEBUG) e.printStackTrace();
+ logger.warn("Unable to open cache database, proceeding without cache");
+ logger.debug("Stack trace: ", e);
}
}
boolean recreateTable = false;
stat.setString(2, SCHEMA_KEY);
ResultSet result = stat.executeQuery();
if (!result.next()) {
- if (!config.quiet) System.out.println("No schema version found in database");
+ logger.debug("No schema version found in database");
recreateTable=true;
} else {
Integer currentSchema = (Integer) result.getObject("programme");
if (currentSchema<SCHEMA_VERSION) {
- if (!config.quiet) System.out.println("Current cache database schema version " + currentSchema + " is lower than my version " + SCHEMA_VERSION);
+ logger.debug("Current cache database schema version " + currentSchema + " is lower than my version " + SCHEMA_VERSION);
recreateTable = true;
} else if (currentSchema>SCHEMA_VERSION) {
- if (!config.quiet) System.out.println("Got a database schema from the future, since my version is " + SCHEMA_VERSION+ " and yours is " + currentSchema);
+ logger.warn("Got a database schema from the future, since my version is " + SCHEMA_VERSION+ " and yours is " + currentSchema);
recreateTable = true;
}
stat.close();
} catch (SQLException e) {
if (!config.quiet) {
- System.out.println("Got SQL exception when trying to find current database schema");
- System.out.flush();
- if (config.logLevel>=Config.LOG_DEBUG) e.printStackTrace();
- System.out.flush();
+ logger.warn("Got SQL exception when trying to find current database schema");
+ logger.debug("Stack trace", e);
}
recreateTable = true;
}
if (recreateTable) {
- if (!config.quiet) System.out.println("Unknown cache schema, removing and recreating cache");
+ logger.info("Unknown cache schema, removing and recreating cache");
try {
Statement stat = db.createStatement();
// System.out.println("Dropping old table");
stat2.executeUpdate();
} catch (SQLException e) {
if (!config.quiet) {
- System.out.println("Unable to create cache database, proceeding without cache");
- System.out.flush();
- if (config.logLevel>=Config.LOG_DEBUG) e.printStackTrace();
- System.out.flush();
+ logger.warn("Unable to create cache database, proceeding without cache");
+ logger.debug("stack trace: ", e);
}
db = null;
}
clearSourceStatement = db.prepareStatement("DELETE FROM cache WHERE source=?");
} catch (SQLException e) {
if (!config.quiet) {
- System.out.println("Unable to prepare statements, proceeding without cache");
- System.out.flush();
- if (config.logLevel>=Config.LOG_DEBUG) e.printStackTrace();
- System.out.flush();
+ logger.warn("Unable to prepare statements, proceeding without cache");
+ logger.debug("stack trace: ", e);
}
db = null;
}
//System.out.println(putStatement.toString());
int count = putStatement.executeUpdate();
if (count!=1 && !config.quiet) {
- System.out.println("Weird, cache database update statement affected " + count + " rows");
+ logger.warn("Weird, cache database update statement affected " + count + " rows");
}
} catch (SQLException e) {
- System.out.println("Error writing programme ("+source+","+id+") to cache");
- if (config.logLevel>=Config.LOG_DEBUG) e.printStackTrace();
+ logger.warn("Error writing programme ("+source+","+id+") to cache");
+ logger.debug("stack trace:", e);
}
}
stat = db.createStatement();
int count = stat.executeUpdate("DELETE FROM cache WHERE date<CURRENT_DATE - 3 DAY");
if (!config.quiet && count>0) {
- System.out.println("Purged " + count + " old entries from cache");
+ logger.info("Purged " + count + " old entries from cache");
}
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
- e.printStackTrace();
+ logger.debug("stack trace:", e);
}
}