src/utilities/xml/XMLErrors.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "XMLErrors.hpp" | ||
| 7 | |||
| 8 | #include <libxml/xmlerror.h> | ||
| 9 | #include "../core/LogMessage.hpp" | ||
| 10 | #include "../core/ASCIIStrings.hpp" | ||
| 11 | |||
| 12 | #include <cstdarg> // For va_list | ||
| 13 | #include <cstdio> // std::vsnprintf | ||
| 14 | #include <vector> | ||
| 15 | |||
| 16 | namespace openstudio { | ||
| 17 | |||
| 18 | namespace detail { | ||
| 19 | |||
| 20 | 2677 | std::string build_structured_message(const std::string& levelName, const xmlError& error) { | |
| 21 |
1/1✓ Branch 1 taken 2677 times.
|
2677 | std::ostringstream oss; |
| 22 | |||
| 23 | // We currently don't decode domain and code to their symbolic | ||
| 24 | // representation as it doesn't seem to be worth it in practice, the error | ||
| 25 | // message is usually clear enough, while these numbers can be used for | ||
| 26 | // automatic classification of messages. | ||
| 27 | |||
| 28 | // Message is '\n' terminated, so remove that | ||
| 29 |
1/1✓ Branch 1 taken 2677 times.
|
2677 | std::string message(error.message); |
| 30 |
1/1✓ Branch 1 taken 2677 times.
|
2677 | ascii_trim(message); |
| 31 | |||
| 32 |
8/8✓ Branch 1 taken 2677 times.
✓ Branch 4 taken 2677 times.
✓ Branch 7 taken 2677 times.
✓ Branch 10 taken 2677 times.
✓ Branch 13 taken 2677 times.
✓ Branch 16 taken 2677 times.
✓ Branch 19 taken 2677 times.
✓ Branch 22 taken 2677 times.
|
2677 | oss << "XML " << levelName << " " << error.domain << "." << error.code << ": " << message; |
| 33 | |||
| 34 |
1/2✓ Branch 0 taken 2677 times.
✗ Branch 1 not taken.
|
2677 | if (error.file) { |
| 35 |
2/2✓ Branch 1 taken 2677 times.
✓ Branch 4 taken 2677 times.
|
2677 | oss << "\n at " << error.file; |
| 36 |
1/2✓ Branch 0 taken 2677 times.
✗ Branch 1 not taken.
|
2677 | if (error.line > 0) { |
| 37 |
2/2✓ Branch 1 taken 2677 times.
✓ Branch 4 taken 2677 times.
|
2677 | oss << ":" << error.line; |
| 38 | |||
| 39 | // Column information, if available, is passed in the second int | ||
| 40 | // field (first one is used with the first string field, see below). | ||
| 41 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2677 times.
|
2677 | if (error.int2 > 0) { |
| 42 | ✗ | oss << "," << error.int2; | |
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 |
2/2✓ Branch 0 taken 2571 times.
✓ Branch 1 taken 106 times.
|
2677 | if (error.str1) { |
| 48 |
3/3✓ Branch 1 taken 2571 times.
✓ Branch 4 taken 2571 times.
✓ Branch 7 taken 2571 times.
|
2571 | oss << " while processing \"" << error.str1 << "\""; |
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2571 times.
|
2571 | if (error.int1 > 0) { |
| 50 | ✗ | oss << " at position " << error.int1; | |
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 |
1/1✓ Branch 1 taken 2677 times.
|
5354 | return oss.str(); |
| 55 | 2677 | } | |
| 56 | |||
| 57 | // cppcheck-suppress constParameterPointer | ||
| 58 | 2677 | void callback_messages_structured_error(void* errorCollectorVoidPtr, xmlError* error) { | |
| 59 | // This shouldn't happen, but better be safe than sorry | ||
| 60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2677 times.
|
2677 | if (!error) { |
| 61 | ✗ | return; | |
| 62 | } | ||
| 63 | |||
| 64 |
1/2✓ Branch 0 taken 2677 times.
✗ Branch 1 not taken.
|
2677 | if (error->message) { |
| 65 | 2677 | LogLevel level = LogLevel::Trace; | |
| 66 | 2677 | std::string levelName; | |
| 67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2677 times.
|
2677 | if (error->level == XML_ERR_NONE) { |
| 68 | ✗ | levelName = "None"; | |
| 69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2677 times.
|
2677 | } else if (error->level == XML_ERR_WARNING) { |
| 70 | // Some libxml warnings are pretty fatal errors, e.g. failing | ||
| 71 | // to open the input file is reported as a warning with code | ||
| 72 | // XML_IO_LOAD_ERROR, so treat them as such. | ||
| 73 | ✗ | if (error->code == XML_IO_LOAD_ERROR) { | |
| 74 | ✗ | level = LogLevel::Error; | |
| 75 | ✗ | levelName = "error"; | |
| 76 | } else { | ||
| 77 | ✗ | level = LogLevel::Warn; | |
| 78 | ✗ | levelName = "warning"; | |
| 79 | } | ||
| 80 | |||
| 81 |
1/2✓ Branch 0 taken 2677 times.
✗ Branch 1 not taken.
|
2677 | } else if (error->level == XML_ERR_ERROR) { |
| 82 | 2677 | level = LogLevel::Error; | |
| 83 |
1/1✓ Branch 1 taken 2677 times.
|
2677 | levelName = "error"; |
| 84 | ✗ | } else if (error->level == XML_ERR_FATAL) { | |
| 85 | ✗ | level = LogLevel::Fatal; | |
| 86 | ✗ | levelName = "fatal error"; | |
| 87 | } | ||
| 88 | |||
| 89 | 2677 | auto* errorCollector = static_cast<ErrorCollector*>(errorCollectorVoidPtr); | |
| 90 |
2/2✓ Branch 1 taken 2677 times.
✓ Branch 4 taken 2677 times.
|
2677 | errorCollector->logMessages.emplace_back(level, "XMLValidator", build_structured_message(levelName, *error)); |
| 91 | 2677 | } | |
| 92 | } | ||
| 93 | |||
| 94 | ✗ | std::string get_string_from_va(const char* fmt, va_list args) { | |
| 95 | // Make a second list. We calc std::vsnprintf with a buf_size of 0, so nothing is written but the number of bytes that would be written **not** | ||
| 96 | // including the null terminator is still calculated and returned | ||
| 97 | va_list args_for_size_calc; | ||
| 98 | ✗ | va_copy(args_for_size_calc, args); | |
| 99 | ✗ | const int nchars = std::vsnprintf(nullptr, 0, fmt, args_for_size_calc) + 1; | |
| 100 | ✗ | va_end(args_for_size_calc); | |
| 101 | |||
| 102 | ✗ | std::vector<char> buf(nchars); | |
| 103 | ✗ | std::vsnprintf(buf.data(), buf.size(), fmt, args); | |
| 104 | |||
| 105 | ✗ | return std::string(buf.begin(), buf.end()); | |
| 106 | ✗ | } | |
| 107 | |||
| 108 | ✗ | void callback_messages_warning(void* errorCollectorVoidPtr, const char* fmt, ...) { | |
| 109 | // cf: https://en.cppreference.com/w/cpp/io/c/vfprintf | ||
| 110 | |||
| 111 | // Make a va_list | ||
| 112 | va_list args; | ||
| 113 | // Setup args by using the `fmt` as the last know explicit argument | ||
| 114 | // The libxml2 documentation is lacking, but the first argument is a format-string, the rest of the va_args are the values to put in there | ||
| 115 | ✗ | va_start(args, fmt); | |
| 116 | |||
| 117 | ✗ | std::string message = get_string_from_va(fmt, args); | |
| 118 | ✗ | va_end(args); | |
| 119 | |||
| 120 | ✗ | auto* errorCollector = static_cast<ErrorCollector*>(errorCollectorVoidPtr); | |
| 121 | ✗ | errorCollector->logMessages.emplace_back(LogLevel::Warn, "XMLValidator", message); | |
| 122 | ✗ | } | |
| 123 | |||
| 124 | ✗ | void callback_messages_error(void* errorCollectorVoidPtr, const char* fmt, ...) { | |
| 125 | // cf: https://en.cppreference.com/w/cpp/io/c/vfprintf | ||
| 126 | |||
| 127 | // Make a va_list | ||
| 128 | va_list args; | ||
| 129 | // Setup args by using the `fmt` as the last know explicit argument | ||
| 130 | // The libxml2 documentation is lacking, but the first argument is a format-string, the rest of the va_args are the values to put in there | ||
| 131 | ✗ | va_start(args, fmt); | |
| 132 | |||
| 133 | ✗ | std::string message = get_string_from_va(fmt, args); | |
| 134 | ✗ | va_end(args); | |
| 135 | |||
| 136 | ✗ | auto* errorCollector = static_cast<ErrorCollector*>(errorCollectorVoidPtr); | |
| 137 | ✗ | errorCollector->logMessages.emplace_back(LogLevel::Error, "XMLValidator", message); | |
| 138 | ✗ | } | |
| 139 | |||
| 140 | } // namespace detail | ||
| 141 | |||
| 142 | } // namespace openstudio | ||
| 143 |