Skip to content

Commit 69c6b4e

Browse files
authored
Merge pull request #1927 from joto/refactor-thread-num-log
Various changes around threads and logging
2 parents c3014b2 + 3192b7b commit 69c6b4e

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

src/logging.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include "logging.hpp"
1111

12+
#include <osmium/thread/util.hpp>
13+
1214
thread_local unsigned int this_thread_num = 0;
1315

1416
/// Global logger singleton
@@ -31,7 +33,7 @@ std::string logger::generate_common_prefix(fmt::text_style const &ts,
3133
fmt::localtime(std::time(nullptr)));
3234

3335
if (m_current_level == log_level::debug) {
34-
str += fmt::format(ts, "[{}] ", this_thread_num);
36+
str += fmt::format(ts, "[{:02d}] ", this_thread_num);
3537
}
3638

3739
if (prefix) {
@@ -40,3 +42,15 @@ std::string logger::generate_common_prefix(fmt::text_style const &ts,
4042

4143
return str;
4244
}
45+
46+
void logger::init_thread(unsigned int num) const
47+
{
48+
// Store thread number in thread local variable
49+
this_thread_num = num;
50+
51+
// Set thread name in operating system.
52+
// On Linux thread names have a maximum length of 16 characters.
53+
std::string name{"_osm2pgsql_"};
54+
name.append(std::to_string(num));
55+
osmium::thread::set_thread_name(name.c_str());
56+
}

src/logging.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
#include <cstdio>
2121
#include <utility>
2222

23-
extern thread_local unsigned int this_thread_num;
24-
2523
enum class log_level
2624
{
2725
debug = 1,
@@ -36,9 +34,6 @@ enum class log_level
3634
*/
3735
class logger
3836
{
39-
std::string generate_common_prefix(fmt::text_style const &ts,
40-
char const *prefix);
41-
4237
public:
4338
template <typename S, typename... TArgs>
4439
void log(log_level with_level, char const *prefix,
@@ -84,7 +79,12 @@ class logger
8479
void needs_leading_return() noexcept { m_needs_leading_return = true; }
8580
void no_leading_return() noexcept { m_needs_leading_return = false; }
8681

82+
void init_thread(unsigned int num) const;
83+
8784
private:
85+
std::string generate_common_prefix(fmt::text_style const &ts,
86+
char const *prefix);
87+
8888
log_level m_current_level = log_level::info;
8989
bool m_log_sql = false;
9090
bool m_log_sql_data = false;

src/thread-pool.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
#include "thread-pool.hpp"
1111

12-
#include <osmium/thread/util.hpp>
13-
1412
#include <cassert>
1513
#include <string>
1614

@@ -52,10 +50,7 @@ void thread_pool_t::shutdown_all_workers()
5250

5351
void thread_pool_t::worker_thread(unsigned int thread_num)
5452
{
55-
std::string name{"_osm2pgsql_worker_"};
56-
name.append(std::to_string(thread_num));
57-
osmium::thread::set_thread_name(name.c_str());
58-
this_thread_num = thread_num + 1;
53+
get_logger().init_thread(thread_num + 1);
5954

6055
while (true) {
6156
osmium::thread::function_wrapper task;

0 commit comments

Comments
 (0)