NANA
error.cpp
1#include "../include/core/error.h"
2#include <iostream>
3#include <ostream>
4
5namespace NANA {
6
8
9
10Exception::Exception()
11{
12 code = 0;
13 line = 0;
14}
15
16Exception::Exception(int _code, const String& _err, const String& _func, const String& _file, int _line)
17 : code(_code), err(_err), func(_func), file(_file), line(_line)
18{
19 formatMessage();
20}
21
22Exception::~Exception() throw() {}
23
27const char* Exception::what() const throw()
28{
29 return msg.c_str();
30}
31
32void Exception::formatMessage()
33{
34 size_t pos = err.find('\n');
35 bool multiline = pos != String::npos;
36 if (multiline)
37 {
38 size_t prev_pos = 0;
39 while (pos != String::npos)
40 {
41 std::cout << "> " << err.substr(prev_pos, pos - prev_pos) << std::endl;
42 prev_pos = pos + 1;
43 pos = err.find('\n', prev_pos);
44 }
45 std::cout << "> " << err.substr(prev_pos);
46 if (err[err.size() - 1] != '\n')
47 std::cout << std::endl;
48 }
49
50}
51
52
54CError* CError::instance(std::string filename)
55{
56 static CError* obj = nullptr;
57 if (nullptr == obj) {
58 obj = new CError(filename);
59 }
60 return obj;
61}
62
63CError::CError(std::string& filename)
64{
65 m_filename = filename;
66}
68
69
70}
71