Debian Codegraph @

srtcore/logging.cpp

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2018 Haivision Systems Inc.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11 /*****************************************************************************
12 written by
13 Haivision Systems Inc.
14 *****************************************************************************/
15
16
17 #include "srt_compat.h"
18 #include "logging.h"
19
20 using namespace std;
21
22
23 namespace srt_logging
24 {
25
26
27 #if ENABLE_LOGGING
28
29 LogDispatcher::Proxy::Proxy(LogDispatcher& guy) : that(guy), that_enabled(that.CheckEnabled())
30 {
31 if (that_enabled)
32 {
33 i_file = "";
34 i_line = 0;
35 flags = that.src_config->flags;
36 // Create logger prefix
37 that.CreateLogLinePrefix(os);
38 }
39 }
40
41 LogDispatcher::Proxy LogDispatcher::operator()()
42 {
43 return Proxy(*this);
44 }
45
46 void LogDispatcher::CreateLogLinePrefix(std::ostringstream& serr)
47 {
48 using namespace std;
49 using namespace srt;
50
51 SRT_STATIC_ASSERT(ThreadName::BUFSIZE >= sizeof("hh:mm:ss.") * 2, // multiply 2 for some margin
52 "ThreadName::BUFSIZE is too small to be used for strftime");
53 char tmp_buf[ThreadName::BUFSIZE];
54 if ( !isset(SRT_LOGF_DISABLE_TIME) )
55 {
56 // Not necessary if sending through the queue.
57 timeval tv;
58 gettimeofday(&tv, NULL);
59 struct tm tm = SysLocalTime((time_t) tv.tv_sec);
60
61 if (strftime(tmp_buf, sizeof(tmp_buf), "%X.", &tm))
62 {
63 serr << tmp_buf << setw(6) << setfill('0') << tv.tv_usec;
64 }
65 }
66
67 string out_prefix;
68 if ( !isset(SRT_LOGF_DISABLE_SEVERITY) )
69 {
70 out_prefix = prefix;
71 }
72
73 // Note: ThreadName::get needs a buffer of size min. ThreadName::BUFSIZE
74 if ( !isset(SRT_LOGF_DISABLE_THREADNAME) && ThreadName::get(tmp_buf) )
75 {
76 serr << "/" << tmp_buf << out_prefix << ": ";
77 }
78 else
79 {
80 serr << out_prefix << ": ";
81 }
82 }
83
84 std::string LogDispatcher::Proxy::ExtractName(std::string pretty_function)
85 {
86 if ( pretty_function == "" )
87 return "";
88 size_t pos = pretty_function.find('(');
89 if ( pos == std::string::npos )
90 return pretty_function; // return unchanged.
91
92 pretty_function = pretty_function.substr(0, pos);
93
94 // There are also template instantiations where the instantiating
95 // parameters are encrypted inside. Therefore, search for the first
96 // open < and if found, search for symmetric >.
97
98 int depth = 1;
99 pos = pretty_function.find('<');
100 if ( pos != std::string::npos )
101 {
102 size_t end = pos+1;
103 for(;;)
104 {
105 ++pos;
106 if ( pos == pretty_function.size() )
107 {
108 --pos;
109 break;
110 }
111 if ( pretty_function[pos] == '<' )
112 {
113 ++depth;
114 continue;
115 }
116
117 if ( pretty_function[pos] == '>' )
118 {
119 --depth;
120 if ( depth <= 0 )
121 break;
122 continue;
123 }
124 }
125
126 std::string afterpart = pretty_function.substr(pos+1);
127 pretty_function = pretty_function.substr(0, end) + ">" + afterpart;
128 }
129
130 // Now see how many :: can be found in the name.
131 // If this occurs more than once, take the last two.
132 pos = pretty_function.rfind("::");
133
134 if ( pos == std::string::npos || pos < 2 )
135 return pretty_function; // return whatever this is. No scope name.
136
137 // Find the next occurrence of :: - if found, copy up to it. If not,
138 // return whatever is found.
139 pos -= 2;
140 pos = pretty_function.rfind("::", pos);
141 if ( pos == std::string::npos )
142 return pretty_function; // nothing to cut
143
144 return pretty_function.substr(pos+2);
145 }
146 #endif
147
148 } // (end namespace srt_logging)
149