Thrill  0.1
logger.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/common/logger.cpp
3  *
4  * Simple and less simple logging classes.
5  *
6  * Part of Project Thrill - http://project-thrill.org
7  *
8  * Copyright (C) 2015 Timo Bingmann <[email protected]>
9  *
10  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
11  ******************************************************************************/
12 
13 #include <thrill/common/logger.hpp>
14 #include <thrill/mem/manager.hpp>
15 
16 #include <functional>
17 #include <iomanip>
18 #include <iostream>
19 #include <map>
20 #include <mutex>
21 #include <string>
22 #include <thread>
23 #include <utility>
24 
25 namespace thrill {
26 namespace common {
27 
28 //! memory manager singleton for Logger
29 mem::Manager g_logger_mem_manager(nullptr, "Logger");
30 
31 #if !__APPLE__
32 
33 //! thread name
34 static thread_local char s_thread_name[64] = { 0 };
35 
36 //! thread message counter
37 static thread_local size_t s_message_counter = 0;
38 
39 //! Defines a name for the current thread, only if no name was set previously
40 void NameThisThread(const std::string& name) {
41  snprintf(s_thread_name, sizeof(s_thread_name), "%s", name.c_str());
42  s_message_counter = 0;
43 }
44 
45 #else
46 
47 // old std::map implementation, because APPLE does not support thread_local
48 
49 using StringCount = std::pair<std::string, size_t>;
50 
51 template <typename Type>
53 
54 //! deque without malloc tracking
55 template <typename Key, typename T, typename Compare = std::less<Key> >
56 using logger_map = std::map<Key, T, Compare,
57  LoggerAllocator<std::pair<const Key, T> > >;
58 
59 //! mutex for s_threads map
60 static std::mutex s_mutex;
61 
62 //! map thread id -> (name, message counter)
63 static logger_map<std::thread::id, StringCount> s_threads;
64 
65 //! Defines a name for the current thread, only if no name was set previously
66 void NameThisThread(const std::string& name) {
67  std::lock_guard<std::mutex> lock(s_mutex);
68  s_threads[std::this_thread::get_id()] = StringCount(name, 0);
69 }
70 
71 #endif
72 
73 /******************************************************************************/
74 
75 class ThreadLoggerPrefixHook final : public tlx::LoggerPrefixHook
76 {
77 public:
78  //! constructor
79  ThreadLoggerPrefixHook();
80 
81  //! virtual destructor
82  ~ThreadLoggerPrefixHook();
83 
84  //! method to add prefix to log lines
85  void add_log_prefix(std::ostream& os) final;
86 
87 private:
88  tlx::LoggerPrefixHook* prev_;
89 };
90 
91 //! default logger singleton
92 static ThreadLoggerPrefixHook s_default_logger;
93 
94 ThreadLoggerPrefixHook::ThreadLoggerPrefixHook() {
95  prev_ = set_logger_prefix_hook(&s_default_logger);
96 }
97 
98 ThreadLoggerPrefixHook::~ThreadLoggerPrefixHook() {
100 }
101 
102 void ThreadLoggerPrefixHook::add_log_prefix(std::ostream& os) {
103  os << '[';
104 #if !__APPLE__
105 
106  if (*s_thread_name != 0) {
107  os << s_thread_name << ' ';
108  }
109  else {
110  os << "unknown " << std::this_thread::get_id() << ' ';
111  }
112 
113  std::ios::fmtflags flags(os.flags());
114  os << std::setfill('0') << std::setw(6) << s_message_counter++;
115  os.flags(flags);
116 
117 #else
118  // old std::map implementation, because APPLE does not support thread_local
119 
120  std::lock_guard<std::mutex> lock(s_mutex);
121 
122  auto it = s_threads.find(std::this_thread::get_id());
123  if (it != s_threads.end()) {
124  StringCount& sc = it->second;
125  std::ios::fmtflags flags(os.flags());
126  os << sc.first << ' '
127  << std::setfill('0') << std::setw(6) << sc.second++;
128  os.flags(flags);
129  }
130  else {
131  os << "unknown " << std::this_thread::get_id();
132  }
133 
134 #endif
135  os << ']' << ' ';
136 }
137 
138 } // namespace common
139 } // namespace thrill
140 
141 /******************************************************************************/
mem::Manager g_logger_mem_manager(nullptr, "Logger")
memory manager singleton for Logger
Definition: logger.hpp:28
double T
static thread_local size_t s_message_counter
thread message counter
Definition: logger.cpp:37
static ThreadLoggerPrefixHook s_default_logger
default logger singleton
Definition: logger.cpp:92
void NameThisThread(const std::string &name)
Defines a name for the current thread, only if no name was set previously.
Definition: logger.cpp:40
static thread_local char s_thread_name[64]
thread name
Definition: logger.cpp:34
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
Abstract class to implement prefix output hooks for logging.
Definition: core.hpp:162
LoggerPrefixHook * set_logger_prefix_hook(LoggerPrefixHook *hook)
Definition: core.cpp:80