Bayeux  3.4.1
Core Foundation library for SuperNEMO
tracer.h
Go to the documentation of this file.
1 //
8 // Copyright (c) 2013-2017 by François Mauger <mauger@lpccaen.in2p3.fr>
9 //
10 // This file is part of datatools.
11 //
12 // datatools is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation, either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // datatools is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with datatools. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef DATATOOLS_TRACER_H
26 #define DATATOOLS_TRACER_H
27 
28 // Standard Library:
29 #include <iostream>
30 #include <fstream>
31 #include <string>
32 #include <memory>
33 
34 // This Project:
35 #include <datatools/utils.h>
36 
37 namespace datatools {
38 
40  class tracer
41  {
42  public:
43 
44  tracer();
45 
46  tracer(int id_, const std::string & filename_, const std::string & label_ = "");
47 
48  ~tracer();
49 
50  void set_id(int);
51 
52  int get_id() const;
53 
54  void set_label(const std::string &);
55 
56  const std::string & get_label() const;
57 
58  void set_filename(const std::string &);
59 
60  const std::string & get_filename() const;
61 
62  void set_preserved_file(bool pf_);
63 
64  bool is_preserved_file() const;
65 
66  bool is_initialized() const;
67 
68  void initialize();
69 
70  void initialize(int id_);
71 
72  void initialize(int id_, const std::string & filename_);
73 
74  void initialize(int id_,
75  const std::string & filename_,
76  bool preserved_file_);
77 
78  void reset();
79 
80  std::ofstream & out(bool increment_ = true);
81 
82  template<class Type>
83  void trace(const Type & value_, bool increment_ = true)
84  {
85  out(increment_) << ' ' << value_;
86  if (increment_) out(false) << '\n';
87  }
88 
89  static datatools::tracer & grab_global_tracer(int id_,
90  const std::string & filename_ = "");
91 
92  private:
93 
94  int _id_;
95  std::string _label_;
96  std::string _filename_;
97  bool _preserved_file_;
98  std::unique_ptr<std::ofstream> _fout_;
99  int _counter_;
100 
101  };
102 
103 } // namespace datatools
104 
105 #define DT_TRACER_INIT(TracerId,TracerFilename) \
106  { \
107  ::datatools::tracer::grab_global_tracer(TracerId,TracerFilename); \
108  }
109 
110 
111 #define DT_TRACER_TRACE(TracerId,Value) \
112  { \
113  ::datatools::tracer & t = ::datatools::tracer::grab_global_tracer(TracerId); \
114  if (! t.is_initialized()) t.initialize(TracerId); \
115  t.trace(Value); \
116  }
117 
118 
119 #define DT_TRACER_MESSAGE(TracerId,Message) \
120  { \
121  ::datatools::tracer & t = ::datatools::tracer::grab_global_tracer(TracerId); \
122  if (! t.is_initialized()) t.initialize(TracerId); \
123  t.out(true) << ' ' << Message << std::endl ; \
124  }
125 
126 
127 #define DT_TRACER_RESET(TracerId) \
128  { \
129  ::datatools::tracer::grab_global_tracer(TracerId).reset(); \
130  }
131 
132 
133 #endif // DATATOOLS_TRACER_H
134 
135 // Local Variables: --
136 // mode: c++ --
137 // c-file-style: "gnu" --
138 // tab-width: 2 --
139 // End: --
std::ofstream & out(bool increment_=true)
int get_id() const
bool is_preserved_file() const
const std::string & get_filename() const
void set_label(const std::string &)
A tracer object stores some arbitrary output in a trace file.
Definition: tracer.h:40
static datatools::tracer & grab_global_tracer(int id_, const std::string &filename_="")
void set_preserved_file(bool pf_)
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
const std::string & get_label() const
void set_filename(const std::string &)
bool is_initialized() const
void trace(const Type &value_, bool increment_=true)
Definition: tracer.h:83