Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
exiting.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_TRACE_EXITING_HH
27 # define MLN_TRACE_EXITING_HH
28 
32 
33 # include <string>
34 # include <iostream>
35 # include <stack>
36 
37 # include <mln/core/contract.hh>
38 # include <mln/trace/quiet.hh>
39 
40 namespace mln
41 {
42 
43  namespace trace
44  {
45 
46  void exiting(const std::string& scope);
47 
48 
49  extern std::stack<std::clock_t> start_times;
50  extern std::stack<std::string> scopes; // For testing purpose
51  // (entering/exiting scope matching).
52 
53 # ifndef MLN_INCLUDE_ONLY
54 
55  inline
56  void exiting(const std::string& scope)
57  {
58  if (quiet)
59  return;
60 
61  if (scopes.empty())
62  {
63  std::cerr << "error: missing 'entering' scope (exiting is '" << scope << "')" << std::endl;
64  quiet = true;
65  }
66  else
67  {
68  if (scopes.top() != scope)
69  {
70  std::cerr << "error: bad matching scope (entering is '" << scopes.top()
71  << "' v. exiting is '" << scope << "')" << std::endl;
72  quiet = true;
73  }
74  scopes.pop();
75  }
76 
77  bool has_inner_trace = (internal::max_tab == tab);
78  --tab;
79 
80  if (!has_inner_trace)
81  for (unsigned i = 0; i < tab; ++i)
82  std::cout << " ";
83 
84  std::cout << "} ";
85 
86  if (!has_inner_trace)
87  std::cout << scope << " ";
88 
89  mln_assertion(! start_times.empty());
90  std::clock_t now = std::clock();
91 
92  if (start_times.top() > now)
93  {
94  std::cerr << "warning: bad timer in trace handling" << std::endl;
95  // FIXME: So what?
96  }
97 
98  if (start_times.top() < now)
99  {
100  std::cout << "- "
101  << ((float(now) - float(start_times.top())) / CLOCKS_PER_SEC)
102  << "s ";
103  }
104 
105  start_times.pop();
106 
107  if (has_inner_trace || (internal::max_tab - tab > 1))
108  std::cout << std::endl;
109  }
110 
111 # endif // ! MLN_INCLUDE_ONLY
112 
113  } // end of namespace mln::trace
114 
115 } // end of namespace mln
116 
117 
118 #endif // ! MLN_TRACE_EXITING_HH