Flow123d  release_2.2.0-914-gf1a3a4f
asserts.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file asserts.cc
15  * @brief Definitions of ASSERTS.
16  */
17 
18 
19 #include "system/asserts.hh"
20 #include "system/logger.hh"
21 
22 
23 namespace feal {
24 
25 /*******************************************************************
26  * implementation of Exc_assert
27  */
28 
30 : line_(0),
31  what_type_msg_("Program Error: Violated assert! ") {}
32 
33 
34 void Exc_assert::print_info(std::ostringstream &out) const
35 {
36  out << std::endl << "> In file: " << file_name_ << "(" << line_ << "): Throw in function " << function_ << std::endl;
37  out << "> Expression: \'" << expression_ << "\'" << "\n";
38  if (current_val_.size()) {
39  out << "> Values:" << std::endl;
40  for (auto val : current_val_) {
41  out << " " << val << std::endl;
42  }
43  }
44 }
45 
46 
47 std::string Exc_assert::what_type_msg() const {
48  return what_type_msg_;
49 }
50 
51 
52 std::ostringstream &Exc_assert::form_message(std::ostringstream &converter) const {
53 
54  converter << "--------------------------------------------------------" << std::endl;
55  converter << this->what_type_msg();
56  print_info(converter);
57 
58  print_stacktrace(converter);
59  converter << std::endl << "--------------------------------------------------------" << std::endl;
60 
61  return converter;
62 }
63 
64 
65 /*******************************************************************
66  * implementation of Assert
67  */
68 
70  if (!thrown_) {
71  // We can't throw exception in destructor, we need use this construction
72  std::cerr << exception_.what();
73  abort();
74  }
75 }
76 
77 
78 Assert& Assert::set_context(const char* file_name, const char* function, const int line)
79 {
80  exception_.file_name_ = std::string(file_name);
81  exception_.function_ = std::string(function);
82  exception_.line_ = line;
83 
84  return *this;
85 }
86 
87 
88 void Assert::error(std::string error_msg)
89 {
90  exception_.what_type_msg_ += error_msg;
91  thrown_ = true;
92  THROW( exception_ );
93 }
94 
95 
96 void Assert::warning(std::string warning_msg)
97 {
98  thrown_ = true;
99  std::ostringstream info_str, stack_str;
100  exception_.print_info(info_str);
101  exception_.print_stacktrace(stack_str);
102  WarningOut() << warning_msg << info_str.str() << StreamMask::log << stack_str.str();
103 }
104 
105 } // namespace feal
Class defining debugging messages.
Definition: asserts.hh:120
void warning(std::string warning_msg="")
Generate warning with given message.
Definition: asserts.cc:96
~Assert()
Destructor.
Definition: asserts.cc:69
Definitions of ASSERTS.
std::ostringstream & form_message(std::ostringstream &) const override
Override ExceptionBase::form_message()
Definition: asserts.cc:52
void print_info(std::ostringstream &out) const override
Print formatted assert message.
Definition: asserts.cc:34
std::string function_
Actual function.
Definition: asserts.hh:54
Definition: asserts.cc:23
static StreamMask log
Predefined mask of log file output.
Definition: logger.hh:58
std::string file_name_
Actual file.
Definition: asserts.hh:53
std::string what_type_msg() const override
Override ExceptionBase::what_type_msg()
Definition: asserts.cc:47
std::string expression_
Assertion expression.
Definition: asserts.hh:52
std::vector< std::string > current_val_
Formated strings of names and values of given variables.
Definition: asserts.hh:56
void error(std::string error_msg="")
Generate error with given message.
Definition: asserts.cc:88
#define WarningOut()
Macro defining &#39;warning&#39; record of log.
Definition: logger.hh:246
int line_
Actual line.
Definition: asserts.hh:55
std::string what_type_msg_
String representation of message type (Program error, Warning, ...)
Definition: asserts.hh:57
void print_stacktrace(std::ostream &out) const
Prints formated stacktrace into given stream out.
Definition: exceptions.cc:42
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
Assert & set_context(const char *file_name, const char *function, const int line)
Stores values for printing out line number, function, etc.
Definition: asserts.cc:78