program_name.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #include "y_absl/flags/internal/program_name.h"
  16. #include <util/generic/string.h>
  17. #include "y_absl/base/attributes.h"
  18. #include "y_absl/base/config.h"
  19. #include "y_absl/base/const_init.h"
  20. #include "y_absl/base/thread_annotations.h"
  21. #include "y_absl/flags/internal/path_util.h"
  22. #include "y_absl/strings/string_view.h"
  23. #include "y_absl/synchronization/mutex.h"
  24. namespace y_absl {
  25. Y_ABSL_NAMESPACE_BEGIN
  26. namespace flags_internal {
  27. Y_ABSL_CONST_INIT static y_absl::Mutex program_name_guard(y_absl::kConstInit);
  28. Y_ABSL_CONST_INIT static TString* program_name
  29. Y_ABSL_GUARDED_BY(program_name_guard) = nullptr;
  30. TString ProgramInvocationName() {
  31. y_absl::MutexLock l(&program_name_guard);
  32. return program_name ? *program_name : "UNKNOWN";
  33. }
  34. TString ShortProgramInvocationName() {
  35. y_absl::MutexLock l(&program_name_guard);
  36. return program_name ? TString(flags_internal::Basename(*program_name))
  37. : "UNKNOWN";
  38. }
  39. void SetProgramInvocationName(y_absl::string_view prog_name_str) {
  40. y_absl::MutexLock l(&program_name_guard);
  41. if (!program_name)
  42. program_name = new TString(prog_name_str);
  43. else
  44. program_name->assign(prog_name_str.data(), prog_name_str.size());
  45. }
  46. } // namespace flags_internal
  47. Y_ABSL_NAMESPACE_END
  48. } // namespace y_absl