00001
00002
00003 #include "osl/checkmate/checkmateRecorder.h"
00004 #include "osl/checkmate/checkHashRecord.h"
00005 #include "osl/checkmate/checkMoveList.h"
00006 #include "osl/checkmate/checkStack.h"
00007 #include "osl/state/simpleState.h"
00008 #include "osl/record/csa.h"
00009 #include <string>
00010 #include <iostream>
00011 #include <fstream>
00012 #include <signal.h>
00013
00015 int osl::checkmate::CheckmateRecorder::DepthTracer::maxVerboseLogDepth=1;
00016 int osl::checkmate::CheckmateRecorder::DepthTracer::maxLogDepth=1;
00018 int osl::checkmate::CheckmateRecorder::DepthTracer::depth=0;
00019 osl::checkmate::CheckMove *osl::checkmate::CheckmateRecorder::nextMove;
00020 const char * osl::checkmate::CheckmateRecorder::leaveReason=0;
00021 const osl::SimpleState* osl::checkmate::CheckmateRecorder::currentState=0;
00022
00023 std::ostream& osl::checkmate::CheckmateRecorder::DepthTracer::stream()
00024 {
00025 static std::ofstream nullStream("/dev/null");
00026 if (DepthTracer::maxVerboseLogDepth <= depth)
00027 return nullStream;
00028 return CheckmateRecorder::stream();
00029 }
00030
00031 namespace osl
00032 {
00033 namespace checkmate
00034 {
00035 static CheckStack check_stack;
00036 #ifdef CHECKMATE_DEBUG
00037 void print_check_stack(int)
00038 {
00039 printf("oops\n");
00040 printf("stack size %ld\n", (long int)check_stack.size());
00041 CheckmateRecorder::dumpStack();
00042 }
00043 class CheckSignalInitializer
00044 {
00045 public:
00046 CheckSignalInitializer()
00047 {
00048 signal(SIGINT, print_check_stack);
00049 }
00050 } initializer;
00051 #endif
00052 }
00053 }
00054
00055 namespace
00056 {
00057 std::ostream& makeOutlineStream()
00058 {
00059 static std::ofstream os("checkmate.log");
00060 os << ";;; -*-mode:outline-*-\n"
00061 << "* root\n";
00062 return os;
00063 }
00064 std::string makeHeader(int depth)
00065 {
00066 assert(depth >= 0);
00067 std::string header(depth+1, '*');
00068 header += ' ';
00069 return header;
00070 }
00071 }
00072
00073 std::ostream& osl::checkmate::CheckmateRecorder::stream()
00074 {
00075 static std::ostream& os = makeOutlineStream();
00076 return os;
00077 }
00078
00079
00080 void osl::checkmate::CheckmateRecorder::dumpStack()
00081 {
00082 if (currentState)
00083 std::cerr << *currentState << "\n";
00084 std::cerr << check_stack;
00085 if (! check_stack.empty())
00086 check_stack[check_stack.size()-1].record->dump(std::cerr, 3);
00087 }
00088
00089 void osl::checkmate::CheckmateRecorder::
00090 dumpRecord(const SimpleState& state,
00091 const CheckHashRecord *record,
00092 unsigned int proofLimit,
00093 unsigned int disProofLimit,
00094 unsigned int currentProofNumber,
00095 unsigned int currentDisProofNumber)
00096 {
00097 stream() << "CheckmateRecorder::dumpRecord\n";
00098 stream() << "proofLimit " << proofLimit << "\n"
00099 << "disProofLimit " << disProofLimit << "\n"
00100 << "cur proofNumber " << currentProofNumber << "\n"
00101 << "cur disProofNumber " << currentDisProofNumber << "\n";
00102 stream() << state;
00103 stream() << record << "\n";
00104 if (record)
00105 {
00106 record->dump(stream(), 1);
00107 }
00108 stream() << std::flush;
00109 }
00110
00111 void osl::checkmate::CheckmateRecorder::writeRootLog(
00112 #ifdef CHECKMATE_DEBUG
00113 const char *msg, unsigned int tableSize, unsigned int continuousNoExpandLoop
00114 #else
00115 const char *, unsigned int, unsigned int
00116 #endif
00117 )
00118 {
00119 #ifdef CHECKMATE_DEBUG
00120 if (DepthTracer::maxVerboseLogDepth <= 1)
00121 return;
00122 stream() << msg << " table " << tableSize
00123 << " continuousNoExpandLoop "<< continuousNoExpandLoop << "\n"
00124 << std::flush;
00125 #endif
00126 }
00127
00128 void osl::checkmate::CheckmateRecorder::stat(const char *msg, Player P,
00129 unsigned int tableSize,
00130 unsigned int totalNodeCount,
00131 unsigned int totalNodeLimit)
00132 {
00133 std::cerr << msg << " (" << P << ") "
00134 << tableSize << "/"
00135 << totalNodeCount << "/" << totalNodeLimit << std::endl;
00136 }
00137
00138 void osl::checkmate::
00139 CheckmateRecorder::enter(int depth, const char *name,
00140 CheckHashRecord *record,
00141 const HashKey& key, const PathEncoding& path,
00142 unsigned int proofLimit, unsigned int disProofLimit)
00143 {
00144 #ifndef OSL_SMP
00145 check_assert(depth >= 0);
00146 check_stack.push_back(CheckStackEntry(nextMove, name, record, key, path,
00147 proofLimit, disProofLimit));
00148
00149 if (DepthTracer::maxVerboseLogDepth <= depth)
00150 return;
00151
00152 stream() << makeHeader(depth) << "==> " << name << " ";
00153 if (nextMove)
00154 {
00155 csaShow(stream(), nextMove->move);
00156 if (nextMove->record != record)
00157 {
00158 std::cerr << "nextMove->record " << nextMove->record
00159 << " != record " << record << " " << nextMove->move << "\n";
00160 stream() << "nextMove->record " << nextMove->record
00161 << " != record " << record << " " << nextMove->move << "\n";
00162 }
00163 }
00164 else
00165 stream() << "root";
00166 stream() << " limit(" << proofLimit << "," << disProofLimit << ")";
00167 if (record)
00168 {
00169 stream() << " depth " << record->distance
00170 << " " << record->proofDisproof()
00171 << " #moves " << record->moves.size();
00172 #if 0
00173
00174 if (! record->interposeMoves.empty())
00175 stream() << " i " << record->interposeMoves.size();
00176 if (! record->noPromoteMoves.empty())
00177 stream() << " np " << record->noPromoteMoves.size();
00178 if (! record->upwardMoves.empty())
00179 stream() << " up " << record->upwardMoves.size();
00180 #endif
00181 if (record->isConfluence)
00182 stream() << " (c)";
00183 if (record->useMaxInsteadOfSum)
00184 stream() << " (max)";
00185 }
00186 stream() << ' ' << record << ' ' << path << "\n" << std::flush;
00187 #ifdef CHECKMATE_DEBUG
00188 # if 0
00189 if (record)
00190 record->dump(stream(), 1);
00191 # endif
00192 #endif
00193
00194 #endif
00195 }
00196
00197 void osl::checkmate::CheckmateRecorder::leave(int depth, const char *name,
00198 const CheckHashRecord *record)
00199 {
00200 #ifndef OSL_SMP
00201 check_assert(depth >= 0);
00202 check_stack.pop_back();
00203
00204 if (DepthTracer::maxVerboseLogDepth <= depth)
00205 return;
00206
00207 stream() << makeHeader(depth) << "<== " << name << " "
00208 << "(" << record->distance << ") "
00209 << record->proofDisproof();
00210 if (leaveReason)
00211 {
00212 stream() << " " << leaveReason;
00213 leaveReason = 0;
00214 }
00215 stream() << " " << record << "\n" << std::flush;
00216 #endif
00217 }
00218
00219 void osl::checkmate::
00220 checkAbort(const char *func, const char *file, int line, const char *exp)
00221 {
00222 std::cerr << "check_assert failed: (" << exp << "), function " << func
00223 << ", file " << file << ", line " << line << ".\n";
00224 CheckmateRecorder::dumpStack();
00225 }
00226
00227
00228
00229
00230
00231