Skip to content

Commit

Permalink
Refactor Logger
Browse files Browse the repository at this point in the history
Bench: 3070816
  • Loading branch information
SzilBalazs committed Jul 30, 2023
1 parent 4e852f8 commit 1104e64
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 109 deletions.
10 changes: 1 addition & 9 deletions src/core/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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;

Expand All @@ -451,8 +445,6 @@ namespace core {

states.clear();
states.emplace_back();

logger.info("Board::board_clear", "Board has been cleared");
}
};

Expand Down
11 changes: 0 additions & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand All @@ -66,6 +56,5 @@ int main(int argc, char *argv[]) {
protocol.start();
}

logger.info("main", "Exiting with return code 0");
return 0;
}
2 changes: 1 addition & 1 deletion src/network/data_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/network/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -61,14 +61,14 @@ namespace nn {
file.read(reinterpret_cast<char *>(&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() {
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/network/nnue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
4 changes: 2 additions & 2 deletions src/network/train.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -229,7 +229,7 @@ namespace nn {
}
file.close();

logger.print("Found", entry_count, "positions");
Logger("Found", entry_count, "positions");
}
};
} // namespace nn
4 changes: 2 additions & 2 deletions src/search/search_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/selfplay/selfplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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) {
Expand All @@ -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);
}

Expand Down Expand Up @@ -176,7 +176,7 @@ namespace selfplay {

std::vector<std::thread> threads;

logger.print("Starting", thread_count, "threads...");
Logger("Starting", thread_count, "threads...");
for (unsigned int id = 0; id < thread_count; id++) {
std::vector<std::string> fens;
for (unsigned int i = id; i < starting_fens.size(); i += thread_count) {
Expand Down Expand Up @@ -225,7 +225,7 @@ namespace selfplay {
th.join();
}

logger.print("Finished generating data");
Logger("Finished generating data");

combine_data(directory_path, output_path);
}
Expand Down
22 changes: 7 additions & 15 deletions src/uci/uci.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -129,7 +129,7 @@ namespace uci {
commands.emplace_back("perft", [&](context tokens) {
int depth = find_element<int>(tokens, "perft").value_or(5);
U64 node_count = test::perft<true, false>(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);
Expand All @@ -155,8 +155,6 @@ namespace uci {
}
}
});

logger.info("UCI::register_commands", "Registered ", commands.size(), "commands");
}

void UCI::register_options() {
Expand All @@ -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);
Expand All @@ -192,8 +188,6 @@ namespace uci {
break;
}

logger.info("UCI::start", "in>", line);

std::vector<std::string> tokens = convert_to_tokens(line);

for (const Command &cmd : commands) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -273,7 +266,6 @@ namespace uci {
return opt.get_value<T>();
}
}
logger.error("UCI::get_option", "Unable to find option", name);
throw std::invalid_argument("UCI::get_option(): unable to find option " + name);
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
65 changes: 11 additions & 54 deletions src/utils/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,27 @@

#pragma once

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

// #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<typename... Args>
explicit Logger(Args... args) {
print(args...);
std::cout << ss.str() << std::flush;
}

template<typename T, typename... Args>
void print(T a, Args... args) {
std::cout << a << " ";
ss << a << " ";
print(args...);
}

void print() {
std::cout << std::endl;
ss << "\n";
}

template<typename T, typename... Args>
void info(T a, Args... args) {
#ifdef LOGGING
file << "[INFO] " << a << "(): ";
log(args...);
#endif
}

template<typename T, typename... Args>
void error(T a, Args... args) {
#ifdef LOGGING
file << "[INFO] " << a << "(): ";
log(args...);
#endif
}

template<typename T, typename... Args>
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();
}
Loading

0 comments on commit 1104e64

Please sign in to comment.