Skip to content

Commit

Permalink
update foxxll and tlx, use tlx's logger
Browse files Browse the repository at this point in the history
  • Loading branch information
bingmann committed May 23, 2018
1 parent 7008013 commit 458e409
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 244 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
url = https://github.com/tlx/tlx.git
[submodule "extlib/foxxll"]
path = extlib/foxxll
url = https://github.com/foxxll/foxxll.git
url = https://github.com/stxxl/foxxll.git
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Clemens Wallrath <clemens.wallrath@nerven.gift>
Emanuel Jöbstl <emanuel.joebstl@gmail.com>
Florian Kurpicz <florian.kurpicz@tu-dortmund.de>
Huyen Chau Nguyen <hello@chau-nguyen.de>
Johannes Singler <singler@ira.uka.de>
Johannes Singler <singler@kit.edu>
Lorenz Hübschle-Schneider <lorenz@4z2.de>
Martin Aumüller <martin.aumueller@tu-ilmenau.de>
Matthias Stumpp <mstumpp@gmail.com>
Expand Down
2 changes: 1 addition & 1 deletion extlib/foxxll
Submodule foxxll updated 140 files
2 changes: 1 addition & 1 deletion extlib/tlx
Submodule tlx updated 126 files
118 changes: 45 additions & 73 deletions thrill/common/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
namespace thrill {
namespace common {

/******************************************************************************/

//! memory manager singleton for Logger
mem::Manager g_logger_mem_manager(nullptr, "Logger");

Expand All @@ -44,20 +42,6 @@ void NameThisThread(const mem::by_string& name) {
s_message_counter = 0;
}

//! Outputs the name of the current thread or 'unknown [id]'
void FormatNameForThisThread(std::ostream& os) {
if (!s_thread_name.empty()) {
os << s_thread_name << ' ';
}
else {
os << "unknown " << std::this_thread::get_id() << ' ';
}

std::ios::fmtflags flags(os.flags());
os << std::setfill('0') << std::setw(6) << s_message_counter++;
os.flags(flags);
}

#else

// old std::map implementation, because APPLE does not support thread_local
Expand All @@ -84,8 +68,50 @@ void NameThisThread(const mem::by_string& name) {
s_threads[std::this_thread::get_id()] = StringCount(name, 0);
}

//! Outputs the name of the current thread or 'unknown [id]'
void FormatNameForThisThread(std::ostream& os) {
#endif

/******************************************************************************/

class ThreadLoggerPrefixHook final : public tlx::LoggerPrefixHook
{
public:
//! constructor
ThreadLoggerPrefixHook();

//! virtual destructor
~ThreadLoggerPrefixHook();

//! method to add prefix to log lines
void add_log_prefix(std::ostream& os) final;
};

//! default logger singleton
static ThreadLoggerPrefixHook s_default_logger;

ThreadLoggerPrefixHook::ThreadLoggerPrefixHook() {
set_logger_prefix_hook(&s_default_logger);
}

ThreadLoggerPrefixHook::~ThreadLoggerPrefixHook() { }

void ThreadLoggerPrefixHook::add_log_prefix(std::ostream& os) {
os << '[';
#if !__APPLE__

if (!s_thread_name.empty()) {
os << s_thread_name << ' ';
}
else {
os << "unknown " << std::this_thread::get_id() << ' ';
}

std::ios::fmtflags flags(os.flags());
os << std::setfill('0') << std::setw(6) << s_message_counter++;
os.flags(flags);

#else
// old std::map implementation, because APPLE does not support thread_local

std::lock_guard<std::mutex> lock(s_mutex);

auto it = s_threads.find(std::this_thread::get_id());
Expand All @@ -99,63 +125,9 @@ void FormatNameForThisThread(std::ostream& os) {
else {
os << "unknown " << std::this_thread::get_id();
}
}

#endif

//! Returns the name of the current thread or 'unknown [id]'
std::string GetNameForThisThread() {
std::ostringstream oss;
FormatNameForThisThread(oss);
return oss.str();
}

/******************************************************************************/

//! mutex for log output
static std::mutex s_logger_mutex;

void Logger::Output(const char* str) {
// lock the global mutex of logger for serialized output in
// multi-threaded programs.
std::unique_lock<std::mutex> lock(s_logger_mutex);
std::cout << str;
}

void Logger::Output(const std::string& str) {
// lock the global mutex of logger for serialized output in
// multi-threaded programs.
std::unique_lock<std::mutex> lock(s_logger_mutex);
std::cout << str;
}

void Logger::Output(const mem::safe_string& str) {
// lock the global mutex of logger for serialized output in
// multi-threaded programs.
std::unique_lock<std::mutex> lock(s_logger_mutex);
std::cout << str;
}

Logger::Logger() {
oss_ << '[';
FormatNameForThisThread(oss_);
oss_ << "] ";
}

Logger::~Logger() {
oss_ << '\n';
Output(oss_.str());
}

SpacingLogger::SpacingLogger() {
oss_ << '[';
FormatNameForThisThread(oss_);
oss_ << "] ";
}

SpacingLogger::~SpacingLogger() {
oss_ << '\n';
Logger::Output(oss_.str());
os << ']' << ' ';
}

} // namespace common
Expand Down
170 changes: 3 additions & 167 deletions thrill/common/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@

#include <thrill/mem/allocator.hpp>
#include <thrill/mem/pool.hpp>
#include <tlx/logger.hpp>
#include <tlx/logger/array.hpp>
#include <tlx/logger/tuple.hpp>
#include <tlx/meta/call_foreach_tuple.hpp>

#include <array>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

namespace thrill {
namespace common {

Expand Down Expand Up @@ -98,168 +94,8 @@ execution and communication are collected for later analysis. Something else is
needed here.
*/
class Logger
{
private:
//! collector stream
mem::safe_ostringstream oss_;

public:
//! mutex synchronized output to std::cout
static void Output(const char* str);
//! mutex synchronized output to std::cout
static void Output(const std::string& str);
//! mutex synchronized output to std::cout
static void Output(const mem::safe_string& str);

Logger();

//! output any type, including io manipulators
template <typename AnyType>
Logger& operator << (const AnyType& at) {
oss_ << at;
return *this;
}

//! destructor: output a newline
~Logger();
};

/*!
* A logging class which outputs spaces between elements pushed via
* operator<<. Depending on the real parameter the output may be suppressed.
*/
class SpacingLogger
{
private:
//! true until the first element it outputted.
bool first_ = true;

//! collector stream
mem::safe_ostringstream oss_;

public:
SpacingLogger();

//! output any type, including io manipulators
template <typename AnyType>
SpacingLogger& operator << (const AnyType& at) {
if (!first_) oss_ << ' ';
else first_ = false;

oss_ << at;

return *this;
}

//! destructor: output a newline
~SpacingLogger();
};

class LoggerVoidify
{
public:
void operator & (Logger&) { }
void operator & (SpacingLogger&) { }
};

//! Explicitly specify the condition for logging
#define LOGC(cond) \
!(cond) ? (void)0 : \
::thrill::common::LoggerVoidify() & ::thrill::common::Logger()

//! Default logging method: output if the local debug variable is true.
#define LOG LOGC(debug)

//! Override default output: never or always output log.
#define LOG0 LOGC(false)
#define LOG1 LOGC(true)

//! Explicitly specify the condition for logging
#define sLOGC(cond) \
!(cond) ? (void)0 : \
::thrill::common::LoggerVoidify() & ::thrill::common::SpacingLogger()

//! Default logging method: output if the local debug variable is true.
#define sLOG sLOGC(debug)

//! Override default output: never or always output log.
#define sLOG0 sLOGC(false)
#define sLOG1 sLOGC(true)

} // namespace common

namespace mem {

/******************************************************************************/
// Nice LOG/sLOG Formatters for std::pair, std::tuple, std::vector, and
// std::array types

using log_stream = safe_ostringstream;

template <typename A, typename B>
log_stream& operator << (log_stream& os, const std::pair<A, B>& p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}

template <typename Tuple, size_t N>
struct LogStreamTuplePrinter {
static void print(log_stream& os, const Tuple& t) {
LogStreamTuplePrinter<Tuple, N - 1>::print(os, t);
os << ',' << std::get<N - 1>(t);
}
};

template <typename Tuple>
struct LogStreamTuplePrinter<Tuple, 1>{
static void print(log_stream& os, const Tuple& t) {
os << std::get<0>(t);
}
};

template <typename Tuple>
struct LogStreamTuplePrinter<Tuple, 0>{
static void print(log_stream&, const Tuple&) { }
};

template <typename... Args>
log_stream& operator << (log_stream& os, const std::tuple<Args...>& t) {
os << '(';
LogStreamTuplePrinter<std::tuple<Args...>, sizeof ... (Args)>::print(os, t);
os << ')';
return os;
}

//! Logging helper to print arrays as [a1,a2,a3,...]
template <typename T, size_t N>
log_stream& operator << (log_stream& os, const std::array<T, N>& data) {
os << '[';
for (typename std::array<T, N>::const_iterator it = data.begin();
it != data.end(); ++it)
{
if (it != data.begin()) os << ',';
os << *it;
}
os << ']';
return os;
}

//! Logging helper to print vectors as [a1,a2,a3,...]
template <typename T>
log_stream& operator << (log_stream& os, const std::vector<T>& data) {
os << '[';
for (typename std::vector<T>::const_iterator it = data.begin();
it != data.end(); ++it)
{
if (it != data.begin()) os << ',';
os << *it;
}
os << ']';
return os;
}

} // namespace mem
} // namespace thrill

#endif // !THRILL_COMMON_LOGGER_HEADER
Expand Down

0 comments on commit 458e409

Please sign in to comment.