/src/abseil-cpp/absl/flags/internal/program_name.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // Copyright 2019 The Abseil Authors. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | // you may not use this file except in compliance with the License. |
6 | | // You may obtain a copy of the License at |
7 | | // |
8 | | // https://www.apache.org/licenses/LICENSE-2.0 |
9 | | // |
10 | | // Unless required by applicable law or agreed to in writing, software |
11 | | // distributed under the License is distributed on an "AS IS" BASIS, |
12 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | // See the License for the specific language governing permissions and |
14 | | // limitations under the License. |
15 | | |
16 | | #include "absl/flags/internal/program_name.h" |
17 | | |
18 | | #include <string> |
19 | | |
20 | | #include "absl/base/attributes.h" |
21 | | #include "absl/base/config.h" |
22 | | #include "absl/base/no_destructor.h" |
23 | | #include "absl/base/thread_annotations.h" |
24 | | #include "absl/flags/internal/path_util.h" |
25 | | #include "absl/strings/string_view.h" |
26 | | #include "absl/synchronization/mutex.h" |
27 | | |
28 | | namespace absl { |
29 | | ABSL_NAMESPACE_BEGIN |
30 | | namespace flags_internal { |
31 | | |
32 | 0 | static absl::Mutex& ProgramNameMutex() { |
33 | 0 | static absl::NoDestructor<absl::Mutex> mutex; |
34 | 0 | return *mutex; |
35 | 0 | } |
36 | | ABSL_CONST_INIT static std::string* program_name ABSL_GUARDED_BY( |
37 | | ProgramNameMutex()) ABSL_PT_GUARDED_BY(ProgramNameMutex()) = nullptr; |
38 | | |
39 | 0 | std::string ProgramInvocationName() { |
40 | 0 | absl::MutexLock l(ProgramNameMutex()); |
41 | 0 | return program_name ? *program_name : "UNKNOWN"; |
42 | 0 | } |
43 | | |
44 | 0 | std::string ShortProgramInvocationName() { |
45 | 0 | absl::MutexLock l(ProgramNameMutex()); |
46 | 0 | return program_name ? std::string(flags_internal::Basename(*program_name)) |
47 | 0 | : "UNKNOWN"; |
48 | 0 | } |
49 | | |
50 | 0 | void SetProgramInvocationName(absl::string_view prog_name_str) { |
51 | 0 | absl::MutexLock l(ProgramNameMutex()); |
52 | 0 | if (!program_name) { |
53 | 0 | program_name = new std::string(prog_name_str); |
54 | 0 | } else { |
55 | 0 | program_name->assign(prog_name_str.data(), prog_name_str.size()); |
56 | 0 | } |
57 | 0 | } |
58 | | |
59 | | } // namespace flags_internal |
60 | | ABSL_NAMESPACE_END |
61 | | } // namespace absl |