Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / src / shrpx_log_config.cc
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 #include "util.h"
27
28 using namespace nghttp2;
29
30 namespace shrpx {
31
32 LogConfig::LogConfig()
33     : accesslog_fd(-1), errorlog_fd(-1), errorlog_tty(false) {}
34
35 #ifndef NOTHREADS
36 static pthread_key_t lckey;
37 static pthread_once_t lckey_once = PTHREAD_ONCE_INIT;
38
39 static void make_key(void) { pthread_key_create(&lckey, NULL); }
40
41 LogConfig *log_config(void) {
42   pthread_once(&lckey_once, make_key);
43   LogConfig *config = (LogConfig *)pthread_getspecific(lckey);
44   if (!config) {
45     config = new LogConfig();
46     pthread_setspecific(lckey, config);
47   }
48   return config;
49 }
50 #else
51 static LogConfig *config = new LogConfig();
52 LogConfig *log_config(void) { return config; }
53 #endif // NOTHREADS
54
55 void
56 LogConfig::update_tstamp(const std::chrono::system_clock::time_point &now) {
57   auto t0 = std::chrono::system_clock::to_time_t(time_str_updated_);
58   auto t1 = std::chrono::system_clock::to_time_t(now);
59   if (t0 == t1) {
60     return;
61   }
62
63   time_str_updated_ = now;
64
65   time_local_str = util::format_common_log(now);
66   time_iso8601_str = util::format_iso8601(now);
67 }
68
69 } // namespace shrpx