#include "log/backend-stderr.hpp"
#include "log/formatter.hpp"
-#include <sstream>
-#include <iomanip>
+#include <boost/tokenizer.hpp>
namespace security_containers {
namespace log {
const std::string& func,
const std::string& message)
{
- std::ostringstream logLine;
+ typedef boost::char_separator<char> charSeparator;
+ typedef boost::tokenizer<charSeparator> tokenizer;
// example log string
// 06:52:35.123 [ERROR] src/util/fs.cpp:43 readFileContent: /file/file.txt is missing
- logLine << LogFormatter::setConsoleColor(logLevel)
- << LogFormatter::getCurrentTime() << ' '
- << std::left << std::setw(8) << '[' + LogFormatter::toString(logLevel) + ']'
- << std::left << std::setw(52) << file + ':' + std::to_string(line) + ' ' + func + ':'
- << message
- << LogFormatter::setDefaultConsoleColor() << std::endl;
-
- fprintf(stderr, "%s", logLine.str().c_str());
+ const std::string logColor = LogFormatter::getConsoleColor(logLevel);
+ const std::string defaultColor = LogFormatter::getDefaultConsoleColor();
+ const std::string header = LogFormatter::getHeader(logLevel, file, line, func);
+ tokenizer tokens(message, charSeparator("\n"));
+ for(const auto& line : tokens) {
+ if (!line.empty()) {
+ fprintf(stderr,
+ "%s%s%s%s\n",
+ logColor.c_str(),
+ header.c_str(),
+ line.c_str(),
+ defaultColor.c_str());
+ }
+ }
}
#include <sys/time.h>
#include <cassert>
+#include <sstream>
+#include <iomanip>
namespace security_containers {
namespace log {
+const int TIME_COLUMN_LENGTH = 12;
+const int SEVERITY_COLUMN_LENGTH = 8;
+const int FILE_COLUMN_LENGTH = 52;
+
std::string LogFormatter::getCurrentTime(void)
{
- char time[13];
+ char time[TIME_COLUMN_LENGTH + 1];
struct timeval tv;
gettimeofday(&tv, NULL);
struct tm* tm = localtime(&tv.tv_sec);
return std::string(time);
}
-std::string LogFormatter::setConsoleColor(LogLevel logLevel)
+std::string LogFormatter::getConsoleColor(LogLevel logLevel)
{
switch (logLevel) {
case LogLevel::ERROR:
}
}
-std::string LogFormatter::setDefaultConsoleColor(void)
+std::string LogFormatter::getDefaultConsoleColor(void)
{
return getConsoleEscapeSequence(Attributes::DEFAULT, Color::DEFAULT);
}
return file.substr(SOURCE_DIR.size());
}
+std::string LogFormatter::getHeader(LogLevel logLevel,
+ const std::string& file,
+ const unsigned int& line,
+ const std::string& func)
+{
+ std::ostringstream logLine;
+ logLine << getCurrentTime() << ' '
+ << std::left << std::setw(SEVERITY_COLUMN_LENGTH) << '[' + toString(logLevel) + ']'
+ << std::left << std::setw(FILE_COLUMN_LENGTH)
+ << file + ':' + std::to_string(line) + ' ' + func + ':';
+ return logLine.str();
+}
+
} // namespace log
} // namespace security_containers
class LogFormatter {
public:
static std::string getCurrentTime(void);
- static std::string setConsoleColor(LogLevel logLevel);
- static std::string setDefaultConsoleColor(void);
+ static std::string getConsoleColor(LogLevel logLevel);
+ static std::string getDefaultConsoleColor(void);
static std::string toString(LogLevel logLevel);
static std::string stripProjectDir(const std::string& file);
+ static std::string getHeader(LogLevel logLevel,
+ const std::string& file,
+ const unsigned int& line,
+ const std::string& func);
};
} // namespace log