Add interface for changing maximum log file size.

GitOrigin-RevId: dccd781c0467d75a5a396b71e6401e68534919db
This commit is contained in:
levlam 2018-01-09 12:40:56 +03:00
parent bd6592e78b
commit 0f07341e91
5 changed files with 52 additions and 13 deletions

View File

@ -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 <algorithm>
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<int64>(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

View File

@ -12,6 +12,7 @@
* By default TDLib writes logs to stderr or an OS specific log and uses a verbosity level of 5.
*/
#include <cstdint>
#include <string>
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.

View File

@ -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<std::int64_t>(max_file_size));
}
void td_set_log_verbosity_level(int new_verbosity_level) {

View File

@ -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.

View File

@ -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_;