• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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<char *>(&tid), sizeof(tid)});
54   thread_id = util::format_hex(std::span{&tid_hash, 1});
55 }
56 
57 #ifndef NOTHREADS
58 #  ifdef HAVE_THREAD_LOCAL
59 namespace {
60 thread_local std::unique_ptr<LogConfig> config = std::make_unique<LogConfig>();
61 } // namespace
62 
log_config()63 LogConfig *log_config() { return config.get(); }
delete_log_config()64 void delete_log_config() {}
65 #  else  // !HAVE_THREAD_LOCAL
66 namespace {
67 pthread_key_t lckey;
68 pthread_once_t lckey_once = PTHREAD_ONCE_INIT;
69 } // namespace
70 
71 namespace {
make_key()72 void make_key() { pthread_key_create(&lckey, nullptr); }
73 } // namespace
74 
log_config()75 LogConfig *log_config() {
76   pthread_once(&lckey_once, make_key);
77   LogConfig *config = (LogConfig *)pthread_getspecific(lckey);
78   if (!config) {
79     config = new LogConfig();
80     pthread_setspecific(lckey, config);
81   }
82   return config;
83 }
84 
delete_log_config()85 void delete_log_config() { delete log_config(); }
86 #  endif // !HAVE_THREAD_LOCAL
87 #else    // NOTHREADS
88 namespace {
89 std::unique_ptr<LogConfig> config = std::make_unique<LogConfig>();
90 } // namespace
91 
log_config()92 LogConfig *log_config() { return config.get(); }
93 
delete_log_config()94 void delete_log_config() {}
95 #endif   // NOTHREADS
96 
update_tstamp_millis(const std::chrono::system_clock::time_point & now)97 void LogConfig::update_tstamp_millis(
98     const std::chrono::system_clock::time_point &now) {
99   if (std::chrono::duration_cast<std::chrono::milliseconds>(
100           now.time_since_epoch()) ==
101       std::chrono::duration_cast<std::chrono::milliseconds>(
102           time_str_updated.time_since_epoch())) {
103     return;
104   }
105 
106   time_str_updated = now;
107 
108   tstamp = std::make_shared<Timestamp>(now);
109 }
110 
update_tstamp(const std::chrono::system_clock::time_point & now)111 void LogConfig::update_tstamp(
112     const std::chrono::system_clock::time_point &now) {
113   if (std::chrono::duration_cast<std::chrono::seconds>(
114           now.time_since_epoch()) ==
115       std::chrono::duration_cast<std::chrono::seconds>(
116           time_str_updated.time_since_epoch())) {
117     return;
118   }
119 
120   time_str_updated = now;
121 
122   tstamp = std::make_shared<Timestamp>(now);
123 }
124 
125 } // namespace shrpx
126