osrm-backend/include/util/json_logger.hpp

63 lines
1.0 KiB
C++
Raw Normal View History

#ifndef JSON_LOGGER_HPP
#define JSON_LOGGER_HPP
2016-01-02 11:13:44 -05:00
#include "osrm/json_container.hpp"
#include <boost/thread/tss.hpp>
#include <string>
#include <unordered_map>
namespace osrm
{
2016-01-05 10:51:13 -05:00
namespace util
{
namespace json
{
// Used to append additional debugging information to the JSON response in a
// thread safe manner.
class Logger
{
2016-01-05 10:51:13 -05:00
using MapT = std::unordered_map<std::string, Value>;
public:
2016-01-05 06:04:04 -05:00
static Logger *get()
{
static Logger logger;
2015-03-22 19:28:38 -04:00
bool return_logger = true;
#ifdef NDEBUG
2015-03-22 19:28:38 -04:00
return_logger = false;
#endif
#ifdef ENABLE_JSON_LOGGING
return_logger = true;
#endif
2015-03-22 19:28:38 -04:00
if (return_logger)
{
return &logger;
}
return nullptr;
}
2016-01-05 06:04:04 -05:00
void initialize(const std::string &name)
{
if (!map.get())
{
map.reset(new MapT());
}
(*map)[name] = Object();
}
2016-01-05 06:04:04 -05:00
void render(const std::string &name, Object &obj) const { obj.values["debug"] = map->at(name); }
boost::thread_specific_ptr<MapT> map;
};
2016-01-05 10:51:13 -05:00
}
}
}
#endif /* JSON_LOGGER_HPP */