From c5c692d61dde158fa302f21875fc660765afe998 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 13 Jul 2016 06:20:51 +0200 Subject: [PATCH] Useful logging when called with --debug. The old --debug is now --debug-telegram. --- .../CommandLineController.java | 44 +++++++++++++++-- .../telegram_backup/CommandLineOptions.java | 4 ++ .../telegram_backup/DownloadManager.java | 48 +++++++++++++++++-- .../de/fabianonline/telegram_backup/Log.java | 34 +++++++++++++ 4 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 src/main/java/de/fabianonline/telegram_backup/Log.java diff --git a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java index 4f77cbb..7108507 100644 --- a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java +++ b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java @@ -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 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 ' to use account .\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 Use account ."); 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 Downloads at most the most recent messages."); System.out.println(" -t, --target Target directory for the files."); diff --git a/src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java b/src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java index 699a1b2..451a605 100644 --- a/src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java +++ b/src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java @@ -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; diff --git a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java index d4c498f..fa1ec29 100644 --- a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java +++ b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java @@ -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 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 { diff --git a/src/main/java/de/fabianonline/telegram_backup/Log.java b/src/main/java/de/fabianonline/telegram_backup/Log.java new file mode 100644 index 0000000..70f1485 --- /dev/null +++ b/src/main/java/de/fabianonline/telegram_backup/Log.java @@ -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 . */ + +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); + } +}