1 // Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
5 #include "common/utils/file_logbackend.h"
7 #include <manifest_parser/utils/logging.h>
20 constexpr mode_t kDefaultMode640 = S_IRUSR | S_IWUSR | S_IRGRP;
26 FileLogBackend::FileLogBackend(std::string file_name, int rotation_size,
28 : file_name_(std::move(file_name)), rotation_size_(rotation_size),
29 max_rotation_(max_rotation), log_stream_(
30 std::unique_ptr<std::ostringstream>(new std::ostringstream())) {
34 void FileLogBackend::WriteLog(LogLevel level, const std::string& /* tag */,
35 const std::string& logstr) {
36 if (level != LogLevel::LOG_DEBUG)
37 *log_stream_ << GetTimeStamp() << GetPid() << logstr << std::endl;
40 void FileLogBackend::WriteLogToFile() {
41 if (file_name_.empty())
44 int size = GetFileSize(file_name_);
45 if (size > rotation_size_)
49 std::ofstream ofs(file_name_.c_str(), std::ios::app);
50 ofs << log_stream_->str();
53 // clean the log stream
57 if (chmod(file_name_.c_str(), kDefaultMode640) != 0)
58 LOG(ERROR) << "Failed to set permission on log, errno : " << errno;
61 bool FileLogBackend::Rotate() {
62 for (int i = max_rotation_; i > 0; i--) {
63 std::string old_log = file_name_ + "." + std::to_string(i);
65 struct stat tmp_buffer;
66 if (stat(old_log.c_str(), &tmp_buffer) != 0)
69 // the oldest log will be removed
70 if (i == max_rotation_) {
71 if (std::remove(old_log.c_str()) != 0)
74 std::string new_log = file_name_ + "." + std::to_string(i + 1);
75 if (std::rename(old_log.c_str(), new_log.c_str()) != 0)
79 std::string new_log = file_name_ + ".1";
80 if (std::rename(file_name_.c_str(), new_log.c_str()) != 0)
86 int FileLogBackend::GetFileSize(const std::string& file_name) {
88 int ret = stat(file_name.c_str(), &sb);
89 return ret == 0 ? sb.st_size : -1;
92 std::string FileLogBackend::GetTimeStamp() {
94 clock_gettime(CLOCK_REALTIME, &ts);
96 time_t seconds = ts.tv_sec;
98 if (!gmtime_r(&seconds, &gmt))
100 int32_t miliseconds = ts.tv_nsec / 1000000;
103 strftime(buf, sizeof(buf), "%Y%m%d.%H%M%S", &gmt);
105 snprintf(timestamp, sizeof(timestamp), "%s.%03dUTC|", buf, miliseconds);
110 std::string FileLogBackend::GetPid() {
111 return std::to_string(getpid()) + "|";