32 ss <<
"The instance of " << this->
getClassName() <<
" is not OK.";
37 char* Loggable::staticLogFileName =
nullptr;
39 Loggable::Loggable(
bool verbose_output, callbackFn verbose_callback,
bool add_newline) :
40 verbose_output(verbose_output),
41 verbose_callback(verbose_callback),
43 print_timestamps(true),
44 erase_on_beginning(false),
45 file_output_only(false),
46 log_file_written(false),
47 add_newline(add_newline)
51 void Loggable::set_logFile_name(
const char* filename)
54 int strlength = std::strlen(filename);
55 this->
logFileName = malloc_with_check<char>(strlength + 1);
59 void Loggable::set_logFile_name(std::string filename)
61 this->set_logFile_name(filename.c_str());
64 void Loggable::set_static_logFile_name(
const char* filename)
66 free_with_check(Loggable::staticLogFileName);
67 int strlength = std::strlen(filename);
68 Loggable::staticLogFileName = malloc_with_check<char>(strlength + 1);
69 strcpy(Loggable::staticLogFileName, filename);
72 void Loggable::set_static_logFile_name(std::string filename)
74 Loggable::set_static_logFile_name(filename.c_str());
77 void Loggable::set_file_output_only(
bool onOff)
79 this->file_output_only = onOff;
84 return this->verbose_output;
89 return this->verbose_callback;
92 void Loggable::Static::info(
const char* msg, ...)
95 char* text_contents = text + 1;
103 va_start(arglist, msg);
104 vsprintf(text_contents, msg, arglist);
109 HANDLE h_console = GetStdHandle(STD_OUTPUT_HANDLE);
112 WORD console_attr_red = FOREGROUND_RED, console_attr_green = FOREGROUND_GREEN, console_attr_blue = FOREGROUND_BLUE;
114 WORD console_attrs = 0;
115 console_attrs |= console_attr_green;
118 SetConsoleTextAttribute(h_console, console_attrs);
122 BOOL write_success = WriteConsoleA(h_console, text_contents, strlen(text_contents), &num_written,
nullptr);
123 std::cout << std::endl;
127 # define FOREGROUND_RED 1
128 # define FOREGROUND_GREEN 2
129 # define FOREGROUND_BLUE 4
131 int console_attrs = 0;
132 bool console_bold =
true;
134 printf(
"\033[%dm", console_attrs + 30);
141 printf(
"%s\033[0m\n", text_contents);
146 void Loggable::Static::warn(
const char* msg, ...)
149 char* text_contents = text + 1;
157 va_start(arglist, msg);
158 vsprintf(text_contents, msg, arglist);
163 HANDLE h_console = GetStdHandle(STD_OUTPUT_HANDLE);
166 WORD console_attr_red = FOREGROUND_RED, console_attr_green = FOREGROUND_GREEN, console_attr_blue = FOREGROUND_BLUE;
168 WORD console_attrs = 0;
169 console_attrs |= console_attr_red | console_attr_green;
172 SetConsoleTextAttribute(h_console, console_attrs);
176 BOOL write_success = WriteConsoleA(h_console, text_contents, strlen(text_contents), &num_written,
nullptr);
177 std::cout << std::endl;
180 # define FOREGROUND_RED 1
181 # define FOREGROUND_GREEN 2
182 # define FOREGROUND_BLUE 4
184 int console_attrs = 1;
185 console_attrs |= FOREGROUND_RED | FOREGROUND_GREEN;
186 bool console_bold =
false;
188 printf(
"\033[%dm", console_attrs + 30);
195 printf(
"%s\033[0m\n", text_contents);
200 void Loggable::Static::error(
const char* msg, ...)
203 char* text_contents = text + 1;
211 va_start(arglist, msg);
212 vsprintf(text_contents, msg, arglist);
217 HANDLE h_console = GetStdHandle(STD_OUTPUT_HANDLE);
220 WORD console_attr_red = FOREGROUND_RED, console_attr_green = FOREGROUND_GREEN, console_attr_blue = FOREGROUND_BLUE;
222 WORD console_attrs = 0;
223 console_attrs |= console_attr_red;
226 SetConsoleTextAttribute(h_console, console_attrs);
230 BOOL write_success = WriteConsoleA(h_console, text_contents, strlen(text_contents), &num_written,
nullptr);
231 std::cout << std::endl;
234 # define FOREGROUND_RED 1
235 # define FOREGROUND_GREEN 2
236 # define FOREGROUND_BLUE 4
238 int console_attrs = 1;
239 console_attrs |= FOREGROUND_RED;
240 bool console_bold =
false;
242 printf(
"\033[%dm", console_attrs + 30);
249 printf(
"%s\033[0m\n", text_contents);
254 void Loggable::error(
const char* msg, ...)
const
256 if (!this->verbose_output)
260 char* text_contents = text + 1;
268 va_start(arglist, msg);
269 vsprintf(text_contents, msg, arglist);
274 void Loggable::error_if(
bool cond,
const char* msg, ...)
const
276 if (!this->verbose_output)
282 char* text_contents = text + 1;
290 va_start(arglist, msg);
291 vsprintf(text_contents, msg, arglist);
297 void Loggable::warn(
const char* msg, ...)
const
299 if (!this->verbose_output)
303 char* text_contents = text + 1;
311 va_start(arglist, msg);
312 vsprintf(text_contents, msg, arglist);
317 void Loggable::warn_if(
bool cond,
const char* msg, ...)
const
319 if (!this->verbose_output)
325 char* text_contents = text + 1;
333 va_start(arglist, msg);
334 vsprintf(text_contents, msg, arglist);
339 void Loggable::info(
const char* msg, ...)
const
341 if (!this->verbose_output)
345 char* text_contents = text + 1;
353 va_start(arglist, msg);
354 vsprintf(text_contents, msg, arglist);
358 void Loggable::info_if(
bool cond,
const char* msg, ...)
const
360 if (!this->verbose_output)
366 char* text_contents = text + 1;
374 va_start(arglist, msg);
375 vsprintf(text_contents, msg, arglist);
381 bool Loggable::write_console(
const char code,
const char* text)
const
385 HANDLE h_console = GetStdHandle(STD_OUTPUT_HANDLE);
386 if (h_console == INVALID_HANDLE_VALUE)
390 CONSOLE_SCREEN_BUFFER_INFO console_info;
391 if (!GetConsoleScreenBufferInfo(h_console, &console_info))
395 WORD console_attr_red = FOREGROUND_RED, console_attr_green = FOREGROUND_GREEN, console_attr_blue = FOREGROUND_BLUE;
397 WORD console_attrs = 0;
401 case HERMES_EC_WARNING: console_attrs |= console_attr_red | console_attr_green;
break;
407 SetConsoleTextAttribute(h_console, console_attrs);
411 BOOL write_success = WriteConsoleA(h_console, text, strlen(text), &num_written,
nullptr);
414 SetConsoleTextAttribute(h_console, console_info.wAttributes);
422 # define FOREGROUND_RED 1
423 # define FOREGROUND_GREEN 2
424 # define FOREGROUND_BLUE 4
426 int console_attrs = 0;
427 bool console_bold =
false;
430 case HERMES_EC_WARNING: console_attrs |= FOREGROUND_RED | FOREGROUND_GREEN;
break;
435 printf(
"\033[%dm", console_attrs + 30);
442 printf(
"%s\033[0m", text);
448 Loggable::HermesLogEventInfo* Loggable::hermes_build_log_info(
char event)
const
450 return new Loggable::HermesLogEventInfo(event,
__CURRENT_FUNCTION, __FILE__, __LINE__);
453 void Loggable::hermes_fwrite(
const void* ptr,
size_t size,
size_t nitems, FILE* stream)
const
455 if (fwrite(ptr, size, nitems, stream) != nitems || ferror(stream))
459 void Loggable::hermes_fread(
void* ptr,
size_t size,
size_t nitems, FILE* stream)
const
461 size_t ret = fread(ptr, size, nitems, stream);
464 else if (ferror(stream))
468 Loggable::HermesLogEventInfo::HermesLogEventInfo(
const char code,
const char* src_function,
const char* src_file,
const int src_line)
469 : code(code), src_function(src_function), src_file(src_file), src_line(src_line)
474 this->print_timestamps = onOff;
479 this->erase_on_beginning = onOff;
482 void Loggable::hermes_log_message(
const char code,
const char* msg)
const
484 #pragma omp critical (hermes_log_message)
487 if (!this->file_output_only)
489 if (!write_console(code, msg))
497 HermesLogEventInfo* info = this->hermes_build_log_info(code);
504 if (this->erase_on_beginning && !this->log_file_written)
505 file = fopen(log_file_name,
"wt");
507 file = fopen(log_file_name,
"at");
511 if (!log_file_written)
514 (
const_cast<Loggable*
>(
this))->log_file_written =
true;
515 if (!this->erase_on_beginning)
520 fprintf(file,
"\n\n");
524 if (print_timestamps)
529 struct tm* now_tm = gmtime(&now);
530 char time_buf[BUF_SZ];
531 strftime(time_buf, BUF_SZ,
"%y%m%d-%H:%M", now_tm);
533 fprintf(file,
"%s\t%s", time_buf, msg);
536 fprintf(file,
"%s", msg);
538 if (this->add_newline)
543 if (this->verbose_callback !=
nullptr)
544 this->verbose_callback(msg);
554 this->verbose_output = to_set;
559 this->verbose_callback = callback;
565 #ifdef _WINDOWS //Windows
567 if (QueryPerformanceFrequency(&freq))
568 frequency = (double)freq.QuadPart;
575 TimeMeasurable::SysTime TimeMeasurable::get_time()
const
577 #ifdef _WINDOWS //Windows
581 QueryPerformanceCounter(&ticks);
582 return ticks.QuadPart;
588 #elif defined(__APPLE__) //Mac
594 clock_gettime(CLOCK_REALTIME, &tm);
599 double TimeMeasurable::period_in_seconds(
const SysTime& begin,
const SysTime& end)
const
601 #ifdef _WINDOWS //Windows
602 uint64_t period = end - begin;
604 return period / frequency;
606 return period / (double)CLOCKS_PER_SEC;
609 long period_nsec = end.tv_nsec - begin.tv_nsec;
613 period_nsec += 1000000000UL;
615 long period_sec = (long)(end.tv_sec - begin.tv_sec) + sec_corr;
616 return period_sec + (period_nsec / 1E9);
622 SysTime cur_time = get_time();
625 double secs = period_in_seconds(last_time, cur_time);
632 last_time = cur_time;
647 return to_string(accum);
657 return to_string(last_period);
670 last_time = get_time();
675 std::string TimeMeasurable::to_string(
double secs)
const
681 int hours = (int)secs / (3600);
682 int mins = (int)fmod(secs, 3600) / 60;
683 secs = fmod(secs, 60);
685 std::stringstream str;
687 str << hours <<
"h ";
688 if (hours > 0 || mins > 0)
696 IntegrableWithGlobalOrder::IntegrableWithGlobalOrder() : global_integration_order_set(false), global_integration_order(0)
701 this->global_integration_order = order;
702 this->global_integration_order_set =
true;
705 SettableComputationTime::SettableComputationTime() : time(0.0), time_step(0.0)
714 void SettableComputationTime::set_time_step(
double time_step)
716 this->time_step = time_step;
719 OutputAttachable::OutputAttachable()
749 const T& OutputAttachable::get_parameter_value(
const Parameter<T>& parameter)
751 return *parameter.value;
755 T& OutputAttachable::get_parameter_value(Parameter<T>& parameter)
757 return *parameter.value;
761 void OutputAttachable::set_parameter_value(Parameter<T>& parameter, T* value)
763 parameter.value = value;
766 template HERMES_API
const unsigned int& OutputAttachable::get_parameter_value<unsigned int>(
const Parameter<unsigned int>& parameter);
767 template HERMES_API
const double& OutputAttachable::get_parameter_value<double>(
const Parameter<double>& parameter);
768 template HERMES_API
const bool& OutputAttachable::get_parameter_value<bool>(
const Parameter<bool>& parameter);
769 template HERMES_API
const std::vector<unsigned int>& OutputAttachable::get_parameter_value<std::vector<unsigned int> >(
const Parameter<std::vector<unsigned int> >& parameter);
770 template HERMES_API
const std::vector<double>& OutputAttachable::get_parameter_value<std::vector<double> >(
const Parameter<std::vector<double> >& parameter);
771 template HERMES_API
const std::vector<bool>& OutputAttachable::get_parameter_value<std::vector<bool> >(
const Parameter<std::vector<bool> >& parameter);
773 template HERMES_API
unsigned int& OutputAttachable::get_parameter_value<unsigned int>(Parameter<unsigned int>& parameter);
774 template HERMES_API
double& OutputAttachable::get_parameter_value<double>(Parameter<double>& parameter);
775 template HERMES_API
bool& OutputAttachable::get_parameter_value<bool>(Parameter<bool>& parameter);
776 template HERMES_API std::vector<unsigned int>& OutputAttachable::get_parameter_value<std::vector<unsigned int> >(Parameter<std::vector<unsigned int> >& parameter);
777 template HERMES_API std::vector<double>& OutputAttachable::get_parameter_value<std::vector<double> >(Parameter<std::vector<double> >& parameter);
778 template HERMES_API std::vector<bool>& OutputAttachable::get_parameter_value<std::vector<bool> >(Parameter<std::vector<bool> >& parameter);
780 template HERMES_API
void OutputAttachable::set_parameter_value<unsigned int>(Parameter<unsigned int>& parameter,
unsigned int* value);
781 template HERMES_API
void OutputAttachable::set_parameter_value<double>(Parameter<double>& parameter,
double* value);
782 template HERMES_API
void OutputAttachable::set_parameter_value<bool>(Parameter<bool>& parameter,
bool* value);
783 template HERMES_API
void OutputAttachable::set_parameter_value<std::vector<unsigned int> >(Parameter<std::vector<unsigned int> >& parameter, std::vector<unsigned int>* value);
784 template HERMES_API
void OutputAttachable::set_parameter_value<std::vector<double> >(Parameter<std::vector<double> >& parameter, std::vector<double>* value);
785 template HERMES_API
void OutputAttachable::set_parameter_value<std::vector<bool> >(Parameter<std::vector<bool> >& parameter, std::vector<bool>* value);
virtual void set_verbose_callback(callbackFn callback)
General namespace for the Hermes library.
double last() const
Returns last measured period (in seconds).
Exception interface Basically a std::exception, but with a constructor with string and with print_msg...
virtual bool isOkay() const =0
Ask if the instance is fine.
const TimeMeasurable & tick_reset()
Starts a new_ period and resets accumulated time.
File containing common definitions, and basic global enums etc. for HermesCommon. ...
#define HERMES_LOG_FILE_DELIM_SIZE
A size of a delimiter in a log file.
std::string accumulated_str() const
Returns accumulated time in human readable form.
virtual bool on_step_begin()
virtual bool on_initialization()
void check() const
Method to handle the state.
#define HERMES_EC_ERROR
An event code: warnings.
Class using time measurement Can be used directly (is not abstract), so one can use e...
virtual void set_time(double time)
set time information for time-dependent problems.
virtual void set_global_integration_order(unsigned int order)
#define HERMES_EC_WARNING
An event code: warnings.
virtual bool on_initial_step_end()
#define HERMES_EC_INFO
An event code: info about results.
char * logFileName
Logfile name.
const std::string & name() const
Returns a name of the time period if any.
const TimeMeasurable & tick(TimeMeasurable::TimerPeriodTickType type=HERMES_ACCUMULATE)
Starts/ends a new_ period.
TimeMeasurable(const char *name=nullptr)
Constructs internal structures and starts measuring.
Basic matrix classes and operations.
Skip period between ticks, i.e., do not accumulate it.
void set_erase_on_beginning(bool onOff)
Sets the logFile being always erased before logging.
TimerPeriodTickType
Tick type. Used by the class Hermes::TimePeriod.
const TimeMeasurable & reset()
Resets accumulated time.
std::string last_str() const
Returns last measured period in human readable form.
double accumulated() const
Returns accumulated time (in seconds).
bool get_verbose_output() const
Returns the current value of verbose_output;.
void set_timestamps(bool onOff)
Sets the addition of a time stamp on each line in the log file. By default it is on.
virtual std::string getClassName() const =0
Get class name, for the purpose of messaging.
Mix-in classes for one functionality, for various classes to be derived from.
File containing common definitions, and basic global enums etc. for HermesCommon. ...
virtual void set_verbose_output(bool to_set)
Accumulate a period between ticks.
callbackFn get_verbose_callback() const
Returns the current value of verbose_callback;.
virtual bool on_step_end()
#define __CURRENT_FUNCTION
A platform-dependent string defining a current function.