Skip to content

Commit

Permalink
Update logging format
Browse files Browse the repository at this point in the history
  • Loading branch information
davschneller committed Jul 8, 2024
1 parent 9b926a9 commit 13ea706
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
28 changes: 18 additions & 10 deletions logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#ifndef UTILS_LOGGER_H_
#define UTILS_LOGGER_H_

#include "utils/stringutils.h"
#include "utils/timeutils.h"

#include <algorithm>
#include <chrono>
#include <csignal>
#include <cstdlib>
#include <ctime>
Expand Down Expand Up @@ -57,9 +59,6 @@ namespace utils {
*/
class Logger {
public:
// cf. https://stackoverflow.com/a/72665316
const static inline std::string LogDateFormat = "%Y-%m-%dT%H:%M:%S.%f";

/** Message type */
enum DebugType {
/** A debug messages */
Expand Down Expand Up @@ -105,28 +104,37 @@ class Logger {
* 0 will be printed
*/
Logger(DebugType t, int rank) : stream(new Stream(t, rank)) {
stream->buffer << utils::TimeUtils::timeAsString(LogDateFormat.c_str());
auto timepoint = std::chrono::system_clock::now();
auto milli = std::chrono::duration_cast<std::chrono::milliseconds>(
timepoint.time_since_epoch())
.count();
time_t time = std::chrono::system_clock::to_time_t(timepoint);

stream->buffer << utils::TimeUtils::timeAsString("%F %T", time) << "."
<< StringUtils::padLeft(std::to_string(milli), 3, ' ');

switch (t) {
case LOG_DEBUG:
stream->buffer << "| DEBG | ";
stream->buffer << " debug ";
break;
case LOG_INFO:
stream->buffer << "| INFO |";
stream->buffer << " info ";
break;
case LOG_WARNING:
stream->buffer << "| WARN | ";
stream->buffer << " warning ";
break;
case LOG_ERROR:
stream->buffer << "| ERROR | ";
stream->buffer << " error ";
break;
default:
stream->buffer << "| UNKNOWN | ";
stream->buffer << " unknown ";
break;
}

if (rank >= 0) {
stream->buffer << "RANK " << rank << " | ";
stream->buffer << rank << " ";
} else {
stream->buffer << "- ";
}
}
/**
Expand Down
30 changes: 30 additions & 0 deletions stringutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ class StringUtils {
str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}

static std::string padLeft(const std::string &str, std::size_t size,
char padchar) {
if (str.size() >= size) {
return str;
} else {
std::stringstream stream;
std::size_t padlen = size - str.size();
for (std::size_t i = 0; i < padlen; ++i) {
stream << padchar;
}
stream << str;
return stream.str();
}
}

static std::string padRight(const std::string &str, std::size_t size,
char padchar) {
if (str.size() >= size) {
return str;
} else {
std::stringstream stream;
stream << str;
std::size_t padlen = size - str.size();
for (std::size_t i = 0; i < padlen; ++i) {
stream << padchar;
}
return stream.str();
}
}

/**
* Converts arbitrary datatypes (all datatypes which support the << stream
* operator) into std::string
Expand Down

0 comments on commit 13ea706

Please sign in to comment.