From a47d5d5511c8064bc16e059e6cd04b12c1af76d2 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 28 Jan 2018 19:38:59 +0300 Subject: [PATCH] Return whether Log::set_file_path succeded or not. GitOrigin-RevId: a928f8691ebedfd7451bf8bd7957071786b50349 --- CMakeLists.txt | 2 +- README.md | 2 +- example/cpp/CMakeLists.txt | 2 +- example/java/CMakeLists.txt | 8 +-- example/java/org/drinkless/tdlib/Log.java | 3 +- .../org/drinkless/tdlib/example/Example.java | 69 +++++++++++-------- example/java/td_jni.cpp | 6 +- example/python/tdjson_example.py | 2 +- td/telegram/Log.cpp | 9 +-- td/telegram/Log.h | 3 +- td/telegram/Td.h | 2 +- td/telegram/td_log.cpp | 4 +- td/telegram/td_log.h | 3 +- 13 files changed, 64 insertions(+), 51 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e03962b6..b9f541d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR) -project(TDLib VERSION 1.0.5 LANGUAGES CXX C) +project(TDLib VERSION 1.0.6 LANGUAGES CXX C) option(TD_ENABLE_JNI "Use \"ON\" to enable JNI-compatible TDLib API.") diff --git a/README.md b/README.md index 32035799..71bfdc41 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,7 @@ target_link_libraries(YourTarget PRIVATE Td::TdStatic) Or you could install `TDLib` and then reference it in your CMakeLists.txt like this: ``` -find_package(Td 1.0.5 REQUIRED) +find_package(Td 1.0.6 REQUIRED) target_link_libraries(YourTarget PRIVATE Td::TdStatic) ``` See [example/cpp/CMakeLists.txt](https://github.com/tdlib/td/tree/master/example/cpp/CMakeLists.txt). diff --git a/example/cpp/CMakeLists.txt b/example/cpp/CMakeLists.txt index e5bf1d5c..c66d3236 100644 --- a/example/cpp/CMakeLists.txt +++ b/example/cpp/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.1 FATAL_ERROR) project(TdExample VERSION 1.0 LANGUAGES CXX) -find_package(Td 1.0.5 REQUIRED) +find_package(Td 1.0.6 REQUIRED) add_executable(tdjson_example tdjson_example.cpp) target_link_libraries(tdjson_example PRIVATE Td::TdJson) diff --git a/example/java/CMakeLists.txt b/example/java/CMakeLists.txt index f0b0ac6e..e8313a68 100644 --- a/example/java/CMakeLists.txt +++ b/example/java/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.1) +cmake_minimum_required(VERSION 3.1 FATAL_ERROR) project(TdJavaExample VERSION 1.0 LANGUAGES CXX) @@ -29,7 +29,7 @@ endif() add_custom_target(td_generate_java_api COMMAND ${GENERATE_JAVA_API_CMD} - COMMENT "Generate Java TDLib API source files" + COMMENT "Generating Java TDLib API source files" DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/td/bin/td_generate_java_api ${TD_API_TLO_PATH} ${TD_API_TL_PATH} ${JAVADOC_TL_DOCUMENTATION_GENERATOR_PATH} ) @@ -38,7 +38,7 @@ get_filename_component(JAVA_OUTPUT_DIRECTORY ${CMAKE_INSTALL_PREFIX}/bin REALPAT file(MAKE_DIRECTORY ${JAVA_OUTPUT_DIRECTORY}) add_custom_target(build_java COMMAND ${Java_JAVAC_EXECUTABLE} -d ${JAVA_OUTPUT_DIRECTORY} ${JAVA_SOURCE_PATH}/example/Example.java ${JAVA_SOURCE_PATH}/Client.java ${JAVA_SOURCE_PATH}/Log.java ${JAVA_SOURCE_PATH}/TdApi.java - COMMENT "Build Java code" + COMMENT "Building Java code" DEPENDS td_generate_java_api ) @@ -46,7 +46,7 @@ set(JAVA_SOURCE_PATH "${TD_API_JAVA_PATH}/${TD_API_JAVA_PACKAGE}") add_custom_target(generate_javadoc COMMAND ${Java_JAVADOC_EXECUTABLE} -d ${JAVA_OUTPUT_DIRECTORY}/../docs org.drinkless.tdlib WORKING_DIRECTORY ${TD_API_JAVA_PATH} - COMMENT "Generate Javadoc documentation" + COMMENT "Generating Javadoc documentation" DEPENDS td_generate_java_api ) diff --git a/example/java/org/drinkless/tdlib/Log.java b/example/java/org/drinkless/tdlib/Log.java index 1b3b6713..06a0290e 100644 --- a/example/java/org/drinkless/tdlib/Log.java +++ b/example/java/org/drinkless/tdlib/Log.java @@ -31,8 +31,9 @@ public final class Log { * * @param filePath Path to a file for writing TDLib internal log. Use an empty path to * switch back to logging to the System.err. + * @return whether opening the log file succeeded */ - public static native void setFilePath(String filePath); + public static native boolean setFilePath(String filePath); /** * Changes maximum size of TDLib log file. diff --git a/example/java/org/drinkless/tdlib/example/Example.java b/example/java/org/drinkless/tdlib/example/Example.java index d03f6ea8..af689b7c 100644 --- a/example/java/org/drinkless/tdlib/example/Example.java +++ b/example/java/org/drinkless/tdlib/example/Example.java @@ -11,6 +11,8 @@ import org.drinkless.tdlib.Log; import org.drinkless.tdlib.TdApi; import java.io.Console; +import java.io.IOError; +import java.io.IOException; import java.util.NavigableSet; import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; @@ -161,39 +163,43 @@ public final class Example { } private static void getCommand() { - String command = System.console().readLine("Enter command (gcs - GetChats, gc - GetChat, me - GetMe, sm - SendMessage, lo - LogOut, q - Quit): "); + String command = System.console().readLine("Enter command (gcs - GetChats, gc - GetChat, me - GetMe, sm - SendMessage, lo - LogOut, q - Quit): "); String[] commands = command.split(" ", 2); - switch (commands[0]) { - case "gcs": { - int limit = 20; - if (commands.length > 1) { - limit = toInt(commands[1]); + try { + switch (commands[0]) { + case "gcs": { + int limit = 20; + if (commands.length > 1) { + limit = toInt(commands[1]); + } + getChatList(limit); + break; } - getChatList(limit); - break; + case "gc": + client.send(new TdApi.GetChat(getChatId(commands[1])), defaultHandler); + break; + case "me": + client.send(new TdApi.GetMe(), defaultHandler); + break; + case "sm": { + String[] args = commands[1].split(" ", 2); + sendMessage(getChatId(args[0]), args[1]); + break; + } + case "lo": + haveAuthorization = false; + client.send(new TdApi.LogOut(), defaultHandler); + break; + case "q": + quiting = true; + haveAuthorization = false; + client.send(new TdApi.Close(), defaultHandler); + break; + default: + System.err.println("Unsupported command: " + command); } - case "gc": - client.send(new TdApi.GetChat(getChatId(commands[1])), defaultHandler); - break; - case "me": - client.send(new TdApi.GetMe(), defaultHandler); - break; - case "sm": { - String[] args = commands[1].split(" ", 2); - sendMessage(getChatId(args[0]), args[1]); - break; - } - case "lo": - haveAuthorization = false; - client.send(new TdApi.LogOut(), defaultHandler); - break; - case "q": - quiting = true; - haveAuthorization = false; - client.send(new TdApi.Close(), defaultHandler); - break; - default: - System.err.println("Unsupported command: " + command); + } catch (ArrayIndexOutOfBoundsException e) { + print("Not enough arguments"); } } @@ -259,6 +265,9 @@ public final class Example { public static void main(String[] args) throws InterruptedException { // disable TDLib log Log.setVerbosityLevel(0); + if (!Log.setFilePath("log")) { + throw new IOError(new IOException("Write access to the current directory is required")); + } // create client client = Client.create(new UpdatesHandler(), null, null); diff --git a/example/java/td_jni.cpp b/example/java/td_jni.cpp index ca02d843..18c9b424 100644 --- a/example/java/td_jni.cpp +++ b/example/java/td_jni.cpp @@ -73,8 +73,8 @@ static void Log_setVerbosityLevel(JNIEnv *env, jclass clazz, jint new_log_verbos td::Log::set_verbosity_level(static_cast(new_log_verbosity_level)); } -static void Log_setFilePath(JNIEnv *env, jclass clazz, jstring file_path) { - td::Log::set_file_path(td::jni::from_jstring(env, file_path)); +static jboolean Log_setFilePath(JNIEnv *env, jclass clazz, jstring file_path) { + return td::Log::set_file_path(td::jni::from_jstring(env, file_path)) ? JNI_TRUE : JNI_FALSE; } static void Log_setMaxFileSize(JNIEnv *env, jclass clazz, jlong max_file_size) { @@ -132,7 +132,7 @@ static jint register_native(JavaVM *vm) { register_method(client_class, "destroyNativeClient", "(J)V", Client_destroyNativeClient); register_method(log_class, "setVerbosityLevel", "(I)V", Log_setVerbosityLevel); - register_method(log_class, "setFilePath", "(Ljava/lang/String;)V", Log_setFilePath); + register_method(log_class, "setFilePath", "(Ljava/lang/String;)Z", Log_setFilePath); register_method(log_class, "setMaxFileSize", "(J)V", Log_setMaxFileSize); register_method(object_class, "toString", "()Ljava/lang/String;", Object_toString); diff --git a/example/python/tdjson_example.py b/example/python/tdjson_example.py index 2dfe5dd5..9df28746 100644 --- a/example/python/tdjson_example.py +++ b/example/python/tdjson_example.py @@ -30,7 +30,7 @@ td_json_client_destroy.restype = None td_json_client_destroy.argtypes = [c_void_p] td_set_log_file_path = tdjson.td_set_log_file_path -td_set_log_file_path.restype = None +td_set_log_file_path.restype = c_int td_set_log_file_path.argtypes = [c_char_p] td_set_log_max_file_size = tdjson.td_set_log_max_file_size diff --git a/td/telegram/Log.cpp b/td/telegram/Log.cpp index 57e5b7bb..143385fc 100644 --- a/td/telegram/Log.cpp +++ b/td/telegram/Log.cpp @@ -24,17 +24,18 @@ static void fatal_error_callback_wrapper(CSlice message) { fatal_error_callback(message.c_str()); } -void Log::set_file_path(string file_path) { +bool Log::set_file_path(string file_path) { if (file_path.empty()) { log_interface = default_log_interface; - return; + return true; } if (file_log.init(file_path, max_log_file_size)) { log_interface = &ts_log; - } else { - LOG(FATAL) << "Can't init file log"; + return true; } + + return false; } void Log::set_max_file_size(int64 max_file_size) { diff --git a/td/telegram/Log.h b/td/telegram/Log.h index f7219158..7d5e0f43 100644 --- a/td/telegram/Log.h +++ b/td/telegram/Log.h @@ -30,8 +30,9 @@ class Log { * * \param[in] file_path Path to a file where the internal TDLib log will be written. Use an empty path to * switch back to the default logging behaviour. + * \return True on success, or false otherwise, i.e. if the file can't be opened for writing. */ - static void set_file_path(std::string file_path); + static bool set_file_path(std::string file_path); /** * Sets maximum size of the file to where the internal TDLib log is written before the file will be auto-rotated. diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 66d94a50..e0445a73 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -187,7 +187,7 @@ class Td final : public NetQueryCallback { static td_api::object_ptr static_request(td_api::object_ptr function); private: - static constexpr const char *tdlib_version = "1.0.5"; + static constexpr const char *tdlib_version = "1.0.6"; static constexpr int32 ONLINE_TIMEOUT = 240; void send_result(uint64 id, tl_object_ptr object); diff --git a/td/telegram/td_log.cpp b/td/telegram/td_log.cpp index 0bffe0c7..2eb36057 100644 --- a/td/telegram/td_log.cpp +++ b/td/telegram/td_log.cpp @@ -10,8 +10,8 @@ #include -void td_set_log_file_path(const char *file_path) { - td::Log::set_file_path(file_path == nullptr ? "" : file_path); +int td_set_log_file_path(const char *file_path) { + return static_cast(td::Log::set_file_path(file_path == nullptr ? "" : file_path)); } void td_set_log_max_file_size(long long max_file_size) { diff --git a/td/telegram/td_log.h b/td/telegram/td_log.h index 7aa8549c..87ec2606 100644 --- a/td/telegram/td_log.h +++ b/td/telegram/td_log.h @@ -25,8 +25,9 @@ extern "C" { * * \param[in] file_path Null-terminated path to a file where the internal TDLib log will be written. * Use an empty path to switch back to the default logging behaviour. + * \return True 1 on success, or 0 otherwise, i.e. if the file can't be opened for writing. */ -TDJSON_EXPORT void td_set_log_file_path(const char *file_path); +TDJSON_EXPORT int td_set_log_file_path(const char *file_path); /** * Sets maximum size of the file to where the internal TDLib log is written before the file will be auto-rotated.