The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tracer.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Contains code for tracing the code.
18  */
19 
20 #ifndef TRACER_HPP_INCLUDED
21 #define TRACER_HPP_INCLUDED
22 
23 #include <boost/noncopyable.hpp>
24 
25 #include <map>
26 #include <string>
27 
28 /** Helper structure for gathering the tracing statistics. */
29 struct ttracer
30  : private boost::noncopyable
31 {
32  /**
33  * Helper structure to print the tracing statistics.
34  *
35  * When the constructor gets a valid @ref ttracer pointer it prints the
36  * tracing statistics in its destructor. This allows the structure to be
37  * initialised at the beginning of a scope and print the statistics once
38  * the scope is left. (This makes it easier to write the tracing macros.)
39  */
40  struct tprint
41  : private boost::noncopyable
42  {
43  explicit tprint(const ttracer* const tracer__);
44 
45  ~tprint();
46 
47  /** The tracer, whose statistics to print. */
48  const ttracer* const tracer;
49  };
50 
51  explicit ttracer(const char* const function__);
52 
53  /** The total number of runs. */
54  int run;
55 
56  /** The function being traced. */
57  const char* const function;
58 
59  /**
60  * The tracer counters.
61  *
62  * The first pair contains a line number and a name of the marker.
63  * This has two advantages:
64  * * A line number is always unique, thus if using markers with the same
65  * name, they are not the same marker.
66  * * The markers are sorted in order of appearance and not in order of
67  * their names.
68  *
69  * The second pair contains the number of times a marker is hit.
70  */
71  std::map<std::pair<int, std::string>, int> counters;
72 };
73 
74 /**
75  * The beginning of a traced scope.
76  *
77  * It is not intended that tracer scopes are nested, but it should work at the
78  * moment.
79  *
80  * @param interval The interval between printing the statistics.
81  */
82 #ifdef __GNUC__
83 #define TRACER_ENTRY(interval) \
84  static ttracer tracer(__PRETTY_FUNCTION__); \
85  ttracer::tprint print((++tracer.run % interval) == 0 ? &tracer : nullptr)
86 #else
87 #define TRACER_ENTRY(interval) \
88  static ttracer tracer(__FUNCTION__); \
89  ttracer::tprint print((++tracer.run % interval) == 0 ? &tracer : nullptr)
90 #endif
91 
92 /**
93  * A trace count point.
94  *
95  * When this macro is reached the counter for this marker is increased.
96  *
97  * @param marker A string with the name of the marker.
98  */
99 #define TRACER_COUNT(marker) \
100  do { \
101  ++tracer.counters[std::make_pair(__LINE__, marker)]; \
102  } while(0)
103 
104 #endif
Helper structure for gathering the tracing statistics.
Definition: tracer.hpp:29
std::map< std::pair< int, std::string >, int > counters
The tracer counters.
Definition: tracer.hpp:71
int run
The total number of runs.
Definition: tracer.hpp:54
ttracer(const char *const function__)
Definition: tracer.cpp:58
Helper structure to print the tracing statistics.
Definition: tracer.hpp:40
tprint(const ttracer *const tracer__)
Definition: tracer.cpp:20
const ttracer *const tracer
The tracer, whose statistics to print.
Definition: tracer.hpp:48