16#include <unordered_map>
31#if defined(linux) || defined(__linux) || defined(__linux__)
33#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
34 defined(__DragonFly__)
36#elif defined(sun) || defined(__sun)
42#elif defined(__CYGWIN__)
44#elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
46#elif defined(_WIN64) || defined(__WIN64__) || defined(WIN64)
48#elif defined(__BEOS__)
50#elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
52#elif defined(__IBMCPP__) || defined(_AIX)
54#elif defined(__amigaos__)
56#elif defined(__QNXNTO__)
60#if defined(FIMS_WIN32) || defined(FIMS_WIN64)
67#elif defined(FIMS_LINUX) || defined(FIMS_MACOS) || defined(FIMS_BSD)
72#if !defined(__PRETTY_FUNCTION__) && !defined(__GNUC__)
74#define __PRETTY_FUNCTION__ __FUNCTION__
85#define TMB_FIMS_REAL_TYPE double
87#define TMBAD_FIMS_TYPE TMBad::ad_aug
89#define TMB_FIMS_FIRST_ORDER AD<TMB_FIMS_REAL_TYPE>
90#define TMB_FIMS_SECOND_ORDER AD<TMB_FIMS_FIRST_ORDER>
91#define TMB_FIMS_THIRD_ORDER AD<TMB_FIMS_SECOND_ORDER>
147 std::stringstream ss;
148 ss <<
"\"timestamp\": " <<
"\"" << this->timestamp <<
"\"" <<
",\n";
149 ss <<
"\"level\": " <<
"\"" << this->level <<
"\",\n";
150 ss <<
"\"message\": " <<
"\"" << this->message <<
"\",\n";
151 ss <<
"\"id\": " <<
"\"" << this->rank <<
"\",\n";
152 ss <<
"\"user\": " <<
"\"" << this->user <<
"\",\n";
153 ss <<
"\"wd\": " <<
"\"" << this->wd <<
"\",\n";
154 ss <<
"\"file\": " <<
"\"" << this->file <<
"\",\n";
155 ss <<
"\"routine\": " <<
"\"" << this->routine <<
"\",\n";
156 ss <<
"\"line\": " <<
"\"" << this->line <<
"\"\n";
165 std::vector<std::string> entries;
166 std::vector<LogEntry> log_entries;
167 size_t entry_number = 0;
168 std::string path =
"fims.log";
169 size_t warning_count = 0;
170 size_t error_count = 0;
177 std::string get_user() {
179 char username[UNLEN + 1];
180 DWORD username_len = UNLEN + 1;
181 if (GetUserNameA(username, &username_len)) {
182 return std::string(username);
184 return "[unknown user]";
187#elif defined(FIMS_LINUX) || defined(FIMS_MACOS) || defined(FIMS_BSD)
188 const char* user_env = getenv(
"USER");
189 if (user_env)
return std::string(user_env);
191 uid_t uid = getuid();
192 struct passwd* pw = getpwuid(uid);
193 if (pw && pw->pw_name) {
194 return std::string(pw->pw_name);
196 return "[unknown user]";
200 return "[unsupported platform]";
236 if (this->write_on_exit) {
237 std::ofstream log(this->path);
253 const std::filesystem::path& relativePath) {
254 std::filesystem::path absolutePath =
255 std::filesystem::absolute(relativePath);
257 std::filesystem::path result;
258 for (
const auto& part : absolutePath) {
260 if (!result.empty()) {
261 result = result.parent_path();
268 return result.generic_string();
276 void set_path(std::string path) { this->path = path; }
295 std::filesystem::path relativePath = file;
296 std::filesystem::path absolutePath =
298 std::filesystem::path cwd = std::filesystem::current_path();
299 std::stringstream ss;
300 auto now = std::chrono::system_clock::now();
301 std::time_t now_time = std::chrono::system_clock::to_time_t(now);
302 std::string ctime_no_newline = strtok(ctime(&now_time),
"\n");
308 l.
rank = this->log_entries.size();
309 l.
user = this->get_user();
310 l.
wd = cwd.generic_string();
311 l.
file = absolutePath.string();
314 this->log_entries.push_back(l);
327 std::filesystem::path relativePath = file;
328 std::filesystem::path absolutePath =
330 std::filesystem::path cwd = std::filesystem::current_path();
331 std::stringstream ss;
332 auto now = std::chrono::system_clock::now();
333 std::time_t now_time = std::chrono::system_clock::to_time_t(now);
334 std::string ctime_no_newline = strtok(ctime(&now_time),
"\n");
340 l.
rank = this->log_entries.size();
341 l.
user = this->get_user();
342 l.
wd = cwd.generic_string();
343 l.
file = absolutePath.string();
346 this->log_entries.push_back(l);
360 std::filesystem::path relativePath = file;
361 std::filesystem::path absolutePath =
363 std::filesystem::path cwd = std::filesystem::current_path();
365 std::stringstream ss;
366 auto now = std::chrono::system_clock::now();
367 std::time_t now_time = std::chrono::system_clock::to_time_t(now);
368 std::string ctime_no_newline = strtok(ctime(&now_time),
"\n");
374 l.
rank = this->log_entries.size();
375 l.
user = this->get_user();
376 l.
wd = cwd.generic_string();
377 l.
file = absolutePath.string();
380 this->log_entries.push_back(l);
382 if (this->throw_on_error) {
383 std::stringstream ss;
385 throw std::runtime_error(ss.str().c_str());
399 this->warning_count++;
400 std::filesystem::path relativePath = file;
401 std::filesystem::path absolutePath =
403 std::filesystem::path cwd = std::filesystem::current_path();
405 std::stringstream ss;
406 auto now = std::chrono::system_clock::now();
407 std::time_t now_time = std::chrono::system_clock::to_time_t(now);
408 std::string ctime_no_newline = strtok(ctime(&now_time),
"\n");
414 l.
rank = this->log_entries.size();
415 l.
user = this->get_user();
416 l.
wd = cwd.generic_string();
417 l.
file = absolutePath.string();
420 this->log_entries.push_back(l);
429 std::stringstream ss;
430 if (log_entries.size() == 0) {
434 for (
size_t i = 0; i < log_entries.size() - 1; i++) {
435 ss <<
"{\n" << this->log_entries[i].to_string() <<
"},\n";
438 << this->log_entries[log_entries.size() - 1].to_string() <<
"}\n]";
449 std::stringstream ss;
450 std::vector<LogEntry> errors;
451 for (
size_t i = 0; i < log_entries.size(); i++) {
452 if (log_entries[i].level ==
"error") {
453 errors.push_back(this->log_entries[i]);
457 if (errors.size() == 0) {
461 for (
size_t i = 0; i < errors.size() - 1; i++) {
462 ss <<
"{\n" << errors[i].to_string() <<
"},\n";
465 ss <<
"{\n" << errors[errors.size() - 1].to_string() <<
"}\n]";
476 std::stringstream ss;
477 std::vector<LogEntry> warnings;
478 for (
size_t i = 0; i < log_entries.size(); i++) {
479 if (log_entries[i].level ==
"warning") {
480 warnings.push_back(this->log_entries[i]);
484 if (warnings.size() == 0) {
488 for (
size_t i = 0; i < warnings.size() - 1; i++) {
489 ss <<
"{\n" << warnings[i].to_string() <<
"},\n";
492 ss <<
"{\n" << warnings[warnings.size() - 1].to_string() <<
"}\n]";
503 std::stringstream ss;
504 std::vector<LogEntry> info;
505 for (
size_t i = 0; i < log_entries.size(); i++) {
506 if (log_entries[i].level ==
"info") {
507 info.push_back(this->log_entries[i]);
511 if (info.size() == 0) {
515 for (
size_t i = 0; i < info.size() - 1; i++) {
516 ss <<
"{\n" << info[i].to_string() <<
"},\n";
519 ss <<
"{\n" << info[info.size() - 1].to_string() <<
"}\n]";
531 std::stringstream ss;
532 std::vector<LogEntry> info;
533 for (
size_t i = 0; i < log_entries.size(); i++) {
534 if (log_entries[i].file.find(module) != std::string::npos) {
535 info.push_back(this->log_entries[i]);
539 if (info.size() == 0) {
543 for (
size_t i = 0; i < info.size() - 1; i++) {
544 ss <<
"{\n" << info[i].to_string() <<
"},\n";
547 ss <<
"{\n" << info[info.size() - 1].to_string() <<
"}\n]";
567 this->entries.clear();
568 this->log_entries.clear();
569 this->warning_count = 0;
570 this->entry_number = 0;
580#define FIMS_DEBUG_LOG(MESSAGE) \
581 FIMSLog::fims_log->debug_message(MESSAGE, __LINE__, __FILE__, \
582 __PRETTY_FUNCTION__);
586#define FIMS_DEBUG_LOG(MESSAGE)
590#define FIMS_INFO_LOG(MESSAGE) \
591 fims::FIMSLog::fims_log->info_message( \
592 MESSAGE, __LINE__, __FILE__, \
593 __PRETTY_FUNCTION__);
595#define FIMS_WARNING_LOG(MESSAGE) \
596 fims::FIMSLog::fims_log->warning_message( \
597 MESSAGE, __LINE__, __FILE__, \
598 __PRETTY_FUNCTION__);
600#define FIMS_ERROR_LOG(MESSAGE) \
601 fims::FIMSLog::fims_log->error_message( \
602 MESSAGE, __LINE__, __FILE__, \
603 __PRETTY_FUNCTION__);
605#define FIMS_STR(s) #s
615void WriteAtExit(
int sig) {
616 std::string signal_error =
"NA";
619 signal_error =
"Invalid memory access (segmentation fault)";
622 signal_error =
"External interrupt, possibly initiated by the user.";
626 "Abnormal termination condition, possible call to std::abort.";
629 signal_error =
"Erroneous arithmetic operation.";
632 signal_error =
"Invalid program image or invalid instruction";
635 signal_error =
"Termination request, sent to the program.";
638 signal_error =
"Unknown signal thrown";
648 std::signal(sig, SIG_DFL);
660 std::stringstream ss;
void debug_message(std::string str, int line, const char *file, const char *func)
Definition def.hpp:325
void error_message(std::string str, int line, const char *file, const char *func)
Definition def.hpp:357
void clear()
Clears all pointers/references of a FIMS model.
Definition def.hpp:566
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:210
bool throw_on_error
A boolean specifying if the program is stopped upon the first error, where the default is FALSE....
Definition def.hpp:217
std::string get_log()
Definition def.hpp:428
void warning_message(std::string str, int line, const char *file, const char *func)
Definition def.hpp:397
size_t get_warning_count() const
Get the counts of the number of warnings.
Definition def.hpp:560
std::string get_module(const std::string &module)
Definition def.hpp:530
std::string get_warnings()
Definition def.hpp:475
std::string get_path()
Definition def.hpp:283
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:224
FIMSLog()
Definition def.hpp:229
std::string get_errors()
Definition def.hpp:448
void set_path(std::string path)
Definition def.hpp:276
~FIMSLog()
Definition def.hpp:235
std::filesystem::path getAbsolutePathWithoutDotDot(const std::filesystem::path &relativePath)
Get the Absolute Path Without Dot Dot object.
Definition def.hpp:252
size_t get_error_count() const
Get the counts of the number of errors.
Definition def.hpp:555
void info_message(std::string str, int line, const char *file, const char *func)
Definition def.hpp:293
std::string get_info()
Definition def.hpp:502
std::string to_string(T v)
Definition def.hpp:668
std::string wd
Definition def.hpp:129
size_t rank
Definition def.hpp:121
std::string file
Definition def.hpp:132
std::string routine
Definition def.hpp:138
std::string level
Definition def.hpp:116
std::string message
Definition def.hpp:109
int line
Definition def.hpp:141
std::string timestamp
Definition def.hpp:104
std::string to_string()
Definition def.hpp:146
std::string user
Definition def.hpp:124