2016-02-22 05:21:15 +08:00
|
|
|
#pragma once
|
|
|
|
|
2016-07-18 01:32:35 +08:00
|
|
|
#include <iostream>
|
2016-08-29 01:50:54 +08:00
|
|
|
#include <sstream>
|
2016-09-05 17:02:48 +08:00
|
|
|
#include <string>
|
2016-10-19 23:07:40 +08:00
|
|
|
#include <map>
|
2016-02-22 05:21:15 +08:00
|
|
|
|
2016-08-08 16:32:34 +08:00
|
|
|
#include "fmt/format.h"
|
2016-09-05 17:02:48 +08:00
|
|
|
#include "logging/default.hpp"
|
2016-07-18 01:32:35 +08:00
|
|
|
#include "storage/model/properties/properties.hpp"
|
2016-09-05 17:02:48 +08:00
|
|
|
#include "storage/model/properties/property.hpp"
|
2016-08-02 22:41:53 +08:00
|
|
|
#include "storage/model/properties/traversers/consolewriter.hpp"
|
2016-08-08 16:32:34 +08:00
|
|
|
#include "storage/model/properties/traversers/jsonwriter.hpp"
|
2016-08-29 01:50:54 +08:00
|
|
|
#include "utils/types/byte.hpp"
|
2016-11-02 23:05:02 +08:00
|
|
|
#include "utils/exceptions/basic_exception.hpp"
|
2016-07-18 01:32:35 +08:00
|
|
|
|
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
template <class T>
|
|
|
|
void print_props(const Properties<T> &properties);
|
2016-07-18 01:32:35 +08:00
|
|
|
|
2016-08-10 16:39:02 +08:00
|
|
|
#ifdef NDEBUG
|
2016-07-18 01:32:35 +08:00
|
|
|
#define PRINT_PROPS(_)
|
2016-08-10 16:39:02 +08:00
|
|
|
#else
|
|
|
|
#define PRINT_PROPS(_PROPS_) print_props(_PROPS_);
|
2016-07-18 01:32:35 +08:00
|
|
|
#endif
|
2016-08-02 22:41:53 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
template <class T>
|
|
|
|
void cout_properties(const Properties<T> &properties);
|
2016-08-10 16:39:02 +08:00
|
|
|
|
2016-09-05 17:02:48 +08:00
|
|
|
template <class T>
|
|
|
|
void cout_property(const StoredProperty<T> &property);
|
2016-08-02 22:41:53 +08:00
|
|
|
|
2016-08-10 16:39:02 +08:00
|
|
|
// this is a nice way how to avoid multiple definition problem with
|
|
|
|
// headers because it will create a unique namespace for each compilation unit
|
|
|
|
// http://stackoverflow.com/questions/2727582/multiple-definition-in-header-file
|
|
|
|
namespace
|
2016-08-02 22:41:53 +08:00
|
|
|
{
|
2016-08-08 16:32:34 +08:00
|
|
|
|
2016-11-02 23:05:02 +08:00
|
|
|
class CodeLineFormatException : public BasicException
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using BasicException::BasicException;
|
|
|
|
};
|
|
|
|
|
2016-08-08 16:32:34 +08:00
|
|
|
template <typename... Args>
|
|
|
|
std::string format(const std::string &format_str, const Args &... args)
|
|
|
|
{
|
|
|
|
return fmt::format(format_str, args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
std::string code_line(const std::string &format_str, const Args &... args)
|
|
|
|
{
|
2016-09-05 17:02:48 +08:00
|
|
|
try {
|
|
|
|
return "\t" + format(format_str, args...) + "\n";
|
|
|
|
} catch (std::runtime_error &e) {
|
2016-11-02 23:05:02 +08:00
|
|
|
throw CodeLineFormatException(std::string(e.what()) + " " + format_str);
|
2016-09-05 17:02:48 +08:00
|
|
|
}
|
2016-08-08 16:32:34 +08:00
|
|
|
}
|
2016-10-19 23:07:40 +08:00
|
|
|
|
|
|
|
using name_properties_t = std::vector<std::pair<std::string, Property>>;
|
|
|
|
|
|
|
|
auto query_properties(const std::map<std::string, int64_t> &indices,
|
|
|
|
properties_t &values)
|
|
|
|
{
|
|
|
|
name_properties_t properties;
|
|
|
|
for (auto &property_index : indices) {
|
|
|
|
properties.push_back(
|
|
|
|
std::make_pair(std::move(property_index.first),
|
|
|
|
std::move(values[property_index.second])));
|
|
|
|
}
|
|
|
|
return properties;
|
2016-08-10 16:39:02 +08:00
|
|
|
}
|
2016-08-29 01:50:54 +08:00
|
|
|
|
|
|
|
class CoutSocket
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CoutSocket() : logger(logging::log->logger("Cout Socket")) {}
|
|
|
|
|
2016-09-05 17:02:48 +08:00
|
|
|
int write(const std::string &str)
|
2016-08-29 01:50:54 +08:00
|
|
|
{
|
|
|
|
logger.info(str);
|
|
|
|
return str.size();
|
|
|
|
}
|
|
|
|
|
2016-09-05 17:02:48 +08:00
|
|
|
int write(const char *data, size_t len)
|
2016-08-29 01:50:54 +08:00
|
|
|
{
|
|
|
|
logger.info(std::string(data, len));
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2016-09-05 17:02:48 +08:00
|
|
|
int write(const byte *data, size_t len)
|
2016-08-29 01:50:54 +08:00
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
ss << data[i];
|
|
|
|
}
|
|
|
|
std::string output(ss.str());
|
|
|
|
cout << output << endl;
|
|
|
|
logger.info(output);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Logger logger;
|
|
|
|
};
|
2016-10-19 23:07:40 +08:00
|
|
|
|
|
|
|
}
|