CUBRID Engine  latest
error_context.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * error_context.hpp - interface for error context
21  */
22 
23 #ifndef _ERROR_CONTEXT_HPP_
24 #define _ERROR_CONTEXT_HPP_
25 
26 #include <cstddef>
27 
28 #include <stack>
29 
30 namespace cuberr
31 {
32  const std::size_t ER_EMERGENCY_BUF_SIZE = 256;
33 
34  // legacy structures
35  union er_va_arg
36  {
37  int int_value; /* holders for the values that we actually */
38  void *pointer_value; /* retrieve from the va_list. */
39  double double_value;
40  long double longdouble_value;
41  const char *string_value;
42  long long longlong_value;
43  };
44 
45  struct er_message
46  {
47  public:
48  er_message (const bool &logging);
49  ~er_message ();
50 
51  void swap (er_message &other);
52 
53  void clear_error (void); // clear error and message
54  void set_error (int error_id, int error_severity, const char *filename, int line_no); // set error
55  void reserve_message_area (std::size_t size);
56  void clear_message_area (void);
57  void clear_args (void);
58 
59  int err_id; /* Error identifier of the current message */
60  int severity; /* Warning, Error, FATAL Error, etc... */
61  const char *file_name; /* File where the error is set */
62  int line_no; /* Line in the file where the error is set */
63  std::size_t msg_area_size; /* Size of the message area */
64  char *msg_area; /* Pointer to message area */
65  er_va_arg *args; /* Array of va_list entries */
66  int nargs; /* Length of array */
67  char msg_buffer[ER_EMERGENCY_BUF_SIZE]; // message buffer
68 
69  private:
70  // not copy constructible
72 
73  const bool &m_logging; // reference to context logging
74  };
75 
76  class context
77  {
78  public:
79 
80  // constructor
81  // automatic_registration - if true, registration/deregistration is handled during construct/destruct
82  // logging - if true, extensive logging is activated.
83  // NOTE: "nested" logging is possible and er_Log_file_mutex may be locked twice causing a hang.
84  // fix this if you want to use the error context logging.
85  //
86  context (bool automatic_registration = false, bool logging = false);
87 
88  ~context ();
89 
90  er_message &get_current_error_level (void);
91 
92  void register_thread_local (void);
93  void deregister_thread_local (void);
94 
95  void clear_current_error_level (void);
96  void push_error_stack (void);
97  void pop_error_stack (er_message &popped); // caller will destroy popped
98  void pop_error_stack_and_destroy (void);
99  bool has_error_stack (void);
100 
101  const bool &get_logging (void);
102 
103  static context &get_thread_local_context (void);
104  static er_message &get_thread_local_error (void);
105 
106  private:
107  void clear_all_levels (void);
108  void clear_stack (void);
109 
111  std::stack<er_message> m_stack;
113  bool m_logging; // activate logging
114  bool m_destroyed; // set to true on destruction
115  };
116 } // namespace cuberr
117 
118 #endif // _ERROR_CONTEXT_HPP_
long long longlong_value
const char * string_value
er_message m_base_level
long double longdouble_value
std::stack< er_message > m_stack
const bool & m_logging
const std::size_t ER_EMERGENCY_BUF_SIZE
const char * file_name
std::size_t msg_area_size