1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 16:56:16 +00:00

Completely reworked logging.

* Using slf4j and logback for logging.
* Disabled all logging output from kotlogram by default.
This commit is contained in:
Fabian Schlenz 2016-07-20 06:37:24 +02:00
parent a43b81ee91
commit 1481e5625e
7 changed files with 128 additions and 149 deletions

View File

@ -14,9 +14,13 @@ repositories {
} }
dependencies { dependencies {
compile 'com.github.badoualy:kotlogram:497e5dd62d9bcb341f584164b04b4b537ce9d295' compile('com.github.badoualy:kotlogram:497e5dd62d9bcb341f584164b04b4b537ce9d295') {
exclude module: 'slf4j-simple'
}
compile 'org.xerial:sqlite-jdbc:3.8.11.2' compile 'org.xerial:sqlite-jdbc:3.8.11.2'
compile 'com.github.spullara.mustache.java:compiler:0.8.18' compile 'com.github.spullara.mustache.java:compiler:0.8.18'
compile 'org.slf4j:slf4j-api:1.7.21'
compile 'ch.qos.logback:logback-classic:1.1.7'
} }
run { run {

View File

@ -31,13 +31,17 @@ import java.util.List;
import java.util.Scanner; import java.util.Scanner;
import java.util.Vector; import java.util.Vector;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
public class CommandLineController { public class CommandLineController {
private static Logger logger = LoggerFactory.getLogger(CommandLineController.class);
private ApiStorage storage; private ApiStorage storage;
public TelegramApp app; public TelegramApp app;
public UserManager user = null; public UserManager user = null;
public CommandLineController() { public CommandLineController() {
Log.debug("CommandLineController started. App version %s", Config.APP_APPVER); logger.info("CommandLineController started. App version {}", Config.APP_APPVER);
System.out.println("Telegram_Backup version " + Config.APP_APPVER + ", Copyright (C) 2016 Fabian Schlenz"); System.out.println("Telegram_Backup version " + Config.APP_APPVER + ", Copyright (C) 2016 Fabian Schlenz");
System.out.println(); System.out.println();
System.out.println("Telegram_Backup comes with ABSOLUTELY NO WARRANTY. This is free software, and you are"); System.out.println("Telegram_Backup comes with ABSOLUTELY NO WARRANTY. This is free software, and you are");
@ -54,46 +58,41 @@ public class CommandLineController {
System.exit(0); System.exit(0);
} }
Log.debug("Target dir at startup: %s", Config.FILE_BASE); logger.debug("Target dir at startup: {}", Config.FILE_BASE);
if (CommandLineOptions.val_target != null) { if (CommandLineOptions.val_target != null) {
Config.FILE_BASE = CommandLineOptions.val_target; Config.FILE_BASE = CommandLineOptions.val_target;
} }
Log.debug("Target dir after options: %s", Config.FILE_BASE); logger.debug("Target dir after options: {}", Config.FILE_BASE);
System.out.println("Base directory for files: " + Config.FILE_BASE); System.out.println("Base directory for files: " + Config.FILE_BASE);
if (CommandLineOptions.cmd_list_accounts) this.list_accounts(); if (CommandLineOptions.cmd_list_accounts) this.list_accounts();
Log.debug("Initializing TelegramApp"); logger.debug("Initializing TelegramApp");
if (CommandLineOptions.cmd_debug_telegram) { if (CommandLineOptions.cmd_debug_telegram) {
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG"); //System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG");
} else { } else {
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "ERROR"); //System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "ERROR");
} }
app = new TelegramApp(Config.APP_ID, Config.APP_HASH, Config.APP_MODEL, Config.APP_SYSVER, Config.APP_APPVER, Config.APP_LANG); app = new TelegramApp(Config.APP_ID, Config.APP_HASH, Config.APP_MODEL, Config.APP_SYSVER, Config.APP_APPVER, Config.APP_LANG);
Log.debug("Checking accounts"); logger.trace("Checking accounts");
Log.up();
String account = null; String account = null;
Vector<String> accounts = Utils.getAccounts(); Vector<String> accounts = Utils.getAccounts();
if (CommandLineOptions.cmd_login) { if (CommandLineOptions.cmd_login) {
Log.debug("Login requested, doing nothing."); logger.debug("Login requested, doing nothing.");
// do nothing // do nothing
} else if (CommandLineOptions.val_account!=null) { } else if (CommandLineOptions.val_account!=null) {
Log.debug("Account requested: %s", CommandLineOptions.val_account); logger.debug("Account requested: {}", CommandLineOptions.val_account);
Log.debug("Checking accounts for match."); logger.trace("Checking accounts for match.");
Log.up();
boolean found = false; boolean found = false;
for (String acc : accounts) { for (String acc : accounts) {
Log.debug("Checking %s", acc); logger.trace("Checking {}", acc);
Log.up();
if (acc.equals(CommandLineOptions.val_account)) { if (acc.equals(CommandLineOptions.val_account)) {
found=true; found=true;
Log.debug("Matches."); logger.trace("Matches.");
} }
Log.down();
} }
Log.down();
if (!found) { if (!found) {
show_error("Couldn't find account '" + CommandLineOptions.val_account + "'. Maybe you want to use '--login' first?"); show_error("Couldn't find account '" + CommandLineOptions.val_account + "'. Maybe you want to use '--login' first?");
} }
@ -109,25 +108,23 @@ public class CommandLineController {
"Use '--account <x>' to use account <x>.\n" + "Use '--account <x>' to use account <x>.\n" +
"Use '--list-accounts' to see all available accounts."); "Use '--list-accounts' to see all available accounts.");
} }
Log.debug("accounts.size(): %d", accounts.size()); logger.debug("accounts.size(): {}", accounts.size());
Log.debug("account: %s", account); logger.debug("account: {}", account);
Log.debug("CommandLineOptions.cmd_login: %s", CommandLineOptions.cmd_login); logger.debug("CommandLineOptions.cmd_login: {}", CommandLineOptions.cmd_login);
Log.down();
logger.info("Initializing ApiStorage");
Log.debug("Initializing ApiStorage");
storage = new ApiStorage(account); storage = new ApiStorage(account);
Log.debug("Initializing TelegramUpdateHandler"); logger.info("Initializing TelegramUpdateHandler");
TelegramUpdateHandler handler = new TelegramUpdateHandler(); TelegramUpdateHandler handler = new TelegramUpdateHandler();
Log.debug("Creating Client"); logger.info("Creating Client");
TelegramClient client = Kotlogram.getDefaultClient(app, storage, Kotlogram.PROD_DC4, handler); TelegramClient client = Kotlogram.getDefaultClient(app, storage, Kotlogram.PROD_DC4, handler);
try { try {
Log.debug("Creating UserManager"); logger.info("Creating UserManager");
user = new UserManager(client); user = new UserManager(client);
Log.debug("CommandLineOptions.val_export: %s", CommandLineOptions.val_export); logger.debug("CommandLineOptions.val_export: {}", CommandLineOptions.val_export);
if (CommandLineOptions.val_export != null) { if (CommandLineOptions.val_export != null) {
Log.up();
if (CommandLineOptions.val_export.toLowerCase().equals("html")) { if (CommandLineOptions.val_export.toLowerCase().equals("html")) {
(new HTMLExporter()).export(user); (new HTMLExporter()).export(user);
System.exit(0); System.exit(0);
@ -139,23 +136,20 @@ public class CommandLineController {
} }
} }
Log.debug("CommandLineOptions.cmd_login: %s", CommandLineOptions.cmd_login); logger.debug("CommandLineOptions.cmd_login: {}", CommandLineOptions.cmd_login);
if (CommandLineOptions.cmd_login) { if (CommandLineOptions.cmd_login) {
Log.up();
cmd_login(); cmd_login();
System.exit(0); System.exit(0);
} }
System.out.println("You are logged in as " + user.getUserString()); System.out.println("You are logged in as " + user.getUserString());
Log.debug("Initializing Download Manager"); logger.info("Initializing Download Manager");
Log.up();
DownloadManager d = new DownloadManager(user, client, new CommandLineDownloadProgress()); DownloadManager d = new DownloadManager(user, client, new CommandLineDownloadProgress());
Log.debug("Calling DownloadManager.downloadMessages with limit %d", CommandLineOptions.val_limit_messages); logger.debug("Calling DownloadManager.downloadMessages with limit {}", CommandLineOptions.val_limit_messages);
d.downloadMessages(CommandLineOptions.val_limit_messages); d.downloadMessages(CommandLineOptions.val_limit_messages);
Log.debug("Calling DownloadManager.downloadMedia"); logger.debug("Calling DownloadManager.downloadMedia");
d.downloadMedia(); d.downloadMedia();
Log.down();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {

View File

@ -17,10 +17,45 @@
package de.fabianonline.telegram_backup; package de.fabianonline.telegram_backup;
import de.fabianonline.telegram_backup.CommandLineController; import de.fabianonline.telegram_backup.CommandLineController;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.classic.Level;
public class CommandLineRunner { public class CommandLineRunner {
public static void main(String[] args) { public static void main(String[] args) {
CommandLineOptions.parseOptions(args); CommandLineOptions.parseOptions(args);
// Set up logging
Logger logger = (Logger)LoggerFactory.getLogger(CommandLineRunner.class);
Logger rootLogger = (Logger)LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
LoggerContext rootContext = rootLogger.getLoggerContext();
rootContext.reset();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(rootContext);
encoder.setPattern("%d{HH:mm:ss} %-5level %-35.-35(%logger{0}.%method): %message%n");
encoder.start();
ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
appender.setContext(rootContext);
appender.setEncoder(encoder);
appender.start();
rootLogger.addAppender(appender);
rootLogger.setLevel(Level.OFF);
if (CommandLineOptions.cmd_debug) {
((Logger)LoggerFactory.getLogger("de.fabianonline.telegram_backup")).setLevel(Level.TRACE);
}
if (CommandLineOptions.cmd_debug_telegram) {
((Logger)LoggerFactory.getLogger("com.github.badoualy")).setLevel(Level.TRACE);
}
if (true || CommandLineOptions.cmd_console) { if (true || CommandLineOptions.cmd_console) {
// Always use the console for now. // Always use the console for now.
new CommandLineController(); new CommandLineController();

View File

@ -19,6 +19,8 @@ package de.fabianonline.telegram_backup;
import com.github.badoualy.telegram.tl.api.*; import com.github.badoualy.telegram.tl.api.*;
import com.github.badoualy.telegram.tl.core.TLVector; import com.github.badoualy.telegram.tl.core.TLVector;
import com.github.badoualy.telegram.api.TelegramClient; import com.github.badoualy.telegram.api.TelegramClient;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
@ -49,6 +51,7 @@ public class Database {
private Statement stmt; private Statement stmt;
public UserManager user_manager; public UserManager user_manager;
public TelegramClient client; public TelegramClient client;
private final static Logger logger = LoggerFactory.getLogger(Database.class);
public Database(UserManager user_manager, TelegramClient client) { public Database(UserManager user_manager, TelegramClient client) {
this(user_manager, client, true); this(user_manager, client, true);
@ -91,7 +94,7 @@ public class Database {
try { try {
String src = user_manager.getFileBase() + Config.FILE_NAME_DB; String src = user_manager.getFileBase() + Config.FILE_NAME_DB;
String dst = user_manager.getFileBase() + filename; String dst = user_manager.getFileBase() + filename;
Log.debug("Copying %s to %s", src, dst); logger.debug("Copying {} to {}", src, dst);
Files.copy( Files.copy(
new File(src).toPath(), new File(src).toPath(),
new File(dst).toPath(), new File(dst).toPath(),

View File

@ -10,19 +10,21 @@ import java.sql.Types;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import com.github.badoualy.telegram.tl.api.*; import com.github.badoualy.telegram.tl.api.*;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory; import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory;
import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager; import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager;
public class DatabaseUpdates { public class DatabaseUpdates {
protected Connection conn; protected Connection conn;
protected Database db; protected Database db;
private static final Logger logger = LoggerFactory.getLogger(DatabaseUpdates.class);
private static LinkedList<DatabaseUpdate> updates = new LinkedList<DatabaseUpdate>(); private static LinkedList<DatabaseUpdate> updates = new LinkedList<DatabaseUpdate>();
public DatabaseUpdates(Connection conn, Database db) { public DatabaseUpdates(Connection conn, Database db) {
this.conn = conn; this.conn = conn;
this.db = db; this.db = db;
Log.debug("Registering Database Updates..."); logger.debug("Registering Database Updates...");
Log.up();
register(new DB_Update_1(conn, db)); register(new DB_Update_1(conn, db));
register(new DB_Update_2(conn, db)); register(new DB_Update_2(conn, db));
register(new DB_Update_3(conn, db)); register(new DB_Update_3(conn, db));
@ -30,69 +32,58 @@ public class DatabaseUpdates {
register(new DB_Update_5(conn, db)); register(new DB_Update_5(conn, db));
register(new DB_Update_6(conn, db)); register(new DB_Update_6(conn, db));
register(new DB_Update_7(conn, db)); register(new DB_Update_7(conn, db));
Log.down();
} }
public void doUpdates() { public void doUpdates() {
try { try {
Statement stmt = conn.createStatement(); Statement stmt = conn.createStatement();
ResultSet rs; ResultSet rs;
Log.debug("DatabaseUpdate.doUpdates running"); logger.debug("DatabaseUpdate.doUpdates running");
Log.up();
logger.debug("Getting current database version");
Log.debug("Getting current database version");
Log.up();
int version; int version;
Log.debug("Checking if table database_versions exists"); logger.debug("Checking if table database_versions exists");
rs = stmt.executeQuery("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='database_versions'"); rs = stmt.executeQuery("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='database_versions'");
rs.next(); rs.next();
if (rs.getInt(1)==0) { if (rs.getInt(1)==0) {
Log.debug("Table does not exist"); logger.debug("Table does not exist");
version = 0; version = 0;
} else { } else {
Log.debug("Table exists. Checking max version"); logger.debug("Table exists. Checking max version");
rs.close(); rs.close();
rs = stmt.executeQuery("SELECT MAX(version) FROM database_versions"); rs = stmt.executeQuery("SELECT MAX(version) FROM database_versions");
rs.next(); rs.next();
version = rs.getInt(1); version = rs.getInt(1);
rs.close(); rs.close();
} }
Log.debug("version: %d", version); logger.debug("version: {}", version);
Log.down();
System.out.println("Database version: " + version); System.out.println("Database version: " + version);
Log.debug("Max available database version is %d", getMaxPossibleVersion()); logger.debug("Max available database version is {}", getMaxPossibleVersion());
if (version < getMaxPossibleVersion()) { if (version < getMaxPossibleVersion()) {
Log.debug("Update is necessary. %d => %d.", version, getMaxPossibleVersion()); logger.debug("Update is necessary. {} => {}.", version, getMaxPossibleVersion());
boolean backup = false; boolean backup = false;
for (int i=version+1; i<=getMaxPossibleVersion(); i++) { for (int i=version+1; i<=getMaxPossibleVersion(); i++) {
if (getUpdateToVersion(i).needsBackup()) { if (getUpdateToVersion(i).needsBackup()) {
Log.debug("Update to version %d needs a backup", i); logger.debug("Update to version {} needs a backup", i);
backup=true; backup=true;
} }
} }
if (backup) { if (backup) {
Log.debug("Performing backup"); logger.debug("Performing backup");
Log.up();
db.backupDatabase(version); db.backupDatabase(version);
Log.down();
} }
Log.debug("Applying updates"); logger.debug("Applying updates");
Log.up();
try { try {
for (int i=version+1; i<=getMaxPossibleVersion(); i++) { for (int i=version+1; i<=getMaxPossibleVersion(); i++) {
Log.up();
getUpdateToVersion(i).doUpdate(); getUpdateToVersion(i).doUpdate();
Log.down();
} }
} catch (SQLException e) { throw new RuntimeException(e); } } catch (SQLException e) { throw new RuntimeException(e); }
Log.down();
} else { } else {
Log.debug("No update necessary."); logger.debug("No update necessary.");
} }
Log.down();
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -105,7 +96,7 @@ public class DatabaseUpdates {
} }
private void register(DatabaseUpdate d) { private void register(DatabaseUpdate d) {
Log.debug("Registering %s as update to version %d", d.getClass().getName(), d.getVersion()); logger.debug("Registering {} as update to version {}", d.getClass().getName(), d.getVersion());
if (d.getVersion() != updates.size()+1) { if (d.getVersion() != updates.size()+1) {
throw new RuntimeException("Tried to register DB update to version " + d.getVersion() + ", but would need update to version " + (updates.size()+1)); throw new RuntimeException("Tried to register DB update to version " + d.getVersion() + ", but would need update to version " + (updates.size()+1));
} }
@ -117,6 +108,7 @@ abstract class DatabaseUpdate {
protected Connection conn; protected Connection conn;
protected Statement stmt; protected Statement stmt;
protected Database db; protected Database db;
protected static final Logger logger = LoggerFactory.getLogger(DatabaseUpdate.class);
public DatabaseUpdate(Connection conn, Database db) { public DatabaseUpdate(Connection conn, Database db) {
this.conn = conn; this.conn = conn;
try { try {
@ -126,12 +118,10 @@ abstract class DatabaseUpdate {
} }
public void doUpdate() throws SQLException { public void doUpdate() throws SQLException {
Log.debug("Applying update to version %d", getVersion()); logger.debug("Applying update to version {}", getVersion());
System.out.println(" Updating to version " + getVersion() + "..."); System.out.println(" Updating to version " + getVersion() + "...");
Log.up();
_doUpdate(); _doUpdate();
Log.down(); logger.debug("Saving current database version to the db");
Log.debug("Saving current database version to the db");
stmt.executeUpdate("INSERT INTO database_versions (version) VALUES (" + getVersion() + ")"); stmt.executeUpdate("INSERT INTO database_versions (version) VALUES (" + getVersion() + ")");
} }

View File

@ -33,6 +33,8 @@ import com.github.badoualy.telegram.tl.api.*;
import com.github.badoualy.telegram.tl.api.upload.TLFile; import com.github.badoualy.telegram.tl.api.upload.TLFile;
import com.github.badoualy.telegram.tl.exception.RpcErrorException; import com.github.badoualy.telegram.tl.exception.RpcErrorException;
import com.github.badoualy.telegram.tl.api.request.TLRequestUploadGetFile; import com.github.badoualy.telegram.tl.api.request.TLRequestUploadGetFile;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import java.io.IOException; import java.io.IOException;
import java.io.File; import java.io.File;
@ -55,6 +57,7 @@ public class DownloadManager {
DownloadProgressInterface prog = null; DownloadProgressInterface prog = null;
static TelegramClient download_client; static TelegramClient download_client;
static boolean last_download_succeeded = true; static boolean last_download_succeeded = true;
static final Logger logger = LoggerFactory.getLogger(DownloadManager.class);
public DownloadManager(UserManager u, TelegramClient c, DownloadProgressInterface p) { public DownloadManager(UserManager u, TelegramClient c, DownloadProgressInterface p) {
this.user = u; this.user = u;
@ -88,11 +91,9 @@ public class DownloadManager {
} }
public void _downloadMessages(Integer limit) throws RpcErrorException, IOException, TimeoutException { public void _downloadMessages(Integer limit) throws RpcErrorException, IOException, TimeoutException {
Log.debug("This is _downloadMessages with limit %d", limit); logger.info("This is _downloadMessages with limit {}", limit);
Log.up();
int dialog_limit = 100; int dialog_limit = 100;
Log.debug("Downloading the last %d dialogs", dialog_limit); logger.info("Downloading the last {} dialogs", dialog_limit);
Log.up();
System.out.println("Downloading most recent dialogs... "); System.out.println("Downloading most recent dialogs... ");
int max_message_id = 0; int max_message_id = 0;
TLAbsDialogs dialogs = client.messagesGetDialogs( TLAbsDialogs dialogs = client.messagesGetDialogs(
@ -100,15 +101,13 @@ public class DownloadManager {
0, 0,
new TLInputPeerEmpty(), new TLInputPeerEmpty(),
dialog_limit); dialog_limit);
Log.debug("Got %d dialogs", dialogs.getDialogs().size()); logger.debug("Got {} dialogs", dialogs.getDialogs().size());
Log.up();
for (TLDialog d : dialogs.getDialogs()) { for (TLDialog d : dialogs.getDialogs()) {
if (d.getTopMessage() > max_message_id) { if (d.getTopMessage() > max_message_id) {
Log.debug("Updating top message id: %d => %d", max_message_id, d.getTopMessage()); logger.trace("Updating top message id: {} => {}", max_message_id, d.getTopMessage());
max_message_id = d.getTopMessage(); max_message_id = d.getTopMessage();
} }
} }
Log.down();
System.out.println("Top message ID is " + max_message_id); System.out.println("Top message ID is " + max_message_id);
int max_database_id = db.getTopMessageID(); int max_database_id = db.getTopMessageID();
System.out.println("Top message ID in database is " + max_database_id); System.out.println("Top message ID in database is " + max_database_id);
@ -117,8 +116,7 @@ public class DownloadManager {
max_database_id = Math.max(max_database_id, max_message_id-limit); max_database_id = Math.max(max_database_id, max_message_id-limit);
System.out.println("New top message id 'in database' is " + max_database_id); System.out.println("New top message id 'in database' is " + max_database_id);
} }
Log.down();
if (max_database_id == max_message_id) { if (max_database_id == max_message_id) {
System.out.println("No new messages to download."); System.out.println("No new messages to download.");
} else if (max_database_id > max_message_id) { } else if (max_database_id > max_message_id) {
@ -131,14 +129,13 @@ public class DownloadManager {
downloadMessages(ids); downloadMessages(ids);
} }
Log.debug("Searching for missing messages in the db"); logger.info("Searching for missing messages in the db");
Log.up();
int count_missing = 0; int count_missing = 0;
System.out.println("Checking message database for completeness..."); System.out.println("Checking message database for completeness...");
int db_count = db.getMessageCount(); int db_count = db.getMessageCount();
int db_max = db.getTopMessageID(); int db_max = db.getTopMessageID();
Log.debug("db_count: %d", db_count); logger.debug("db_count: {}", db_count);
Log.debug("db_max: %d", db_max); logger.debug("db_max: {}", db_max);
if (db_count != db_max) { if (db_count != db_max) {
if (limit != null) { if (limit != null) {
@ -152,40 +149,35 @@ public class DownloadManager {
} }
} }
Log.debug("Logging this run"); logger.info("Logging this run");
db.logRun(Math.min(max_database_id + 1, max_message_id), max_message_id, count_missing); db.logRun(Math.min(max_database_id + 1, max_message_id), max_message_id, count_missing);
Log.down();
} }
private void downloadMessages(List<Integer> ids) throws RpcErrorException, IOException { private void downloadMessages(List<Integer> ids) throws RpcErrorException, IOException {
prog.onMessageDownloadStart(ids.size()); prog.onMessageDownloadStart(ids.size());
Log.debug("Entering download loop"); logger.debug("Entering download loop");
Log.up();
while (ids.size()>0) { while (ids.size()>0) {
Log.debug("Loop"); logger.trace("Loop");
Log.up();
TLIntVector vector = new TLIntVector(); TLIntVector vector = new TLIntVector();
for (int i=0; i<100; i++) { for (int i=0; i<100; i++) {
if (ids.size()==0) break; if (ids.size()==0) break;
vector.add(ids.remove(0)); vector.add(ids.remove(0));
} }
Log.debug("vector.size(): %d", vector.size()); logger.trace("vector.size(): {}", vector.size());
Log.debug("ids.size(): %d", ids.size()); logger.trace("ids.size(): {}", ids.size());
TLAbsMessages response = client.messagesGetMessages(vector); TLAbsMessages response = client.messagesGetMessages(vector);
prog.onMessageDownloaded(response.getMessages().size()); prog.onMessageDownloaded(response.getMessages().size());
db.saveMessages(response.getMessages(), Kotlogram.API_LAYER); db.saveMessages(response.getMessages(), Kotlogram.API_LAYER);
db.saveChats(response.getChats()); db.saveChats(response.getChats());
db.saveUsers(response.getUsers()); db.saveUsers(response.getUsers());
Log.debug("Sleeping"); logger.trace("Sleeping");
try { try {
Thread.sleep(Config.DELAY_AFTER_GET_MESSAGES); Thread.sleep(Config.DELAY_AFTER_GET_MESSAGES);
} catch (InterruptedException e) {} } catch (InterruptedException e) {}
Log.down();
} }
Log.down(); logger.debug("Finished.");
Log.debug("Finished.");
prog.onMessageDownloadFinished(); prog.onMessageDownloadFinished();
} }
@ -205,7 +197,6 @@ public class DownloadManager {
throw e; throw e;
} }
} catch (TimeoutException e) { } catch (TimeoutException e) {
Log.down();
completed = false; completed = false;
System.out.println(""); System.out.println("");
System.out.println("Telegram took too long to respond to our request."); System.out.println("Telegram took too long to respond to our request.");
@ -217,39 +208,35 @@ public class DownloadManager {
} }
private void _downloadMedia() throws RpcErrorException, IOException, TimeoutException { private void _downloadMedia() throws RpcErrorException, IOException, TimeoutException {
Log.debug("This is _downloadMedia"); logger.info("This is _downloadMedia");
Log.debug("Checking if there are messages in the DB with a too old API layer"); logger.info("Checking if there are messages in the DB with a too old API layer");
LinkedList<Integer> ids = db.getIdsFromQuery("SELECT id FROM messages WHERE has_media=1 AND api_layer<" + Kotlogram.API_LAYER); LinkedList<Integer> ids = db.getIdsFromQuery("SELECT id FROM messages WHERE has_media=1 AND api_layer<" + Kotlogram.API_LAYER);
if (ids.size()>0) { if (ids.size()>0) {
System.out.println("You have " + ids.size() + " messages in your db that need an update. Doing that now."); System.out.println("You have " + ids.size() + " messages in your db that need an update. Doing that now.");
Log.debug("Found %d messages", ids.size()); logger.debug("Found {} messages", ids.size());
downloadMessages(ids); downloadMessages(ids);
} }
LinkedList<TLMessage> messages = this.db.getMessagesWithMedia(); LinkedList<TLMessage> messages = this.db.getMessagesWithMedia();
Log.debug("Database returned %d messages with media", messages.size()); logger.debug("Database returned {} messages with media", messages.size());
prog.onMediaDownloadStart(messages.size()); prog.onMediaDownloadStart(messages.size());
Log.up();
for (TLMessage msg : messages) { for (TLMessage msg : messages) {
AbstractMediaFileManager m = FileManagerFactory.getFileManager(msg, user, client); AbstractMediaFileManager m = FileManagerFactory.getFileManager(msg, user, client);
Log.debug("Message ID: %d Media type: %-10.10s FileManager type: %-10.10s isEmpty: %-5s isDownloaded: %-5s", logger.trace("message {}, {}, {}, {}, {}",
msg.getId(), msg.getId(),
msg.getMedia().getClass().getSimpleName().replace("TLMessageMedia", ""), msg.getMedia().getClass().getSimpleName().replace("TLMessageMedia", ""),
m.getClass().getSimpleName().replace("FileManager", ""), m.getClass().getSimpleName(),
m.isEmpty(), m.isEmpty() ? "empty" : "non-empty",
m.isDownloaded()); m.isDownloaded() ? "downloaded" : "not downloaded");
if (m.isEmpty()) { if (m.isEmpty()) {
prog.onMediaDownloadedEmpty(); prog.onMediaDownloadedEmpty();
} else if (m.isDownloaded()) { } else if (m.isDownloaded()) {
prog.onMediaAlreadyPresent(m); prog.onMediaAlreadyPresent(m);
} else { } else {
Log.up();
m.download(); m.download();
Log.down();
prog.onMediaDownloaded(m); prog.onMediaDownloaded(m);
} }
} }
Log.down();
prog.onMediaDownloadFinished(); prog.onMediaDownloadFinished();
} }
@ -273,24 +260,24 @@ public class DownloadManager {
FileOutputStream fos = null; FileOutputStream fos = null;
try { try {
String temp_filename = target + ".downloading"; String temp_filename = target + ".downloading";
Log.debug("Temporary filename %s", temp_filename); logger.debug("Temporary filename {}", temp_filename);
int offset = 0; int offset = 0;
if (new File(temp_filename).isFile()) { if (new File(temp_filename).isFile()) {
Log.debug("Temporary filename already exists; continuing this file"); logger.info("Temporary filename already exists; continuing this file");
offset = (int)new File(temp_filename).length(); offset = (int)new File(temp_filename).length();
if (offset >= size) { if (offset >= size) {
Log.debug("Temporary file size is >= the target size. Assuming corrupt file & deleting it"); logger.warn("Temporary file size is >= the target size. Assuming corrupt file & deleting it");
new File(temp_filename).delete(); new File(temp_filename).delete();
offset = 0; offset = 0;
} }
} }
Log.debug("offset before the loop is %d", offset); logger.debug("offset before the loop is {}", offset);
fos = new FileOutputStream(temp_filename, true); fos = new FileOutputStream(temp_filename, true);
TLFile response; TLFile response;
do { do {
int block_size = size; int block_size = size;
Log.debug("offset: %8d block_size: %7d size: %8d", offset, block_size, size); logger.trace("offset: {} block_size: {} size: {}", offset, block_size, size);
TLRequestUploadGetFile req = new TLRequestUploadGetFile(loc, offset, block_size); TLRequestUploadGetFile req = new TLRequestUploadGetFile(loc, offset, block_size);
if (dcID==null) { if (dcID==null) {
response = (TLFile) download_client.executeRpcQuery(req); response = (TLFile) download_client.executeRpcQuery(req);
@ -299,7 +286,7 @@ public class DownloadManager {
} }
offset += response.getBytes().getData().length; offset += response.getBytes().getData().length;
Log.debug("response: %8d total size: %8d", response.getBytes().getData().length, offset); logger.trace("response: {} total size: {}", response.getBytes().getData().length, offset);
fos.write(response.getBytes().getData()); fos.write(response.getBytes().getData());
fos.flush(); fos.flush();
@ -311,7 +298,7 @@ public class DownloadManager {
new File(temp_filename).delete(); new File(temp_filename).delete();
System.exit(1); System.exit(1);
} }
Log.debug("Renaming %s to %s", temp_filename, target); logger.debug("Renaming {} to {}", temp_filename, target);
Files.move(new File(temp_filename).toPath(), new File(target).toPath(), StandardCopyOption.REPLACE_EXISTING); Files.move(new File(temp_filename).toPath(), new File(target).toPath(), StandardCopyOption.REPLACE_EXISTING);
last_download_succeeded = true; last_download_succeeded = true;
return true; return true;
@ -328,7 +315,7 @@ public class DownloadManager {
} }
last_download_succeeded = false; last_download_succeeded = false;
System.out.println("Got an Internal Server Error from Telegram. Skipping this file for now. Next run of telegram_backup will continue to download this file."); System.out.println("Got an Internal Server Error from Telegram. Skipping this file for now. Next run of telegram_backup will continue to download this file.");
Log.debug(ex.toString()); logger.warn(ex.toString());
return false; return false;
} }
System.out.println("RpcErrorException happened while downloading " + target); System.out.println("RpcErrorException happened while downloading " + target);

View File

@ -1,34 +0,0 @@
/* Telegram_Backup
* Copyright (C) 2016 Fabian Schlenz
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
package de.fabianonline.telegram_backup;
public class Log {
static int level = 0;
static final int factor = 2;
public static void up() { level++; }
public static void down() { level--; if (level<0) level=0; }
public static void debug(String s, Object... o) {
if (!CommandLineOptions.cmd_debug) return;
Object o2[] = new Object[o.length+1];
System.arraycopy(o, 0, o2, 0, o.length);
o2[o2.length-1]="";
String format = "DEBUG:" + '%' + o2.length + "$" + (level*factor+1) + "s" + s + "\n";
System.out.printf(format, o2);
}
}