Leosac  0.7.0
OpenSourceAccessControl
ExceptionsTools.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac 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 Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "ExceptionsTools.hpp"
21 #include "tools/Colorize.hpp"
22 #include "tools/log.hpp"
23 
24 namespace
25 {
30 std::string build_prepend_identation(int level)
31 {
32  std::string prepend;
33  if (level)
34  prepend = '|';
35  for (int i = 0; i < level; ++i)
36  prepend += "--";
37  if (level)
38  prepend += "> ";
39 
40  return prepend;
41 }
42 }
43 
44 void Leosac::print_exception(const std::exception &e, int level /* = 0 */)
45 {
46  std::cerr << build_prepend_identation(level) << "exception: " << e.what()
47  << '\n';
48  try
49  {
50  std::rethrow_if_nested(e);
51  }
52  catch (const std::exception &e2)
53  {
54  print_exception(e2, level + 1);
55  }
56  catch (...)
57  {
58  std::cerr << "Unknown exception type." << std::endl;
59  }
60 }
61 
62 void Leosac::log_exception(const std::exception &e, int level /* = 0 */)
63 {
64  ERROR(build_prepend_identation(level) << "exception: " << e.what());
65  try
66  {
67  std::rethrow_if_nested(e);
68  }
69  catch (const std::exception &e2)
70  {
71  log_exception(e2, level + 1);
72  }
73  catch (...)
74  {
75  ERROR("Unknown exception type.");
76  }
77 }
void log_exception(const std::exception &e, int level=0)
Recursively log exceptions using the logging macro.
void print_exception(const std::exception &e, int level=0)
Recursively print the exception trace to std::cerr.
Definition: log.hpp:32