Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json log format #115

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/triton/common/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace triton { namespace common {
//
class Error {
public:
enum class Code {
enum class Code : uint8_t {
SUCCESS,
UNKNOWN,
INTERNAL,
Expand Down
52 changes: 47 additions & 5 deletions include/triton/common/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
#include <sstream>
#include <string>
#include <vector>
#ifdef _WIN32
// suppress the min and max definitions in Windef.h.
#define NOMINMAX
#include <Windows.h>
#else
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#endif


namespace triton { namespace common {

Expand All @@ -41,20 +52,51 @@ class LogMessage {
// Log levels.
enum Level { kERROR = 0, kWARNING = 1, kINFO = 2 };

LogMessage(const char* file, int line, uint32_t level);
LogMessage(const char* file, int line, uint32_t level)
: path_(file), line_(line),
level_(std::min(level, (uint32_t)Level::kINFO)), pid_(GetProcessId())
{
SetTimestamp();
size_t path_start = path_.rfind('/');
if (path_start != std::string::npos) {
path_ = path_.substr(path_start + 1, std::string::npos);
}
}


~LogMessage();

std::stringstream& stream() { return stream_; }
std::stringstream& stream() { return message_; }

private:
static const std::vector<char> level_name_;
std::stringstream stream_;
static const std::array<const char*, Level::kINFO + 1> LEVEL_NAMES_;
std::string path_;
const int line_;
const uint32_t level_;
const uint32_t pid_;
void LogPreamble(std::stringstream& stream);
void LogTimestamp(std::stringstream& stream);

#ifdef _WIN32
SYSTEMTIME timestamp_;
void SetTimestamp() { GetSystemTime(&timestamp_); }
static uint32_t GetProcessId()
{
return static_cast<uint32_t>(GetCurrentProcessId());
};
#else
struct timeval timestamp_;
void SetTimestamp() { gettimeofday(&timestamp_, NULL); }
static uint32_t GetProcessId() { return static_cast<uint32_t>(getpid()); };
#endif

std::stringstream message_;
};

// Global logger for messages. Controls how log messages are reported.
class Logger {
public:
enum class Format { kDEFAULT, kISO8601 };
enum class Format { kDEFAULT, kISO8601, kJSONL };

Logger();

Expand Down
14 changes: 14 additions & 0 deletions include/triton/common/triton_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once


#ifdef _WIN32
// Remove GetObject definition from windows.h, which prevents calls to
// RapidJSON's GetObject.
Expand Down Expand Up @@ -109,6 +110,19 @@ class TritonJson {
std::string buffer_;
};

static std::string EscapeString(const std::string& input)
{
WriteBuffer writebuffer;
const unsigned int writeFlags = rapidjson::kWriteNanAndInfFlag;
// Provide default template arguments to pass writeFlags
rapidjson::Writer<
WriteBuffer, rapidjson::UTF8<>, rapidjson::UTF8<>,
rapidjson::CrtAllocator, writeFlags>
writer(writebuffer);
writer.String(input.c_str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to ask if this can fail if the input has some malformed or half-formed JSON in it, but it seems like it won't: Tencent/rapidjson#1767 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error handling was a little tricky w.r.t to we usually log an error - so I didn't want to create a recursion by accident. Writing and error causes an error .... so would probably have to fail silently ... or print directly to the stdout ...

return writebuffer.Contents();
}

//
// Value representing the entire document or an element within a
// document.
Expand Down
197 changes: 128 additions & 69 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,28 @@

#include "triton/common/logging.h"

#ifdef _WIN32
// suppress the min and max definitions in Windef.h.
#define NOMINMAX
#include <Windows.h>
#else
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#endif
#include <algorithm>
#include <iomanip>
#include <iostream>

#include "triton/common/error.h"

static const uint8_t INTERNAL_ERROR =
static_cast<uint8_t>(triton::common::Error::Code::INTERNAL);

static const uint8_t SUCCESS =
static_cast<uint8_t>(triton::common::Error::Code::SUCCESS);

#define TRITONJSON_STATUSTYPE uint8_t
#define TRITONJSON_STATUSRETURN(M) \
do { \
LOG_ERROR << (M) << ": " << INTERNAL_ERROR; \
return INTERNAL_ERROR; \
} while (false)

#define TRITONJSON_STATUSSUCCESS SUCCESS
#include "triton/common/triton_json.h"

namespace triton { namespace common {

Logger gLogger_;
Expand All @@ -66,82 +74,133 @@ Logger::Flush()
std::cerr << std::flush;
}

const std::array<const char*, LogMessage::Level::kINFO + 1>
LogMessage::LEVEL_NAMES_{"ERROR", "WARNING", "INFO"};

const std::vector<char> LogMessage::level_name_{'E', 'W', 'I'};
#ifdef _WIN32

LogMessage::LogMessage(const char* file, int line, uint32_t level)
void
LogMessage::LogTimestamp(std::stringstream& stream)
{
std::string path(file);
size_t pos = path.rfind('/');
if (pos != std::string::npos) {
path = path.substr(pos + 1, std::string::npos);
switch (gLogger_.LogFormat()) {
case Logger::Format::kDEFAULT: {
stream << std::setfill('0') << std::setw(2) << timestamp_.wMonth
<< std::setw(2) << timestamp_.wDay << ' ' << std::setw(2)
<< timestamp_.wHour << ':' << std::setw(2) << timestamp_.wMinute
<< ':' << std::setw(2) << timestamp_.wSecond << '.' << std::setw(6)
<< timestamp_.wMilliseconds * 1000;
break;
}
case Logger::Format::kISO8601: {
stream << timestamp_.wYear << '-' << std::setfill('0') << std::setw(2)
<< timestamp_.wMonth << '-' << std::setw(2) << timestamp_.wDay
<< 'T' << std::setw(2) << timestamp_.wHour << ':' << std::setw(2)
<< timestamp_.wMinute << ':' << std::setw(2) << timestamp_.wSecond
<< "Z";
break;
}
case Logger::Format::JSONL: {
stream << timestamp_.wYear << '-' << std::setfill('0') << std::setw(2)
<< timestamp_.wMonth << '-' << std::setw(2) << timestamp_.wDay
<< 'T' << std::setw(2) << timestamp_.wHour << ':' << std::setw(2)
<< timestamp_.wMinute << ':' << std::setw(2) << timestamp_.wSecond
<< '.' << std::setw(6) << timestamp_.wMilliseconds * 1000 << "Z";
break;
}
}
}
#else
void
LogMessage::LogTimestamp(std::stringstream& stream)
{
struct tm tm_time;
gmtime_r(((time_t*)&(timestamp_.tv_sec)), &tm_time);

// 'L' below is placeholder for showing log level
switch (gLogger_.LogFormat()) {
case Logger::Format::kDEFAULT: {
// LMMDD hh:mm:ss.ssssss
#ifdef _WIN32
SYSTEMTIME system_time;
GetSystemTime(&system_time);
stream_ << level_name_[std::min(level, (uint32_t)Level::kINFO)]
<< std::setfill('0') << std::setw(2) << system_time.wMonth
<< std::setw(2) << system_time.wDay << ' ' << std::setw(2)
<< system_time.wHour << ':' << std::setw(2) << system_time.wMinute
<< ':' << std::setw(2) << system_time.wSecond << '.'
<< std::setw(6) << system_time.wMilliseconds * 1000 << ' '
<< static_cast<uint32_t>(GetCurrentProcessId()) << ' ' << path
<< ':' << line << "] ";
#else
struct timeval tv;
gettimeofday(&tv, NULL);
struct tm tm_time;
gmtime_r(((time_t*)&(tv.tv_sec)), &tm_time);
stream_ << level_name_[std::min(level, (uint32_t)Level::kINFO)]
<< std::setfill('0') << std::setw(2) << (tm_time.tm_mon + 1)
<< std::setw(2) << tm_time.tm_mday << ' ' << std::setw(2)
<< tm_time.tm_hour << ':' << std::setw(2) << tm_time.tm_min << ':'
<< std::setw(2) << tm_time.tm_sec << '.' << std::setw(6)
<< tv.tv_usec << ' ' << static_cast<uint32_t>(getpid()) << ' '
<< path << ':' << line << "] ";
#endif
stream << std::setfill('0') << std::setw(2) << (tm_time.tm_mon + 1)
<< std::setw(2) << tm_time.tm_mday << ' ' << std::setw(2)
<< tm_time.tm_hour << ':' << std::setw(2) << tm_time.tm_min << ':'
<< std::setw(2) << tm_time.tm_sec << '.' << std::setw(6)
<< timestamp_.tv_usec;
break;
}
case Logger::Format::kISO8601: {
// YYYY-MM-DDThh:mm:ssZ L
#ifdef _WIN32
SYSTEMTIME system_time;
GetSystemTime(&system_time);
stream_ << system_time.wYear << '-' << std::setfill('0') << std::setw(2)
<< system_time.wMonth << '-' << std::setw(2) << system_time.wDay
<< 'T' << std::setw(2) << system_time.wHour << ':' << std::setw(2)
<< system_time.wMinute << ':' << std::setw(2)
<< system_time.wSecond << "Z "
<< level_name_[std::min(level, (uint32_t)Level::kINFO)] << ' '
<< static_cast<uint32_t>(GetCurrentProcessId()) << ' ' << path
<< ':' << line << "] ";
#else
struct timeval tv;
gettimeofday(&tv, NULL);
struct tm tm_time;
gmtime_r(((time_t*)&(tv.tv_sec)), &tm_time);
stream_ << (tm_time.tm_year + 1900) << '-' << std::setfill('0')
<< std::setw(2) << (tm_time.tm_mon + 1) << '-' << std::setw(2)
<< tm_time.tm_mday << 'T' << std::setw(2) << tm_time.tm_hour
<< ':' << std::setw(2) << tm_time.tm_min << ':' << std::setw(2)
<< tm_time.tm_sec << "Z "
<< level_name_[std::min(level, (uint32_t)Level::kINFO)] << ' '
<< static_cast<uint32_t>(getpid()) << ' ' << path << ':' << line
<< "] ";
stream << (tm_time.tm_year + 1900) << '-' << std::setfill('0')
<< std::setw(2) << (tm_time.tm_mon + 1) << '-' << std::setw(2)
<< tm_time.tm_mday << 'T' << std::setw(2) << tm_time.tm_hour << ':'
<< std::setw(2) << tm_time.tm_min << ':' << std::setw(2)
<< tm_time.tm_sec << "Z";
break;
}
case Logger::Format::kJSONL: {
stream << (tm_time.tm_year + 1900) << '-' << std::setfill('0')
<< std::setw(2) << (tm_time.tm_mon + 1) << '-' << std::setw(2)
<< tm_time.tm_mday << 'T' << std::setw(2) << tm_time.tm_hour << ':'
<< std::setw(2) << tm_time.tm_min << ':' << std::setw(2)
<< tm_time.tm_sec << '.' << std::setw(6) << timestamp_.tv_usec
<< "Z";
break;
}
}
}

#endif

void
LogMessage::LogPreamble(std::stringstream& stream)
{
switch (gLogger_.LogFormat()) {
case Logger::Format::kDEFAULT: {
stream << LEVEL_NAMES_[level_][0];
LogTimestamp(stream);
stream << ' ' << pid_ << ' ' << path_ << ':' << line_ << "] ";

break;
}
case Logger::Format::kISO8601: {
LogTimestamp(stream);
stream << " " << LEVEL_NAMES_[level_][0] << ' ' << pid_ << ' ' << path_
<< ':' << line_ << "] ";
break;
}
case Logger::Format::kJSONL: {
break;
}
}
}


LogMessage::~LogMessage()
{
gLogger_.Log(stream_.str());
}
// gLogger_.SetLogFormat(Logger::Format::kJSONL);

switch (gLogger_.LogFormat()) {
case Logger::Format::kDEFAULT:
case Logger::Format::kISO8601: {
std::stringstream preamble;
LogPreamble(preamble);
// std::string escaped(message_.str());
rmccorm4 marked this conversation as resolved.
Show resolved Hide resolved
std::string escaped = TritonJson::EscapeString(message_.str());
preamble << escaped;
gLogger_.Log(preamble.str());
break;
}
case Logger::Format::kJSONL: {
TritonJson::Value logMessage(TritonJson::ValueType::OBJECT);
TritonJson::WriteBuffer buffer;
std::stringstream timestamp;
LogTimestamp(timestamp);
logMessage.AddString("file", path_);
logMessage.AddInt("line", line_);
logMessage.AddString("level", LEVEL_NAMES_[level_]);
logMessage.AddInt("process_id", pid_);
logMessage.AddString("message", message_.str());
logMessage.AddString("timestamp", timestamp.str());
logMessage.Write(&buffer);
gLogger_.Log(buffer.Contents());
break;
}
}
}
}} // namespace triton::common
Loading