Also moved a few logging deps from lib_spark -> system: * bts_chrono.h * bts_no_copy.h * defer.h * dispatcher.h/cpp * file_utils.h/cpp * type_name.h
91 lines
2.7 KiB
C++
91 lines
2.7 KiB
C++
#include "ErrorReportService.h"
|
|
|
|
#include "logging/logging.h"
|
|
#include "spark/config/config.h"
|
|
#include "spark/utils/bts_assert.h"
|
|
#include "spark/utils/datetime.h"
|
|
#include "system/protobufs/system_config.pb.h"
|
|
|
|
#include <boost/date_time/gregorian/gregorian.hpp>
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
|
#include <boost/filesystem.hpp>
|
|
#include <fstream>
|
|
#include <string>
|
|
|
|
namespace bts {
|
|
namespace support {
|
|
|
|
using bts::system::protobufs::SystemConfig;
|
|
|
|
std::string get_report_dir() {
|
|
return config::get<SystemConfig>().data_path() + "error_reports/";
|
|
}
|
|
|
|
ErrorReportServer::ErrorReportServer(bts::api::ServiceContext &context)
|
|
: ErrorReportServiceBase(context) {
|
|
// create directory
|
|
auto report_dir = get_report_dir();
|
|
if (!boost::filesystem::exists(report_dir)) {
|
|
boost::filesystem::create_directories(report_dir);
|
|
}
|
|
}
|
|
|
|
ErrorReportServer::~ErrorReportServer() {}
|
|
|
|
void write_file(const std::string &destination, const std::string &data) {
|
|
if (data.size() > 0) {
|
|
std::ofstream file;
|
|
file.open(destination, std::ios::binary);
|
|
if (file.is_open()) {
|
|
file << data;
|
|
file.close();
|
|
}
|
|
}
|
|
}
|
|
|
|
void ErrorReportServer::send_report(
|
|
const ErrorReport &request, const api::Response<ErrorResponse> &reply) {
|
|
|
|
// generate unique id using date
|
|
auto dt = datetime::local_now();
|
|
auto dir_name = request.user() + "-" + datetime::FIX_timestamp(dt);
|
|
dir_name = os::sanitize_file_name(dir_name);
|
|
dir_name = get_report_dir() + dir_name + "/";
|
|
|
|
// create directory with that name
|
|
boost::filesystem::create_directories(dir_name);
|
|
|
|
// Write data to files in that directory
|
|
|
|
// user_text.txt
|
|
std::ofstream file;
|
|
file.open(dir_name + "user_text.txt");
|
|
if (file.is_open()) {
|
|
file << request.user() << " - " << request.contact() << std::endl;
|
|
file << "================" << std::endl;
|
|
file << "Error Description: " << request.error_message() << std::endl;
|
|
file << "================" << std::endl;
|
|
file << "User Description: " << request.user_description() << std::endl;
|
|
file.close();
|
|
}
|
|
// logs.txt
|
|
write_file(dir_name + "logs.txt", request.log());
|
|
// screenshot.jpeg
|
|
write_file(dir_name + "screenshot.jpeg", request.image());
|
|
for (const auto &fd : request.files()) {
|
|
// boost::filesystem::path p = fd.name();
|
|
auto name = os::sanitize_file_name(fd.name());
|
|
write_file(dir_name + name, fd.contents());
|
|
}
|
|
|
|
// log error for visibility
|
|
LOG(Gen, Error, "Support request from {} - {} {}", request.user(),
|
|
request.contact(), request.user_description());
|
|
|
|
ErrorResponse r;
|
|
reply(r);
|
|
}
|
|
|
|
} // namespace support
|
|
} // namespace bts
|