[Libreoffice-commits] online.git: common/Log.cpp common/Log.hpp
Ashod Nakashian
ashod.nakashian at collabora.co.uk
Mon Jan 23 04:35:05 UTC 2017
common/Log.cpp | 49 ++++++++++++++++++++-----------------------------
common/Log.hpp | 41 +++++++++++++++++++++++------------------
2 files changed, 43 insertions(+), 47 deletions(-)
New commits:
commit ffe092f5ce4fc1fb15cbaca3ee5f1930dd74affd
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date: Thu Jan 19 19:26:57 2017 -0500
wsd: log cleanup
Change-Id: I569f7a81fa80686810c4d5aa65e03b02be189db7
Reviewed-on: https://gerrit.libreoffice.org/33417
Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
Tested-by: Ashod Nakashian <ashnakash at gmail.com>
diff --git a/common/Log.cpp b/common/Log.cpp
index e201dbf..5defd6b 100644
--- a/common/Log.cpp
+++ b/common/Log.cpp
@@ -8,6 +8,8 @@
*/
#include <sys/prctl.h>
+#include <sys/syscall.h>
+#include <unistd.h>
#include <atomic>
#include <cassert>
@@ -16,9 +18,6 @@
#include <sstream>
#include <string>
-#include <sys/syscall.h>
-#include <unistd.h>
-
#include <Poco/ConsoleChannel.h>
#include <Poco/DateTimeFormatter.h>
#include <Poco/FileChannel.h>
@@ -76,36 +75,28 @@ namespace Log
}
}
- static void getPrefix(char *buffer, const char* level)
+ char* prefix(char* buffer, const char* level, const long osTid)
{
- char procName[32]; // we really need only 16
+ char procName[32];
if (prctl(PR_GET_NAME, reinterpret_cast<unsigned long>(procName), 0, 0, 0) != 0)
{
strncpy(procName, "<noid>", sizeof(procName) - 1);
}
- const char* appName = (Source.inited ? Source.id.c_str() : "<shutdown>");
- assert(strlen(appName) + 32 + 28 < 1024 - 1);
-
Poco::DateTime time;
- snprintf(buffer, 1023, "%s-%.04lu %.2u:%.2u:%.2u.%.6u [ %s ] %s ", appName,
- syscall(SYS_gettid),
- time.hour(), time.minute(), time.second(),
- time.millisecond() * 1000 + time.microsecond(),
- procName, level);
- }
-
- std::string prefix(const char* level)
- {
- char buffer[1024];
- getPrefix(buffer, level);
- return std::string(buffer);
+ snprintf(buffer, 1023, "%s-%.04lu %.2u:%.2u:%.2u.%.6u [ %s ] %s ",
+ (Source.inited ? Source.id.c_str() : "<shutdown>"),
+ osTid,
+ time.hour(), time.minute(), time.second(),
+ time.millisecond() * 1000 + time.microsecond(),
+ procName, level);
+ return buffer;
}
void signalLogPrefix()
{
char buffer[1024];
- getPrefix(buffer, "SIG");
+ prefix(buffer, "SIG", syscall(SYS_gettid));
signalLog(buffer);
}
@@ -167,42 +158,42 @@ namespace Log
void trace(const std::string& msg)
{
- logger().trace(prefix("TRC") + msg);
+ LOG_TRC(msg);
}
void debug(const std::string& msg)
{
- logger().debug(prefix("DBG") + msg);
+ LOG_DBG(msg);
}
void info(const std::string& msg)
{
- logger().information(prefix("INF") + msg);
+ LOG_INF(msg);
}
void warn(const std::string& msg)
{
- logger().warning(prefix("WRN") + msg);
+ LOG_WRN(msg);
}
void error(const std::string& msg)
{
- logger().error(prefix("ERR") + msg);
+ LOG_ERR(msg);
}
void syserror(const std::string& msg)
{
- logger().error(prefix("ERR") + msg + " (errno: " + std::string(std::strerror(errno)) + ")");
+ LOG_SYS(msg);
}
void fatal(const std::string& msg)
{
- logger().fatal(prefix("FTL") + msg);
+ LOG_FTL(msg);
}
void sysfatal(const std::string& msg)
{
- logger().fatal(prefix("FTL") + msg + " (errno: " + std::string(std::strerror(errno)) + ")");
+ LOG_SFL(msg);
}
}
diff --git a/common/Log.hpp b/common/Log.hpp
index b4d42c7..d8788a2 100644
--- a/common/Log.hpp
+++ b/common/Log.hpp
@@ -10,6 +10,9 @@
#ifndef INCLUDED_LOG_HPP
#define INCLUDED_LOG_HPP
+#include <sys/syscall.h>
+#include <unistd.h>
+
#include <functional>
#include <sstream>
#include <string>
@@ -24,7 +27,8 @@ namespace Log
const bool logToFile,
std::map<std::string, std::string> config);
Poco::Logger& logger();
- std::string prefix(const char* level);
+
+ char* prefix(char* buffer, const char* level, const long osTid);
void trace(const std::string& msg);
void debug(const std::string& msg);
@@ -35,12 +39,12 @@ namespace Log
void fatal(const std::string& msg);
void sysfatal(const std::string& msg);
- inline bool traceEnabled() { return logger().getLevel() >= Poco::Message::PRIO_TRACE; }
- inline bool debugEnabled() { return logger().getLevel() >= Poco::Message::PRIO_DEBUG; }
- inline bool infoEnabled() { return logger().getLevel() >= Poco::Message::PRIO_INFORMATION; }
- inline bool warnEnabled() { return logger().getLevel() >= Poco::Message::PRIO_WARNING; }
- inline bool errorEnabled() { return logger().getLevel() >= Poco::Message::PRIO_ERROR; }
- inline bool fatalEnabled() { return logger().getLevel() >= Poco::Message::PRIO_FATAL; }
+ inline bool traceEnabled() { return logger().trace(); }
+ inline bool debugEnabled() { return logger().debug(); }
+ inline bool infoEnabled() { return logger().information(); }
+ inline bool warnEnabled() { return logger().warning(); }
+ inline bool errorEnabled() { return logger().error(); }
+ inline bool fatalEnabled() { return logger().fatal(); }
/// Signal safe prefix logging
void signalLogPrefix();
@@ -68,11 +72,12 @@ namespace Log
{
}
- StreamLogger(std::function<void(const std::string&)> func, const char* level)
+ StreamLogger(std::function<void(const std::string&)> func, const char*level)
: _func(std::move(func)),
_enabled(true)
{
- _stream << prefix(level);
+ char buffer[1024];
+ _stream << prefix(buffer, level, syscall(SYS_gettid));
}
StreamLogger(StreamLogger&& sl) noexcept
@@ -170,15 +175,15 @@ namespace Log
}
}
-#define LOG_BODY(LVL, X) std::ostringstream oss_; oss_ << Log::prefix(LVL) << std::boolalpha << X << "| " << __FILE__ << ':' << __LINE__
-#define LOG_TRC(X) do { if (Log::traceEnabled()) { LOG_BODY("TRC", X); Log::logger().trace(oss_.str()); } } while (false)
-#define LOG_DBG(X) do { if (Log::debugEnabled()) { LOG_BODY("DBG", X); Log::logger().debug(oss_.str()); } } while (false)
-#define LOG_INF(X) do { if (Log::infoEnabled()) { LOG_BODY("INF", X); Log::logger().information(oss_.str()); } } while (false)
-#define LOG_WRN(X) do { if (Log::warnEnabled()) { LOG_BODY("WRN", X); Log::logger().warning(oss_.str()); } } while (false)
-#define LOG_ERR(X) do { if (Log::errorEnabled()) { LOG_BODY("ERR", X); Log::logger().error(oss_.str()); } } while (false)
-#define LOG_SYS(X) do { if (Log::errorEnabled()) { LOG_BODY("ERR", X << " (errno: " << std::strerror(errno) << ")"); Log::logger().error(oss_.str()); } } while (false)
-#define LOG_FTL(X) do { if (Log::fatalEnabled()) { LOG_BODY("FTL", X); Log::logger().fatal(oss_.str()); } } while (false)
-#define LOG_SFL(X) do { if (Log::errorEnabled()) { LOG_BODY("FTL", X << " (errno: " << std::strerror(errno) << ")"); Log::logger().fatal(oss_.str()); } } while (false)
+#define LOG_BODY_(PRIO, LVL, X) Poco::Message m_(l_.name(), "", Poco::Message::PRIO_##PRIO); char b_[1024]; std::ostringstream oss_(Log::prefix(b_, LVL, m_.getOsTid()), std::ostringstream::ate); oss_ << std::boolalpha << X << "| " << __FILE__ << ':' << __LINE__; m_.setText(oss_.str()); l_.log(m_);
+#define LOG_TRC(X) do { auto& l_ = Log::logger(); if (l_.trace()) { LOG_BODY_(TRACE, "TRC", X); } } while (false)
+#define LOG_DBG(X) do { auto& l_ = Log::logger(); if (l_.debug()) { LOG_BODY_(DEBUG, "DBG", X); } } while (false)
+#define LOG_INF(X) do { auto& l_ = Log::logger(); if (l_.information()) { LOG_BODY_(INFORMATION, "INF", X); } } while (false)
+#define LOG_WRN(X) do { auto& l_ = Log::logger(); if (l_.warning()) { LOG_BODY_(WARNING, "WRN", X); } } while (false)
+#define LOG_ERR(X) do { auto& l_ = Log::logger(); if (l_.error()) { LOG_BODY_(ERROR, "ERR", X); } } while (false)
+#define LOG_SYS(X) do { auto& l_ = Log::logger(); if (l_.error()) { LOG_BODY_(ERROR, "ERR", X << " (errno: " << std::strerror(errno) << ")"); } } while (false)
+#define LOG_FTL(X) do { auto& l_ = Log::logger(); if (l_.fatal()) { LOG_BODY_(FATAL, "FTL", X); } } while (false)
+#define LOG_SFL(X) do { auto& l_ = Log::logger(); if (l_.error()) { LOG_BODY_(FATAL, "FTL", X << " (errno: " << std::strerror(errno) << ")"); } } while (false)
#define LOG_CHECK(X) do { if (!(X)) { LOG_ERR("Check failed. Expected (" #X ")."); } } while (false)
#define LOG_CHECK_RET(X, RET) do { if (!(X)) { LOG_ERR("Check failed. Expected (" #X ")."); return RET; } } while (false)
More information about the Libreoffice-commits
mailing list