15#include <unordered_map>
30#if defined(linux) || defined(__linux) || defined(__linux__)
32#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
33 defined(__DragonFly__)
35#elif defined(sun) || defined(__sun)
41#elif defined(__CYGWIN__)
43#elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
45#elif defined(_WIN64) || defined(__WIN64__) || defined(WIN64)
47#elif defined(__BEOS__)
49#elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
51#elif defined(__IBMCPP__) || defined(_AIX)
53#elif defined(__amigaos__)
55#elif defined(__QNXNTO__)
59#if defined(FIMS_WIN32) || defined(FIMS_WIN64)
68#elif defined(FIMS_LINUX) || defined(FIMS_MACOS) || defined(FIMS_BSD)
73#if !defined(__PRETTY_FUNCTION__) && !defined(__GNUC__)
75#define __PRETTY_FUNCTION__ __FUNCTION__
84#define TMB_FIMS_REAL_TYPE double
85#define TMBAD_FIMS_TYPE TMBad::ad_aug
177 std::stringstream ss;
178 ss <<
"\"timestamp\": " <<
"\"" << this->timestamp <<
"\"" <<
",\n";
179 ss <<
"\"level\": " <<
"\"" << this->level <<
"\",\n";
180 ss <<
"\"message\": " <<
"\"" << this->message <<
"\",\n";
181 ss <<
"\"id\": " <<
"\"" << this->rank <<
"\",\n";
182 ss <<
"\"user\": " <<
"\"" << this->user <<
"\",\n";
183 ss <<
"\"wd\": " <<
"\"" << this->wd <<
"\",\n";
184 ss <<
"\"file\": " <<
"\"" << this->file <<
"\",\n";
185 ss <<
"\"routine\": " <<
"\"" << this->routine <<
"\",\n";
186 ss <<
"\"line\": " <<
"\"" << this->line <<
"\"\n";
204 std::vector<std::string> entries;
205 std::vector<LogEntry> log_entries;
206 size_t entry_number = 0;
207 std::string path =
"fims.log";
208 size_t warning_count = 0;
209 size_t error_count = 0;
216 std::string get_user() {
218 char username[UNLEN + 1];
219 DWORD username_len = UNLEN + 1;
220 if (GetUserNameA(username, &username_len)) {
221 return std::string(username);
223 return "[unknown user]";
226#elif defined(FIMS_LINUX) || defined(FIMS_MACOS) || defined(FIMS_BSD)
227 const char* user_env = getenv(
"USER");
228 if (user_env)
return std::string(user_env);
230 uid_t uid = getuid();
231 struct passwd* pw = getpwuid(uid);
232 if (pw && pw->pw_name) {
233 return std::string(pw->pw_name);
235 return "[unknown user]";
239 return "[unsupported platform]";
275 if (this->write_on_exit) {
276 std::ofstream log(this->path);
292 const std::filesystem::path& relativePath) {
293 std::filesystem::path absolutePath =
294 std::filesystem::absolute(relativePath);
296 std::filesystem::path result;
297 for (
const auto& part : absolutePath) {
299 if (!result.empty()) {
300 result = result.parent_path();
307 return result.generic_string();
325 void set_path(std::string path) { this->path = path; }
343 std::filesystem::path relativePath = file;
344 std::filesystem::path absolutePath =
346 std::filesystem::path cwd = std::filesystem::current_path();
347 std::stringstream ss;
348 auto now = std::chrono::system_clock::now();
349 std::time_t now_time = std::chrono::system_clock::to_time_t(now);
350 std::string ctime_no_newline = strtok(ctime(&now_time),
"\n");
356 l.
rank = this->log_entries.size();
357 l.
user = this->get_user();
358 l.
wd = cwd.generic_string();
359 l.
file = absolutePath.string();
362 this->log_entries.push_back(l);
375 std::filesystem::path relativePath = file;
376 std::filesystem::path absolutePath =
378 std::filesystem::path cwd = std::filesystem::current_path();
380 std::stringstream ss;
381 auto now = std::chrono::system_clock::now();
382 std::time_t now_time = std::chrono::system_clock::to_time_t(now);
383 std::string ctime_no_newline = strtok(ctime(&now_time),
"\n");
389 l.
rank = this->log_entries.size();
390 l.
user = this->get_user();
391 l.
wd = cwd.generic_string();
392 l.
file = absolutePath.string();
395 this->log_entries.push_back(l);
397 if (this->throw_on_error) {
398 std::stringstream ss;
400 throw std::runtime_error(ss.str().c_str());
413 this->warning_count++;
414 std::filesystem::path relativePath = file;
415 std::filesystem::path absolutePath =
417 std::filesystem::path cwd = std::filesystem::current_path();
419 std::stringstream ss;
420 auto now = std::chrono::system_clock::now();
421 std::time_t now_time = std::chrono::system_clock::to_time_t(now);
422 std::string ctime_no_newline = strtok(ctime(&now_time),
"\n");
428 l.
rank = this->log_entries.size();
429 l.
user = this->get_user();
430 l.
wd = cwd.generic_string();
431 l.
file = absolutePath.string();
434 this->log_entries.push_back(l);
452 std::stringstream ss;
453 if (log_entries.size() == 0) {
457 for (
size_t i = 0; i < log_entries.size() - 1; i++) {
458 ss <<
"{\n" << this->log_entries[i].to_string() <<
"},\n";
461 << this->log_entries[log_entries.size() - 1].to_string() <<
"}\n]";
481 std::stringstream ss;
482 std::vector<LogEntry> errors;
483 for (
size_t i = 0; i < log_entries.size(); i++) {
484 if (log_entries[i].level ==
"error") {
485 errors.push_back(this->log_entries[i]);
489 if (errors.size() == 0) {
493 for (
size_t i = 0; i < errors.size() - 1; i++) {
494 ss <<
"{\n" << errors[i].to_string() <<
"},\n";
497 ss <<
"{\n" << errors[errors.size() - 1].to_string() <<
"}\n]";
518 std::stringstream ss;
519 std::vector<LogEntry> warnings;
520 for (
size_t i = 0; i < log_entries.size(); i++) {
521 if (log_entries[i].level ==
"warning") {
522 warnings.push_back(this->log_entries[i]);
526 if (warnings.size() == 0) {
530 for (
size_t i = 0; i < warnings.size() - 1; i++) {
531 ss <<
"{\n" << warnings[i].to_string() <<
"},\n";
534 ss <<
"{\n" << warnings[warnings.size() - 1].to_string() <<
"}\n]";
554 std::stringstream ss;
555 std::vector<LogEntry> info;
556 for (
size_t i = 0; i < log_entries.size(); i++) {
557 if (log_entries[i].level ==
"info") {
558 info.push_back(this->log_entries[i]);
562 if (info.size() == 0) {
566 for (
size_t i = 0; i < info.size() - 1; i++) {
567 ss <<
"{\n" << info[i].to_string() <<
"},\n";
570 ss <<
"{\n" << info[info.size() - 1].to_string() <<
"}\n]";
605 this->entries.clear();
606 this->log_entries.clear();
607 this->error_count = 0;
608 this->warning_count = 0;
609 this->entry_number = 0;
627#define FIMS_INFO_LOG(MESSAGE) \
628 fims::FIMSLog::fims_log->info_message(MESSAGE, __LINE__, __FILE__, \
629 __PRETTY_FUNCTION__);
639#define FIMS_WARNING_LOG(MESSAGE) \
640 fims::FIMSLog::fims_log->warning_message(MESSAGE, __LINE__, __FILE__, \
641 __PRETTY_FUNCTION__);
655#define FIMS_ERROR_LOG(MESSAGE) \
656 fims::FIMSLog::fims_log->error_message(MESSAGE, __LINE__, __FILE__, \
657 __PRETTY_FUNCTION__);
664#define FIMS_STR(s) #s
681 std::string signal_error =
"NA";
684 signal_error =
"Invalid memory access (segmentation fault)";
687 signal_error =
"External interrupt, possibly initiated by the user.";
691 "Abnormal termination condition, possible call to std::abort.";
694 signal_error =
"Erroneous arithmetic operation.";
697 signal_error =
"Invalid program image or invalid instruction";
700 signal_error =
"Termination request, sent to the program.";
703 signal_error =
"Unknown signal thrown";
713 std::signal(sig, SIG_DFL);
727 std::stringstream ss;
Singleton logger for FIMS.
Definition def.hpp:203
void error_message(std::string str, int line, const char *file, const char *func)
Definition def.hpp:372
void clear()
Clear in-memory logging state.
Definition def.hpp:604
bool write_on_exit
A boolean specifying if the log file is written when the session is terminated. The default is TRUE.
Definition def.hpp:249
bool throw_on_error
A boolean specifying if the program is stopped upon the first error, where the default is FALSE....
Definition def.hpp:256
std::string get_log()
Return all stored log entries as a JSON array string.
Definition def.hpp:451
void warning_message(std::string str, int line, const char *file, const char *func)
Definition def.hpp:411
size_t get_warning_count() const
Return the number of warning-log entries currently stored.
Definition def.hpp:595
std::string get_warnings()
Return only warning-level log entries as a JSON array string.
Definition def.hpp:517
std::string get_path()
Get the current output path for on-disk logs.
Definition def.hpp:332
static std::shared_ptr< FIMSLog > fims_log
A singleton instance of the log, i.e., where there is only one log. The object is created when the ....
Definition def.hpp:263
FIMSLog()
Definition def.hpp:268
std::string get_errors()
Return only error-level log entries as a JSON array string.
Definition def.hpp:480
void set_path(std::string path)
Set the destination file path for writing logs to disk.
Definition def.hpp:325
~FIMSLog()
Definition def.hpp:274
std::filesystem::path getAbsolutePathWithoutDotDot(const std::filesystem::path &relativePath)
Get the absolute path without dot dot notation.
Definition def.hpp:291
size_t get_error_count() const
Return the number of error-log entries currently stored.
Definition def.hpp:584
void info_message(std::string str, int line, const char *file, const char *func)
Definition def.hpp:341
std::string get_info()
Return only info-level log entries as a JSON array string.
Definition def.hpp:553
void WriteAtExit(int sig)
Signal handler that records a terminal error and flushes log entries.
Definition def.hpp:680
std::string to_string(T v)
Definition def.hpp:726
A data structure with defined fields for a single log record.
Definition def.hpp:112
std::string wd
The working directory for the environment that created the log.
Definition def.hpp:151
size_t rank
The message identifier corresponding to creation order.
Definition def.hpp:140
std::string file
The full file path of the file that triggered the log entry.
Definition def.hpp:157
std::string routine
The function or method that initiated the log entry.
Definition def.hpp:165
std::string level
The logging level associated with the entry.
Definition def.hpp:134
std::string message
The description of the log entry.
Definition def.hpp:126
int line
The line in file where the log entry was initiated.
Definition def.hpp:170
std::string timestamp
The date and time that the log entry was created.
Definition def.hpp:119
std::string to_string()
Serialize this entry to a JSON object string.
Definition def.hpp:176
std::string user
The user name registered on the machine where the log was created.
Definition def.hpp:145