@@ -267,6 +267,19 @@ app.use(sseMiddleware)
267267
268268setupStream ( app )
269269
270+ const logStream = isEnvTrue ( "LOG_FILE" ) ? fs . createWriteStream ( "log.txt" , { flags : "a" } ) : null
271+
272+ let logFileT0 = new Date ( ) . getTime ( )
273+
274+ function logFile ( content ) {
275+ if ( logStream ) {
276+ let time = new Date ( ) . getTime ( )
277+ let elapsed = time - logFileT0
278+ logFileT0 = time
279+ logStream . write ( `${ elapsed } > ${ content } \n` )
280+ }
281+ }
282+
270283function logPage ( content ) {
271284 if ( disableLogs ) {
272285 return
@@ -511,7 +524,13 @@ function requestBook(state){
511524 } )
512525}
513526
527+ function getUseBook ( moves ) {
528+ return ( useBook || useMongoBook || usePolyglot || useSolution ) && ( moves . length <= bookDepth )
529+ }
530+
514531async function makeMove ( gameId , state , moves , analyzejob , actualengine ) {
532+ logFile ( `makeMove ${ gameId } ${ state } ${ moves } ${ analyzejob } ${ actualengine } ` )
533+
515534 if ( state . realtime && playingGameId && ( gameId != playingGameId ) ) {
516535 logPage ( `refused to make move for real time game ${ gameId } ( already playing real time : ${ playingGameId } )` )
517536
@@ -549,7 +568,7 @@ async function makeMove(gameId, state, moves, analyzejob, actualengine){
549568 let bookalgeb = null
550569 let bookSource = null
551570
552- if ( ( useBook || useMongoBook || usePolyglot || useSolution ) && ( moves . length <= bookDepth ) ) {
571+ if ( getUseBook ( moves ) ) {
553572 let blob = await requestBook ( state )
554573
555574 if ( blob ) {
@@ -588,6 +607,8 @@ async function makeMove(gameId, state, moves, analyzejob, actualengine){
588607 }
589608
590609 if ( ! enginePromise ) {
610+ logFile ( `making engine move` )
611+
591612 let doPonder = ( ( state . botTime > ( 15 * SECOND ) ) || isEnvTrue ( "ALLOW_LATE_PONDER" ) ) && allowPonder
592613
593614 if ( isEnvTrue ( 'REDUCE_LATE_TIME' ) ) {
@@ -619,6 +640,8 @@ async function makeMove(gameId, state, moves, analyzejob, actualengine){
619640 } else {
620641 if ( state . realtime ) engine . stop ( )
621642 }
643+
644+ logFile ( `launching search` )
622645
623646 enginePromise . then ( result => {
624647 let bestmove = result . bestmove
@@ -635,9 +658,12 @@ async function makeMove(gameId, state, moves, analyzejob, actualengine){
635658
636659 logPage ( logMsg )
637660
661+ logFile ( `bestmove ${ bestmove } ponder ${ ponder } score ${ score } ` )
662+
638663 lichessUtils . postApi ( {
639664 url : lichessUtils . makeBotMoveUrl ( gameId , bestmove ) , log : logApi , token : process . env . TOKEN ,
640665 callback : content => {
666+ logFile ( `make move response ${ content } ` )
641667 if ( logApi ) logPage ( `move ack: ${ content } ` )
642668 if ( content . match ( / e r r o r / ) ) {
643669 if ( state . realtime && playingGameId && ( gameId == playingGameId ) ) {
@@ -681,6 +707,8 @@ function playGame(gameId){
681707 playGame ( gameId )
682708 }
683709 } , callback : async function ( blob ) {
710+ logFile ( `game event ${ JSON . stringify ( blob ) } ` )
711+
684712 if ( ! playgame ) return
685713
686714 lastPlayedAt = new Date ( ) . getTime ( )
@@ -806,18 +834,26 @@ function playGame(gameId){
806834 state . realtime = realtime
807835
808836 if ( state . moves ) {
837+ logFile ( `setting up fen from moves` )
838+
809839 moves = state . moves . split ( " " )
810840 state . movesArray = moves
811841
812- if ( useScalachess && ( state . variant != "standard" ) ) {
813- let result = makeUciMoves ( state . variant , state . initialFen , moves )
814- state . fen = result . fen
815- } else {
816- if ( useScalachess ) logPage ( `switched to chess.js for variant standard` )
817- const chess = new Chess ( )
818- for ( let move of moves ) chess . move ( move , { sloppy :true } )
819- state . fen = chess . fen ( )
820- }
842+ if ( getUseBook ( moves ) ) {
843+ if ( useScalachess && ( state . variant != "standard" ) ) {
844+ let result = makeUciMoves ( state . variant , state . initialFen , moves )
845+ state . fen = result . fen
846+ } else {
847+ if ( useScalachess ) logPage ( `switched to chess.js for variant standard` )
848+ const chess = new Chess ( )
849+ for ( let move of moves ) chess . move ( move , { sloppy :true } )
850+ state . fen = chess . fen ( )
851+ }
852+
853+ logFile ( `setting up fen from moves done` )
854+ } else {
855+ logFile ( `skipping setting up fen` )
856+ }
821857 }
822858
823859 if ( abortGameTimeout && ( ( botWhite && ( state . movesArray . length > 1 ) ) || ( ( ! botWhite ) && ( state . movesArray . length > 0 ) ) ) ) {
@@ -1108,6 +1144,20 @@ app.get('/chr', (req, res) => {
11081144 }
11091145} )
11101146
1147+ app . get ( '/logfile' , ( req , res ) => {
1148+ if ( logStream ) {
1149+ let lines = fs . readFileSync ( "log.txt" ) . toString ( ) . split ( "\n" ) . reverse ( )
1150+
1151+ if ( lines . length > 10000 ) {
1152+ lines = lines . slice ( 0 , 10000 )
1153+ }
1154+
1155+ res . send ( lines . join ( `<br>\n` ) )
1156+ } else {
1157+ res . send ( `log file disabled` )
1158+ }
1159+ } )
1160+
11111161app . get ( '/docs' , ( req , res ) => {
11121162 if ( ! docs ) {
11131163 res . send ( `page not ready, try again a little later` )
0 commit comments