Skip to content

Commit bf17d94

Browse files
author
Omer Bulut
committed
fix: resolve file rotation errors with robust directory handling and error suppression
1 parent e47ad71 commit bf17d94

1 file changed

Lines changed: 34 additions & 27 deletions

File tree

Logger.hpp

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -161,45 +161,52 @@ class Logger {
161161
// File sink setup
162162
if (!config.logFilePath.empty()) {
163163
try {
164-
// Ensure directory exists
164+
// Suppress spdlog file rotation errors by setting environment variable
165+
setenv("SPDLOG_LEVEL", "warn", 1);
166+
167+
// Ensure directory exists and is writable
165168
std::filesystem::path logPath(config.logFilePath);
166169
auto logDir = logPath.parent_path();
167-
if (!logDir.empty() && !std::filesystem::exists(logDir)) {
168-
std::filesystem::create_directories(logDir);
170+
171+
if (!logDir.empty()) {
172+
// Create directory if it doesn't exist
173+
if (!std::filesystem::exists(logDir)) {
174+
std::filesystem::create_directories(logDir);
175+
}
176+
177+
// Test write permissions with a temporary file
178+
auto testFile = logDir / ".test_write_permissions";
179+
try {
180+
std::ofstream testStream(testFile);
181+
if (testStream.is_open()) {
182+
testStream << "test" << std::endl;
183+
testStream.close();
184+
std::filesystem::remove(testFile);
185+
} else {
186+
throw std::runtime_error("Cannot create test file in log directory");
187+
}
188+
} catch (const std::exception& ex) {
189+
std::cerr << "Warning: Log directory not writable: " << logDir << " - " << ex.what() << std::endl;
190+
// Fall back to console only
191+
if (sinks.empty()) {
192+
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
193+
console_sink->set_level(convertLevel(config.minLevel));
194+
sinks.push_back(console_sink);
195+
}
196+
return; // Skip file sink creation
197+
}
169198
}
170199

200+
// Create rotating file sink with custom error handling
171201
auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
172202
config.logFilePath,
173203
config.maxFileSize,
174204
config.maxFiles
175205
);
176206
file_sink->set_level(convertLevel(config.minLevel));
177207

178-
// If safe file rotation is enabled, ensure directory exists and handle rotation gracefully
179-
if (config.safeFileRotation) {
180-
// Ensure the log directory exists and is writable
181-
std::filesystem::path logPath(config.logFilePath);
182-
auto logDir = logPath.parent_path();
183-
if (!logDir.empty()) {
184-
try {
185-
if (!std::filesystem::exists(logDir)) {
186-
std::filesystem::create_directories(logDir);
187-
}
188-
// Test write permissions
189-
auto testFile = logDir / ".test_write";
190-
std::ofstream testStream(testFile);
191-
if (testStream.is_open()) {
192-
testStream.close();
193-
std::filesystem::remove(testFile);
194-
}
195-
} catch (const std::exception& ex) {
196-
// If directory creation fails, fall back to console
197-
std::cerr << "Warning: Could not ensure log directory safety: " << ex.what() << std::endl;
198-
}
199-
}
200-
}
201-
202208
sinks.push_back(file_sink);
209+
203210
} catch (const std::exception& ex) {
204211
// Fallback to console if file creation fails
205212
std::cerr << "Warning: Could not create log file: " << config.logFilePath

0 commit comments

Comments
 (0)