OpenTTD
debug.h
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD 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, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #ifndef DEBUG_H
13 #define DEBUG_H
14 
15 #include "cpu.h"
16 #include <chrono>
17 
18 /* Debugging messages policy:
19  * These should be the severities used for direct DEBUG() calls
20  * maximum debugging level should be 10 if really deep, deep
21  * debugging is needed.
22  * (there is room for exceptions, but you have to have a good cause):
23  * 0 - errors or severe warnings
24  * 1 - other non-fatal, non-severe warnings
25  * 2 - crude progress indicator of functionality
26  * 3 - important debugging messages (function entry)
27  * 4 - debugging messages (crude loop status, etc.)
28  * 5 - detailed debugging information
29  * 6.. - extremely detailed spamming
30  */
31 
37 #define DEBUG(name, level, ...) if ((level) == 0 || _debug_ ## name ## _level >= (level)) debug(#name, __VA_ARGS__)
38 
39 extern int _debug_driver_level;
40 extern int _debug_grf_level;
41 extern int _debug_map_level;
42 extern int _debug_misc_level;
43 extern int _debug_net_level;
44 extern int _debug_sprite_level;
45 extern int _debug_oldloader_level;
46 extern int _debug_npf_level;
47 extern int _debug_yapf_level;
48 extern int _debug_freetype_level;
49 extern int _debug_script_level;
50 extern int _debug_sl_level;
51 extern int _debug_gamelog_level;
52 extern int _debug_desync_level;
53 extern int _debug_console_level;
54 #ifdef RANDOM_DEBUG
55 extern int _debug_random_level;
56 #endif
57 
58 void CDECL debug(const char *dbg, const char *format, ...) WARN_FORMAT(2, 3);
59 
60 char *DumpDebugFacilityNames(char *buf, char *last);
61 void SetDebugString(const char *s);
62 const char *GetDebugString();
63 
64 /* Shorter form for passing filename and linenumber */
65 #define FILE_LINE __FILE__, __LINE__
66 
67 /* Used for profiling
68  *
69  * Usage:
70  * TIC();
71  * --Do your code--
72  * TOC("A name", 1);
73  *
74  * When you run the TIC() / TOC() multiple times, you can increase the '1'
75  * to only display average stats every N values. Some things to know:
76  *
77  * for (int i = 0; i < 5; i++) {
78  * TIC();
79  * --Do your code--
80  * TOC("A name", 5);
81  * }
82  *
83  * Is the correct usage for multiple TIC() / TOC() calls.
84  *
85  * TIC() / TOC() creates its own block, so make sure not the mangle
86  * it with another block.
87  *
88  * The output is counted in CPU cycles, and not comparable across
89  * machines. Mainly useful for local optimisations.
90  **/
91 #define TIC() {\
92  uint64 _xxx_ = ottd_rdtsc();\
93  static uint64 _sum_ = 0;\
94  static uint32 _i_ = 0;
95 
96 #define TOC(str, count)\
97  _sum_ += ottd_rdtsc() - _xxx_;\
98  if (++_i_ == count) {\
99  DEBUG(misc, 0, "[%s] " OTTD_PRINTF64 " [avg: %.1f]", str, _sum_, _sum_/(double)_i_);\
100  _i_ = 0;\
101  _sum_ = 0;\
102  }\
103 }
104 
105 /* Chrono based version. The output is in microseconds. */
106 #define TICC() {\
107  auto _start_ = std::chrono::high_resolution_clock::now();\
108  static uint64 _sum_ = 0;\
109  static uint32 _i_ = 0;
110 
111 #define TOCC(str, _count_)\
112  _sum_ += (std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - _start_)).count();\
113  if (++_i_ == _count_) {\
114  DEBUG(misc, 0, "[%s] " OTTD_PRINTF64 " us [avg: %.1f us]", str, _sum_, _sum_/(double)_i_);\
115  _i_ = 0;\
116  _sum_ = 0;\
117  }\
118 }
119 
120 
121 void ShowInfo(const char *str);
122 void CDECL ShowInfoF(const char *str, ...) WARN_FORMAT(1, 2);
123 
124 const char *GetLogPrefix();
125 
127 extern uint32 _realtime_tick;
128 
129 #endif /* DEBUG_H */
void CDECL const char * GetLogPrefix()
Get the prefix for logs; if show_date_in_logs is enabled it returns the date, otherwise it returns no...
Definition: debug.cpp:251
void CDECL debug(const char *dbg, const char *format,...)
Output a debug line.
Definition: debug.cpp:156
void CDECL ShowInfoF(const char *str,...)
Shows some information on the console/a popup box depending on the OS.
Definition: openttd.cpp:136
const char * GetDebugString()
Print out the current debug-level.
Definition: debug.cpp:228
Functions related to CPU specific instructions.
void SetDebugString(const char *s)
Set debugging levels by parsing the text in s.
Definition: debug.cpp:174
void CDECL char * DumpDebugFacilityNames(char *buf, char *last)
Dump the available debug facility names in the help text.
Definition: debug.cpp:86
uint32 _realtime_tick
The real time in the game.
Definition: debug.cpp:50