mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-16 00:02:14 +11:00
✨ made log settings Macros
This commit is contained in:
parent
1adb23f5b8
commit
d5490ebc56
8 changed files with 16 additions and 14 deletions
|
|
@ -1,8 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
// Setting both values to 0 does not compile logging (useful for if disk/memory is very valuable)
|
||||
constexpr unsigned max_log_size = 100;
|
||||
constexpr unsigned pre_log_size = 10;
|
||||
#define MAX_LOG_SIZE 100
|
||||
#define PRE_LOG_SIZE 10
|
||||
|
||||
constexpr unsigned max_name_size = 50;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
#include <json/json.h> // to be removed later
|
||||
#include <array>
|
||||
#include <algorithm>
|
||||
#include "change_flag.h"
|
||||
#include "consts.hpp"
|
||||
#include "change_flag.h"
|
||||
#include "transaction.h"
|
||||
|
||||
struct Log
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ struct User
|
|||
{
|
||||
uint32_t balance = 0;
|
||||
uint64_t password;
|
||||
#if MAX_LOG_SIZE > 0
|
||||
Log log;
|
||||
#endif
|
||||
|
||||
User(const std::string &init_pass);
|
||||
User(uint32_t init_bal, const std::string &init_pass);
|
||||
|
|
|
|||
6
main.cpp
6
main.cpp
|
|
@ -32,9 +32,9 @@ int main(int argc, char **argv)
|
|||
<< "\nThreads : " << get_nprocs() + 1
|
||||
<< 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(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(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 || !(MAX_LOG_SIZE % PRE_LOG_SIZE), "The maximum log size must be divisible by the preallocation size.\n");
|
||||
|
||||
if (argc != 3)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
|||
}
|
||||
|
||||
BankResponse state;
|
||||
if constexpr (max_log_size > 0)
|
||||
if constexpr (MAX_LOG_SIZE > 0)
|
||||
{
|
||||
Transaction temp(a_name, b_name, amount);
|
||||
std::shared_lock<std::shared_mutex> lock{send_funds_l};
|
||||
|
|
@ -304,7 +304,7 @@ void Bank::Load()
|
|||
user_save.close();
|
||||
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"]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ void api::GetBal(req_args, const std::string &name) const
|
|||
}
|
||||
void api::GetLog(req_args)
|
||||
{
|
||||
if constexpr (max_log_size > 0)
|
||||
if constexpr (MAX_LOG_SIZE > 0)
|
||||
{
|
||||
RESPONSE_PARSE(bank.GetLogs(NAME_PARAM));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
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
|
||||
{
|
||||
|
|
@ -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
|
||||
{
|
||||
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
|
||||
log_flag.SetChangesOn();
|
||||
|
|
|
|||
|
|
@ -26,8 +26,8 @@ User::User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j) : ba
|
|||
{
|
||||
if (log_j.size())
|
||||
{
|
||||
log.data.reserve(std::min(pre_log_size * ((log_j.size() / pre_log_size) + 1), max_log_size));
|
||||
for (uint32_t i = (log_j.size() - max_log_size) * (log_j.size() > max_log_size); i < log_j.size(); i++)
|
||||
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++)
|
||||
{
|
||||
log.data.push_back(Transaction(
|
||||
log_j[i]["from"].asCString(),
|
||||
|
|
@ -47,7 +47,7 @@ Json::Value User::Serialize() const
|
|||
Json::Value res;
|
||||
res["balance"] = (Json::UInt)balance;
|
||||
res["password"] = (Json::UInt64)password;
|
||||
if constexpr (max_log_size > 0)
|
||||
if constexpr (MAX_LOG_SIZE > 0)
|
||||
{
|
||||
res["log"] = log.Serialize();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue