1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2014 Tatsuhiro Tsujikawa
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include "shrpx_log_config.h"
26
27 #include <unistd.h>
28
29 #include <thread>
30 #include <sstream>
31
32 #include "util.h"
33
34 using namespace nghttp2;
35
36 namespace shrpx {
37
Timestamp(const std::chrono::system_clock::time_point & tp)38 Timestamp::Timestamp(const std::chrono::system_clock::time_point &tp) {
39 time_local = util::format_common_log(time_local_buf.data(), tp);
40 time_iso8601 = util::format_iso8601(time_iso8601_buf.data(), tp);
41 time_http = util::format_http_date(time_http_buf.data(), tp);
42 }
43
LogConfig()44 LogConfig::LogConfig()
45 : time_str_updated(std::chrono::system_clock::now()),
46 tstamp(std::make_shared<Timestamp>(time_str_updated)),
47 pid(getpid()),
48 accesslog_fd(-1),
49 errorlog_fd(-1),
50 errorlog_tty(false) {
51 auto tid = std::this_thread::get_id();
52 auto tid_hash =
53 util::hash32(StringRef{reinterpret_cast<uint8_t *>(&tid),
54 reinterpret_cast<uint8_t *>(&tid) + sizeof(tid)});
55 thread_id = util::format_hex(reinterpret_cast<uint8_t *>(&tid_hash),
56 sizeof(tid_hash));
57 }
58
59 #ifndef NOTHREADS
60 # ifdef HAVE_THREAD_LOCAL
61 namespace {
62 thread_local std::unique_ptr<LogConfig> config = std::make_unique<LogConfig>();
63 } // namespace
64
log_config()65 LogConfig *log_config() { return config.get(); }
delete_log_config()66 void delete_log_config() {}
67 # else // !HAVE_THREAD_LOCAL
68 namespace {
69 pthread_key_t lckey;
70 pthread_once_t lckey_once = PTHREAD_ONCE_INIT;
71 } // namespace
72
73 namespace {
make_key()74 void make_key() { pthread_key_create(&lckey, NULL); }
75 } // namespace
76
log_config()77 LogConfig *log_config() {
78 pthread_once(&lckey_once, make_key);
79 LogConfig *config = (LogConfig *)pthread_getspecific(lckey);
80 if (!config) {
81 config = new LogConfig();
82 pthread_setspecific(lckey, config);
83 }
84 return config;
85 }
86
delete_log_config()87 void delete_log_config() { delete log_config(); }
88 # endif // !HAVE_THREAD_LOCAL
89 #else // NOTHREADS
90 namespace {
91 std::unique_ptr<LogConfig> config = std::make_unique<LogConfig>();
92 } // namespace
93
log_config()94 LogConfig *log_config() { return config.get(); }
95
delete_log_config()96 void delete_log_config() {}
97 #endif // NOTHREADS
98
update_tstamp_millis(const std::chrono::system_clock::time_point & now)99 void LogConfig::update_tstamp_millis(
100 const std::chrono::system_clock::time_point &now) {
101 if (std::chrono::duration_cast<std::chrono::milliseconds>(
102 now.time_since_epoch()) ==
103 std::chrono::duration_cast<std::chrono::milliseconds>(
104 time_str_updated.time_since_epoch())) {
105 return;
106 }
107
108 time_str_updated = now;
109
110 tstamp = std::make_shared<Timestamp>(now);
111 }
112
update_tstamp(const std::chrono::system_clock::time_point & now)113 void LogConfig::update_tstamp(
114 const std::chrono::system_clock::time_point &now) {
115 if (std::chrono::duration_cast<std::chrono::seconds>(
116 now.time_since_epoch()) ==
117 std::chrono::duration_cast<std::chrono::seconds>(
118 time_str_updated.time_since_epoch())) {
119 return;
120 }
121
122 time_str_updated = now;
123
124 tstamp = std::make_shared<Timestamp>(now);
125 }
126
127 } // namespace shrpx
128