Skip to content
This repository was archived by the owner on Jan 27, 2025. It is now read-only.

Commit d71c6ec

Browse files
committed
fix(common): seperate log into multiple files
1 parent be19435 commit d71c6ec

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

src/common/logging.cc

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
#include "logging.h"
22

33
#include <chrono>
4-
#include <cstdlib>
54
#include <ctime>
65
#include <iomanip>
76
#include <sstream>
87
#include <string>
98
#include <system_error>
109

10+
#include "env.h"
11+
#include "llvm/ADT/StringRef.h"
1112
#include "llvm/Support/FileSystem.h"
1213
#include "llvm/Support/raw_ostream.h"
14+
#include "path.h"
1315

1416
using namespace llvm;
1517

1618
static std::unique_ptr<Logger> _logger = nullptr;
1719

20+
Logger::Logger(StringRef filename, std::error_code& ec,
21+
sys::fs::OpenFlags flags)
22+
: raw_fd_ostream(filename, ec, flags), _filename(filename.str()) {}
23+
24+
Logger::~Logger() {
25+
this->close();
26+
if (fs::exists(this->filename()) && (fs::file_size(this->filename()) < 64)) {
27+
fs::remove(this->filename());
28+
}
29+
}
30+
31+
fs::path Logger::filename() const { return this->_filename; }
32+
1833
int Logger::log_level() const { return this->_log_level; }
1934

2035
void Logger::set_log_level(const int new_log_level) {
@@ -30,20 +45,31 @@ void Logger::set_log_level(const int new_log_level) {
3045
}
3146
}
3247

33-
const char* Logger::get_log_path() {
34-
const char* log_path = std::getenv("LOG_PATH");
35-
if (log_path) {
36-
return log_path;
37-
} else {
38-
return LOG_PATH;
48+
fs::path Logger::get_log_path() {
49+
std::string log_path = get_env("LOG_PATH", LOG_PATH);
50+
auto pos = log_path.find('*');
51+
if (pos != std::string::npos) {
52+
log_path.replace(pos, 1, Logger::time());
3953
}
54+
return log_path;
4055
}
4156

4257
Logger& Logger::log(const char* fmt) {
4358
(*this) << fmt;
4459
return *this;
4560
}
4661

62+
std::string Logger::time() {
63+
std::ostringstream oss;
64+
auto now = std::chrono::high_resolution_clock::now();
65+
auto t = std::chrono::high_resolution_clock::to_time_t(now);
66+
auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(now);
67+
std::chrono::nanoseconds nanoseconds = now - seconds;
68+
oss << std::put_time(std::localtime(&t), "%F %T") << "." << std::setw(9)
69+
<< std::setfill('0') << nanoseconds.count();
70+
return oss.str();
71+
}
72+
4773
Logger& Logger::change_color(raw_ostream::Colors color, bool bold,
4874
bool background) {
4975
if (this->has_colors()) {
@@ -65,9 +91,8 @@ std::string Logger::get_prompt(const std::string& type) {
6591
auto t = std::chrono::high_resolution_clock::to_time_t(now);
6692
auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(now);
6793
std::chrono::nanoseconds nanoseconds = now - seconds;
68-
oss << '[' << std::put_time(std::localtime(&t), "%F %T") << "."
69-
<< std::setw(9) << std::setfill('0') << nanoseconds.count() << "] "
70-
<< std::setw(10) << std::setfill(' ') << ('[' + type + ']') << ' ';
94+
oss << '[' << Logger::time() << "] " << std::setw(10) << std::setfill(' ')
95+
<< ('[' + type + ']') << ' ';
7196
return oss.str();
7297
}
7398

@@ -76,8 +101,8 @@ Logger& logger() { return *_logger; }
76101
static void __attribute__((constructor)) _logger_init() {
77102
if (_logger) return;
78103
std::error_code ec;
79-
_logger = std::make_unique<Logger>(Logger::get_log_path(), ec,
80-
sys::fs::OpenFlags::OF_Append);
104+
_logger = std::make_unique<Logger>(Logger::get_log_path().string(), ec,
105+
sys::fs::OpenFlags::OF_None);
81106
logger().set_log_level();
82107
}
83108

0 commit comments

Comments
 (0)