Skip to content

Commit

Permalink
Spdlog removed. Boost log usage added instead #111
Browse files Browse the repository at this point in the history
Co-authored-by: nemothenoone <[email protected]>
  • Loading branch information
nkaskov and nemothenoone committed Nov 24, 2023
1 parent 40d9008 commit 82299b0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ endmacro()

cm_project(assigner WORKSPACE_NAME ${CMAKE_WORKSPACE_NAME} LANGUAGES C CXX)

if(NOT Boost_FOUND AND NOT CMAKE_CROSSCOMPILING)
cm_find_package(Boost COMPONENTS REQUIRED filesystem log log_setup program_options thread system)
endif()

cm_find_package(CM)
find_package(spdlog REQUIRED)
include(CMDeploy)
Expand Down
74 changes: 31 additions & 43 deletions include/nil/blueprint/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,69 +26,57 @@
#ifndef CRYPTO3_ASSIGNER_NIL_BLUEPRINT_LOGGER_HPP
#define CRYPTO3_ASSIGNER_NIL_BLUEPRINT_LOGGER_HPP

#include <llvm/IR/Instructions.h>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>

#include <spdlog/spdlog.h>
#include <llvm/IR/Instructions.h>

namespace nil {
namespace blueprint {
class logger {
const llvm::BasicBlock *current_block = nullptr;
const llvm::Function *current_function = nullptr;
public:
enum class level {
DEBUG,
INFO,
ERROR,
};

logger() {
spdlog::set_pattern("%L %v");
spdlog::set_level(spdlog::level::info);
logger(boost::log::trivial::severity_level lvl = boost::log::trivial::info) {
boost::log::core::get()->set_filter(boost::log::trivial::severity >= lvl);
}

void set_level(level lvl) {
this->lvl = lvl;
switch (lvl) {
case level::DEBUG:
spdlog::set_level(spdlog::level::debug);
break;
case level::INFO:
spdlog::set_level(spdlog::level::info);
break;
case level::ERROR:
spdlog::set_level(spdlog::level::err);
break;
}
void set_level(boost::log::trivial::severity_level lvl) {
boost::log::core::get()->set_filter(boost::log::trivial::severity >= lvl);
}

// TODO: these two functions can be substituted by one when std::format is widely supported
void debug(boost::basic_format<char> formated_debug_message) {
BOOST_LOG_TRIVIAL(debug) << boost::str(formated_debug_message);
}

template<typename... Args>
void debug(std::string_view fmt, const Args &...args) {
spdlog::debug(fmt, args...);
void debug(std::string_view debug_message) {
BOOST_LOG_TRIVIAL(debug) << debug_message;
}

void log_instruction(const llvm::Instruction *inst) {
if (lvl != level::DEBUG) {
return;
}
if (inst->getFunction() != current_function) {
current_function = inst->getFunction();
spdlog::debug("{}:", current_function->getName().str());

BOOST_LOG_TRIVIAL(debug) << current_function->getName().str();
}
if (inst->getParent() != current_block) {
current_block = inst->getParent();
spdlog::debug("\t{}: ", current_block->getNameOrAsOperand());

BOOST_LOG_TRIVIAL(debug) << current_block->getNameOrAsOperand();
}
std::string inst_name = inst->getNameOrAsOperand();
const char *opcode_name = inst->getOpcodeName();
if (inst_name == "<badref>") {
BOOST_LOG_TRIVIAL(debug) << "\t\t" << opcode_name;
} else {
BOOST_LOG_TRIVIAL(debug) << "\t\t" << opcode_name << " -> " << inst_name;
}
std::string str;
llvm::raw_string_ostream ss(str);
inst->print(ss);
spdlog::debug("\t\t{}", str);
}

private:
const llvm::BasicBlock *current_block = nullptr;
const llvm::Function *current_function = nullptr;
level lvl = level::INFO;
};
}
}
} // namespace blueprint
} // namespace nil

#endif // CRYPTO3_ASSIGNER_NIL_BLUEPRINT_LOGGER_HPP
#endif // CRYPTO3_ASSIGNER_NIL_BLUEPRINT_LOGGER_HPP
23 changes: 12 additions & 11 deletions include/nil/blueprint/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#include <variant>
#include <stack>

#include <boost/format.hpp>
#include <boost/log/trivial.hpp>

#include <nil/blueprint/blueprint/plonk/assignment_proxy.hpp>
#include <nil/blueprint/blueprint/plonk/circuit_proxy.hpp>
#include <nil/blueprint/components/hashes/poseidon/plonk/poseidon.hpp>
Expand Down Expand Up @@ -90,13 +93,11 @@ namespace nil {
template<typename BlueprintFieldType, typename ArithmetizationParams, bool PrintCircuitOutput>
struct parser {

parser(long stack_size, bool detailed_logging, std::uint32_t max_num_provers, const std::string &kind = "") :
parser(long stack_size, boost::log::trivial::severity_level log_level, std::uint32_t max_num_provers, const std::string &kind = "") :
stack_memory(stack_size),
maxNumProvers(max_num_provers),
currProverIdx(0) {
if (detailed_logging) {
log.set_level(logger::level::DEBUG);
}
currProverIdx(0),
log(log_level) {

detail::PolicyManager::set_policy(kind);

Expand Down Expand Up @@ -627,7 +628,7 @@ namespace nil {
void handle_ptrtoint(const llvm::Value *inst, llvm::Value *operand, stack_frame<var> &frame, bool next_prover) {
ptr_type ptr = resolve_number<ptr_type>(frame, operand);
size_t offset = stack_memory.ptrtoint(ptr);
log.debug("PtrToInt {} {}", ptr, offset);
log.debug(boost::format("PtrToInt %1% %2%") % ptr % offset);
frame.scalars[inst] = put_into_assignment(offset, next_prover);
}

Expand Down Expand Up @@ -1110,7 +1111,7 @@ namespace nil {
auto vec = layout_resolver->get_type_layout<BlueprintFieldType>(alloca->getAllocatedType());

ptr_type res_ptr = stack_memory.add_cells(vec);
log.debug("Alloca: {}", res_ptr);
log.debug(boost::format("Alloca: %1%") % res_ptr);
frame.scalars[inst] = put_into_assignment(res_ptr, next_prover);
return inst->getNextNonDebugInstruction();
}
Expand All @@ -1129,21 +1130,21 @@ namespace nil {
}
std::ostringstream oss;
oss << gep_res.data;
log.debug("GEP: {}", oss.str());
log.debug(boost::format("GEP: %1%") % oss.str());
frame.scalars[gep] = put_into_assignment(gep_res, next_prover);
return inst->getNextNonDebugInstruction();
}
case llvm::Instruction::Load: {
auto *load_inst = llvm::cast<llvm::LoadInst>(inst);
ptr_type ptr = resolve_number<ptr_type>(frame, load_inst->getPointerOperand());
log.debug("Load: {}", ptr);
log.debug(boost::format("Load: %1%") % ptr);
handle_load(ptr, load_inst, frame);
return inst->getNextNonDebugInstruction();
}
case llvm::Instruction::Store: {
auto *store_inst = llvm::cast<llvm::StoreInst>(inst);
ptr_type ptr = resolve_number<ptr_type>(frame, store_inst->getPointerOperand());
log.debug("Store: {}", ptr);
log.debug(boost::format("Store: %1%") % ptr);
const llvm::Value *val = store_inst->getValueOperand();
handle_store(ptr, val, frame);
return inst->getNextNonDebugInstruction();
Expand Down Expand Up @@ -1183,7 +1184,7 @@ namespace nil {
size_t offset = resolve_number<size_t>(frame, inst->getOperand(0));
oss << var_value(assignments[currProverIdx], frame.scalars[inst->getOperand(0)]).data;
ptr_type ptr = stack_memory.inttoptr(offset);
log.debug("IntToPtr: {} {}", oss.str(), ptr);
log.debug(boost::format("IntToPtr %1% %2%") % oss.str() % ptr);
ASSERT(ptr != 0);
frame.scalars[inst] = put_into_assignment(ptr, next_prover);
return inst->getNextNonDebugInstruction();
Expand Down

0 comments on commit 82299b0

Please sign in to comment.