From 0f07341e91abf8f4ae7e14e7ecff03e59bcf7ed7 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 9 Jan 2018 12:40:56 +0300 Subject: [PATCH] Add interface for changing maximum log file size. GitOrigin-RevId: dccd781c0467d75a5a396b71e6401e68534919db --- td/telegram/Log.cpp | 22 +++++++++++++++++----- td/telegram/Log.h | 16 +++++++++++++--- td/telegram/td_log.cpp | 8 ++++++-- td/telegram/td_log.h | 15 ++++++++++++--- tdutils/td/utils/FileLog.h | 4 ++++ 5 files changed, 52 insertions(+), 13 deletions(-) diff --git a/td/telegram/Log.cpp b/td/telegram/Log.cpp index 4b7f8d1c3..a6b6e6722 100644 --- a/td/telegram/Log.cpp +++ b/td/telegram/Log.cpp @@ -6,23 +6,35 @@ // #include "td/telegram/Log.h" +#include "td/utils/common.h" #include "td/utils/FileLog.h" #include "td/utils/logging.h" +#include + namespace td { -void Log::set_file_path(string path) { - if (path.empty()) { + +static FileLog file_log; +static TsLog ts_log(&file_log); +static int64 max_log_file_size = 10 << 20; + +void Log::set_file_path(string file_path) { + if (file_path.empty()) { log_interface = default_log_interface; return; } - static FileLog file_log; - static TsLog ts_log(&file_log); - file_log.init(path); + file_log.init(file_path, max_log_file_size); log_interface = &ts_log; } +void Log::set_max_file_size(int64 max_file_size) { + max_log_file_size = std::max(max_file_size, static_cast(0)); + file_log.set_rotate_threshold(max_log_file_size); +} + void Log::set_verbosity_level(int new_verbosity_level) { SET_VERBOSITY_LEVEL(VERBOSITY_NAME(FATAL) + new_verbosity_level); } + } // namespace td diff --git a/td/telegram/Log.h b/td/telegram/Log.h index cf40c2e28..fcd43aee9 100644 --- a/td/telegram/Log.h +++ b/td/telegram/Log.h @@ -12,6 +12,7 @@ * By default TDLib writes logs to stderr or an OS specific log and uses a verbosity level of 5. */ +#include #include namespace td { @@ -27,10 +28,19 @@ class Log { * By default TDLib writes logs to stderr or an OS specific log. * Use this method to write the log to a file instead. * - * \param[in] 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. + * \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. */ - static void set_file_path(std::string path); + static void 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. + * Unused if log is not written to a file. Defaults to 10 MB. + * + * \param[in] max_file_size Maximum size of the file to where the internal TDLib log is written before the file + * will be auto-rotated. Should be positive. + */ + static void set_max_file_size(std::int64_t max_file_size); /** * Sets the verbosity level of the internal logging of TDLib. diff --git a/td/telegram/td_log.cpp b/td/telegram/td_log.cpp index 37568db11..f82cd1664 100644 --- a/td/telegram/td_log.cpp +++ b/td/telegram/td_log.cpp @@ -8,8 +8,12 @@ #include "td/telegram/Log.h" -void td_set_log_file_path(const char *path) { - td::Log::set_file_path(path); +void td_set_log_file_path(const char *file_path) { + td::Log::set_file_path(file_path == nullptr ? "" : file_path); +} + +void td_set_log_max_file_size(long long max_file_size) { + td::Log::set_max_file_size(static_cast(max_file_size)); } void td_set_log_verbosity_level(int new_verbosity_level) { diff --git a/td/telegram/td_log.h b/td/telegram/td_log.h index 3acd56d6c..d3794edfa 100644 --- a/td/telegram/td_log.h +++ b/td/telegram/td_log.h @@ -23,10 +23,19 @@ extern "C" { * By default TDLib writes logs to stderr or an OS specific log. * Use this method to write the log to a file instead. * - * \param[in] 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. + * \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. */ -TDJSON_EXPORT void td_set_log_file_path(const char *path); +TDJSON_EXPORT void 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. + * Unused if log is not written to a file. Defaults to 10 MB. + * + * \param[in] max_file_size Maximum size of the file to where the internal TDLib log is written before the file + * will be auto-rotated. Should be positive. + */ +TDJSON_EXPORT void td_set_log_max_file_size(long long max_file_size); /** * Sets the verbosity level of the internal logging of TDLib. diff --git a/tdutils/td/utils/FileLog.h b/tdutils/td/utils/FileLog.h index 93e960241..0520d0250 100644 --- a/tdutils/td/utils/FileLog.h +++ b/tdutils/td/utils/FileLog.h @@ -66,6 +66,10 @@ class FileLog : public LogInterface { rotate_threshold_ = rotate_threshold; } + void set_rotate_threshold(int64 rotate_threshold) { + rotate_threshold_ = rotate_threshold; + } + private: FileFd fd_; string path_;