Initialize ErrorHandler early and ensure immediate log file creation
- Added early initialization of `ErrorHandler` in `main.cc` for robust error handling. - Modified `ErrorHandler` to create the log file immediately, ensuring its presence in the state directory. - Simplified conditional checks for log file operations and updated timestamp handling to use `system_clock`.
This commit is contained in:
@@ -20,6 +20,8 @@ ErrorHandler::ErrorHandler()
|
|||||||
fs::create_directories(log_dir);
|
fs::create_directories(log_dir);
|
||||||
}
|
}
|
||||||
log_file_path_ = (log_dir / "error.log").string();
|
log_file_path_ = (log_dir / "error.log").string();
|
||||||
|
// Create the log file immediately so it exists in the state directory
|
||||||
|
ensure_log_file();
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
// If we can't create the directory, disable file logging
|
// If we can't create the directory, disable file logging
|
||||||
file_logging_enabled_ = false;
|
file_logging_enabled_ = false;
|
||||||
@@ -34,11 +36,7 @@ ErrorHandler::ErrorHandler()
|
|||||||
ErrorHandler::~ErrorHandler()
|
ErrorHandler::~ErrorHandler()
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lg(mtx_);
|
std::lock_guard<std::mutex> lg(mtx_);
|
||||||
if (log_file_ &&log_file_
|
if (log_file_ && log_file_->is_open()) {
|
||||||
->
|
|
||||||
is_open()
|
|
||||||
)
|
|
||||||
{
|
|
||||||
log_file_->flush();
|
log_file_->flush();
|
||||||
log_file_->close();
|
log_file_->close();
|
||||||
}
|
}
|
||||||
@@ -249,11 +247,8 @@ void
|
|||||||
ErrorHandler::ensure_log_file()
|
ErrorHandler::ensure_log_file()
|
||||||
{
|
{
|
||||||
// Must be called with mtx_ held
|
// Must be called with mtx_ held
|
||||||
if (log_file_ &&log_file_
|
if (log_file_ && log_file_->is_open())
|
||||||
->
|
return;
|
||||||
is_open()
|
|
||||||
)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (log_file_path_.empty())
|
if (log_file_path_.empty())
|
||||||
return;
|
return;
|
||||||
@@ -313,6 +308,6 @@ std::uint64_t
|
|||||||
ErrorHandler::now_ns()
|
ErrorHandler::now_ns()
|
||||||
{
|
{
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
return duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count();
|
return duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
|
||||||
}
|
}
|
||||||
} // namespace kte
|
} // namespace kte
|
||||||
3
main.cc
3
main.cc
@@ -117,6 +117,9 @@ main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
std::setlocale(LC_ALL, "");
|
std::setlocale(LC_ALL, "");
|
||||||
|
|
||||||
|
// Ensure the error handler (and its log file) is initialised early.
|
||||||
|
kte::ErrorHandler::Instance();
|
||||||
|
|
||||||
Editor editor;
|
Editor editor;
|
||||||
|
|
||||||
// CLI parsing using getopt_long
|
// CLI parsing using getopt_long
|
||||||
|
|||||||
Reference in New Issue
Block a user