Move the ttracer class to its own file.
The class also makes sense to use outside the floating point emulation code.
This commit is contained in:
parent
6a63680815
commit
79f4a85e33
5 changed files with 166 additions and 107 deletions
|
@ -663,6 +663,7 @@ set(libwesnoth-game_STAT_SRC
|
|||
terrain_translation.cpp
|
||||
text.cpp
|
||||
time_of_day.cpp
|
||||
tracer.cpp
|
||||
video.cpp
|
||||
theme.cpp
|
||||
widgets/button.cpp
|
||||
|
@ -805,6 +806,7 @@ set(exploder_SRC
|
|||
tools/exploder_composer.cpp
|
||||
tools/dummy_video.cpp
|
||||
sdl_utils.cpp
|
||||
tracer.cpp
|
||||
loadscreen_empty.cpp
|
||||
)
|
||||
|
||||
|
@ -820,6 +822,7 @@ set(cutter_SRC
|
|||
tools/exploder_cutter.cpp
|
||||
tools/dummy_video.cpp
|
||||
sdl_utils.cpp
|
||||
tracer.cpp
|
||||
loadscreen_empty.cpp
|
||||
)
|
||||
|
||||
|
@ -870,6 +873,7 @@ set(wesmage_SRC
|
|||
tools/dummy_video.cpp
|
||||
tools/exploder_utils.cpp
|
||||
sdl_utils.cpp
|
||||
tracer.cpp
|
||||
loadscreen_empty.cpp
|
||||
)
|
||||
|
||||
|
|
|
@ -149,6 +149,7 @@ libcampaignd = env.Library("campaignd", libcampaignd_sources, OBJPREFIX = "campa
|
|||
|
||||
libwesnoth_sdl_sources = Split("""
|
||||
sdl_utils.cpp
|
||||
tracer.cpp
|
||||
""")
|
||||
libwesnoth_sdl = client_env.Library("wesnoth_sdl", libwesnoth_sdl_sources)
|
||||
|
||||
|
@ -558,6 +559,7 @@ wesmage_sources = Split("""
|
|||
tools/dummy_video.cpp
|
||||
tools/exploder_utils.cpp
|
||||
sdl_utils.cpp
|
||||
tracer.cpp
|
||||
loadscreen_empty.cpp
|
||||
""")
|
||||
client_env.WesnothProgram("wesmage", wesmage_sources + [libwesnoth_core], have_client_prereqs, OBJPREFIX = "wesmage_", LIBS = ["$LIBS", "png"])
|
||||
|
|
|
@ -125,113 +125,9 @@
|
|||
#endif
|
||||
|
||||
#ifdef FLOATING_POINT_EMULATION_TRACER_ENABLE
|
||||
#include <boost/foreach.hpp>
|
||||
#include <map>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
/** Helper structure for gathering the tracing statistics. */
|
||||
struct ttracer
|
||||
{
|
||||
/**
|
||||
* Helper structure to print the tracing statistics.
|
||||
*
|
||||
* When the constructor gets a valid @ref ttracer pointer it prints the
|
||||
* tracing statistics in its destructor. This allows the structure to be
|
||||
* initialised at the beginning of a scope and print the statistics once
|
||||
* the scope is left. (This makes it easier to write the tracing macros.)
|
||||
*/
|
||||
struct tprint
|
||||
{
|
||||
explicit tprint(const ttracer* const tracer__)
|
||||
: tracer(tracer__)
|
||||
{
|
||||
}
|
||||
|
||||
~tprint()
|
||||
{
|
||||
if(!tracer) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::cerr << "Run statistics:\n"
|
||||
<< "Runs:\t" << std::dec << tracer->run << "\n";
|
||||
|
||||
typedef std::pair<std::pair<int, std::string>, int> thack;
|
||||
size_t maximum_length = 0;
|
||||
BOOST_FOREACH(const thack& counter, tracer->counters) {
|
||||
maximum_length = std::max(
|
||||
maximum_length
|
||||
, counter.first.second.length());
|
||||
}
|
||||
|
||||
std::ios_base::fmtflags original_flag = std::cerr.setf(
|
||||
std::ios_base::left
|
||||
, std::ios_base::adjustfield);
|
||||
|
||||
BOOST_FOREACH(const thack& counter, tracer->counters) {
|
||||
std::cerr << "Marker: "
|
||||
<< std::left
|
||||
<< std::setw(maximum_length) << counter.first.second
|
||||
<< std::right
|
||||
<< " [" << std::setw(5) << counter.first.first << ']'
|
||||
<< " hits " << counter.second << "\n";
|
||||
}
|
||||
|
||||
std::cerr.setf(original_flag, std::ios_base::adjustfield);
|
||||
}
|
||||
|
||||
/** The tracer, whose statistics to print. */
|
||||
const ttracer* const tracer;
|
||||
};
|
||||
|
||||
ttracer()
|
||||
: run(0)
|
||||
, counters()
|
||||
{
|
||||
}
|
||||
|
||||
/** The total number of runs. */
|
||||
int run;
|
||||
|
||||
/**
|
||||
* The tracer counters.
|
||||
*
|
||||
* The first pair contains a line number and a name of the marker.
|
||||
* This has two advantages:
|
||||
* * A line number is always unique, thus if using markers with the same
|
||||
* name, they are not the same marker.
|
||||
* * The markers are sorted in order of appearance and not in order of
|
||||
* their names.
|
||||
*
|
||||
* The second pair contains the number of times a marker is hit.
|
||||
*/
|
||||
std::map<std::pair<int, std::string>, int> counters;
|
||||
};
|
||||
|
||||
/**
|
||||
* The beginning of a traced scope.
|
||||
*
|
||||
* It is not intended that tracer scopes are nested, but it should work at the
|
||||
* moment.
|
||||
*
|
||||
* @param interval The interval between printing the statistics.
|
||||
*/
|
||||
#define FLOATING_POINT_EMULATION_TRACER_ENTRY(interval) \
|
||||
static ttracer tracer; \
|
||||
ttracer::tprint print((++tracer.run % interval) == 0 ? &tracer : NULL)
|
||||
|
||||
/**
|
||||
* A trace count point.
|
||||
*
|
||||
* When this macro is reached the counter for this marker is increased.
|
||||
*
|
||||
* @param marker A string with the name of the marker.
|
||||
*/
|
||||
#define FLOATING_POINT_EMULATION_TRACER_COUNT(marker) \
|
||||
do { \
|
||||
++tracer.counters[std::make_pair(__LINE__, marker)]; \
|
||||
} while(0)
|
||||
#include "tracer.hpp"
|
||||
#define FLOATING_POINT_EMULATION_TRACER_ENTRY(interval) TRACER_ENTRY(interval)
|
||||
#define FLOATING_POINT_EMULATION_TRACER_COUNT(marker) TRACER_COUNT(marker)
|
||||
#else
|
||||
#define FLOATING_POINT_EMULATION_TRACER_ENTRY(interval) \
|
||||
do { \
|
||||
|
|
65
src/tracer.cpp
Normal file
65
src/tracer.cpp
Normal file
|
@ -0,0 +1,65 @@
|
|||
/* $Id$ */
|
||||
/*
|
||||
Copyright (C) 2012 by Mark de Wever <koraq@xs4all.nl>
|
||||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#include "tracer.hpp"
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
ttracer::tprint::tprint(const ttracer* const tracer__)
|
||||
: tracer(tracer__)
|
||||
{
|
||||
}
|
||||
|
||||
ttracer::tprint::~tprint()
|
||||
{
|
||||
if(!tracer) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::cerr << "Run statistics:\n"
|
||||
<< "Runs:\t" << std::dec << tracer->run << "\n";
|
||||
|
||||
typedef std::pair<std::pair<int, std::string>, int> thack;
|
||||
size_t maximum_length = 0;
|
||||
BOOST_FOREACH(const thack& counter, tracer->counters) {
|
||||
maximum_length = std::max(
|
||||
maximum_length
|
||||
, counter.first.second.length());
|
||||
}
|
||||
|
||||
std::ios_base::fmtflags original_flag = std::cerr.setf(
|
||||
std::ios_base::left
|
||||
, std::ios_base::adjustfield);
|
||||
|
||||
BOOST_FOREACH(const thack& counter, tracer->counters) {
|
||||
std::cerr << "Marker: "
|
||||
<< std::left
|
||||
<< std::setw(maximum_length) << counter.first.second
|
||||
<< std::right
|
||||
<< " [" << std::setw(5) << counter.first.first << ']'
|
||||
<< " hits " << counter.second << "\n";
|
||||
}
|
||||
|
||||
std::cerr.setf(original_flag, std::ios_base::adjustfield);
|
||||
}
|
||||
|
||||
ttracer::ttracer()
|
||||
: run(0)
|
||||
, counters()
|
||||
{
|
||||
}
|
92
src/tracer.hpp
Normal file
92
src/tracer.hpp
Normal file
|
@ -0,0 +1,92 @@
|
|||
/* $Id$ */
|
||||
/*
|
||||
Copyright (C) 2012 by Mark de Wever <koraq@xs4all.nl>
|
||||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains code for tracing the code.
|
||||
*/
|
||||
|
||||
#ifndef TRACER_HPP_INCLUDED
|
||||
#define TRACER_HPP_INCLUDED
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
/** Helper structure for gathering the tracing statistics. */
|
||||
struct ttracer
|
||||
{
|
||||
/**
|
||||
* Helper structure to print the tracing statistics.
|
||||
*
|
||||
* When the constructor gets a valid @ref ttracer pointer it prints the
|
||||
* tracing statistics in its destructor. This allows the structure to be
|
||||
* initialised at the beginning of a scope and print the statistics once
|
||||
* the scope is left. (This makes it easier to write the tracing macros.)
|
||||
*/
|
||||
struct tprint
|
||||
{
|
||||
explicit tprint(const ttracer* const tracer__);
|
||||
|
||||
~tprint();
|
||||
|
||||
/** The tracer, whose statistics to print. */
|
||||
const ttracer* const tracer;
|
||||
};
|
||||
|
||||
ttracer();
|
||||
|
||||
/** The total number of runs. */
|
||||
int run;
|
||||
|
||||
/**
|
||||
* The tracer counters.
|
||||
*
|
||||
* The first pair contains a line number and a name of the marker.
|
||||
* This has two advantages:
|
||||
* * A line number is always unique, thus if using markers with the same
|
||||
* name, they are not the same marker.
|
||||
* * The markers are sorted in order of appearance and not in order of
|
||||
* their names.
|
||||
*
|
||||
* The second pair contains the number of times a marker is hit.
|
||||
*/
|
||||
std::map<std::pair<int, std::string>, int> counters;
|
||||
};
|
||||
|
||||
/**
|
||||
* The beginning of a traced scope.
|
||||
*
|
||||
* It is not intended that tracer scopes are nested, but it should work at the
|
||||
* moment.
|
||||
*
|
||||
* @param interval The interval between printing the statistics.
|
||||
*/
|
||||
#define TRACER_ENTRY(interval) \
|
||||
static ttracer tracer; \
|
||||
ttracer::tprint print((++tracer.run % interval) == 0 ? &tracer : NULL)
|
||||
|
||||
/**
|
||||
* A trace count point.
|
||||
*
|
||||
* When this macro is reached the counter for this marker is increased.
|
||||
*
|
||||
* @param marker A string with the name of the marker.
|
||||
*/
|
||||
#define TRACER_COUNT(marker) \
|
||||
do { \
|
||||
++tracer.counters[std::make_pair(__LINE__, marker)]; \
|
||||
} while(0)
|
||||
|
||||
#endif
|
Loading…
Add table
Reference in a new issue