Add time stamp to logging

This commit is contained in:
Thomas Krijnen
2021-09-13 14:52:23 +02:00
parent 1058dc7099
commit 99cd952c98
+14
View File
@@ -32,8 +32,17 @@
#include <mutex>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <iomanip>
namespace {
std::string get_time() {
std::ostringstream oss;
time_t now = time(nullptr);
oss << std::put_time(localtime(&now), "%F %T");
return oss.str();
}
template <typename T>
struct severity_strings {
@@ -49,6 +58,7 @@ namespace {
template <typename T>
void plain_text_message(T& os, const boost::optional<IfcUtil::IfcBaseClass*>& current_product, Logger::Severity type, const std::string& message, const IfcUtil::IfcBaseInterface* instance) {
os << "[" << severity_strings<typename T::char_type>::value[type] << "] ";
os << "[" << get_time().c_str() << "] ";
if (current_product) {
std::string global_id = *((IfcUtil::IfcBaseEntity*)*current_product)->get("GlobalId");
os << "{" << global_id.c_str() << "} ";
@@ -75,6 +85,7 @@ namespace {
boost::property_tree::basic_ptree<std::basic_string<typename T::char_type>, std::basic_string<typename T::char_type> > pt;
// @todo this is crazy
static const typename T::char_type time_string[] = { 't', 'i', 'm', 'e', 0 };
static const typename T::char_type level_string[] = { 'l', 'e', 'v', 'e', 'l', 0 };
static const typename T::char_type product_string[] = { 'p', 'r', 'o', 'd', 'u', 'c', 't', 0 };
static const typename T::char_type message_string[] = { 'm', 'e', 's', 's', 'a', 'g', 'e', 0 };
@@ -88,6 +99,9 @@ namespace {
if (instance) {
pt.put(instance_string, string_as<typename T::char_type>(instance->data().toString()));
}
pt.put(time_string, string_as<typename T::char_type>(get_time()));
boost::property_tree::write_json(os, pt, false);
}
}