Skip to content

Commit

Permalink
Added logging support to sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenewald committed Oct 6, 2024
1 parent 0fd7af6 commit 3de19bb
Show file tree
Hide file tree
Showing 20 changed files with 149 additions and 59 deletions.
56 changes: 53 additions & 3 deletions exchange/src/common/logging/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
# define TZ_FORMAT " %z"
#endif

#define SHORT_LOGLINE_FORMAT "%(logger_name:<8): %(message)"

namespace nutc {
namespace logging {

using namespace quill; // NOLINT(*-using-namespace)
using cc = quill::ConsoleColours;

void
init(quill::LogLevel log_level)
init(const std::string& log_file, quill::LogLevel log_level)
{
detail::application_log_level = log_level;

Expand Down Expand Up @@ -78,8 +80,8 @@ init(quill::LogLevel log_level)
auto handler_cfg = quill::FileHandlerConfig{};
handler_cfg.set_open_mode('w');

const std::string log_file = LOG_FILE;
auto file_handler = quill::file_handler(log_file, handler_cfg);
std::string full_log_path = fmt::format("{}/{}", LOG_DIR, log_file);
auto file_handler = quill::file_handler(full_log_path, handler_cfg);

file_handler->set_pattern(
LOGLINE_FORMAT,
Expand All @@ -106,5 +108,53 @@ init(quill::LogLevel log_level)
LOG_INFO(main_logger, "Logging initialized!");
}

void
init_file_only(
const std::string& log_file, std::uint32_t max_size_bytes, quill::LogLevel log_level
)
{
detail::application_log_level = log_level;

// Create the logs directory
if (!std::filesystem::is_directory(LOG_DIR))
std::filesystem::create_directory(LOG_DIR);

// Create our config object
quill::Config cfg;

// Set main logger name
cfg.default_logger_name = "main";

auto handler_cfg = quill::RotatingFileHandlerConfig{};
handler_cfg.set_open_mode('w');
handler_cfg.set_max_backup_files(0);
handler_cfg.set_overwrite_rolled_files(true);
handler_cfg.set_rotation_max_file_size(max_size_bytes);

std::string full_log_path = fmt::format("{}/{}", LOG_DIR, log_file);
auto file_handler = quill::rotating_file_handler(full_log_path, handler_cfg);

file_handler->set_pattern(SHORT_LOGLINE_FORMAT);
file_handler->set_log_level(log_level);

cfg.default_handlers.emplace_back(file_handler);

// Send the config
quill::configure(cfg);

// Set backtrace and log level on the main logger
quill::Logger* main_logger = quill::get_logger();
main_logger->init_backtrace(LOG_BACKTRACE_SIZE, quill::LogLevel::Error);
main_logger->set_log_level(log_level);

// Set the thread name
set_thread_name("MainThread");

// Start the logging backend thread
quill::start();

LOG_INFO(main_logger, "Logging initialized!");
}

} // namespace logging
} // namespace nutc
9 changes: 7 additions & 2 deletions exchange/src/common/logging/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <string>

#define LOG_DIR "logs"
#define LOG_FILE (LOG_DIR "/app.log")
#define LOG_BACKTRACE_SIZE 10

namespace nutc {
Expand Down Expand Up @@ -52,7 +51,11 @@ set_thread_name(const std::string& name)
/**
* Set up logging.
*/
void init(quill::LogLevel log_level = DEFAULT_LOG_LEVEL);
void init(const std::string& log_file, quill::LogLevel log_level = DEFAULT_LOG_LEVEL);
void init_file_only(
const std::string& log_file, std::uint32_t max_size_bytes,
quill::LogLevel log_level = DEFAULT_LOG_LEVEL
);

/******************************************************************************
* LOGGERS
Expand All @@ -78,6 +81,8 @@ get_main_logger()

// Create loggers here for every category
CREATE_LOG_CATEGORY(sandbox_server);
CREATE_LOG_CATEGORY(algo_print);
CREATE_LOG_CATEGORY(ALGO_ERROR);
CREATE_LOG_CATEGORY(pipe_reader);
CREATE_LOG_CATEGORY(testing);

Expand Down
2 changes: 1 addition & 1 deletion exchange/src/common/types/ticker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace nutc::common {
// NOTE: this must be the same as Side in template.hpp
enum class Ticker : std::uint8_t { ETH = 0, BTC = 1, LTC = 2 }; // NOLINT
static inline constexpr auto TICKERS = {Ticker::ETH, Ticker::BTC, Ticker::LTC};
static constexpr auto TICKERS = {Ticker::ETH, Ticker::BTC, Ticker::LTC};

inline std::string
to_string(Ticker ticker)
Expand Down
4 changes: 2 additions & 2 deletions exchange/src/exchange/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "algos/algo_manager.hpp"
#include "common/logging/logging.hpp"
#include "common/util.hpp"
#include "exchange/algos/algo_manager.hpp"
#include "exchange/config/dynamic/argparse.hpp"
#include "exchange/config/dynamic/config.hpp"
#include "common/logging/logging.hpp"
#include "exchange/matching_cycle/base/base_cycle.hpp"
#include "exchange/matching_cycle/cycle_interface.hpp"
#include "exchange/matching_cycle/dev/dev_cycle.hpp"
Expand Down Expand Up @@ -52,7 +52,7 @@ main_event_loop(std::unique_ptr<MatchingCycleInterface> cycle)
int
main(int argc, const char** argv)
{
nutc::logging::init(quill::LogLevel::Info);
nutc::logging::init("exchange.log", quill::LogLevel::Info);
std::signal(SIGINT, [](auto) { std::exit(0); });
std::signal(SIGPIPE, SIG_IGN);

Expand Down
14 changes: 7 additions & 7 deletions exchange/src/exchange/sandbox_server/crow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ CrowServer::CrowServer() :
std::string algorithm_data = req.body;
add_pending_trader_(algo_id, language_enum, algorithm_data);
crow::json::wvalue response_json({
{"success", true},
{"message", "Algorithm Successfully Submitted" }
{"success", true },
{"message", "Algorithm Successfully Submitted"}
});
res.body = response_json.dump();
return res;
Expand Down Expand Up @@ -125,15 +125,15 @@ CrowServer::start_remove_timer_(
);
return;
}
if (!err_code) {
log_i(sandbox_server, "Removing trader {}", trader->get_display_name());
trader->disable();
}
else {
if (err_code) {
log_e(
sandbox_server, "Unable to remove trader {}", trader->get_display_name()
);
}
else {
log_i(sandbox_server, "Removing trader {}", trader->get_display_name());
trader->disable();
}
});
timers_.push_back(std::move(timer));
}
Expand Down
3 changes: 1 addition & 2 deletions exchange/src/linter/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <iostream>
#include <string>
#include <thread>
#include <tuple>

static void
process_arguments(int argc, const char** argv)
Expand Down Expand Up @@ -44,7 +43,7 @@ main(int argc, const char** argv)
process_arguments(argc, argv);

// Start logging and print the build info
nutc::logging::init(quill::LogLevel::Info);
nutc::logging::init("linter.log", quill::LogLevel::Info);
log_i(main, "Starting NUTC Linter");

auto server_thread = nutc::crow::get_server_thread();
Expand Down
5 changes: 3 additions & 2 deletions exchange/src/linter/runtime/cpp/cpp_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ CppRuntime::init()
|| on_orderbook_update_func_ == nullptr || on_account_update_func_ == nullptr) {
return fmt::format("[linter] failed to dynamically load functions");
}
strategy_object_ =
init_func(m_market_order_func, m_limit_order_func, m_cancel_order_func);
strategy_object_ = init_func(
m_market_order_func, m_limit_order_func, m_cancel_order_func, log_text
);

return std::nullopt;
}
Expand Down
37 changes: 15 additions & 22 deletions exchange/src/linter/runtime/cpp/cpp_runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,36 @@ namespace nutc::lint {
class CppRuntime : public Runtime {
public:
CppRuntime(
std::string algo,
LimitOrderFunction limit_order,
MarketOrderFunction market_order,
CancelOrderFunction cancel_order
std::string algo, LimitOrderFunction limit_order,
MarketOrderFunction market_order, CancelOrderFunction cancel_order
);

~CppRuntime() override;

std::optional<std::string> init() override;

void fire_on_trade_update(
common::Ticker ticker, common::Side side, float price, float quantity
) const override;
common::Ticker ticker, common::Side side, float price, float quantity
) const override;

void fire_on_orderbook_update(
common::Ticker ticker, common::Side side, float price, float quantity
) const override;
common::Ticker ticker, common::Side side, float price, float quantity
) const override;

void fire_on_account_update(
common::Ticker ticker,
common::Side side,
float price,
float quantity,
float capital
) const override;
private:
common::Ticker ticker, common::Side side, float price, float quantity,
float capital
) const override;

private:
using Strategy = void;
using InitFunc = Strategy* (*)(MarketOrderFunction,
LimitOrderFunction,
CancelOrderFunction);
using InitFunc = Strategy* (*)(MarketOrderFunction, LimitOrderFunction,
CancelOrderFunction, PrintLnFunction);
using OnTradeUpdateFunc =
void (*)(Strategy*, common::Ticker, common::Side, float, float);
void (*)(Strategy*, common::Ticker, common::Side, float, float);
using OnOrderBookUpdateFunc = OnTradeUpdateFunc;
using OnAccountUpdateFunc = void (*)(
Strategy*, common::Ticker, common::Side, float, float, float
);
using OnAccountUpdateFunc =
void (*)(Strategy*, common::Ticker, common::Side, float, float, float);

OnTradeUpdateFunc on_trade_update_func_;
OnOrderBookUpdateFunc on_orderbook_update_func_;
Expand Down
5 changes: 5 additions & 0 deletions exchange/src/linter/runtime/runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using MarketOrderFunction =
std::function<bool(common::Side side, common::Ticker ticker, float quantity)>;
using CancelOrderFunction =
std::function<bool(common::Ticker ticker, std::int64_t order_id)>;
using PrintLnFunction = std::function<void(const std::string& text)>;

class Runtime {
public:
Expand Down Expand Up @@ -54,6 +55,10 @@ class Runtime {
LimitOrderFunction m_limit_order_func;
MarketOrderFunction m_market_order_func;
CancelOrderFunction m_cancel_order_func;

static void
log_text(const std::string&)
{}
};

} // namespace nutc::lint
5 changes: 3 additions & 2 deletions exchange/src/linter/spawning/spawning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ LintProcessManager::spawn_client(const std::string& algo_code, AlgoLanguage lang
"{} "
"seconds. Check all "
"functions to see if you have an infinite loop or infinite "
"recursion.\n\nIf you continue to experience this error, "
"reach out to #nuft-support.\n",
"recursion. This could also be a result of a particularly nasty error "
"in your code.\n\nIf you continue to experience this error, "
"reach out on piazza.\n",
LINT_AUTO_TIMEOUT_SECONDS
);

Expand Down
11 changes: 7 additions & 4 deletions exchange/src/wrapper/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "wrapper/runtime/python/python_runtime.hpp"
#include "wrapper/runtime/runtime.hpp"
#include "common/logging/logging.hpp"
#include "wrapper/config/argparse.hpp"
#include "wrapper/messaging/exchange_communicator.hpp"
#include "wrapper/runtime/cpp/cpp_runtime.hpp"
#include "common/logging/logging.hpp"
#include "wrapper/runtime/python/python_runtime.hpp"
#include "wrapper/util/resource_limits.hpp"

#include <boost/algorithm/string/trim.hpp>
Expand All @@ -12,7 +11,6 @@

#include <csignal>


// We stop the exchange with sigint. The wrapper should exit gracefully
void
catch_sigint(int)
Expand All @@ -29,6 +27,11 @@ main(int argc, const char** argv)
std::signal(SIGINT, catch_sigint);
auto [verbosity, trader_id, algo_type] = process_arguments(argc, argv);

static constexpr std::uint32_t MAX_LOG_SIZE = 50'000;
nutc::logging::init_file_only(
fmt::format("{}.log", trader_id), MAX_LOG_SIZE, quill::LogLevel::Info
);

ExchangeCommunicator communicator{trader_id};

if (!set_memory_limit(1024) || !kill_on_exchange_death()) {
Expand Down
2 changes: 1 addition & 1 deletion exchange/src/wrapper/runtime/cpp/cpp_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ CppRuntime::CppRuntime(
}
strategy_object_ = init_func(
communicator_.place_market_order(), communicator_.place_limit_order(),
communicator_.cancel_order()
communicator_.cancel_order(), log_text
);
}

Expand Down
3 changes: 2 additions & 1 deletion exchange/src/wrapper/runtime/cpp/cpp_runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "wrapper/runtime/runtime.hpp"

namespace nutc::wrapper {
using PrintLn = std::function<void(const std::string&)>;

class CppRuntime : public Runtime {
public:
Expand All @@ -28,7 +29,7 @@ class CppRuntime : public Runtime {

using Strategy = void;
using InitFunc = Strategy* (*)(MarketOrderFunction, LimitOrderFunction,
CancelOrderFunction);
CancelOrderFunction, PrintLn);
using on_trade_update_func = void (*)(Strategy*, Ticker, Side, float, float);
using on_orderbook_update_func = void (*)(Strategy*, Ticker, Side, float, float);
using on_account_update_func =
Expand Down
13 changes: 10 additions & 3 deletions exchange/src/wrapper/runtime/python/python_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ PyRuntime::fire_on_trade_update(
ticker, side, static_cast<float>(quantity), static_cast<float>(price)
);
} catch (const py::error_already_set& err) {
std::cerr << err.what() << "\n";
log_error(err.what());
// std::cerr << err.what() << "\n";
}
}

Expand All @@ -35,7 +36,8 @@ PyRuntime::fire_on_orderbook_update(
ticker, side, static_cast<float>(quantity), static_cast<float>(price)
);
} catch (const py::error_already_set& err) {
std::cerr << err.what() << "\n";
log_error(err.what());
// std::cerr << err.what() << "\n";
}
}

Expand All @@ -51,7 +53,8 @@ PyRuntime::fire_on_account_update(
static_cast<float>(capital)
);
} catch (const py::error_already_set& err) {
std::cerr << err.what() << "\n";
log_error(err.what());
// std::cerr << err.what() << "\n";
}
}

Expand Down Expand Up @@ -90,6 +93,7 @@ PyRuntime::create_api_module(
module.def("publish_market_order", publish_market_order);
module.def("publish_limit_order", publish_limit_order);
module.def("cancel_order", cancel_order);
module.def("print", log_text);

auto sys_modules = sys.attr("modules").cast<py::dict>();
sys_modules["nutc_api"] = module;
Expand All @@ -113,6 +117,9 @@ PyRuntime::run_initialization_code(const std::string& py_code)
def cancel_order(ticker: str, order_id: int):
return nutc_api.cancel_order(ticker, order_id)
)");
py::exec(R"(
print = nutc_api.print
)");
py::exec("Side = nutc_api.Side");
py::exec("Ticker = nutc_api.Ticker");
py::exec("strategy = Strategy()");
Expand Down
Loading

0 comments on commit 3de19bb

Please sign in to comment.