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  try {
52  return _lineNumbers.at(sectionId);
53  } catch (const std::out_of_range&) {
54  return -1;
55  }
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  explicit Sample(const char* line, unsigned int lineNumber_)
72  : lineNumber(lineNumber_) {
73  floatType radius = -1.;
74  int int_type = -1;
75 #ifdef MORPHIO_USE_DOUBLE
76  const auto format = "%20u%20d%20lg%20lg%20lg%20lg%20d";
77 #else
78  const auto format = "%20u%20d%20f%20f%20f%20f%20d";
79 #endif
80  valid = sscanf(line,
81  format,
82  &id,
83  &int_type,
84  &point[0],
85  &point[1],
86  &point[2],
87  &radius,
88  &parentId) == 7;
89 
90  type = static_cast<SectionType>(int_type);
91  diameter = radius * 2; // The point array stores diameters.
92  }
93 
94  floatType diameter = -1.;
95  bool valid = false;
96  Point point;
98  int parentId = -1;
99  unsigned int id = 0;
100  unsigned int lineNumber = 0;
101 };
102 
106 {
107  public:
108  ErrorMessages() = default;
109 
114  explicit ErrorMessages(const std::string& uri)
115  : _uri(uri) {}
116 
118  static bool isIgnored(Warning warning);
119 
121  std::string errorLink(long unsigned int lineNumber, ErrorLevel errorLevel) const {
122  std::map<ErrorLevel, std::string> SEVERITY{{ErrorLevel::INFO, "info"},
123  {ErrorLevel::WARNING, "warning"},
124  {ErrorLevel::ERROR, "error"}};
125 
126  const std::map<ErrorLevel, std::string> COLOR{{ErrorLevel::INFO, "\033[1;34m"},
127  {ErrorLevel::WARNING, "\033[1;33m"},
128  {ErrorLevel::ERROR, "\033[1;31m"}};
129 
130  const std::string COLOR_END("\033[0m");
131 
132  return COLOR.at(errorLevel) + _uri + ":" + std::to_string(lineNumber) + ":" +
133  SEVERITY.at(errorLevel) + COLOR_END;
134  }
135 
137  std::string errorMsg(long unsigned int lineNumber,
138  ErrorLevel errorLevel,
139  std::string msg = "") const;
140 
142  // ERRORS
144 
146  std::string ERROR_OPENING_FILE() const;
147 
149  std::string ERROR_LINE_NON_PARSABLE(long unsigned int lineNumber) const;
150 
152  std::string ERROR_UNSUPPORTED_SECTION_TYPE(long unsigned int lineNumber,
153  const SectionType& type) const;
154 
156  std::string ERROR_UNSUPPORTED_VASCULATURE_SECTION_TYPE(long unsigned int lineNumber,
157  const VascularSectionType& type) const;
158 
160  std::string ERROR_MULTIPLE_SOMATA(const std::vector<Sample>& somata) const;
161 
163  std::string ERROR_MISSING_PARENT(const Sample& sample) const;
164 
166  std::string ERROR_SOMA_BIFURCATION(const Sample& sample,
167  const std::vector<Sample>& children) const;
168 
170  std::string ERROR_SOMA_WITH_NEURITE_PARENT(const Sample& sample) const;
171 
173  std::string ERROR_REPEATED_ID(const Sample& originalSample, const Sample& newSample) const;
174 
176  std::string ERROR_SELF_PARENT(const Sample& sample) const;
177 
179  std::string ERROR_NOT_IMPLEMENTED_UNDEFINED_SOMA(const std::string&) const;
180 
182  std::string ERROR_MISSING_MITO_PARENT(int mitoParentId) const;
183 
185  // NEUROLUCIDA
187 
188  std::string ERROR_SOMA_ALREADY_DEFINED(long unsigned int lineNumber) const;
189 
191  std::string ERROR_PARSING_POINT(long unsigned int lineNumber, const std::string& point) const;
192 
194  std::string ERROR_UNKNOWN_TOKEN(long unsigned int lineNumber, const std::string& token) const;
195 
197  std::string ERROR_UNEXPECTED_TOKEN(long unsigned int lineNumber,
198  const std::string& expected,
199  const std::string& got,
200  const std::string& msg) const;
201 
203  std::string ERROR_EOF_REACHED(long unsigned int lineNumber) const;
204 
206  std::string ERROR_EOF_IN_NEURITE(long unsigned int lineNumber) const;
207 
209  std::string ERROR_EOF_UNBALANCED_PARENS(long unsigned int lineNumber) const;
210 
212  std::string ERROR_UNCOMPATIBLE_FLAGS(morphio::Option flag1, morphio::Option flag2) const;
213 
215  // WRITERS
217 
219  std::string ERROR_UNSUPPORTED_SECTION_TYPE(const SectionType& type) const;
220 
222  std::string ERROR_WRONG_EXTENSION(const std::string& filename) const;
223 
225  std::string ERROR_VECTOR_LENGTH_MISMATCH(const std::string& vec1,
226  size_t length1,
227  const std::string& vec2,
228  size_t length2) const;
229 
231  std::string ERROR_PERIMETER_DATA_NOT_WRITABLE();
233  std::string ERROR_ONLY_CHILD_SWC_WRITER(unsigned int parentId) const;
234 
235 
237  // WARNINGS
239 
241  std::string WARNING_MITOCHONDRIA_WRITE_NOT_SUPPORTED() const;
243  std::string WARNING_WRITE_NO_SOMA() const;
245  std::string WARNING_WRITE_EMPTY_MORPHOLOGY() const;
247  std::string WARNING_NO_SOMA_FOUND() const;
249  std::string WARNING_ZERO_DIAMETER(const Sample& sample) const;
251  std::string WARNING_DISCONNECTED_NEURITE(const Sample& sample) const;
253  std::string WARNING_WRONG_DUPLICATE(const std::shared_ptr<morphio::mut::Section>& current,
254  const std::shared_ptr<morphio::mut::Section>& parent) const;
256  std::string WARNING_APPENDING_EMPTY_SECTION(std::shared_ptr<morphio::mut::Section>);
258  std::string WARNING_ONLY_CHILD(const DebugInfo& info,
259  unsigned int parentId,
260  unsigned int childId) const;
261 
263  std::string WARNING_NEUROMORPHO_SOMA_NON_CONFORM(const Sample& root,
264  const Sample& child1,
265  const Sample& child2);
266 
268  std::string WARNING_WRONG_ROOT_POINT(const std::vector<Sample>& children) const;
269 
270  private:
271  std::string _uri;
272 };
273 
274 } // namespace readers
275 
276 } // namespace morphio
morphio::readers::ErrorMessages::ErrorMessages
ErrorMessages(const std::string &uri)
Definition: errorMessages.h:114
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:121
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:105
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