Belofte version 2.1.8
A promising chess program using the UCI or Winboard interface
bel_debug.cpp
Go to the documentation of this file.
1/*---------------------------------------------------------------------+
2 * File: bel_debug.cpp
3 * Project: part of belofte - A Promising Chess Program
4 * Author: yves
5 * SPDX-License-Identifier: GPL-2.0-only
6+----------------------------------------------------------------------*/
7
8#include "belofte.h"
9
10//-----------------------------------------------------------------------
11
15
16void bel_debug::execute(std::string const& args)
17{
18 if (args == "on") {
19 App().bout.setLevel(99);
20#if defined(_DEBUG)
21 //App().bout.attach(DEBUGFILE);
22#endif
23 } else if (args == "off") {
24 App().bout.setLevel(0);
25 //App().bout.detach();
26 // TODO: add abstraction level on different debug commands
27 // so that they do not need to be coded here (dynamic binding ?)
28 } else if (args == "show fen") {
29 App().bout << "info string fen " << Game()->getCurrentPosition().getFEN() << "\n";
30 } else if (args == "help") {
31 App().bout << show_help();
32 } else if (args == "gamehistory") {
33 App().bout << show_gamehistory();
34 } else if (args == "fenhistory") {
35 App().bout << show_fenhistory();
36 } else if (args == "poshistory") {
37 App().bout << show_poshistory();
38 } else if (args == "hashhistory") {
39 App().bout << show_hashhistory();
40 } else if (args == "autosave") {
41 App().bout << "not implemented yet \n";
42 } else if (args == "logging") {
43 App().bout << "not implemented yet \n";
44 } else if (args == "ml") {
46 bMoveList& ml = b.getMoveListRef();
47 ml.generateMoves(b);
48 bPgnMoveList pml(b);
49 App().bout << "info string ml " << ml << "\n";
50 App().bout << "info string ml " << pml << "\n";
51 } else if (args == "setrunning") {
52 AppEI()->setRunning(true);
53 } else if (args == "invert") {
55 } else if (args == "log on") {
56 App().sout.attach(App().getName() + ".log");
57 } else if (args == "log off") {
58 App().sout.detach();
59 } else if (args == "verbose 0") {
60 // TODO: move to verbose command
61 App().sout.setLevel(0);
62 } else if (args == "verbose 1") {
63 App().sout.setLevel(1);
64 } else if (args == "verbose 2") {
65 App().sout.setLevel(2);
66 } else if (args == "verbose 3") {
67 App().sout.setLevel(3);
68 } else if (args == "verbose 4") {
69 App().sout.setLevel(4);
70 } else if (args == "verbose all") {
71 App().sout.setLevel(99);
72 } else if (args == "") {
73 App().bout.setLevel(50);
74 } else {
75 AppEI()->sendError("argument invalid", "debug " + args);
76 }
77}
78
79void bel_debug::info() const
80{
81 App().bout << "Info for " << App().getName() << "\n";
82 App().bout << "Version: " MYVERSION << "\n";
83 App().bout << "Built on: " MYRELEASEDATE " for " MYOS " - " MYPLATFORM << "\n";
84 App().bout << "Compiled by: " BELOFTE_COMPILER " with flags";
85#if defined(CHRONO_MISSING)
86 App().bout << " CHRONO_MISSING";
87#endif
88#if defined(BELOFTE_NORANDOM)
89 App().bout << " BELOFTE_NORANDOM";
90#endif
91#if defined(BELOFTE_UCIMODE)
92 App().bout << " BELOFTE_UCIMODE";
93#endif
94#if defined(BELOFTE_NOSIGNALS)
95 App().bout << " BELOFTE_NOSIGNALS";
96#endif
97#if defined(BELOFTE_NOUNICODE)
98 App().bout << " BELOFTE_NOUNICODE";
99#endif
100#if defined(INCOMPLETE_C11)
101 App().bout << " INCOMPLETE_C11";
102#endif
103#if defined(NODIRENT)
104 App().bout << " NODIRENT";
105#endif
106#if defined(_WIN32) || defined(WIN32)
107 App().bout << " _WIN32";
108#endif
109#if defined(_DEBUG)
110 App().bout << " _DEBUG";
111#endif
112 App().bout << "\n";
113
114 App().bout << "Output detail -"
115 " interface: " << belofte::to_string(App().bout.getLevel())
116 << ", search: " << belofte::to_string(App().sout.getLevel())
117 << (App().sout.isAttached() ? ", file: " + App().sout.fn() : "")
118 << "\n";
119
120 App().bout
121 << "Mode: " << AppEI()->operator std::string() << "\n"
122 << "Eval: " << Game()->getEval()->operator std::string()
123 << (App().getConfig("random", 0) ? " +-rnd" : "")
124 << "\n"
125 << "Algo: " << Game()->getAlgorithm()->operator std::string() << "\n"
126 << Game()->getLevel().operator std::string();
127
128 App().bout.endl();
129}
130
132{
133 int64_t nNodes;
134 int64_t nNonLeafNodes;
135 int64_t nDuration;
136
137 search.m_iterativesearch = false;
138
139 Game()->newGame();
141 nNodes = Game()->DoPerft(search, nDepth);
142 nNonLeafNodes = search.getNonLeafNodes();
143 nDuration = search.getDurationMicroSec();
144
145 Game()->setFEN("n1n5/PPPk4/8/8/8/8/4Kppp/5N1N b - - 0 1");
146 nNodes += Game()->DoPerft(search, nDepth);
147 nNonLeafNodes += search.getNonLeafNodes();
148 nDuration += search.getDurationMicroSec();
149
150 std::stringstream ss;
151 ss << "bench " << search.operator std::string()
152 << " Time: " << belofte::prettyTime(nDuration / 1000LL)
153 << " Nodes: " << belofte::to_string(nNodes)
154 << " Total N.: " << belofte::to_string(nNonLeafNodes + nNodes)
155 << " NPS: " << belofte::to_string(static_cast<int64_t>((nNonLeafNodes + nNodes) * 1000000LL / nDuration));
156 App().bout(-1) << ss.str();
157 App().bout.endl();
158}
159
160std::string bel_debug::show_help() const
161{
162 std::stringstream os;
163 os << "debug help - this message" << "\n"
164 << "debug [off|on] - set level 50, 0 or 99" << "\n"
165 << "debug verbose n - set level n (can be 0-5)" << "\n"
166 << "debug verbose all - set level 99" << "\n"
167 << "debug log on - activate logfile" << "\n"
168 << "debug log off - de-activate logfile" << "\n"
169 << "debug show fen - display FEN string of current position" << "\n"
170 << "debug ml - show moves calculated for current position" << "\n"
171 << "debug invert - invert colours" << "\n"
172 << "debug gamehistory - show complete game" << "\n"
173 << "debug fenhistory - show all fen strings of game" << "\n"
174 << "debug poshistory - show positions of game" << "\n"
175 << "debug setrunning - set xboard persistent running state" << "\n";
176
177 return os.str();
178}
179
180std::string bel_debug::show_gamehistory() const
181{
182 std::stringstream os;
183 positions_t& positions = Game()->getPositions();
184
185 for (bBoard& b : positions) {
186 bMoveList& ml = b.getMoveListRef();
187 ml.generateMoves(b);
188 bPgnMoveList pml(b);
189 os << "\n";
190 os << b.getFEN() << "\n"
191 << b << "\n"
192 << ml << "\n"
193 << pml << "\n";
194 }
195
196 return os.str();
197}
198
199std::string bel_debug::show_poshistory() const
200{
201 std::stringstream os;
202 positions_t const& positions = Game()->getPositions();
203
204 for (bBoard const& b : positions) {
205 os << b << "\n";
206 }
207
208 return os.str();
209}
210
211std::string bel_debug::show_fenhistory() const
212{
213 std::stringstream os;
214 positions_t const& positions = Game()->getPositions();
215
216 for (bBoard const& b : positions) {
217 os << b.getFEN() << "\n";
218 }
219
220 return os.str();
221}
222
223std::string bel_debug::show_hashhistory() const
224{
225 std::stringstream os;
226 positions_t const& positions = Game()->getPositions();
227
228 for (bBoard const& b : positions) {
229 os << b.getHashStr() << " : " << b.getFEN() << "\n";
230 }
231
232 return os.str();
233}
234
235// --------------------------------------------------------------------
236/** Outputwriter class will log to debug file, stdout and log file
237 * in this order if they have been initialized before
238 */
239
240#if defined(__GNUC__)
241#pragma GCC diagnostic push
242#pragma GCC diagnostic ignored "-Wunused-parameter"
243#endif
244
246 : m_os{os}
247 , m_filename{""}
248 , m_fos{}
249{
250}
251
252#if defined(__GNUC__)
253#pragma GCC diagnostic pop
254#endif
255
259
260bool outputWriter::attach(std::string const& fn) {
261 m_fos.open(fn.c_str(), std::ios::out | std::ios::trunc );
262 if (m_fos.is_open()) {
263 m_attached = true;
264 m_filename = fn;
265 }
266 return m_fos.is_open();
267}
268
270 if (isAttached()) {
271 m_attached = false;
272 m_fos.close();
273 m_filename = "";
274 }
275}
276
277// eof
appInstance & App()
Definition belofte.cpp:480
engineInterface * AppEI()
Definition belofte.cpp:486
bGame * Game()
Definition belofte.cpp:491
This is the main include file, needs to be included before any other include.
#define MYRELEASEDATE
Definition belofte.h:44
#define MYVERSION
Definition belofte.h:33
int_fast8_t depth_t
Definition belofte.h:112
long long getDurationMicroSec() const
Definition util.cpp:61
outputWriter sout
normal output
Definition belofte.h:331
outputWriter bout
Definition belofte.h:330
int64_t getConfig(std::string const &s, int64_t v)
Definition belofte.cpp:438
std::string getName() const
Definition belofte.cpp:473
bFen getFEN() const
Definition board.cpp:114
board
Definition board.h:147
void invertColours()
invert colours update kingpos, update colour to move, castle rights, ...
Definition board.cpp:764
bMoveList & getMoveListRef()
return reference to movelist
Definition board.cpp:954
bSearchAlgorithm * getAlgorithm() const
bPositionEvaluation * getEval() const
positions_t & getPositions()
Definition game.h:54
void newGame()
Definition game.cpp:28
bLevel & getLevel()
Definition game.h:53
void setFEN(std::string const &fenstr)
Definition game.cpp:54
int64_t DoPerft(bSearchAlgorithm &sa, int const d)
do perft search at depth
Definition game.cpp:126
bBoard & getCurrentPosition()
Definition game.cpp:73
void setFENInitialPos()
Definition game.cpp:46
movenum_t generateMoves(bBoard const &b)
generate moves if not yet generated
Definition movelist.cpp:259
bool m_iterativesearch
Definition search.h:109
int64_t getNonLeafNodes() const
Definition search.h:93
void info() const
Definition bel_debug.cpp:79
static void run_bench(bSearchAlgorithm &search, depth_t const nDepth)
void execute(std::string const &args)
Definition bel_debug.cpp:16
virtual void setRunning(bool const r)
Definition belofte.h:198
virtual void sendError(std::string const &error, std::string const &description)
Definition usercmd.cpp:156
~outputWriter() override
bool isAttached() const
Definition bel_debug.h:52
outputWriter(std::ostream &os)
Outputwriter class will log to debug file, stdout and log file in this order if they have been initia...
std::string const & fn() const
Definition bel_debug.h:53
bool attach(std::string const &fn)
outputWriter & endl()
Definition bel_debug.h:60
void setLevel(int const l)
Definition bel_debug.h:46
std::vector< bBoard > positions_t
Definition game.h:15
#define BELOFTE_COMPILER
Definition myplatform.h:164
#define MYPLATFORM
Definition myplatform.h:73
#define MYOS
Definition myplatform.h:117