HermesCommon  2.0
mixins.h
Go to the documentation of this file.
1 // This file is part of HermesCommon
2 //
3 // Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
4 // Email: hpfem-group@unr.edu, home page: http://hpfem.org/.
5 //
6 // Hermes2D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published
8 // by the Free Software Foundation; either version 2 of the License,
9 // or (at your option) any later version.
10 //
11 // Hermes2D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Hermes2D; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #ifndef __HERMES_COMMON_MIXINS_H
23 #define __HERMES_COMMON_MIXINS_H
24 
25 #include "common.h"
26 #include "vector.h"
27 #include "exceptions.h"
28 
29 namespace Hermes
30 {
38 
39 
40 
41 
42  namespace Mixins
43  {
47  class HERMES_API Loggable
48  {
49  private:
50  typedef void(*callbackFn)(const char*);
51 
52  public:
54  void set_verbose_output(bool to_set);
55 
57  bool get_verbose_output() const;
58 
61  void set_verbose_callback(callbackFn callback);
62  public:
64  callbackFn get_verbose_callback() const;
65 
67  class HERMES_API Static
68  {
69  public:
70  static void info(const char* msg, ...);
71  static void warn(const char* msg, ...);
72  static void error(const char* msg, ...);
73  };
74  protected:
75  Loggable(bool verbose_output = false, callbackFn verbose_callback = NULL);
76 
77  void info(const char* msg, ...) const;
78  void info_if(bool cond, const char* msg, ...) const;
79  void warn(const char* msg, ...) const;
80  void warn_if(bool cond, const char* msg, ...) const;
81  void error(const char* msg, ...) const;
82  void error_if(bool cond, const char* msg, ...) const;
83 
84  /* file operations */
85  void hermes_fwrite(const void* ptr, size_t size, size_t nitems, FILE* stream) const;
86  void hermes_fread(void* ptr, size_t size, size_t nitems, FILE* stream) const;
87 
88  private:
90 
93  bool write_console(const char code, const char* text) const;
94 
96  class HERMES_API HermesLogEventInfo
97  {
98  public:
99  HermesLogEventInfo(const char code, const char* log_file, const char* src_function, const char* src_file, const int src_line);
100  const char code;
101  const char* log_file;
102  const char* src_function;
103  const char* src_file;
104  const int src_line;
105  };
106 
107  HermesLogEventInfo* hermes_build_log_info(char event) const;
108 
110 
111  class LoggerMonitor
112  {
113  pthread_mutexattr_t mutex_attr;
114  pthread_mutex_t mutex;
115 
116  public:
118  LoggerMonitor()
119  {
120  pthread_mutexattr_init(&mutex_attr);
121  pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
122  pthread_mutex_init(&mutex, &mutex_attr);
123  };
125  ~LoggerMonitor()
126  {
127  pthread_mutex_destroy(&mutex);
128  pthread_mutexattr_destroy(&mutex_attr);
129  };
130 
132  void enter() { pthread_mutex_lock(&mutex); };
133 
135  void leave() { pthread_mutex_unlock(&mutex); };
136  };
137 
138  static LoggerMonitor logger_monitor;
139 
140  static std::map<std::string, bool> logger_written;
141 
143 
149  void hermes_log_message(const char code, const char* msg) const;
150 
153  bool verbose_output;
154 
156  callbackFn verbose_callback;
157  };
158 
165  class HERMES_API TimeMeasurable
166  {
167  public:
168  TimeMeasurable(const char *name = NULL);
169 
172  {
174  HERMES_SKIP
175  };
176 
177  const TimeMeasurable& reset();
178  const TimeMeasurable& tick_reset();
179  const TimeMeasurable& tick(TimeMeasurable::TimerPeriodTickType type = HERMES_ACCUMULATE);
180 
182  const std::string& name() const;
183 
185  double accumulated() const;
186 
188  std::string accumulated_str() const;
189 
191 
192  double last() const;
193 
195  std::string last_str() const;
196 
197  private:
198  #ifdef WIN32 //Windows
199  typedef uint64_t SysTime;
200  double frequency;
201  #else //Linux
202  typedef timespec SysTime;
203  #endif
204  const std::string period_name;
205  double last_period;
206  SysTime last_time;
207  double accum;
208 
209  SysTime get_time() const;
210  double period_in_seconds(const SysTime& begin, const SysTime& end) const;
211  std::string to_string(const double time) const;
212  };
213 
216  class HERMES_API IntegrableWithGlobalOrder
217  {
218  public:
220  void set_global_integration_order(unsigned int order);
221  bool global_integration_order_set;
222  unsigned int global_integration_order;
223  };
224 
227  class HERMES_API SettableComputationTime
228  {
229  public:
232  virtual void set_time(double time);
233  virtual void set_time_step(double time_step);
234  double time;
235  double time_step;
236  };
237 
240  class HERMES_API OutputAttachable
241  {
242  public:
244  virtual void on_initialization();
245  virtual void on_step_begin();
246  virtual void on_step_end();
247  virtual void on_finish();
248  };
249  }
250 }
251 #endif