libfoedus-core
FOEDUS Core Library
fixed_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  */
19 
20 #include <glog/logging.h>
21 
22 #include <iostream>
23 
25 
26 namespace foedus {
28  os_errno_ = 0;
29  error_code_ = kErrorCodeOk;
30  stack_depth_ = 0;
31  custom_message_.clear();
32  if (!src.is_error()) {
33  return *this;
34  }
35 
36  if (src.get_custom_message()) {
37  custom_message_.assign(src.get_custom_message());
38  }
39  stack_depth_ = src.get_stack_depth();
40  for (uint16_t i = 0; i < stack_depth_; ++i) {
41  filenames_[i].assign(src.get_filename(i));
42  funcs_[i].assign(src.get_func(i));
43  linenums_[i] = src.get_linenum(i);
44  }
45  os_errno_ = src.get_os_errno();
46  error_code_ = src.get_error_code();
47  return *this;
48 }
49 
50 void FixedErrorStack::output(std::ostream* ptr) const {
51  std::ostream &o = *ptr; // just to workaround non-const reference rule.
52  if (!is_error()) {
53  o << "No error";
54  } else {
55  o << get_error_name(error_code_) << "(" << error_code_ << "):" << get_message();
56  if (os_errno_ != 0) {
57  o << " (Latest system call error=" << assorted::os_error(os_errno_) << ")";
58  }
59  if (!get_custom_message().empty()) {
60  o << " (Additional message=" << get_custom_message() << ")";
61  }
62 
63  for (uint16_t stack_index = 0; stack_index < get_stack_depth(); ++stack_index) {
64  o << std::endl << " " << get_filename(stack_index)
65  << ":" << get_linenum(stack_index) << ": ";
66  if (!get_func(stack_index).empty()) {
67  o << get_func(stack_index) << "()";
68  }
69  }
71  o << std::endl << " .. and more. Increase kMaxStackDepth to see full stacktraces";
72  }
73  }
74 }
75 
77  if (!is_error()) {
78  return kRetOk;
79  }
80  std::stringstream msg;
81  output(&msg);
82  return ERROR_STACK_MSG(error_code_, msg.str().c_str());
83 }
84 
86  if (!other.is_error()) {
87  clear();
88  return;
89  }
90 
91  error_code_ = other.get_error_code();
92  os_errno_ = other.get_os_errno();
93  stack_depth_ = other.get_stack_depth();
94  custom_message_.clear();
95  if (other.get_custom_message()) {
96  custom_message_ = other.get_custom_message();
97  }
98  for (uint16_t i = 0; i < stack_depth_; ++i) {
99  filenames_[i] = other.get_filename(i);
100  funcs_[i] = other.get_func(i);
101  linenums_[i] = other.get_linenum(i);
102  }
103 }
104 
105 
106 std::ostream& operator<<(std::ostream& o, const FixedErrorStack& obj) {
107  obj.output(&o);
108  return o;
109 }
110 
111 } // namespace foedus
112 
const FixedFuncName & get_func(uint16_t stack_index) const
Returns the function name of the given stack position.
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
uint16_t get_stack_depth() const
Returns the depth of stack this error code has collected.
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
bool is_error() const
Returns if this return code is not kErrorCodeOk.
const char * get_filename(uint16_t stack_index) const
Returns the file name of the given stack position.
ErrorCode get_error_code() const
Return the integer error code.
0 means no-error.
Definition: error_code.hpp:87
FixedErrorStack & operator=(const ErrorStack &src)
Assignment operator.
void assign(const FixedString< MAXLEN2, CHAR > &other) noexcept
Assign operator for all FixedString objects.
ErrorStack to_error_stack() const
Instantiates an ErrorStack object based on this object.
void output(std::ostream *ptr) const
Describe this object to the given stream.
Maximum stack trace depth.
Definition: error_stack.hpp:86
const char * get_message() const
Returns the error message inferred by the error code.
uint16_t get_stack_depth() const
Returns the depth of stack this error code has collected.
Representation of ErrorStack that can be copied to other processes and even serialized to files...
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.
const FixedFileName & get_filename(uint16_t stack_index) const
Returns the file name of the given stack position.
const ErrorStack kRetOk
Normal return value for no-error case.
uint32_t get_linenum(uint16_t stack_index) const
Returns the line number of the given stack position.
#define ERROR_STACK_MSG(e, m)
Overload of ERROR_STACK(e) to receive a custom error message.
void clear() noexcept
Clear string.
int get_os_errno() const
Global errno of the system as of instantiation of this error stack.
const FixedErrorMessage & get_custom_message() const
Returns the custom error message.
void from_error_stack(const ErrorStack &other)
Convert ErrorStack to this object.
bool is_error() const
Returns if this return code is not kErrorCodeOk.