libexcept 1.1.19
Stack trace along C++ exceptions
exception.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
20// self
21//
22#include "libexcept/exception.h"
23
24#include "libexcept/demangle.h"
25
26
27// C++
28//
29#include <iostream>
30#include <memory>
31#include <vector>
32
33
34// C
35//
36#include <execinfo.h>
37#include <link.h>
38#include <unistd.h>
39
40
41
140namespace libexcept
141{
142
143
144
145
146namespace
147{
148
149
167
168
169
170
171} // no name namespace
172
173
174
175
176
177
178
179
180
181
182
183
184
194{
195 return g_collect_stack;
196}
197
198
224{
225 g_collect_stack = collect_stack;
226}
227
228
229
230
231
294exception_base_t::exception_base_t(int const stack_trace_depth)
295{
296 switch(get_collect_stack())
297 {
299 break;
300
302 f_stack_trace = collect_stack_trace(stack_trace_depth);
303 break;
304
307 break;
308
309 }
310}
311
312
330{
331 return f_parameters;
332}
333
334
346std::string exception_base_t::get_parameter(std::string const & name) const
347{
348 auto const it(f_parameters.find(name));
349 if(it == f_parameters.end())
350 {
351 return std::string();
352 }
353
354 return it->second;
355}
356
357
381exception_base_t & exception_base_t::set_parameter(std::string const & name, std::string const & value)
382{
383 if(!name.empty())
384 {
385 f_parameters[name] = value;
386 }
387
388 return *this;
389}
390
391
415 std::string const & what
416 , int const stack_trace_depth)
417 : std::logic_error(what.c_str())
418 , exception_base_t(stack_trace_depth)
419{
420}
421
422
445 char const * what
446 , int const stack_trace_depth)
447 : std::logic_error(what)
448 , exception_base_t(stack_trace_depth)
449{
450}
451
452
464char const * logic_exception_t::what() const throw()
465{
466 return std::logic_error::what();
467}
468
469
470
471
472
473
474
475
476
477
478
495 std::string const & what
496 , int const stack_trace_depth)
497 : std::out_of_range(what.c_str())
498 , exception_base_t(stack_trace_depth)
499{
500}
501
502
525 char const * what
526 , int const stack_trace_depth)
527 : std::out_of_range(what)
528 , exception_base_t(stack_trace_depth)
529{
530}
531
532
544char const * out_of_range_t::what() const throw()
545{
546 return std::out_of_range::what();
547}
548
549
550
551
552
553
554
555
556
557
558
559
570 std::string const & what
571 , int const stack_trace_depth)
572 : std::runtime_error(what.c_str())
573 , exception_base_t(stack_trace_depth)
574{
575}
576
577
596 char const * what
597 , int const stack_trace_depth)
598 : std::runtime_error(what)
599 , exception_base_t(stack_trace_depth)
600{
601}
602
603
615char const * exception_t::what() const throw()
616{
617 return std::runtime_error::what();
618}
619
620
621}
622// namespace libexcept
623// vim: ts=4 sw=4 et
exception_base_t & set_parameter(std::string const &name, std::string const &value)
Set a parameter in this exception.
parameter_t const & get_parameters() const
Retrieve the set of exception parameters.
std::string get_parameter(std::string const &name) const
Retrieve one of the exception parameters.
stack_trace_t f_stack_trace
The variable where the exception stack trace gets saved.
Definition exception.h:79
exception_base_t(int const stack_trace_depth=STACK_TRACE_DEPTH)
Initialize this Snap! exception.
virtual char const * what() const override
Retrieve the what parameter as passed to the constructor.
exception_t(std::string const &what, int const stack_trace_depth=STACK_TRACE_DEPTH)
Initialize an exception from a C++ string.
logic_exception_t(std::string const &what, int const stack_trace_depth=STACK_TRACE_DEPTH)
Initialize an exception from a C++ string.
virtual char const * what() const override
Retrieve the what parameter as passed to the constructor.
virtual char const * what() const override
Retrieve the what parameter as passed to the constructor.
out_of_range_t(std::string const &what, int const stack_trace_depth=STACK_TRACE_DEPTH)
Initialize an exception from a C++ string.
Declarations of demangle functions we support.
Declarations of the exception library.
collect_stack_t g_collect_stack
Global flag to eventually prevent stack trace collection.
void set_collect_stack(collect_stack_t collect_stack)
Set a general flag on whether to collect stack traces or not.
stack_trace_t collect_stack_trace(int stack_trace_depth)
Collect the raw stack trace in a list of strings.
std::map< std::string, std::string > parameter_t
Definition exception.h:57
collect_stack_t get_collect_stack()
Tells you whether the general flag is true or false.
stack_trace_t collect_stack_trace_with_line_numbers(int stack_trace_depth)
Collect the stack trace in a list of strings.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.