-
-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathlogging.cpp
More file actions
64 lines (51 loc) · 1.62 KB
/
logging.cpp
File metadata and controls
64 lines (51 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2026 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "logging.hpp"
#include <osmium/thread/util.hpp>
#include <ctime>
namespace {
thread_local unsigned int this_thread_num = 0;
/// Global logger singleton
logger_t the_logger{};
} // anonymous namespace
/// Access the global logger singleton
logger_t &get_logger() noexcept { return the_logger; }
void logger_t::generate_common_prefix(std::string *str,
fmt::text_style const &ts,
char const *prefix) const
{
auto const now = std::time(nullptr);
std::tm tm_local{};
#ifdef _MSC_VER
if (localtime_s(&tm_local, &now) != 0) {
throw fmt::format_error("time_t value out of range");
}
#else
if (!localtime_r(&now, &tm_local)) {
throw fmt::format_error("time_t value out of range");
}
#endif
*str += fmt::format("{:%F %T} ", tm_local);
if (m_current_level == log_level::debug) {
*str += fmt::format(ts, "[{:02d}] ", this_thread_num);
}
if (prefix) {
*str += fmt::format(ts, "{}: ", prefix);
}
}
void logger_t::init_thread(unsigned int num)
{
// Store thread number in thread local variable
this_thread_num = num;
// Set thread name in operating system.
// On Linux thread names have a maximum length of 16 characters.
std::string name{"_osm2pgsql_"};
name.append(std::to_string(num));
osmium::thread::set_thread_name(name.c_str());
}