blob: 51d698da8b31793f5f07c0e54c1b23a486565275 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001//
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
Austin Schuhb4691e92020-12-31 12:37:18 -080020#include "absl/base/attributes.h"
21#include "absl/base/config.h"
22#include "absl/base/const_init.h"
23#include "absl/base/thread_annotations.h"
Austin Schuh36244a12019-09-21 17:52:38 -070024#include "absl/flags/internal/path_util.h"
Austin Schuhb4691e92020-12-31 12:37:18 -080025#include "absl/strings/string_view.h"
Austin Schuh36244a12019-09-21 17:52:38 -070026#include "absl/synchronization/mutex.h"
27
28namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -080029ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070030namespace flags_internal {
31
32ABSL_CONST_INIT static absl::Mutex program_name_guard(absl::kConstInit);
33ABSL_CONST_INIT static std::string* program_name
34 ABSL_GUARDED_BY(program_name_guard) = nullptr;
35
36std::string ProgramInvocationName() {
37 absl::MutexLock l(&program_name_guard);
38
39 return program_name ? *program_name : "UNKNOWN";
40}
41
42std::string ShortProgramInvocationName() {
43 absl::MutexLock l(&program_name_guard);
44
45 return program_name ? std::string(flags_internal::Basename(*program_name))
46 : "UNKNOWN";
47}
48
49void SetProgramInvocationName(absl::string_view prog_name_str) {
50 absl::MutexLock l(&program_name_guard);
51
52 if (!program_name)
53 program_name = new std::string(prog_name_str);
54 else
55 program_name->assign(prog_name_str.data(), prog_name_str.size());
56}
57
58} // namespace flags_internal
Austin Schuhb4691e92020-12-31 12:37:18 -080059ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -070060} // namespace absl