1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-12-24 22:15:33 +00:00

Useful logging when called with --debug. The old --debug is now --debug-telegram.

This commit is contained in:
Fabian Schlenz 2016-07-13 06:20:51 +02:00
parent 95a02e89c3
commit c5c692d61d
4 changed files with 122 additions and 8 deletions

View File

@ -54,24 +54,42 @@ public class CommandLineController {
System.exit(0);
}
Log.debug("Target dir at startup: %s", Config.FILE_BASE);
if (CommandLineOptions.val_target != null) {
Config.FILE_BASE = CommandLineOptions.val_target;
}
Log.debug("Target dir after options: %s", Config.FILE_BASE);
System.out.println("Base directory for files: " + Config.FILE_BASE);
if (CommandLineOptions.cmd_list_accounts) this.list_accounts();
Log.debug("Initializing TelegramApp");
app = new TelegramApp(Config.APP_ID, Config.APP_HASH, Config.APP_MODEL, Config.APP_SYSVER, Config.APP_APPVER, Config.APP_LANG);
if (CommandLineOptions.cmd_debug) Kotlogram.setDebugLogEnabled(true);
if (CommandLineOptions.cmd_debug_telegram) Kotlogram.setDebugLogEnabled(true);
Log.debug("Checking accounts");
Log.up();
String account = null;
Vector<String> accounts = Utils.getAccounts();
if (CommandLineOptions.cmd_login) {
Log.debug("Login requested, doing nothing.");
// do nothing
} else if (CommandLineOptions.val_account!=null) {
Log.debug("Account requested: %s", CommandLineOptions.val_account);
Log.debug("Checking accounts for match.");
Log.up();
boolean found = false;
for (String acc : accounts) if (acc.equals(CommandLineOptions.val_account)) found=true;
for (String acc : accounts) {
Log.debug("Checking %s", acc);
Log.up();
if (acc.equals(CommandLineOptions.val_account)) {
found=true;
Log.debug("Matches.");
}
Log.down();
}
Log.down();
if (!found) {
show_error("Couldn't find account '" + CommandLineOptions.val_account + "'. Maybe you want to use '--login' first?");
}
@ -87,16 +105,25 @@ public class CommandLineController {
"Use '--account <x>' to use account <x>.\n" +
"Use '--list-accounts' to see all available accounts.");
}
Log.debug("accounts.size(): %d", accounts.size());
Log.debug("account: %s", account);
Log.debug("CommandLineOptions.cmd_login: %s", CommandLineOptions.cmd_login);
Log.down();
Log.debug("Initializing ApiStorage");
storage = new ApiStorage(account);
Log.debug("Initializing TelegramUpdateHandler");
TelegramUpdateHandler handler = new TelegramUpdateHandler();
Log.debug("Creating Client");
TelegramClient client = Kotlogram.getDefaultClient(app, storage, Kotlogram.PROD_DC4, handler);
try {
Log.debug("Creating UserManager");
user = new UserManager(client);
Log.debug("CommandLineOptions.val_export: %s", CommandLineOptions.val_export);
if (CommandLineOptions.val_export != null) {
Log.up();
if (CommandLineOptions.val_export.toLowerCase().equals("html")) {
(new HTMLExporter()).export(user);
System.exit(0);
@ -108,17 +135,23 @@ public class CommandLineController {
}
}
Log.debug("CommandLineOptions.cmd_login: %s", CommandLineOptions.cmd_login);
if (CommandLineOptions.cmd_login) {
Log.up();
cmd_login();
System.exit(0);
}
System.out.println("You are logged in as " + user.getUserString());
Log.debug("Initializing Download Manager");
Log.up();
DownloadManager d = new DownloadManager(user, client, new CommandLineDownloadProgress());
Log.debug("Calling DownloadManager.downloadMessages with limit %d", CommandLineOptions.val_limit_messages);
d.downloadMessages(CommandLineOptions.val_limit_messages);
Log.debug("Calling DownloadManager.downloadMedia");
d.downloadMedia();
Log.down();
} catch (RpcErrorException e) {
e.printStackTrace();
} catch (IOException e) {
@ -177,7 +210,8 @@ public class CommandLineController {
System.out.println(" -h, --help Shows this help.");
System.out.println(" -a, --account <x> Use account <x>.");
System.out.println(" -l, --login Login to an existing telegram account.");
System.out.println(" --debug Show (lots of) debug information.");
System.out.println(" --debug Shows some debug information.");
System.out.println(" --debug-telegram Shows lots of debug messages from the library used to access Telegram.");
System.out.println(" -A, --list-accounts List all existing accounts ");
System.out.println(" --limit-messages <x> Downloads at most the most recent <x> messages.");
System.out.println(" -t, --target <x> Target directory for the files.");

View File

@ -21,6 +21,7 @@ class CommandLineOptions {
public static boolean cmd_help = false;
public static boolean cmd_login = false;
public static boolean cmd_debug = false;
public static boolean cmd_debug_telegram = false;
public static boolean cmd_list_accounts = false;
public static boolean cmd_version = false;
public static boolean cmd_license = false;
@ -65,6 +66,9 @@ class CommandLineOptions {
case "--debug":
cmd_debug = true; break;
case "--debug-telegram":
cmd_debug_telegram = true; break;
case "-A": case "--list-accounts":
cmd_list_accounts = true; break;

View File

@ -82,18 +82,27 @@ public class DownloadManager {
}
public void _downloadMessages(Integer limit) throws RpcErrorException, IOException, TimeoutException {
System.out.println("Downloading most recent dialog... ");
Log.debug("This is _downloadMessages with limit %d", limit);
Log.up();
int dialog_limit = 100;
Log.debug("Downloading the last %d dialogs", dialog_limit);
Log.up();
System.out.println("Downloading most recent dialogs... ");
int max_message_id = 0;
TLAbsDialogs dialogs = client.messagesGetDialogs(
0,
0,
new TLInputPeerEmpty(),
100);
dialog_limit);
Log.debug("Got %d dialogs", dialogs.getDialogs().size());
Log.up();
for (TLAbsDialog d : dialogs.getDialogs()) {
if (d.getTopMessage() > max_message_id) {
Log.debug("Updating top message id: %d => %d", max_message_id, d.getTopMessage());
max_message_id = d.getTopMessage();
}
}
Log.down();
System.out.println("Top message ID is " + max_message_id);
int max_database_id = db.getTopMessageID();
System.out.println("Top message ID in database is " + max_database_id);
@ -102,6 +111,7 @@ public class DownloadManager {
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);
}
Log.down();
if (max_database_id == max_message_id) {
System.out.println("No new messages to download.");
@ -114,29 +124,49 @@ public class DownloadManager {
prog.onMessageDownloadStart(end_id - current_start_id + 1);
Log.debug("Entering download loop");
Log.up();
while (current_start_id <= end_id) {
Log.debug("Loop");
Log.up();
Log.debug("current_start_id: %d", current_start_id);
Log.debug("end_id: %d", end_id);
int my_end_id = Math.min(current_start_id+99, end_id);
Log.debug("my_end_id: %d", my_end_id);
ArrayList<Integer> a = makeIdList(current_start_id, my_end_id);
TLIntVector ids = new TLIntVector();
ids.addAll(a);
my_end_id = ids.get(ids.size()-1);
Log.debug("my_end_id: %d", my_end_id);
current_start_id = my_end_id + 1;
Log.debug("current_start_id: %d", current_start_id);
TLAbsMessages response = client.messagesGetMessages(ids);
prog.onMessageDownloaded(response.getMessages().size());
db.saveMessages(response.getMessages());
db.saveChats(response.getChats());
db.saveUsers(response.getUsers());
Log.debug("Sleeping");
try {
Thread.sleep(Config.DELAY_AFTER_GET_MESSAGES);
} catch (InterruptedException e) {}
Log.down();
}
Log.down();
Log.debug("Finished.");
prog.onMessageDownloadFinished();
}
Log.debug("Searching for missing messages in the db");
Log.up();
int count_missing = 0;
System.out.println("Checking message database for completeness...");
if (db.getMessageCount() != db.getTopMessageID()) {
int db_count = db.getMessageCount();
int db_max = db.getTopMessageID();
Log.debug("db_count: %d", db_count);
Log.debug("db_max: %d", db_max);
if (db_count != db_max) {
if (limit != null) {
System.out.println("You are missing messages in your database. But since you're using '--limit-messages', I won't download these now.");
} else {
@ -144,23 +174,35 @@ public class DownloadManager {
count_missing = ids.size();
System.out.println("Downloading " + ids.size() + " messages that are missing in your database.");
prog.onMessageDownloadStart(ids.size());
Log.debug("Entering download loop");
Log.up();
while (ids.size()>0) {
Log.debug("Loop");
Log.up();
TLIntVector vector = new TLIntVector();
for (int i=0; i<100; i++) {
if (ids.size()==0) break;
vector.add(ids.remove());
}
Log.debug("vector.size(): %d", vector.size());
Log.debug("ids.size(): %d", ids.size());
TLAbsMessages response = client.messagesGetMessages(vector);
prog.onMessageDownloaded(response.getMessages().size());
db.saveMessages(response.getMessages());
db.saveChats(response.getChats());
db.saveUsers(response.getUsers());
Log.debug("sleep");
try { Thread.sleep(Config.DELAY_AFTER_GET_MESSAGES); } catch (InterruptedException e) {}
Log.down();
}
Log.down();
prog.onMessageDownloadFinished();
}
}
Log.down();
Log.debug("Logging this run");
db.logRun(Math.min(max_database_id + 1, max_message_id), max_message_id, count_missing);
Log.down();
}
public void downloadMedia() throws RpcErrorException, IOException {

View File

@ -0,0 +1,34 @@
/* 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);
}
}