Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
codeql/swift/logging/SwiftLogging.cpp at main · github/codeql · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
github
codeql
Repository navigation
Code
Issues
1k
(1k)
Pull requests
480
(480)
Discussions
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
codeql
/
swift
/
logging
/
SwiftLogging.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
210 lines (191 loc) · 6.77 KB
main
Breadcrumbs
codeql
/
swift
/
logging
/
SwiftLogging.cpp
Copy path
Top
File metadata and controls
Code
Blame
210 lines (191 loc) · 6.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#
include
"
swift/logging/SwiftLogging.h
"
#
include
<
filesystem
>
#
include
<
stdlib.h
>
#
include
<
optional
>
#
ifdef
_WIN32
#
include
<
process.h
>
#
else
#
include
<
unistd.h
>
#
endif
#
include
"
absl/strings/str_cat.h
"
#
define
LEVEL_REGEX_PATTERN
"
trace|debug|info|warning|error|critical|no_logs
"
BINLOG_ADAPT_ENUM
(codeql::Log::Level, trace, debug, info, warning, error, critical, no_logs)
namespace
codeql
{
bool
Log::initialized{
false
};
namespace
{
using
LevelRule = std::pair<std::regex, Log::Level>;
using
LevelRules = std::vector<LevelRule>;
Log::Level
getLevelFor
(std::string_view name,
const
LevelRules& rules, Log::Level dflt) {
for
(
auto
it = rules.
rbegin
(); it != rules.
rend
(); ++it) {
if
(
std::regex_match
(
std::begin
(name),
std::end
(name), it->
first
)) {
return
it->
second
;
}
}
return
dflt;
}
const
char
*
getEnvOr
(
const
char
* var,
const
char
* dflt) {
if
(
const
char
* ret =
getenv
(var)) {
return
ret;
}
return
dflt;
}
std::string_view
matchToView
(std::csub_match m) {
return
{m.
first
,
static_cast
<
size_t
>(m.
length
())};
}
Log::Level
stringToLevel
(std::string_view v) {
if
(v ==
"
trace
"
)
return
Log::Level::trace;
if
(v ==
"
debug
"
)
return
Log::Level::debug;
if
(v ==
"
info
"
)
return
Log::Level::info;
if
(v ==
"
warning
"
)
return
Log::Level::warning;
if
(v ==
"
error
"
)
return
Log::Level::error;
if
(v ==
"
critical
"
)
return
Log::Level::critical;
return
Log::Level::no_logs;
}
Log::Level
matchToLevel
(std::csub_match m) {
return
stringToLevel
(
matchToView
(m));
}
}
//
namespace
std::vector<std::string>
Log::collectLevelRulesAndReturnProblems
(
const
char
* envVar) {
std::vector<std::string> problems;
if
(
auto
levels =
getEnvOr
(envVar,
nullptr
)) {
//
expect comma-separated <glob pattern>:<log severity>
std::regex comma{
"
,
"
};
std::regex levelAssignment{
R"(
(?:([*./\w]+)|(?:out:(binary|text|console))):(
)"
LEVEL_REGEX_PATTERN
"
)
"
};
std::cregex_token_iterator begin{levels, levels +
strlen
(levels), comma, -
1
};
std::cregex_token_iterator end{};
for
(
auto
it = begin; it != end; ++it) {
std::cmatch match;
if
(
std::regex_match
(it->
first
, it->
second
, match, levelAssignment)) {
auto
level =
matchToLevel
(match[
3
]);
if
(match[
1
].
matched
) {
auto
pattern = match[
1
].
str
();
//
replace all "*" with ".*" and all "." with "\.", turning the glob pattern into a regex
std::string::size_type pos =
0
;
while
((pos = pattern.
find_first_of
(
"
*.
"
, pos)) != std::string::npos) {
pattern.
insert
(pos, (pattern[pos] ==
'
*
'
) ?
"
.
"
:
"
\\
"
);
pos +=
2
;
}
sourceRules.
emplace_back
(pattern, level);
}
else
{
auto
out =
matchToView
(match[
2
]);
if
(out ==
"
binary
"
) {
binary.
level
= level;
}
else
if
(out ==
"
text
"
) {
text.
level
= level;
}
else
if
(out ==
"
console
"
) {
console.
level
= level;
}
}
}
else
{
problems.
emplace_back
(
"
Malformed log level rule:
"
+ it->
str
());
}
}
}
return
problems;
}
void
Log::configure
() {
//
as we are configuring logging right now, we collect problems and log them at the end
auto
problems =
collectLevelRulesAndReturnProblems
(
"
CODEQL_EXTRACTOR_SWIFT_LOG_LEVELS
"
);
auto
timestamp =
std::chrono::system_clock::now
().
time_since_epoch
().
count
();
auto
logBaseName =
absl::StrCat
(timestamp,
"
-
"
,
getpid
());
if
(text || binary) {
std::filesystem::path logFile =
getEnvOr
(
"
CODEQL_EXTRACTOR_SWIFT_LOG_DIR
"
,
"
extractor-out/log
"
);
logFile /=
"
swift
"
;
logFile /= programName;
logFile /= logBaseName;
std::error_code ec;
std::filesystem::create_directories
(logFile.
parent_path
(), ec);
if
(!ec) {
if
(text) {
logFile.
replace_extension
(
"
.log
"
);
textFile.
open
(logFile);
if
(!textFile) {
problems.
emplace_back
(
"
Unable to open text log file
"
+ logFile.
string
());
text.
level
= Level::no_logs;
}
}
if
(binary) {
logFile.
replace_extension
(
"
.blog
"
);
binary.
output
.
open
(logFile, std::fstream::out | std::fstream::binary);
if
(!binary.
output
) {
problems.
emplace_back
(
"
Unable to open binary log file
"
+ logFile.
string
());
binary.
level
= Level::no_logs;
}
}
}
else
{
problems.
emplace_back
(
"
Unable to create log directory
"
+ logFile.
parent_path
().
string
() +
"
:
"
+ ec.
message
());
binary.
level
= Level::no_logs;
text.
level
= Level::no_logs;
}
}
if
(
auto
diagDir =
getEnvOr
(
"
CODEQL_EXTRACTOR_SWIFT_DIAGNOSTIC_DIR
"
,
nullptr
)) {
std::filesystem::path diagFile = diagDir;
diagFile /= programName;
diagFile /= logBaseName;
diagFile.
replace_extension
(
"
.jsonl
"
);
std::error_code ec;
std::filesystem::create_directories
(diagFile.
parent_path
(), ec);
if
(!ec) {
diagnostics.
open
(diagFile);
if
(!diagnostics) {
problems.
emplace_back
(
"
Unable to open diagnostics json file
"
+ diagFile.
string
());
}
}
else
{
problems.
emplace_back
(
"
Unable to create diagnostics directory
"
+
diagFile.
parent_path
().
string
() +
"
:
"
+ ec.
message
());
}
}
for
(
const
auto
& problem : problems) {
LOG_ERROR
(
"
{}
"
, problem);
}
LOG_INFO
(
"
Logging configured (binary: {}, text: {}, console: {}, diagnostics: {})
"
, binary.
level
,
text.
level
, console.
level
, diagnostics.
good
());
initialized =
true
;
flushImpl
();
}
void
Log::flushImpl
() {
session.
consume
(*
this
);
if
(text) {
textFile.
flush
();
}
if
(binary) {
binary.
output
.
flush
();
}
if
(diagnostics) {
diagnostics.
flush
();
}
}
void
Log::diagnoseImpl
(
const
Diagnostic& source,
const
std::chrono::nanoseconds& elapsed,
std::string_view message) {
using
Clock = std::chrono::system_clock;
Clock::time_point time{std::chrono::duration_cast<Clock::duration>(elapsed)};
if
(diagnostics) {
diagnostics << source.
json
(time, message) <<
'
\n
'
;
}
}
Log::LoggerConfiguration
Log::getLoggerConfigurationImpl
(std::string_view name) {
LoggerConfiguration ret{session, std::string{programName}};
ret.
fullyQualifiedName
+=
'
/
'
;
ret.
fullyQualifiedName
+= name;
ret.
level
=
std::min
({binary.
level
, text.
level
, console.
level
});
ret.
level
=
getLevelFor
(ret.
fullyQualifiedName
, sourceRules, ret.
level
);
//
avoid Logger constructor loop
if
(name !=
"
logging
"
) {
LOG_DEBUG
(
"
Configuring logger {} with level {}
"
, ret.
fullyQualifiedName
, ret.
level
);
}
return
ret;
}
Log&
Log::write
(
const
char
* buffer, std::streamsize size) {
if
(text) text.
write
(buffer, size);
if
(binary) binary.
write
(buffer, size);
if
(console) console.
write
(buffer, size);
return
*
this
;
}
Logger&
Log::logger
() {
static
Logger ret{
getLoggerConfigurationImpl
(
"
logging
"
)};
return
ret;
}
}
//
namespace codeql
You can’t perform that action at this time.