kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/init.cc (about)

     1  /*
     2   * Copyright 2020 The Kythe Authors. All rights reserved.
     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   *   http://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 "kythe/cxx/common/init.h"
    17  
    18  #include <cassert>
    19  #include <cstring>
    20  
    21  #include "absl/debugging/failure_signal_handler.h"
    22  #include "absl/debugging/symbolize.h"
    23  #include "absl/log/initialize.h"
    24  #include "absl/log/log.h"
    25  
    26  namespace kythe {
    27  
    28  #if defined(KYTHE_OVERRIDE_ASSERT_FAIL)
    29  extern "C" {
    30  
    31  #ifndef __THROW
    32  #define __THROW throw()
    33  #endif
    34  
    35  // Make sure we get a legible stack trace when assert from <assert.h> fails.
    36  // This works around https://github.com/abseil/abseil-cpp/issues/769.
    37  //
    38  // We are replacing implementation-defined function (defined in glibc)
    39  // used by the expansion of system defined assert() macro.
    40  void __assert_fail(
    41      const char* assertion, const char* file, unsigned int line,
    42      const char* function) __THROW {  // NOLINT(misc-include-cleaner)
    43    LOG(FATAL) << "assert.h assertion failed at " << file << ":" << line
    44               << (function ? " in " : "") << (function ? function : "") << ": "
    45               << assertion;
    46  }
    47  
    48  // Same as above, but for assert_perror.
    49  void __assert_perror_fail(
    50      int errnum, const char* file, unsigned int line,
    51      const char* function) __THROW {  // NOLINT(misc-include-cleaner)
    52    LOG(FATAL) << "assert.h assert_perror failed at " << file << ":" << line
    53               << " " << (function ? function : "") << ": "
    54               << std::strerror(errnum) << " [" << errnum << "]";
    55  }
    56  }  // extern "C"
    57  #endif
    58  
    59  void InitializeProgram(const char* argv0) {
    60    absl::InitializeLog();
    61    absl::InitializeSymbolizer(argv0);
    62    absl::InstallFailureSignalHandler({});
    63  }
    64  
    65  }  // namespace kythe