errorMessages.h
1 #pragma once
2 
3 #include <map> // std::map
4 #include <memory> // std::shared_ptr
5 #include <set> // std::set
6 #include <stdexcept> // std::out_of_range
7 #include <string> // std::string
8 #include <vector> // std::vector
9 
10 #include <morphio/enums.h> // Warning, Option
11 #include <morphio/mut/section.h> // Warning, Option
12 
13 namespace morphio {
15 void set_maximum_warnings(int n_warnings);
17 void set_raise_warnings(bool is_raise);
19 void set_ignored_warning(Warning warning, bool ignore = true);
21 void set_ignored_warning(const std::vector<Warning>& warning, bool ignore = true);
23 void printError(Warning warning, const std::string& msg);
24 
25 namespace readers {
26 
28 enum ErrorLevel {
29  INFO,
32 };
33 
35 struct DebugInfo {
36  public:
41  explicit DebugInfo(std::string filename = "")
42  : _filename(filename) {}
43 
45  void setLineNumber(uint32_t sectionId, unsigned int line) {
46  _lineNumbers[sectionId] = static_cast<int>(line);
47  }
48 
50  int32_t getLineNumber(uint32_t sectionId) const {
51  const auto it = _lineNumbers.find(sectionId);
52  if (it == _lineNumbers.end()) {
53  return -1;
54  }
55  return it->second;
56  }
58  std::string _filename;
59 
60  private:
61  std::map<unsigned int, int> _lineNumbers;
62 };
63 
64 // TODO: this shouldn't be global static
65 static std::set<Warning> _ignoredWarnings;
66 
68 struct Sample {
69  Sample() = default;
70 
71  floatType diameter = -1.;
72  bool valid = false;
73  Point point{};
75  int parentId = -1;
76  unsigned int id = 0;
77  unsigned int lineNumber = 0;
78 };
79 
83 {
84  public:
85  ErrorMessages() = default;
86 
91  explicit ErrorMessages(const std::string& uri)
92  : _uri(uri) {}
93 
95  static bool isIgnored(Warning warning);
96 
98  std::string errorLink(long unsigned int lineNumber, ErrorLevel errorLevel) const {
99  std::map<ErrorLevel, std::string> SEVERITY{{ErrorLevel::INFO, "info"},
100  {ErrorLevel::WARNING, "warning"},
101  {ErrorLevel::ERROR, "error"}};
102 
103  const std::map<ErrorLevel, std::string> COLOR{{ErrorLevel::INFO, "\033[1;34m"},
104  {ErrorLevel::WARNING, "\033[1;33m"},
105  {ErrorLevel::ERROR, "\033[1;31m"}};
106 
107  const std::string COLOR_END("\033[0m");
108 
109  return COLOR.at(errorLevel) + _uri + ":" + std::to_string(lineNumber) + ":" +
110  SEVERITY.at(errorLevel) + COLOR_END;
111  }
112 
114  std::string errorMsg(long unsigned int lineNumber,
115  ErrorLevel errorLevel,
116  std::string msg = "") const;
117 
119  // ERRORS
121 
123  std::string ERROR_OPENING_FILE() const;
124 
126  std::string ERROR_LINE_NON_PARSABLE(long unsigned int lineNumber) const;
127 
129  std::string ERROR_UNSUPPORTED_SECTION_TYPE(long unsigned int lineNumber,
130  const SectionType& type) const;
131 
133  std::string ERROR_UNSUPPORTED_VASCULATURE_SECTION_TYPE(long unsigned int lineNumber,
134  const VascularSectionType& type) const;
135 
137  std::string ERROR_MULTIPLE_SOMATA(const std::vector<Sample>& somata) const;
138 
140  std::string ERROR_MISSING_PARENT(const Sample& sample) const;
141 
143  std::string ERROR_SOMA_BIFURCATION(const Sample& sample,
144  const std::vector<Sample>& children) const;
145 
147  std::string ERROR_SOMA_WITH_NEURITE_PARENT(const Sample& sample) const;
148 
150  std::string ERROR_REPEATED_ID(const Sample& originalSample, const Sample& newSample) const;
151 
153  std::string ERROR_SELF_PARENT(const Sample& sample) const;
154 
156  std::string ERROR_NOT_IMPLEMENTED_UNDEFINED_SOMA(const std::string&) const;
157 
159  std::string ERROR_MISSING_MITO_PARENT(int mitoParentId) const;
160 
162  // NEUROLUCIDA
164 
165  std::string ERROR_SOMA_ALREADY_DEFINED(long unsigned int lineNumber) const;
166 
168  std::string ERROR_PARSING_POINT(long unsigned int lineNumber, const std::string& point) const;
169 
171  std::string ERROR_UNKNOWN_TOKEN(long unsigned int lineNumber, const std::string& token) const;
172 
174  std::string ERROR_UNEXPECTED_TOKEN(long unsigned int lineNumber,
175  const std::string& expected,
176  const std::string& got,
177  const std::string& msg) const;
178 
180  std::string ERROR_EOF_REACHED(long unsigned int lineNumber) const;
181 
183  std::string ERROR_EOF_IN_NEURITE(long unsigned int lineNumber) const;
184 
186  std::string ERROR_EOF_UNBALANCED_PARENS(long unsigned int lineNumber) const;
187 
189  std::string ERROR_UNCOMPATIBLE_FLAGS(morphio::Option flag1, morphio::Option flag2) const;
190 
192  // WRITERS
194 
196  std::string ERROR_UNSUPPORTED_SECTION_TYPE(const SectionType& type) const;
197 
199  std::string ERROR_WRONG_EXTENSION(const std::string& filename) const;
200 
202  std::string ERROR_VECTOR_LENGTH_MISMATCH(const std::string& vec1,
203  size_t length1,
204  const std::string& vec2,
205  size_t length2) const;
206 
208  std::string ERROR_PERIMETER_DATA_NOT_WRITABLE();
210  std::string ERROR_ONLY_CHILD_SWC_WRITER(unsigned int parentId) const;
211 
212 
214  // WARNINGS
216 
218  std::string WARNING_MITOCHONDRIA_WRITE_NOT_SUPPORTED() const;
220  std::string WARNING_WRITE_NO_SOMA() const;
222  std::string WARNING_WRITE_EMPTY_MORPHOLOGY() const;
224  std::string WARNING_NO_SOMA_FOUND() const;
226  std::string WARNING_ZERO_DIAMETER(const Sample& sample) const;
228  std::string WARNING_DISCONNECTED_NEURITE(const Sample& sample) const;
230  std::string WARNING_WRONG_DUPLICATE(const std::shared_ptr<morphio::mut::Section>& current,
231  const std::shared_ptr<morphio::mut::Section>& parent) const;
233  std::string WARNING_APPENDING_EMPTY_SECTION(std::shared_ptr<morphio::mut::Section>);
235  std::string WARNING_ONLY_CHILD(const DebugInfo& info,
236  unsigned int parentId,
237  unsigned int childId) const;
238 
240  std::string WARNING_NEUROMORPHO_SOMA_NON_CONFORM(const Sample& root,
241  const Sample& child1,
242  const Sample& child2);
243 
245  std::string WARNING_WRONG_ROOT_POINT(const std::vector<Sample>& children) const;
246 
247  private:
248  std::string _uri;
249 };
250 
251 } // namespace readers
252 
253 } // namespace morphio
morphio::readers::ErrorMessages::ErrorMessages
ErrorMessages(const std::string &uri)
Definition: errorMessages.h:91
morphio::enums::SectionType
SectionType
Definition: enums.h:62
morphio::readers::DebugInfo::getLineNumber
int32_t getLineNumber(uint32_t sectionId) const
Definition: errorMessages.h:50
morphio::readers::ErrorMessages::ERROR_UNCOMPATIBLE_FLAGS
std::string ERROR_UNCOMPATIBLE_FLAGS(morphio::Option flag1, morphio::Option flag2) const
morphio::readers::ErrorMessages::ERROR_ONLY_CHILD_SWC_WRITER
std::string ERROR_ONLY_CHILD_SWC_WRITER(unsigned int parentId) const
morphio::readers::ErrorMessages::ERROR_SOMA_WITH_NEURITE_PARENT
std::string ERROR_SOMA_WITH_NEURITE_PARENT(const Sample &sample) const
morphio::readers::ErrorMessages::WARNING_NEUROMORPHO_SOMA_NON_CONFORM
std::string WARNING_NEUROMORPHO_SOMA_NON_CONFORM(const Sample &root, const Sample &child1, const Sample &child2)
morphio::readers::ErrorMessages::ERROR_MISSING_MITO_PARENT
std::string ERROR_MISSING_MITO_PARENT(int mitoParentId) const
morphio::readers::ErrorLevel
ErrorLevel
Definition: errorMessages.h:28
morphio::readers::ErrorMessages::ERROR_REPEATED_ID
std::string ERROR_REPEATED_ID(const Sample &originalSample, const Sample &newSample) const
morphio::enums::SECTION_UNDEFINED
@ SECTION_UNDEFINED
Undefined section.
Definition: enums.h:63
morphio::readers::ErrorMessages::WARNING_WRONG_DUPLICATE
std::string WARNING_WRONG_DUPLICATE(const std::shared_ptr< morphio::mut::Section > &current, const std::shared_ptr< morphio::mut::Section > &parent) const
morphio::readers::ErrorMessages::ERROR_EOF_REACHED
std::string ERROR_EOF_REACHED(long unsigned int lineNumber) const
morphio::readers::DebugInfo::DebugInfo
DebugInfo(std::string filename="")
Definition: errorMessages.h:41
morphio::readers::Sample
Definition: errorMessages.h:68
morphio::readers::ERROR
@ ERROR
Error.
Definition: errorMessages.h:31
morphio::enums::Warning
Warning
Definition: enums.h:25
morphio::readers::ErrorMessages::ERROR_UNSUPPORTED_SECTION_TYPE
std::string ERROR_UNSUPPORTED_SECTION_TYPE(long unsigned int lineNumber, const SectionType &type) const
morphio::readers::ErrorMessages::ERROR_NOT_IMPLEMENTED_UNDEFINED_SOMA
std::string ERROR_NOT_IMPLEMENTED_UNDEFINED_SOMA(const std::string &) const
morphio::readers::ErrorMessages::ERROR_PERIMETER_DATA_NOT_WRITABLE
std::string ERROR_PERIMETER_DATA_NOT_WRITABLE()
morphio::enums::Option
Option
Definition: enums.h:13
morphio::readers::ErrorMessages::ERROR_LINE_NON_PARSABLE
std::string ERROR_LINE_NON_PARSABLE(long unsigned int lineNumber) const
morphio::readers::INFO
@ INFO
Info.
Definition: errorMessages.h:29
morphio::readers::ErrorMessages::ERROR_MISSING_PARENT
std::string ERROR_MISSING_PARENT(const Sample &sample) const
morphio::readers::ErrorMessages::ERROR_EOF_UNBALANCED_PARENS
std::string ERROR_EOF_UNBALANCED_PARENS(long unsigned int lineNumber) const
morphio::readers::ErrorMessages::WARNING_APPENDING_EMPTY_SECTION
std::string WARNING_APPENDING_EMPTY_SECTION(std::shared_ptr< morphio::mut::Section >)
morphio::readers::ErrorMessages::WARNING_ONLY_CHILD
std::string WARNING_ONLY_CHILD(const DebugInfo &info, unsigned int parentId, unsigned int childId) const
morphio::readers::ErrorMessages::ERROR_UNEXPECTED_TOKEN
std::string ERROR_UNEXPECTED_TOKEN(long unsigned int lineNumber, const std::string &expected, const std::string &got, const std::string &msg) const
morphio::readers::ErrorMessages::WARNING_WRONG_ROOT_POINT
std::string WARNING_WRONG_ROOT_POINT(const std::vector< Sample > &children) const
morphio::readers::ErrorMessages::errorMsg
std::string errorMsg(long unsigned int lineNumber, ErrorLevel errorLevel, std::string msg="") const
morphio::readers::ErrorMessages::WARNING_ZERO_DIAMETER
std::string WARNING_ZERO_DIAMETER(const Sample &sample) const
morphio::readers::ErrorMessages::errorLink
std::string errorLink(long unsigned int lineNumber, ErrorLevel errorLevel) const
Definition: errorMessages.h:98
morphio::readers::ErrorMessages::ERROR_SELF_PARENT
std::string ERROR_SELF_PARENT(const Sample &sample) const
morphio::readers::DebugInfo
Definition: errorMessages.h:35
morphio::readers::ErrorMessages::ERROR_SOMA_ALREADY_DEFINED
std::string ERROR_SOMA_ALREADY_DEFINED(long unsigned int lineNumber) const
morphio::readers::ErrorMessages::ERROR_WRONG_EXTENSION
std::string ERROR_WRONG_EXTENSION(const std::string &filename) const
morphio::readers::ErrorMessages::WARNING_NO_SOMA_FOUND
std::string WARNING_NO_SOMA_FOUND() const
morphio::readers::ErrorMessages::WARNING_WRITE_EMPTY_MORPHOLOGY
std::string WARNING_WRITE_EMPTY_MORPHOLOGY() const
morphio::readers::WARNING
@ WARNING
Warning.
Definition: errorMessages.h:30
morphio::readers::ErrorMessages::WARNING_WRITE_NO_SOMA
std::string WARNING_WRITE_NO_SOMA() const
morphio::readers::ErrorMessages::ERROR_OPENING_FILE
std::string ERROR_OPENING_FILE() const
morphio::enums::VascularSectionType
VascularSectionType
Definition: enums.h:99
morphio::readers::ErrorMessages
Definition: errorMessages.h:82
morphio::readers::ErrorMessages::isIgnored
static bool isIgnored(Warning warning)
morphio::readers::ErrorMessages::ERROR_SOMA_BIFURCATION
std::string ERROR_SOMA_BIFURCATION(const Sample &sample, const std::vector< Sample > &children) const
morphio::readers::ErrorMessages::ERROR_UNKNOWN_TOKEN
std::string ERROR_UNKNOWN_TOKEN(long unsigned int lineNumber, const std::string &token) const
morphio::readers::ErrorMessages::ERROR_VECTOR_LENGTH_MISMATCH
std::string ERROR_VECTOR_LENGTH_MISMATCH(const std::string &vec1, size_t length1, const std::string &vec2, size_t length2) const
morphio::readers::ErrorMessages::ERROR_EOF_IN_NEURITE
std::string ERROR_EOF_IN_NEURITE(long unsigned int lineNumber) const
morphio::readers::ErrorMessages::ERROR_UNSUPPORTED_VASCULATURE_SECTION_TYPE
std::string ERROR_UNSUPPORTED_VASCULATURE_SECTION_TYPE(long unsigned int lineNumber, const VascularSectionType &type) const
morphio::readers::ErrorMessages::WARNING_DISCONNECTED_NEURITE
std::string WARNING_DISCONNECTED_NEURITE(const Sample &sample) const
morphio::readers::ErrorMessages::ERROR_PARSING_POINT
std::string ERROR_PARSING_POINT(long unsigned int lineNumber, const std::string &point) const
morphio::readers::ErrorMessages::WARNING_MITOCHONDRIA_WRITE_NOT_SUPPORTED
std::string WARNING_MITOCHONDRIA_WRITE_NOT_SUPPORTED() const
morphio::readers::DebugInfo::_filename
std::string _filename
Definition: errorMessages.h:58
morphio::readers::ErrorMessages::ERROR_MULTIPLE_SOMATA
std::string ERROR_MULTIPLE_SOMATA(const std::vector< Sample > &somata) const
morphio::readers::DebugInfo::setLineNumber
void setLineNumber(uint32_t sectionId, unsigned int line)
Definition: errorMessages.h:45