libfoedus-core
FOEDUS Core Library
error_stack.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #include "foedus/error_stack.hpp"
19 
20 #include <glog/logging.h>
21 
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25 
26 #include "foedus/assert_nd.hpp"
28 
29 namespace foedus {
30 void ErrorStack::output(std::ostream* ptr) const {
31  std::ostream &o = *ptr; // just to workaround non-const reference rule.
32  if (!is_error()) {
33  o << "No error";
34  } else {
35  o << get_error_name(error_code_) << "(" << error_code_ << "):" << get_message();
36  if (os_errno_ != 0) {
37  o << " (Latest system call error=" << assorted::os_error(os_errno_) << ")";
38  }
39  if (get_custom_message()) {
40  o << " (Additional message=" << get_custom_message() << ")";
41  }
42 
43  for (uint16_t stack_index = 0; stack_index < get_stack_depth(); ++stack_index) {
44  o << std::endl << " " << get_filename(stack_index)
45  << ":" << get_linenum(stack_index) << ": ";
46  if (get_func(stack_index) != nullptr) {
47  o << get_func(stack_index) << "()";
48  }
49  }
51  o << std::endl << " .. and more. Increase kMaxStackDepth to see full stacktraces";
52  }
53  }
54 }
55 
60 
61 void ErrorStack::dump_and_abort(const char *abort_message) const {
62  std::stringstream str;
63  str << "foedus::ErrorStack::dump_and_abort: " << abort_message << std::endl << *this << std::endl;
64  str << print_backtrace();
65 
66  static_recent_dump_and_abort += str.str();
67  LOG(FATAL) << str.str();
68  ASSERT_ND(false);
69  std::cout.flush();
70  std::cerr.flush();
71  std::abort();
72 }
73 
76 }
77 
78 std::ostream& operator<<(std::ostream& o, const ErrorStack& obj) {
79  obj.output(&o);
80  return o;
81 }
82 
83 } // namespace foedus
84 
std::string static_recent_dump_and_abort
Leaves recent dump information in a static global variable so that a signal handler can pick it...
Definition: error_stack.cpp:59
static std::string get_recent_dump_and_abort()
Signal handler can get the dump information via this.
Definition: error_stack.cpp:74
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
const char * get_error_name(ErrorCode code)
Returns the names of ErrorCode enum defined in error_code.xmacro.
Definition: error_code.hpp:108
std::ostream & operator<<(std::ostream &o, const Epoch &v)
Definition: epoch.cpp:23
const char * get_filename(uint16_t stack_index) const
Returns the file name of the given stack position.
Maximum stack trace depth.
Definition: error_stack.hpp:86
uint16_t get_stack_depth() const
Returns the depth of stack this error code has collected.
void dump_and_abort(const char *abort_message) const
Describe this object to std::cerr and then abort.
Definition: error_stack.cpp:61
uint32_t get_linenum(uint16_t stack_index) const
Returns the line number of the given stack position.
const char * get_custom_message() const
Returns the custom error message.
std::string os_error()
Thread-safe strerror(errno).
const char * get_func(uint16_t stack_index) const
Returns the function name of the given stack position.
void output(std::ostream *ptr) const
Describe this object to the given stream.
Definition: error_stack.cpp:30
const char * get_message() const
Returns the error message inferred by the error code.
std::string print_backtrace()
Prints out backtrace.
Definition: assert_nd.cpp:37
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
bool is_error() const
Returns if this return code is not kErrorCodeOk.