-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathinit_command_line.cc
109 lines (96 loc) · 3.62 KB
/
init_command_line.cc
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
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "common/util/init_command_line.h"
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
#include "absl/debugging/failure_signal_handler.h"
#include "absl/debugging/symbolize.h"
#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "absl/flags/usage.h"
#include "absl/flags/usage_config.h"
#include "absl/log/globals.h"
#include "absl/log/initialize.h"
#include "absl/strings/numbers.h"
#include "absl/time/time.h"
#include "common/util/generated_verible_build_version.h"
namespace verible {
std::string GetRepositoryVersion() {
#ifdef VERIBLE_GIT_DESCRIBE
return VERIBLE_GIT_DESCRIBE;
#else
return "<unknown repository version>";
#endif
}
// Long-form of build version, might contain multiple lines
static std::string GetBuildVersion() {
std::string result;
// Build a version string with as much as possible info.
#ifdef VERIBLE_GIT_DESCRIBE
result.append(VERIBLE_GIT_DESCRIBE).append("\n");
#endif
#ifdef VERIBLE_GIT_DATE
result.append("Commit\t").append(VERIBLE_GIT_DATE).append("\n");
#endif
#ifdef VERIBLE_BUILD_TIMESTAMP
result.append("Built\t")
.append(absl::FormatTime("%Y-%m-%dT%H:%M:%SZ",
absl::FromTimeT(VERIBLE_BUILD_TIMESTAMP),
absl::UTCTimeZone()))
.append("\n");
#endif
return result;
}
void SetLoggingLevelsFromEnvironment() {
// To avoid confusing and rarely used flags, we just enable logging via
// environment variables.
const char *const stderr_log_level = getenv("VERIBLE_LOGTHRESHOLD");
int log_level = 0;
if (stderr_log_level && absl::SimpleAtoi(stderr_log_level, &log_level)) {
absl::SetStderrThreshold(
static_cast<absl::LogSeverityAtLeast>(std::clamp(log_level, 0, 3)));
}
// Set vlog-level with environment variable. The definition of
// VERIBLE_INTERNAL_SET_VLOGLEVEL() might be different depending on if we
// have an absl implementation or not.
const char *const vlog_level_env = getenv("VERIBLE_VLOG_DETAIL");
int vlog_level = 0;
if (vlog_level_env && absl::SimpleAtoi(vlog_level_env, &vlog_level)) {
absl::SetGlobalVLogLevel(vlog_level);
}
}
// We might want to have argc edited in the future, hence non-const param.
std::vector<absl::string_view> InitCommandLine(
absl::string_view usage,
int *argc, // NOLINT(readability-non-const-parameter)
char ***argv) {
absl::InitializeSymbolizer(*argv[0]);
absl::FlagsUsageConfig usage_config;
usage_config.version_string = GetBuildVersion;
absl::SetFlagsUsageConfig(usage_config);
absl::SetProgramUsageMessage(usage); // copies usage string
SetLoggingLevelsFromEnvironment();
absl::InitializeLog();
// Print stacktrace on issue, but not if --config=asan
// which comes with its own stacktrace handling.
#if !defined(__SANITIZE_ADDRESS__)
absl::FailureSignalHandlerOptions options;
absl::InstallFailureSignalHandler(options);
#endif
const auto positional_parameters = absl::ParseCommandLine(*argc, *argv);
return {positional_parameters.cbegin(), positional_parameters.cend()};
}
} // namespace verible