Logging improvements to allow prettier displays in the server GUI.
[dcpomatic.git] / src / lib / timer.h
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3     Copyright (C) 2000-2007 Paul Davis
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
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 /** @file src/timer.h
22  *  @brief Some timing classes for debugging and profiling.
23  */
24
25 #ifndef DCPOMATIC_TIMER_H
26 #define DCPOMATIC_TIMER_H
27
28 #include <sys/time.h>
29 #include <string>
30 #include <map>
31
32 /** @class PeriodTimer
33  *  @brief A class to allow timing of a period within the caller.
34  *
35  *  On destruction, it will output the time since its construction.
36  */
37 class PeriodTimer
38 {
39 public:
40         PeriodTimer (std::string n);
41         ~PeriodTimer ();
42
43 private:
44
45         /** name to use when giving output */
46         std::string _name;
47         /** time that this class was constructed */
48         struct timeval _start;
49 };
50
51 /** @class StateTimer
52  *  @brief A class to allow measurement of the amount of time a program
53  *  spends in one of a set of states.
54  *
55  *  Once constructed, the caller can call set_state() whenever
56  *  its state changes.  When StateTimer is destroyed, it will
57  *  output (to cout) a summary of the time spent in each state.
58  */
59 class StateTimer
60 {
61 public:
62         StateTimer (std::string n, std::string s);
63         ~StateTimer ();
64
65         void set_state (std::string s);
66
67 private:
68         /** name to add to the output */
69         std::string _name;
70         /** current state */
71         std::string _state;
72         /** time that _state was entered */
73         double _time;
74         /** time that has been spent in each state so far */
75         std::map<std::string, double> _totals;
76 };
77
78 #endif