Commit 227da88c authored by Nguyen, Thien's avatar Nguyen, Thien
Browse files

XACC File Logger to output timestamped log files to a dedicated logs folder


Signed-off-by: Nguyen, Thien's avatarThien Nguyen <nguyentm@ornl.gov>
parent 62a2182f
......@@ -18,7 +18,9 @@
#include <iostream>
#include <sstream>
#include <istream>
#include <iomanip>
#include <dirent.h>
#include "xacc_config.hpp"
#ifdef HAS_LIBUNWIND
#include <libunwind.h>
......@@ -62,6 +64,21 @@ bool directoryExists(const std::string path) {
return bExists;
}
bool makeDirectory(const std::string& path) {
int ret = mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
return (ret == 0);
}
std::string getCurrentTimeForFileName() {
auto time = std::time(nullptr);
std::stringstream ss;
// ISO 8601 without timezone information.
ss << std::put_time(std::localtime(&time), "%F_%T");
auto s = ss.str();
std::replace(s.begin(), s.end(), ':', '-');
return s;
}
void print_backtrace() {
#ifdef HAS_LIBUNWIND
unw_cursor_t cursor;
......@@ -181,20 +198,33 @@ XACCLogger::XACCLogger()
stdOutLogger->set_level(spdlog::level::info);
}
}
void XACCLogger::createFileLogger() {
// Create a file logger instance
{
std::string loggerName = "xacc-file-logger";
auto _log = spdlog::get(loggerName);
if (_log) {
fileLogger = _log;
} else {
const std::string DEFAULT_FILE_NAME = "xacc_log";
fileLogger = spdlog::basic_logger_mt(loggerName, DEFAULT_FILE_NAME);
std::string loggerName = "xacc-file-logger";
auto _log = spdlog::get(loggerName);
if (_log) {
fileLogger = _log;
} else {
const std::string rootPath(XACC_INSTALL_DIR);
// We'll put the log file (timestamped) to the "logs" sub-directory:
std::string logDir = rootPath + "/logs";
if (!directoryExists(logDir)) {
if (!makeDirectory(logDir)) {
// Cannot make the directory, use the current directory instead.
logDir = "";
}
}
fileLogger->set_level(spdlog::level::info);
const std::string DEFAULT_FILE_NAME_PREFIX = "xacc_log_";
const std::string DEFAULT_FILE_NAME_POSTFIX = ".txt";
const std::string fileName = DEFAULT_FILE_NAME_PREFIX + getCurrentTimeForFileName() + DEFAULT_FILE_NAME_POSTFIX;
// Create a file logger using the timestamped filename in the logs folder.
fileLogger = spdlog::basic_logger_mt(loggerName, logDir + "/" + fileName);
}
fileLogger->set_level(spdlog::level::info);
}
void XACCLogger::logToFile(bool enable) {
......
......@@ -167,7 +167,19 @@ protected:
XACCLogger();
std::shared_ptr<spdlog::logger> getLogger() { return useFile ? fileLogger : stdOutLogger; }
std::shared_ptr<spdlog::logger> getLogger() {
static bool fileLoggerUsed = false;
if (!fileLoggerUsed && useFile) {
createFileLogger();
fileLoggerUsed = true;
}
return useFile ? fileLogger : stdOutLogger;
}
// On-demand create a file logger:
// We don't want to create one if not being used.
void createFileLogger();
public:
// Overriding here so we can have a custom constructor
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment