diff --git a/src/core/board.h b/src/core/board.h index bd865d5..af6ad6e 100644 --- a/src/core/board.h +++ b/src/core/board.h @@ -271,8 +271,6 @@ namespace core { void load(const std::string &fen) { board_clear(); - logger.info("Board::load", "Loading fen", fen); - std::stringstream ss(fen); std::string pieces, stm, rights, ep, move50; ss >> pieces >> stm >> rights >> ep >> move50; @@ -295,7 +293,7 @@ namespace core { state.stm = BLACK; state.hash.xor_stm(); } else { - logger.error("Board::load", "Invalid stm string!"); + throw std::runtime_error("Invalid fen string: " + fen); } state.rights = CastlingRights(rights); @@ -310,8 +308,6 @@ namespace core { if (state.ep != NULL_SQUARE) { state.hash.xor_ep(state.ep); } - - logger.info("Board::load", "Finished loading fen"); } [[nodiscard]] std::string get_fen() const { @@ -440,8 +436,6 @@ namespace core { void board_clear() { - logger.info("Board::board_clear", "Clearing board..."); - for (Bitboard &i : bb_pieces) i = 0; for (Bitboard &i : bb_colors) i = 0; @@ -451,8 +445,6 @@ namespace core { states.clear(); states.emplace_back(); - - logger.info("Board::board_clear", "Board has been cleared"); } }; diff --git a/src/main.cpp b/src/main.cpp index 3808495..27fe480 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,20 +32,10 @@ namespace search { Depth lmr_reductions[200][MAX_PLY + 1]; } -Logger logger; - void init_all() { - logger.init("log.txt"); - logger.info("init_all", "Logger has been initialized"); - core::init_masks(); - logger.info("init_all", "Bitboard masks have been initialized"); - core::init_magic(); - logger.info("init_all", "Magic has been initialized"); - search::init_lmr(); - logger.info("init_all", "LMR has been initialized"); } int main(int argc, char *argv[]) { @@ -66,6 +56,5 @@ int main(int argc, char *argv[]) { protocol.start(); } - logger.info("main", "Exiting with return code 0"); return 0; } \ No newline at end of file diff --git a/src/network/data_parser.h b/src/network/data_parser.h index 2b57976..645945d 100644 --- a/src/network/data_parser.h +++ b/src/network/data_parser.h @@ -42,7 +42,7 @@ namespace nn { file.open(path, std::ios::in); if (!file.is_open()) { - logger.print("Unable to open:", path); + Logger("Unable to open:", path); throw std::runtime_error("Unable to open: " + path); } } diff --git a/src/network/network.h b/src/network/network.h index b3e3f81..dc30719 100644 --- a/src/network/network.h +++ b/src/network/network.h @@ -52,7 +52,7 @@ namespace nn { Network(const std::string &network_path) { std::ifstream file(network_path, std::ios::in | std::ios::binary); if (!file.is_open()) { - logger.print("Unable to open: ", network_path); + Logger("Unable to open: ", network_path); randomize(); return; } @@ -61,14 +61,14 @@ namespace nn { file.read(reinterpret_cast(&magic), sizeof(magic)); if (magic != MAGIC) { - logger.print("Invalid network file: ", network_path, " with magic: ", magic); + Logger("Invalid network file: ", network_path, " with magic: ", magic); throw std::invalid_argument("Invalid network file with magic: " + std::to_string(magic)); } l0.load_from_file(file); l1.load_from_file(file); - logger.print("Loaded network file: ", network_path); + Logger("Loaded network file: ", network_path); } Network() { @@ -90,7 +90,7 @@ namespace nn { void write_to_file(const std::string &output_path) { std::ofstream file(output_path, std::ios::out | std::ios::binary); if (!file.is_open()) { - logger.print("Unable to open:", output_path); + Logger("Unable to open:", output_path); throw std::runtime_error("Unable to open: " + output_path); } @@ -107,7 +107,7 @@ namespace nn { void quantize(const std::string &output_path) { std::ofstream file(output_path, std::ios::out | std::ios::binary); if (!file.is_open()) { - logger.print("Unable to open:", output_path); + Logger("Unable to open:", output_path); throw std::runtime_error("Unable to open: " + output_path); } diff --git a/src/network/nnue.h b/src/network/nnue.h index dfb0b13..45fd3e6 100644 --- a/src/network/nnue.h +++ b/src/network/nnue.h @@ -43,7 +43,7 @@ namespace nn { int offset = sizeof(int); if (magic != MAGIC) { - logger.print("Invalid default network file with magic", magic); + Logger("Invalid default network file with magic", magic); throw std::invalid_argument("Invalid default network file with magic" + std::to_string(magic)); } diff --git a/src/network/train.h b/src/network/train.h index c187fd6..2ee9f37 100644 --- a/src/network/train.h +++ b/src/network/train.h @@ -218,7 +218,7 @@ namespace nn { } void index_training_data(const std::string &training_data) { - logger.print("Indexing training data..."); + Logger("Indexing training data..."); entry_count = 0; @@ -229,7 +229,7 @@ namespace nn { } file.close(); - logger.print("Found", entry_count, "positions"); + Logger("Found", entry_count, "positions"); } }; } // namespace nn \ No newline at end of file diff --git a/src/search/search_thread.h b/src/search/search_thread.h index 742d84a..f8bb805 100644 --- a/src/search/search_thread.h +++ b/src/search/search_thread.h @@ -128,7 +128,7 @@ namespace search { if (shared.uci_mode) { int64_t elapsed_time = shared.tm.get_elapsed_time(); - logger.print("info", "depth", int(depth), "seldepth", int(max_ply), + Logger("info", "depth", int(depth), "seldepth", int(max_ply), "nodes", shared.node_count, "score", score_to_string(score), "time", elapsed_time, "nps", calculate_nps(elapsed_time, shared.node_count), @@ -140,7 +140,7 @@ namespace search { if (id == 0) { shared.is_searching = false; if (shared.uci_mode) { - logger.print("bestmove", shared.best_move); + Logger("bestmove", shared.best_move); } } } diff --git a/src/selfplay/selfplay.h b/src/selfplay/selfplay.h index 3f9917c..2726c0d 100644 --- a/src/selfplay/selfplay.h +++ b/src/selfplay/selfplay.h @@ -123,7 +123,7 @@ namespace selfplay { void combine_data(const std::string &path, const std::string &output_file) { - logger.print("Combining files..."); + Logger("Combining files..."); std::ofstream file(output_file, std::ios::app | std::ios::out); for (const auto &entry : std::filesystem::directory_iterator(path)) { @@ -136,7 +136,7 @@ namespace selfplay { } file.close(); - logger.print("Finished combining"); + Logger("Finished combining"); } void start_generation(const search::Limits &limits, const std::string &book_path, const std::string &output_path, unsigned int thread_count, int dropout) { @@ -145,7 +145,7 @@ namespace selfplay { // TODO argument for network file if (!std::filesystem::exists(book_path)) { - logger.print("Invalid book path: " + book_path); + Logger("Invalid book path: " + book_path); throw std::invalid_argument("Invalid book path: " + book_path); } @@ -176,7 +176,7 @@ namespace selfplay { std::vector threads; - logger.print("Starting", thread_count, "threads..."); + Logger("Starting", thread_count, "threads..."); for (unsigned int id = 0; id < thread_count; id++) { std::vector fens; for (unsigned int i = id; i < starting_fens.size(); i += thread_count) { @@ -225,7 +225,7 @@ namespace selfplay { th.join(); } - logger.print("Finished generating data"); + Logger("Finished generating data"); combine_data(directory_path, output_path); } diff --git a/src/uci/uci.h b/src/uci/uci.h index a025558..51e7baf 100644 --- a/src/uci/uci.h +++ b/src/uci/uci.h @@ -79,7 +79,7 @@ namespace uci { greetings(); }); commands.emplace_back("isready", [&](context tokens) { - logger.print("readyok"); + Logger("readyok"); }); commands.emplace_back("position", [&](context tokens) { parse_position(tokens); @@ -90,7 +90,7 @@ namespace uci { commands.emplace_back("eval", [&](context tokens) { nn::NNUE network{}; network.refresh(board.to_features()); - logger.print("Eval:", network.evaluate(board.get_stm())); + Logger("Eval:", network.evaluate(board.get_stm())); }); commands.emplace_back("gen", [&](context tokens) { search::Limits limits; @@ -129,7 +129,7 @@ namespace uci { commands.emplace_back("perft", [&](context tokens) { int depth = find_element(tokens, "perft").value_or(5); U64 node_count = test::perft(board, depth); - logger.print("Total node count: ", node_count); + Logger("Total node count: ", node_count); }); commands.emplace_back("go", [&](context tokens) { search::Limits limits = parse_limits(tokens); @@ -155,8 +155,6 @@ namespace uci { } } }); - - logger.info("UCI::register_commands", "Registered ", commands.size(), "commands"); } void UCI::register_options() { @@ -182,8 +180,6 @@ namespace uci { board.load(STARTING_FEN); - logger.info("UCI::start", "UCI Loop has started!"); - while (should_continue) { std::string line; getline(std::cin, line); @@ -192,8 +188,6 @@ namespace uci { break; } - logger.info("UCI::start", "in>", line); - std::vector tokens = convert_to_tokens(line); for (const Command &cmd : commands) { @@ -205,12 +199,12 @@ namespace uci { } void UCI::greetings() { - logger.print("id", "name", "WhiteCore", VERSION); - logger.print("id author Balazs Szilagyi"); + Logger("id", "name", "WhiteCore", VERSION); + Logger("id author Balazs Szilagyi"); for (const Option &opt : options) { - logger.print(opt.to_string()); + Logger(opt.to_string()); } - logger.print("uciok"); + Logger("uciok"); } search::Limits UCI::parse_limits(UCI::context tokens) { @@ -246,7 +240,6 @@ namespace uci { for (; idx < tokens.size(); idx++) { core::Move move = move_from_string(board, tokens[idx]); if (move == core::NULL_MOVE) { - logger.error("load_position", "Invalid move", tokens[idx]); break; } else { board.make_move(move); @@ -273,7 +266,6 @@ namespace uci { return opt.get_value(); } } - logger.error("UCI::get_option", "Unable to find option", name); throw std::invalid_argument("UCI::get_option(): unable to find option " + name); } diff --git a/src/utils/bench.h b/src/utils/bench.h index 1b1e053..3cb2e60 100644 --- a/src/utils/bench.h +++ b/src/utils/bench.h @@ -78,5 +78,5 @@ void run_bench() { int64_t end_time = now(); int64_t elapsed_time = end_time - start_time + 1; int64_t nps = calculate_nps(elapsed_time, nodes); - logger.print(nodes, "nodes", nps, "nps"); + Logger(nodes, "nodes", nps, "nps"); } diff --git a/src/utils/logger.h b/src/utils/logger.h index df68cf7..c504fe7 100644 --- a/src/utils/logger.h +++ b/src/utils/logger.h @@ -17,70 +17,27 @@ #pragma once -#include #include -#include +#include -// #define LOGGING - -static void exception_handler(); - -struct Logger { - - std::ofstream file; - - Logger() = default; - - void init(const std::string &filename) { -#ifdef LOGGING - file.open(filename, std::ios::out); -#endif - std::set_terminate(exception_handler); +class Logger { +public: + template + explicit Logger(Args... args) { + print(args...); + std::cout << ss.str() << std::flush; } template void print(T a, Args... args) { - std::cout << a << " "; + ss << a << " "; print(args...); } void print() { - std::cout << std::endl; + ss << "\n"; } - template - void info(T a, Args... args) { -#ifdef LOGGING - file << "[INFO] " << a << "(): "; - log(args...); -#endif - } - - template - void error(T a, Args... args) { -#ifdef LOGGING - file << "[INFO] " << a << "(): "; - log(args...); -#endif - } - - template - void log(T a, Args... args) { - file << a << " "; - log(args...); - } - - void log() { - file << std::endl; - } +private: + std::stringstream ss; }; - -extern Logger logger; - -static void exception_handler() { - if (logger.file.is_open()) { - logger.error("Logger", "exception_handler was called: closing log file and aborting"); - logger.file.close(); - } - std::abort(); -} diff --git a/src/utils/utilities.h b/src/utils/utilities.h index 1bdc063..85bf8fa 100644 --- a/src/utils/utilities.h +++ b/src/utils/utilities.h @@ -57,7 +57,7 @@ Square square_from_string(const std::string &s) { } else if ('A' <= s[0] && s[0] <= 'Z') { return Square((s[0] - 'A') + (s[1] - '1') * 8); } else { - logger.error("square_from_string", "Invalid string: " + s); + throw std::runtime_error("Invalid square string: " + s); } return NULL_SQUARE; @@ -80,7 +80,7 @@ Piece piece_from_char(char c) { piece.color = WHITE; c += 32; } else { - logger.error("piece_from_char", "Invalid char"); + throw std::runtime_error("Invalid piece char: " + std::string(c, 1)); } switch (c) { @@ -103,7 +103,7 @@ Piece piece_from_char(char c) { piece.type = KING; break; default: - logger.error("piece_from_char", "Invalid char!"); + throw std::runtime_error("Invalid piece char: " + std::string(c, 1)); } return piece; }