Skip to content

Commit 1fd9c17

Browse files
authored
clean up json_value & server_log (#7142)
1 parent 4cd621c commit 1fd9c17

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

examples/server/utils.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,35 @@ extern bool server_log_json;
4949
#define LOG_WARNING(MSG, ...) server_log("WARN", __func__, __LINE__, MSG, __VA_ARGS__)
5050
#define LOG_INFO( MSG, ...) server_log("INFO", __func__, __LINE__, MSG, __VA_ARGS__)
5151

52-
static inline void server_log(const char *level, const char *function, int line, const char *message, const nlohmann::ordered_json &extra);
52+
static inline void server_log(const char * level, const char * function, int line, const char * message, const json & extra);
5353

5454
template <typename T>
55-
static T json_value(const json &body, const std::string &key, const T &default_value) {
55+
static T json_value(const json & body, const std::string & key, const T & default_value) {
5656
// Fallback null to default value
57-
if (body.contains(key) && !body.at(key).is_null()){
57+
if (body.contains(key) && !body.at(key).is_null()) {
5858
try {
59-
return body.value(key, default_value);
60-
}
61-
catch (nlohmann::json_abi_v3_11_3::detail::type_error const&){
62-
std::string message = "Wrong type supplied for parameter '" + key + "'. Expected '" + typeid(default_value).name() + "', using default value.";
63-
server_log("WARN", __func__, __LINE__, message.c_str(), body);
59+
return body.at(key);
60+
} catch (NLOHMANN_JSON_NAMESPACE::detail::type_error const &) {
61+
std::stringstream ss;
62+
ss << "Wrong type supplied for parameter '" << key << "'. Expected '" << json(default_value).type_name() << "', using default value.";
63+
LOG_WARNING(ss.str().c_str(), body);
6464
return default_value;
6565
}
6666
} else {
6767
return default_value;
6868
}
6969
}
7070

71-
static inline void server_log(const char *level, const char *function, int line, const char *message, const nlohmann::ordered_json &extra) {
71+
static inline void server_log(const char * level, const char * function, int line, const char * message, const json & extra) {
7272
std::stringstream ss_tid;
7373
ss_tid << std::this_thread::get_id();
74-
json log = nlohmann::ordered_json{
74+
json log = json{
7575
{"tid", ss_tid.str()},
7676
{"timestamp", time(nullptr)},
7777
};
7878

7979
if (server_log_json) {
80-
log.merge_patch( {
80+
log.merge_patch({
8181
{"level", level},
8282
{"function", function},
8383
{"line", line},
@@ -98,7 +98,7 @@ static inline void server_log(const char *level, const char *function, int line,
9898
}
9999
std::stringstream ss;
100100
ss << buf << " |";
101-
for (const auto& el : log.items())
101+
for (const auto & el : log.items())
102102
{
103103
const std::string value = el.value().dump(-1, ' ', false, json::error_handler_t::replace);
104104
ss << " " << el.key() << "=" << value;

0 commit comments

Comments
 (0)