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