Do not fail on unsuccessful FileLog.init.

GitOrigin-RevId: c31bcf155821a973b431234d76aa83e3b0e281a0
This commit is contained in:
levlam 2018-01-28 17:48:11 +03:00
parent 6d21c7c912
commit 662471ea48
5 changed files with 27 additions and 14 deletions

View File

@ -30,8 +30,11 @@ void Log::set_file_path(string file_path) {
return;
}
file_log.init(file_path, max_log_file_size);
log_interface = &ts_log;
if (file_log.init(file_path, max_log_file_size)) {
log_interface = &ts_log;
} else {
LOG(FATAL) << "Can't init file log";
}
}
void Log::set_max_file_size(int64 max_file_size) {

View File

@ -2948,10 +2948,9 @@ void main(int argc, char **argv) {
if (*arg == '\0' && i + 1 < argc) {
arg = argv[++i];
}
file_log.init(arg);
file_log.init(arg);
file_log.init(arg);
log_interface = &ts_log;
if (file_log.init(arg) && file_log.init(arg) && file_log.init(arg)) {
log_interface = &ts_log;
}
} else if (!std::strcmp(argv[i], "-W")) {
get_chat_list = true;
} else if (!std::strcmp(argv[i], "--disable-network") || !std::strcmp(argv[i], "-n")) {

View File

@ -17,16 +17,26 @@
namespace td {
void FileLog::init(string path, int64 rotate_threshold) {
fd_.close();
path_ = std::move(path);
bool FileLog::init(string path, int64 rotate_threshold) {
if (path == path_) {
set_rotate_threshold(rotate_threshold);
return true;
}
auto r_fd = FileFd::open(path_, FileFd::Create | FileFd::Write | FileFd::Append);
LOG_IF(FATAL, r_fd.is_error()) << "Can't open log: " << r_fd.error();
auto r_fd = FileFd::open(path, FileFd::Create | FileFd::Write | FileFd::Append);
if (r_fd.is_error()) {
LOG(ERROR) << "Can't open log: " << r_fd.error();
return false;
}
fd_.close();
fd_ = r_fd.move_as_ok();
Fd::duplicate(fd_.get_fd(), Fd::Stderr()).ignore();
path_ = std::move(path);
size_ = fd_.get_size();
rotate_threshold_ = rotate_threshold;
return true;
}
void FileLog::set_rotate_threshold(int64 rotate_threshold) {

View File

@ -17,7 +17,7 @@ class FileLog : public LogInterface {
static constexpr int64 DEFAULT_ROTATE_THRESHOLD = 10 * (1 << 20);
public:
void init(string path, int64 rotate_threshold = DEFAULT_ROTATE_THRESHOLD);
bool init(string path, int64 rotate_threshold = DEFAULT_ROTATE_THRESHOLD);
void set_rotate_threshold(int64 rotate_threshold);

View File

@ -55,8 +55,9 @@ void TestsRunner::init(string dir) {
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(WARNING));
chdir(dir).ensure();
LOG(WARNING) << "Redirect log into " << tag("file", dir + TD_DIR_SLASH + "log.txt");
file_log.init("log.txt", std::numeric_limits<int64>::max());
log_interface = &ts_log;
if (file_log.init("log.txt", std::numeric_limits<int64>::max())) {
log_interface = &ts_log;
}
}
} // namespace td