[impl] Logging

This commit is contained in:
Peter Sykora 2021-08-31 05:28:39 +02:00
parent e36f85cc44
commit 564dda8539
4 changed files with 112 additions and 30 deletions

View File

@ -79,6 +79,23 @@
"CMAKE_SYSTEM_VERSION": "8.1" "CMAKE_SYSTEM_VERSION": "8.1"
}, },
"vendor": { "microsoft.com/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Windows" ] } } "vendor": { "microsoft.com/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Windows" ] } }
},
{
"name": "win64-release",
"displayName": "Windows x64 Release",
"description": "Target Windows with the Visual Studio development environment.",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
"CMAKE_SYSTEM_VERSION": "8.1"
},
"vendor": { "microsoft.com/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Windows" ] } }
} }
], ],
"buildPresets": [ "buildPresets": [
@ -93,6 +110,10 @@
{ {
"name": "win64-debug", "name": "win64-debug",
"configurePreset": "win64-debug" "configurePreset": "win64-debug"
},
{
"name": "win64-release",
"configurePreset": "win64-release"
} }
], ],
"vendor": { "vendor": {

View File

@ -3,6 +3,13 @@
#include "LicenseClient.h" #include "LicenseClient.h"
#include "SystemParamsProvider.h" #include "SystemParamsProvider.h"
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include <boost/asio/signal_set.hpp> #include <boost/asio/signal_set.hpp>
#include <iostream> #include <iostream>
@ -12,6 +19,9 @@
namespace po = boost::program_options; namespace po = boost::program_options;
namespace net = boost::asio; // from <boost/asio.hpp> namespace net = boost::asio; // from <boost/asio.hpp>
namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
volatile bool signalCaught = false; volatile bool signalCaught = false;
@ -21,47 +31,88 @@ void signalHandler(int signum) {
signalCaught = true; signalCaught = true;
} }
void initLogging(std::optional<std::string> logFolder, bool debug = false)
{
if (logFolder.has_value()) {
logging::add_file_log(
keywords::file_name = (boost::filesystem::path(logFolder.value()) / "CCEngineServer_%N.log").native(),
keywords::open_mode = std::ios_base::app,
keywords::rotation_size = 10 * 1024 * 1024,
keywords::max_files = 10,
keywords::format = (
expr::stream
<< expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S")
<< ": <" << logging::trivial::severity
<< "> " << expr::smessage)
);
}
logging::core::get()->set_filter
(
logging::trivial::severity >= ((!debug) ? logging::trivial::info : logging::trivial::debug)
);
logging::add_common_attributes();
}
int main(int argc, const char* argv[]) int main(int argc, const char* argv[])
{ {
// Declare the supported options. // Declare the supported options.
po::options_description desc("Allowed options"); po::options_description generalOptions("General options");
desc.add_options() generalOptions.add_options()
("help", "produce help message") ("help", "produce help message")
("bind_ip", po::value<std::string>()->default_value("0.0.0.0"), "ip address to listen for connections") ("bind_ip", po::value<std::string>()->default_value("0.0.0.0"), "ip address to listen for connections")
("port", po::value<uint16_t>()->default_value(8080), "port") ("port", po::value<uint16_t>()->default_value(8080), "port")
("data_root", po::value<std::string>()->default_value("."), "root directory with data") ("data_root", po::value<std::string>()->default_value("."), "root directory with data")
("threads", po::value<int>()->default_value(4), "port") ("threads", po::value<int>()->default_value(4), "port")
; ("log_folder", po::value<std::string>(), "folder to store logs")
;
po::options_description debugOptions("Debug options");
debugOptions.add_options()
("debug", "Debugging logs")
;
po::options_description allOptions("Allowed options");
allOptions.add(generalOptions).add(debugOptions);
po::variables_map vm; po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm); po::store(po::parse_command_line(argc, argv, allOptions), vm);
po::notify(vm); po::notify(vm);
if (vm.count("help")) { if (vm.count("help")) {
std::cout << desc << "\n"; std::cout << generalOptions << "\n";
return 1; return 1;
} }
std::optional<std::string> logFolder;
if (vm.count("log_folder")) {
logFolder = vm["log_folder"].as<std::string>();
}
initLogging(logFolder, vm.count("debug"));
BOOST_LOG_TRIVIAL(info) << "Starting application...";
BOOST_LOG_TRIVIAL(debug) << "Initializing params provider...";
SystemParamsProvider systemParamsProvider; SystemParamsProvider systemParamsProvider;
LicenseClient licenseClient(systemParamsProvider, "license.dat"); LicenseClient licenseClient(systemParamsProvider, "license.dat");
licenseClient.init(); licenseClient.init();
if (!licenseClient.isActivated()) { if (!licenseClient.isActivated()) {
std::cerr << "Application is not activated. Please contact Catalogue of Currencies support. Exiting..." << std::endl; const std::string msg = "Application is not activated. Please contact Catalogue of Currencies support.";
std::cout << "Activation request: " << licenseClient.buildActivationRequest() << std::endl; BOOST_LOG_TRIVIAL(error) << msg << " Exiting...";
std::cout << msg << std::endl;
std::cout << "Activation request: " << std::endl << licenseClient.buildActivationRequest() << std::endl;
return -2; return -2;
} }
if (vm.count("port")) {
std::cout << "Port "
<< vm["port"].as<uint16_t>() << ".\n";
}
auto addressToListen = vm["bind_ip"].as<std::string>(); auto addressToListen = vm["bind_ip"].as<std::string>();
auto portToListen = vm["port"].as<uint16_t>(); auto portToListen = vm["port"].as<uint16_t>();
auto docRoot = vm["data_root"].as<std::string>(); auto docRoot = vm["data_root"].as<std::string>();
auto threads = vm["threads"].as<int>(); auto threads = vm["threads"].as<int>();
BOOST_LOG_TRIVIAL(debug) << "Will listen on: " << addressToListen << ":" << portToListen;
BOOST_LOG_TRIVIAL(debug) << "Will serve data from : " << docRoot;
BOOST_LOG_TRIVIAL(debug) << "Number of serving threads : " << threads;
auto ccServer = std::make_unique<CCServer>(addressToListen, portToListen, docRoot, threads); auto ccServer = std::make_unique<CCServer>(addressToListen, portToListen, docRoot, threads);
ccServer->run(); ccServer->run();
@ -69,6 +120,7 @@ int main(int argc, const char* argv[])
{ {
std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::this_thread::sleep_for(std::chrono::milliseconds(500));
} }
BOOST_LOG_TRIVIAL(info) << "Caught signal. Shutting down...";
ccServer->shutdown(); ccServer->shutdown();
return 0; return 0;

View File

@ -19,6 +19,7 @@
#include <boost/iostreams/filtering_stream.hpp> #include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/zlib.hpp> #include <boost/iostreams/filter/zlib.hpp>
#include <boost/iostreams/copy.hpp> #include <boost/iostreams/copy.hpp>
#include <boost/log/trivial.hpp>
#include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
#include <boost/range/algorithm_ext/erase.hpp> #include <boost/range/algorithm_ext/erase.hpp>
@ -56,7 +57,7 @@ namespace ASN1 = CryptoPP::ASN1;
namespace namespace
{ {
static const std::string productId = "coc"; static const std::string productId = "cocserver";
static const uint32_t initializationVectorSize = AES::BLOCKSIZE; static const uint32_t initializationVectorSize = AES::BLOCKSIZE;
static const uint32_t macTagSize = 16; static const uint32_t macTagSize = 16;
@ -263,10 +264,12 @@ void LicenseClient::init()
{ {
const auto systemParams = skipEmptyParams(m_systemParamsProvider.retrieveSystemParams()); const auto systemParams = skipEmptyParams(m_systemParamsProvider.retrieveSystemParams());
std::cout << "Collected params: " << std::endl;
for (const auto& entry : systemParams)
{ {
std::cout << "\t" << entry.first << " = " << entry.second << " ~= " << fletcher64(entry.second) << std::endl; BOOST_LOG_TRIVIAL(debug) << "Collected params: ";
for (const auto& entry : systemParams)
{
BOOST_LOG_TRIVIAL(debug) << "\t" << entry.first << " = " << entry.second << " ~= " << fletcher64(entry.second);
}
} }
m_systemParams = hashParams(systemParams); m_systemParams = hashParams(systemParams);
@ -317,7 +320,7 @@ std::string LicenseClient::buildActivationRequest()
); // StringSource ); // StringSource
return std::string("-----BEGIN ACTIVATION REQUEST-----\n") + return std::string("-----BEGIN ACTIVATION REQUEST-----\n") +
base64Encode(cipher, std::make_optional(64)) + base64Encode(std::string(iv, iv + initializationVectorSize) + cipher, std::make_optional(64)) +
std::string("-----END ACTIVATION REQUEST-----\n"); std::string("-----END ACTIVATION REQUEST-----\n");
} }

View File

@ -1,6 +1,7 @@
#include "SystemParamsProvider_linux.h" #include "SystemParamsProvider_linux.h"
#include <boost/process.hpp> #include <boost/process.hpp>
#include <boost/log/trivial.hpp>
#include <cpuid.h> #include <cpuid.h>
#include <future> #include <future>
@ -47,7 +48,7 @@ namespace
std::string line; std::string line;
std::ifstream myfile(fileName); std::ifstream myfile(fileName);
if (myfile.is_open()) { if (myfile.is_open()) {
std::cout << "File " << fileName << " successfully open" << std::endl; BOOST_LOG_TRIVIAL(debug) << "File " << fileName << " successfully open";
if (getline (myfile,line)) { if (getline (myfile,line)) {
return line; return line;
} }
@ -58,6 +59,7 @@ namespace
std::optional<std::string> loadOsId() std::optional<std::string> loadOsId()
{ {
BOOST_LOG_TRIVIAL(debug) << "Trying to find Operating system ID...";
auto machineId = readFileLine("/var/lib/dbus/machine-id"); auto machineId = readFileLine("/var/lib/dbus/machine-id");
if (machineId.value_or("").empty()) { if (machineId.value_or("").empty()) {
machineId = readFileLine("/etc/machine-id"); machineId = readFileLine("/etc/machine-id");
@ -67,16 +69,19 @@ namespace
std::optional<std::string> loadBoardSerial() std::optional<std::string> loadBoardSerial()
{ {
BOOST_LOG_TRIVIAL(debug) << "Trying to find mainboard serial number...";
return readFileLine("/sys/class/dmi/id/board_serial"); return readFileLine("/sys/class/dmi/id/board_serial");
} }
std::optional<std::string> loadComputerUUID() std::optional<std::string> loadComputerUUID()
{ {
BOOST_LOG_TRIVIAL(debug) << "Trying to find product UUID...";
return readFileLine("/sys/class/dmi/id/product_uuid"); return readFileLine("/sys/class/dmi/id/product_uuid");
} }
std::optional<std::string> loadComputerSerial() std::optional<std::string> loadComputerSerial()
{ {
BOOST_LOG_TRIVIAL(debug) << "Trying to find computer serial number...";
auto productSerial = readFileLine("/sys/class/dmi/id/product_serial"); auto productSerial = readFileLine("/sys/class/dmi/id/product_serial");
if(!productSerial.value_or("").empty()) { if(!productSerial.value_or("").empty()) {
@ -101,6 +106,7 @@ namespace
CpuIdInfo loadCpuId() CpuIdInfo loadCpuId()
{ {
BOOST_LOG_TRIVIAL(debug) << "Trying to find processor idenfication...";
uint32_t a, b, c, d; uint32_t a, b, c, d;
__cpuid (0 /* vendor string */, a, b, c, d); __cpuid (0 /* vendor string */, a, b, c, d);
std::cout << "EAX: " << std::hex << std::setw(8) << std::setfill('0') << a << "\nEBX: " << b << "\nECX: " << c << "\nEDX: " << d << "\n"; std::cout << "EAX: " << std::hex << std::setw(8) << std::setfill('0') << a << "\nEBX: " << b << "\nECX: " << c << "\nEDX: " << d << "\n";
@ -131,7 +137,7 @@ namespace
std::regex line_regex(parameterRegex + ": " + valueRegex); std::regex line_regex(parameterRegex + ": " + valueRegex);
std::smatch line_match; std::smatch line_match;
if(std::regex_search(line, line_match, line_regex)) { if(std::regex_search(line, line_match, line_regex)) {
std::cout << "DEBUG: Found " << line_match[1] << ": " << line_match[2] << std::endl; BOOST_LOG_TRIVIAL(trace) << "Matched parameter pair " << line_match[1] << ": " << line_match[2];
return std::make_pair<std::string, std::string>(line_match[1], line_match[2]); return std::make_pair<std::string, std::string>(line_match[1], line_match[2]);
} }
return {}; return {};
@ -173,7 +179,7 @@ namespace
std::smatch section_match; std::smatch section_match;
if(std::regex_search(line, section_match, section_regex)) { if(std::regex_search(line, section_match, section_regex)) {
section = std::atoi(section_match[1].str().c_str()); section = std::atoi(section_match[1].str().c_str());
std::cout << "DEBUG: Found section: " << section_match[1] << std::endl; BOOST_LOG_TRIVIAL(debug) << "Found section: " << section_match[1];
} }
continue; continue;
} }
@ -243,7 +249,7 @@ namespace
if (!line.empty() && line[0] == 'e') { if (!line.empty() && line[0] == 'e') {
std::smatch section_match; std::smatch section_match;
if(std::regex_match(line, section_match, section_regex)) { if(std::regex_match(line, section_match, section_regex)) {
std::cout << "DEBUG: Found device: " << section_match[1] << " with MAC: " << section_match[2] << std::endl; BOOST_LOG_TRIVIAL(debug) << "Found device : " << section_match[1] << " with MAC : " << section_match[2];
output[section_match[1]] = section_match[2]; output[section_match[1]] = section_match[2];
} }
continue; continue;
@ -321,7 +327,7 @@ namespace
return {}; return {};
} }
std::cout << "Found best mount point: " << bestEntry->mountPoint << std::endl; BOOST_LOG_TRIVIAL(debug) << "Found best mount point: " << bestEntry->mountPoint;
int depth = 0; int depth = 0;
while(depth < 10 && !bestEntry->parentName.empty()) { while(depth < 10 && !bestEntry->parentName.empty()) {
@ -391,11 +397,11 @@ namespace detail
} }
auto cpuIdInfo = loadCpuId(); auto cpuIdInfo = loadCpuId();
std::cout << "Vendor ID: " << cpuIdInfo.vendorId << std::endl; BOOST_LOG_TRIVIAL(trace) << "Vendor ID: " << cpuIdInfo.vendorId << std::endl;
std::cout << "Model ID: " << cpuIdInfo.modelId << std::endl; BOOST_LOG_TRIVIAL(trace) << "Model ID: " << cpuIdInfo.modelId << std::endl;
std::cout << "Vendor String: " << cpuIdInfo.vendorString << std::endl; BOOST_LOG_TRIVIAL(trace) << "Vendor String: " << cpuIdInfo.vendorString << std::endl;
std::cout << "Is hypervisor: " << cpuIdInfo.isHypervisor << std::endl; BOOST_LOG_TRIVIAL(trace) << "Is hypervisor: " << cpuIdInfo.isHypervisor << std::endl;
std::cout << "Serial number: " << cpuIdInfo.serialNumber << std::endl; BOOST_LOG_TRIVIAL(trace) << "Serial number: " << cpuIdInfo.serialNumber << std::endl;
result[SystemParamTypes::cpuIdModel] = intToHex(cpuIdInfo.vendorId) + intToHex(cpuIdInfo.modelId); result[SystemParamTypes::cpuIdModel] = intToHex(cpuIdInfo.vendorId) + intToHex(cpuIdInfo.modelId);
if(cpuIdInfo.isHypervisor) { if(cpuIdInfo.isHypervisor) {