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