made log settings Macros

This commit is contained in:
EntireTwix 2021-06-27 21:44:40 -07:00
parent 1adb23f5b8
commit d5490ebc56
8 changed files with 16 additions and 14 deletions

View file

@ -1,8 +1,8 @@
#pragma once #pragma once
// Setting both values to 0 does not compile logging (useful for if disk/memory is very valuable) // Setting both values to 0 does not compile logging (useful for if disk/memory is very valuable)
constexpr unsigned max_log_size = 100; #define MAX_LOG_SIZE 100
constexpr unsigned pre_log_size = 10; #define PRE_LOG_SIZE 10
constexpr unsigned max_name_size = 50; constexpr unsigned max_name_size = 50;

View file

@ -2,8 +2,8 @@
#include <json/json.h> // to be removed later #include <json/json.h> // to be removed later
#include <array> #include <array>
#include <algorithm> #include <algorithm>
#include "change_flag.h"
#include "consts.hpp" #include "consts.hpp"
#include "change_flag.h"
#include "transaction.h" #include "transaction.h"
struct Log struct Log

View file

@ -8,7 +8,9 @@ struct User
{ {
uint32_t balance = 0; uint32_t balance = 0;
uint64_t password; uint64_t password;
#if MAX_LOG_SIZE > 0
Log log; Log log;
#endif
User(const std::string &init_pass); User(const std::string &init_pass);
User(uint32_t init_bal, const std::string &init_pass); User(uint32_t init_bal, const std::string &init_pass);

View file

@ -32,9 +32,9 @@ int main(int argc, char **argv)
<< "\nThreads : " << get_nprocs() + 1 << "\nThreads : " << get_nprocs() + 1
<< std::endl; //flushing before EventLoop << std::endl; //flushing before EventLoop
static_assert(bool(max_log_size) == bool(pre_log_size), "You must either utilize both or neither logging variables.\n"); static_assert(bool(MAX_LOG_SIZE) == bool(PRE_LOG_SIZE), "You must either utilize both or neither logging variables.\n");
static_assert(max_log_size >= pre_log_size, "The maximum log size must be larger than or equal to the amount preallocated.\n"); static_assert(MAX_LOG_SIZE >= PRE_LOG_SIZE, "The maximum log size must be larger than or equal to the amount preallocated.\n");
static_assert(!max_log_size || !(max_log_size % pre_log_size), "The maximum log size must be divisible by the preallocation size.\n"); static_assert(!MAX_LOG_SIZE || !(MAX_LOG_SIZE % PRE_LOG_SIZE), "The maximum log size must be divisible by the preallocation size.\n");
if (argc != 3) if (argc != 3)
{ {

View file

@ -44,7 +44,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
} }
BankResponse state; BankResponse state;
if constexpr (max_log_size > 0) if constexpr (MAX_LOG_SIZE > 0)
{ {
Transaction temp(a_name, b_name, amount); Transaction temp(a_name, b_name, amount);
std::shared_lock<std::shared_mutex> lock{send_funds_l}; std::shared_lock<std::shared_mutex> lock{send_funds_l};
@ -304,7 +304,7 @@ void Bank::Load()
user_save.close(); user_save.close();
for (const auto &u : temp.getMemberNames()) for (const auto &u : temp.getMemberNames())
{ {
if constexpr (max_log_size > 0) if constexpr (MAX_LOG_SIZE > 0)
{ {
users.try_emplace(u, temp[u]["balance"].asUInt(), std::move(temp[u]["password"].asUInt64()), temp[u]["log"]); users.try_emplace(u, temp[u]["balance"].asUInt(), std::move(temp[u]["password"].asUInt64()), temp[u]["log"]);
} }

View file

@ -54,7 +54,7 @@ void api::GetBal(req_args, const std::string &name) const
} }
void api::GetLog(req_args) void api::GetLog(req_args)
{ {
if constexpr (max_log_size > 0) if constexpr (MAX_LOG_SIZE > 0)
{ {
RESPONSE_PARSE(bank.GetLogs(NAME_PARAM)); RESPONSE_PARSE(bank.GetLogs(NAME_PARAM));
} }

View file

@ -2,7 +2,7 @@
void Log::AddTrans(Transaction &&t) void Log::AddTrans(Transaction &&t)
{ {
if (data.size() == max_log_size) // If we hit the max size if (data.size() == MAX_LOG_SIZE) // If we hit the max size
{ {
for (uint32_t i = 1; i < data.size(); i++) // Make room at the back for (uint32_t i = 1; i < data.size(); i++) // Make room at the back
{ {
@ -13,7 +13,7 @@ void Log::AddTrans(Transaction &&t)
} }
else if (data.size() == data.capacity()) // If we haven't hit the max but hit capacity else if (data.size() == data.capacity()) // If we haven't hit the max but hit capacity
{ {
data.reserve(data.capacity() + pre_log_size); // Reserve more memory data.reserve(data.capacity() + PRE_LOG_SIZE); // Reserve more memory
} }
data.push_back(std::move(t)); // In either case we have space under max length, move to new spot data.push_back(std::move(t)); // In either case we have space under max length, move to new spot
log_flag.SetChangesOn(); log_flag.SetChangesOn();

View file

@ -26,8 +26,8 @@ User::User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j) : ba
{ {
if (log_j.size()) if (log_j.size())
{ {
log.data.reserve(std::min(pre_log_size * ((log_j.size() / pre_log_size) + 1), max_log_size)); log.data.reserve(std::min((size_t)PRE_LOG_SIZE * ((log_j.size() / PRE_LOG_SIZE) + 1), (size_t)MAX_LOG_SIZE));
for (uint32_t i = (log_j.size() - max_log_size) * (log_j.size() > max_log_size); i < log_j.size(); i++) for (uint32_t i = (log_j.size() - MAX_LOG_SIZE) * (log_j.size() > MAX_LOG_SIZE); i < log_j.size(); i++)
{ {
log.data.push_back(Transaction( log.data.push_back(Transaction(
log_j[i]["from"].asCString(), log_j[i]["from"].asCString(),
@ -47,7 +47,7 @@ Json::Value User::Serialize() const
Json::Value res; Json::Value res;
res["balance"] = (Json::UInt)balance; res["balance"] = (Json::UInt)balance;
res["password"] = (Json::UInt64)password; res["password"] = (Json::UInt64)password;
if constexpr (max_log_size > 0) if constexpr (MAX_LOG_SIZE > 0)
{ {
res["log"] = log.Serialize(); res["log"] = log.Serialize();
} }