SHORE API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
util.hpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright 2008,2009,2010,2011,2012 Stephan Ossowski, Korbinian Schneeberger,
4  * Felix Ott, Joerg Hagmann, Alf Scotland, Sebastian Bender
5  *
6  * This file is part of SHORE.
7  *
8  * SHORE is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * SHORE is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with SHORE. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
25 
26 #ifndef SHORE_MISC_UTIL_HPP__
27 #define SHORE_MISC_UTIL_HPP__
28 
29 #ifdef HAVE_CONFIG_H
30  #include "config.h"
31 #endif
32 
33 #include <string>
34 #include <vector>
35 #include <ctime>
36 
37 #include <boost/progress.hpp>
38 
39 #ifdef WITH_BACKTRACE
40  #include <execinfo.h>
41 #endif
42 
43 namespace shore {
44 
47 {
48  XFLAG_NA=-1,
49  XFLAG_FALSE=0,
50  XFLAG_TRUE=1,
51  XFLAG_IRRELEVANT=2
52 };
53 
54 #ifdef WITH_BACKTRACE
55 inline void print_backtrace()
58 {
59  void* buf[20];
60  const int bs=::backtrace(buf,shore::size(buf));
61  std::cerr<<"\nBacktrace:\n----------\n";
62  ::backtrace_symbols_fd(buf,bs,STDERR_FILENO);
63 }
64 #endif // WITH_BACKTRACE
65 
68 template<typename T>
69 void throw_exception(const T & ex)
70 {
71  #ifdef DEBUG
72  std::cerr<<"\nException generated: "<<ex.what()<<std::endl;
73  #ifdef WITH_BACKTRACE
74  print_backtrace();
75  #endif // WITH_BACKTRACE
76  #endif // DEBUG
77 
78  throw ex;
79 }
80 
82 int get_maxopenfiles(const bool hardlim=false);
83 
85 int get_nopenfiles();
86 
88 std::string timestr();
89 
91 uint64_t time_stamp();
92 
94 class timer
95 {
96  private:
97 
98  time_t m_start_time;
99  time_t m_last_time;
100 
101  public:
102 
103  timer();
104 
107  std::pair<double,double> bump();
108 
109 };
110 
111 class progress_timer
112 {
113  private:
114 
115  boost::progress_timer m_pt;
116  const std::string m_name;
117 
118  public:
119 
120  progress_timer():m_pt(std::cerr),m_name("#") {}
121 
122  progress_timer(const std::string& name)
123  :m_pt(std::cerr),m_name(name) {}
124 
125  ~progress_timer() { std::cerr<<m_name<<" "<<timestr()<<" | "; }
126 };
127 
128 } // namespace
129 
130 #endif // SHORE_MISC_UTIL_HPP__
131