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)
67#ifndef WIN32_LEAN_AND_MEAN
68#define WIN32_LEAN_AND_MEAN
75#elif defined(FIMS_LINUX) || defined(FIMS_MACOS) || defined(FIMS_BSD)
80#if !defined(__PRETTY_FUNCTION__) && !defined(__GNUC__)
82#define __PRETTY_FUNCTION__ __FUNCTION__
91#define TMB_FIMS_REAL_TYPE double
92#define TMBAD_FIMS_TYPE TMBad::ad_aug
184 std::stringstream ss;
185 ss <<
"\"timestamp\": " <<
"\"" << this->timestamp <<
"\"" <<
",\n";
186 ss <<
"\"level\": " <<
"\"" << this->level <<
"\",\n";
187 ss <<
"\"message\": " <<
"\"" << this->message <<
"\",\n";
188 ss <<
"\"id\": " <<
"\"" << this->rank <<
"\",\n";
189 ss <<
"\"user\": " <<
"\"" << this->user <<
"\",\n";
190 ss <<
"\"wd\": " <<
"\"" << this->wd <<
"\",\n";
191 ss <<
"\"file\": " <<
"\"" << this->file <<
"\",\n";
192 ss <<
"\"routine\": " <<
"\"" << this->routine <<
"\",\n";
193 ss <<
"\"line\": " <<
"\"" << this->line <<
"\"\n";
211 std::vector<std::string> entries;
212 std::vector<LogEntry> log_entries;
213 size_t entry_number = 0;
214 std::string path =
"fims.log";
215 size_t warning_count = 0;
216 size_t error_count = 0;
223 std::string get_user() {
225 char username[UNLEN + 1];
226 DWORD username_len = UNLEN + 1;
227 if (GetUserNameA(username, &username_len)) {
228 return std::string(username);
230 return "[unknown user]";
233#elif defined(FIMS_LINUX) || defined(FIMS_MACOS) || defined(FIMS_BSD)
234 const char* user_env = getenv(
"USER");
235 if (user_env)
return std::string(user_env);
237 uid_t uid = getuid();
238 struct passwd* pw = getpwuid(uid);
239 if (pw && pw->pw_name) {
240 return std::string(pw->pw_name);
242 return "[unknown user]";
246 return "[unsupported platform]";
282 if (this->write_on_exit) {
283 std::ofstream log(this->path);
299 const std::filesystem::path& relativePath) {
300 std::filesystem::path absolutePath =
301 std::filesystem::absolute(relativePath);
303 std::filesystem::path result;
304 for (
const auto& part : absolutePath) {
306 if (!result.empty()) {
307 result = result.parent_path();
314 return result.generic_string();
332 void set_path(std::string path) { this->path = path; }
350 std::filesystem::path relativePath = file;
351 std::filesystem::path absolutePath =
353 std::filesystem::path cwd = std::filesystem::current_path();
354 std::stringstream ss;
355 auto now = std::chrono::system_clock::now();
356 std::time_t now_time = std::chrono::system_clock::to_time_t(now);
357 std::string ctime_no_newline = strtok(ctime(&now_time),
"\n");
363 l.
rank = this->log_entries.size();
364 l.
user = this->get_user();
365 l.
wd = cwd.generic_string();
366 l.
file = absolutePath.string();
369 this->log_entries.push_back(l);
382 std::filesystem::path relativePath = file;
383 std::filesystem::path absolutePath =
385 std::filesystem::path cwd = std::filesystem::current_path();
387 std::stringstream ss;
388 auto now = std::chrono::system_clock::now();
389 std::time_t now_time = std::chrono::system_clock::to_time_t(now);
390 std::string ctime_no_newline = strtok(ctime(&now_time),
"\n");
396 l.
rank = this->log_entries.size();
397 l.
user = this->get_user();
398 l.
wd = cwd.generic_string();
399 l.
file = absolutePath.string();
402 this->log_entries.push_back(l);
404 if (this->throw_on_error) {
405 std::stringstream ss;
407 throw std::runtime_error(ss.str().c_str());
420 this->warning_count++;
421 std::filesystem::path relativePath = file;
422 std::filesystem::path absolutePath =
424 std::filesystem::path cwd = std::filesystem::current_path();
426 std::stringstream ss;
427 auto now = std::chrono::system_clock::now();
428 std::time_t now_time = std::chrono::system_clock::to_time_t(now);
429 std::string ctime_no_newline = strtok(ctime(&now_time),
"\n");
435 l.
rank = this->log_entries.size();
436 l.
user = this->get_user();
437 l.
wd = cwd.generic_string();
438 l.
file = absolutePath.string();
441 this->log_entries.push_back(l);
459 std::stringstream ss;
460 if (log_entries.size() == 0) {
464 for (
size_t i = 0; i < log_entries.size() - 1; i++) {
465 ss <<
"{\n" << this->log_entries[i].to_string() <<
"},\n";
468 << this->log_entries[log_entries.size() - 1].to_string() <<
"}\n]";
488 std::stringstream ss;
489 std::vector<LogEntry> errors;
490 for (
size_t i = 0; i < log_entries.size(); i++) {
491 if (log_entries[i].level ==
"error") {
492 errors.push_back(this->log_entries[i]);
496 if (errors.size() == 0) {
500 for (
size_t i = 0; i < errors.size() - 1; i++) {
501 ss <<
"{\n" << errors[i].to_string() <<
"},\n";
504 ss <<
"{\n" << errors[errors.size() - 1].to_string() <<
"}\n]";
525 std::stringstream ss;
526 std::vector<LogEntry> warnings;
527 for (
size_t i = 0; i < log_entries.size(); i++) {
528 if (log_entries[i].level ==
"warning") {
529 warnings.push_back(this->log_entries[i]);
533 if (warnings.size() == 0) {
537 for (
size_t i = 0; i < warnings.size() - 1; i++) {
538 ss <<
"{\n" << warnings[i].to_string() <<
"},\n";
541 ss <<
"{\n" << warnings[warnings.size() - 1].to_string() <<
"}\n]";
561 std::stringstream ss;
562 std::vector<LogEntry> info;
563 for (
size_t i = 0; i < log_entries.size(); i++) {
564 if (log_entries[i].level ==
"info") {
565 info.push_back(this->log_entries[i]);
569 if (info.size() == 0) {
573 for (
size_t i = 0; i < info.size() - 1; i++) {
574 ss <<
"{\n" << info[i].to_string() <<
"},\n";
577 ss <<
"{\n" << info[info.size() - 1].to_string() <<
"}\n]";
612 this->entries.clear();
613 this->log_entries.clear();
614 this->error_count = 0;
615 this->warning_count = 0;
616 this->entry_number = 0;
620#ifdef FIMS_HEADER_ONLY
636#define FIMS_INFO_LOG(MESSAGE) \
637 fims::FIMSLog::fims_log->info_message(MESSAGE, __LINE__, __FILE__, \
638 __PRETTY_FUNCTION__);
648#define FIMS_WARNING_LOG(MESSAGE) \
649 fims::FIMSLog::fims_log->warning_message(MESSAGE, __LINE__, __FILE__, \
650 __PRETTY_FUNCTION__);
664#define FIMS_ERROR_LOG(MESSAGE) \
665 fims::FIMSLog::fims_log->error_message(MESSAGE, __LINE__, __FILE__, \
666 __PRETTY_FUNCTION__);
673#define FIMS_STR(s) #s
690 std::string signal_error =
"NA";
693 signal_error =
"Invalid memory access (segmentation fault)";
696 signal_error =
"External interrupt, possibly initiated by the user.";
700 "Abnormal termination condition, possible call to std::abort.";
703 signal_error =
"Erroneous arithmetic operation.";
706 signal_error =
"Invalid program image or invalid instruction";
709 signal_error =
"Termination request, sent to the program.";
712 signal_error =
"Unknown signal thrown";
722 std::signal(sig, SIG_DFL);
736 std::stringstream ss;
Singleton logger for FIMS.
Definition def.hpp:210
void error_message(std::string str, int line, const char *file, const char *func)
Definition def.hpp:379
void clear()
Clear in-memory logging state.
Definition def.hpp:611
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:256
bool throw_on_error
A boolean specifying if the program is stopped upon the first error, where the default is FALSE....
Definition def.hpp:263
std::string get_log()
Return all stored log entries as a JSON array string.
Definition def.hpp:458
void warning_message(std::string str, int line, const char *file, const char *func)
Definition def.hpp:418
size_t get_warning_count() const
Return the number of warning-log entries currently stored.
Definition def.hpp:602
std::string get_warnings()
Return only warning-level log entries as a JSON array string.
Definition def.hpp:524
std::string get_path()
Get the current output path for on-disk logs.
Definition def.hpp:339
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:270
FIMSLog()
Definition def.hpp:275
std::string get_errors()
Return only error-level log entries as a JSON array string.
Definition def.hpp:487
void set_path(std::string path)
Set the destination file path for writing logs to disk.
Definition def.hpp:332
~FIMSLog()
Definition def.hpp:281
std::filesystem::path getAbsolutePathWithoutDotDot(const std::filesystem::path &relativePath)
Get the absolute path without dot dot notation.
Definition def.hpp:298
size_t get_error_count() const
Return the number of error-log entries currently stored.
Definition def.hpp:591
void info_message(std::string str, int line, const char *file, const char *func)
Definition def.hpp:348
std::string get_info()
Return only info-level log entries as a JSON array string.
Definition def.hpp:560
void WriteAtExit(int sig)
Signal handler that records a terminal error and flushes log entries.
Definition def.hpp:689
std::string to_string(T v)
Definition def.hpp:735
A data structure with defined fields for a single log record.
Definition def.hpp:119
std::string wd
The working directory for the environment that created the log.
Definition def.hpp:158
size_t rank
The message identifier corresponding to creation order.
Definition def.hpp:147
std::string file
The full file path of the file that triggered the log entry.
Definition def.hpp:164
std::string routine
The function or method that initiated the log entry.
Definition def.hpp:172
std::string level
The logging level associated with the entry.
Definition def.hpp:141
std::string message
The description of the log entry.
Definition def.hpp:133
int line
The line in file where the log entry was initiated.
Definition def.hpp:177
std::string timestamp
The date and time that the log entry was created.
Definition def.hpp:126
std::string to_string()
Serialize this entry to a JSON object string.
Definition def.hpp:183
std::string user
The user name registered on the machine where the log was created.
Definition def.hpp:152