CMakeLists.txt
- added copy_frontend as dependency (else it wont automatically execute request_handler.cpp - implemented db connection - fixed routing of short code All Files - Improved logging
This commit is contained in:
parent
cbfc0b563c
commit
0d89ce5e2d
8 changed files with 251 additions and 89 deletions
|
|
@ -7,7 +7,6 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
#include <soci/soci.h>
|
||||
#include <soci/sqlite3/soci-sqlite3.h>
|
||||
|
||||
class DatabaseService {
|
||||
public:
|
||||
|
|
@ -27,10 +26,10 @@ private:
|
|||
std::string db_path_;
|
||||
std::size_t pool_size_;
|
||||
std::vector<std::shared_ptr<soci::session>> connection_pool_;
|
||||
std::size_t current_index_ = 0; // Index used for round-robin selection
|
||||
std::size_t current_index_ = 0;
|
||||
|
||||
static DatabaseService* INSTANCE;
|
||||
static std::mutex singleton_mutex;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
62
includes/log_utils.hpp
Normal file
62
includes/log_utils.hpp
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
#ifndef LOG_UTILS_HPP
|
||||
#define LOG_UTILS_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <filesystem>
|
||||
#include <unordered_map>
|
||||
|
||||
// Define log levels
|
||||
enum class LogLevel {
|
||||
INFO,
|
||||
ERROR,
|
||||
BADREQUEST
|
||||
};
|
||||
|
||||
// Function to get a color based on file name
|
||||
inline const char* getColorForFile(const std::string& file_name) {
|
||||
// Define color codes
|
||||
const char* colors[] = {
|
||||
"\033[34m", // Blue
|
||||
"\033[32m", // Green
|
||||
"\033[36m", // Cyan
|
||||
"\033[35m", // Magenta
|
||||
"\033[33m" // Yellow
|
||||
};
|
||||
|
||||
// Hash the file name to determine the color
|
||||
std::hash<std::string> hasher;
|
||||
size_t hash = hasher(file_name);
|
||||
return colors[hash % (sizeof(colors) / sizeof(colors[0]))];
|
||||
}
|
||||
|
||||
// Logging function
|
||||
inline void log(const LogLevel level, const std::string& message, const std::string& file_path) {
|
||||
const auto now = std::chrono::system_clock::now();
|
||||
const auto time = std::chrono::system_clock::to_time_t(now);
|
||||
const std::tm tm = *std::localtime(&time);
|
||||
|
||||
const auto color_reset = "\033[0m";
|
||||
const auto color_error = "\033[31m";
|
||||
|
||||
// Extract file name from full path
|
||||
std::string file_name = std::filesystem::path(file_path).filename().string();
|
||||
|
||||
// Determine color based on log level
|
||||
const char* color = level == LogLevel::ERROR || level == LogLevel::BADREQUEST ? color_error : getColorForFile(file_name);
|
||||
|
||||
// Print log
|
||||
std::cout << color
|
||||
<< "[" << (level == LogLevel::INFO ? "INFO" : "ERROR") << "] "
|
||||
<< std::put_time(&tm, "%H:%M:%S") // Time only
|
||||
<< " [" << file_name << "] "
|
||||
<< message
|
||||
<< color_reset << std::endl;
|
||||
}
|
||||
|
||||
// Helper macro to automatically pass the file name
|
||||
#define LOG(level, message) log(level, message, __FILE__)
|
||||
|
||||
#endif // LOG_UTILS_HPP
|
||||
Reference in a new issue