Skip to content

Commit 539a663

Browse files
authored
Merge pull request #599 from mtconnect/add_options_support_for_cors
Add options support for cors
2 parents 061fe91 + 131304b commit 539a663

20 files changed

Lines changed: 690 additions & 82 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set(AGENT_VERSION_MAJOR 2)
33
set(AGENT_VERSION_MINOR 7)
44
set(AGENT_VERSION_PATCH 0)
5-
set(AGENT_VERSION_BUILD 5)
5+
set(AGENT_VERSION_BUILD 6)
66
set(AGENT_VERSION_RC "")
77

88
# This minimum version is to support Visual Studio 2019 and C++ feature checking and FetchContent

src/mtconnect/configuration/agent_config.cpp

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ namespace mtconnect::configuration {
176176
cerr << "Loading configuration from:" << *path << endl;
177177

178178
m_configFile = fs::canonical(*path);
179-
addPathFront(m_configPaths, m_configFile.parent_path());
180-
addPathBack(m_dataPaths, m_configFile.parent_path());
179+
m_configPath = m_configFile.parent_path();
180+
addPathFront(m_configPaths, m_configPath);
181+
addPathBack(m_dataPaths, m_configPath);
181182

182183
ifstream file(m_configFile.c_str());
183184
std::stringstream buffer;
@@ -598,6 +599,7 @@ namespace mtconnect::configuration {
598599
auto &rotationLogInterval = logChannel.m_rotationLogInterval;
599600
auto &logArchivePattern = logChannel.m_logArchivePattern;
600601
auto &logDirectory = logChannel.m_logDirectory;
602+
auto &archiveLogDirectory = logChannel.m_archiveLogDirectory;
601603
auto &logFileName = logChannel.m_logFileName;
602604

603605
maxLogFileSize = ConvertFileSize(options, "max_size", maxLogFileSize);
@@ -618,29 +620,51 @@ namespace mtconnect::configuration {
618620
auto file_name = *GetOption<string>(options, "file_name");
619621
auto archive_pattern = *GetOption<string>(options, "archive_pattern");
620622

623+
// Default the log directory to the configuration file path.
624+
logDirectory = m_configPath;
625+
logFileName = fs::path(file_name);
621626
logArchivePattern = fs::path(archive_pattern);
622-
if (!logArchivePattern.has_filename())
627+
628+
// Determine the log directory based on the provided file name and archive pattern
629+
if (logFileName.is_absolute())
630+
logDirectory = logFileName.parent_path();
631+
else if (logArchivePattern.is_absolute())
632+
logDirectory = logArchivePattern.parent_path();
633+
634+
// If the log file name is relative and has a parent path, use it to determine the log directory
635+
if (logFileName.is_relative() && logFileName.has_parent_path())
623636
{
624-
logArchivePattern = logArchivePattern / archiveFileName(get<string>(options["file_name"]));
637+
logDirectory = logDirectory / logFileName.parent_path();
638+
logFileName = logFileName.filename();
639+
}
640+
else if (logArchivePattern.is_relative() && logArchivePattern.has_parent_path())
641+
{
642+
logDirectory = logDirectory / logArchivePattern.parent_path();
643+
logArchivePattern = logArchivePattern.filename();
625644
}
626645

627-
if (logArchivePattern.is_relative())
628-
logArchivePattern = fs::current_path() / logArchivePattern;
646+
// Make sure the log archive pattern includes a file name, use the default file name as the
647+
// base.
648+
if (!logArchivePattern.has_filename())
649+
logArchivePattern = logArchivePattern / archiveFileName(get<string>(options["file_name"]));
629650

630-
// Get the log directory from the archive path.
631-
logDirectory = logArchivePattern.parent_path();
651+
// Make the logArchivePattern and logFileName absolute paths
652+
logDirectory = logDirectory.lexically_normal();
632653

633-
// If the file name does not specify a log directory, use the
634-
// archive directory
635-
logFileName = fs::path(file_name);
636-
if (!logFileName.has_parent_path())
654+
if (logArchivePattern.is_relative())
655+
logArchivePattern = logDirectory / logArchivePattern;
656+
logArchivePattern = logArchivePattern.lexically_normal();
657+
archiveLogDirectory = logArchivePattern.parent_path();
658+
fs::create_directories(archiveLogDirectory);
659+
660+
if (logFileName.is_relative())
637661
logFileName = logDirectory / logFileName;
638-
else if (logFileName.is_relative())
639-
logFileName = fs::current_path() / logFileName;
662+
logFileName = logFileName.lexically_normal();
663+
fs::create_directories(logFileName.parent_path());
640664

641665
// Create a text file sink
642666
auto sink = boost::make_shared<text_sink>(
643-
kw::file_name = logFileName, kw::target_file_name = logArchivePattern.filename(),
667+
kw::file_name = logFileName.string(), kw::target_file_name = logArchivePattern.string(),
644668
kw::auto_flush = true, kw::rotation_size = logRotationSize,
645669
kw::open_mode = ios_base::out | ios_base::app, kw::format = formatter);
646670

@@ -649,7 +673,8 @@ namespace mtconnect::configuration {
649673

650674
// Set up where the rotated files will be stored
651675
sink->locked_backend()->set_file_collector(logr::sinks::file::make_collector(
652-
kw::target = logDirectory, kw::max_size = maxLogFileSize, kw::max_files = max_index));
676+
kw::target = archiveLogDirectory.string(), kw::max_size = maxLogFileSize,
677+
kw::max_files = max_index));
653678

654679
if (rotationLogInterval > 0)
655680
{
@@ -1248,7 +1273,8 @@ namespace mtconnect::configuration {
12481273
}
12491274
catch (exception &e)
12501275
{
1251-
LOG(info) << "Cannot load plugin " << name << " from " << path << " Reason: " << e.what();
1276+
LOG(debug) << "Plugin " << name << " from " << path << " not found, Reason: " << e.what()
1277+
<< ", trying next path if available.";
12521278
}
12531279
}
12541280

src/mtconnect/configuration/agent_config.hpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ namespace mtconnect {
183183
{
184184
return m_logChannels[channelName].m_logArchivePattern;
185185
}
186+
187+
/// @brief gets the archive log directory
188+
/// @return log directory
189+
const auto &getArchiveLogDirectory(const std::string &channelName = "agent")
190+
{
191+
return m_logChannels[channelName].m_archiveLogDirectory;
192+
}
193+
186194
/// @brief Get the maximum size of all the log files
187195
/// @return the maximum size of all log files
188196
auto getMaxLogFileSize(const std::string &channelName = "agent")
@@ -260,6 +268,10 @@ namespace mtconnect {
260268
/// @param path the path to add
261269
void addPluginPath(const std::filesystem::path &path) { addPathBack(m_pluginPaths, path); }
262270

271+
///@brief set the config path for testing
272+
///@param path the path to set for the config file directory
273+
void setConfigPath(const std::filesystem::path &path) { m_configPath = path; }
274+
263275
protected:
264276
DevicePtr getDefaultDevice();
265277
void loadAdapters(const ptree &tree, const ConfigOptions &options);
@@ -295,7 +307,7 @@ namespace mtconnect {
295307
else
296308
{
297309
LOG(debug) << "Cannot find file '" << file << "' "
298-
<< " in path " << path;
310+
<< " in path " << path << ", continuing...";
299311
}
300312
}
301313

@@ -312,7 +324,7 @@ namespace mtconnect {
312324
if (!ec)
313325
paths.emplace_back(con);
314326
else
315-
LOG(debug) << "Cannot file path: " << path << ", " << ec.message();
327+
LOG(debug) << "Cannot find path: " << path << ", " << ec.message() << ", skipping...";
316328
}
317329

318330
void addPathFront(std::list<std::filesystem::path> &paths, std::filesystem::path path)
@@ -348,6 +360,7 @@ namespace mtconnect {
348360
{
349361
std::string m_channelName;
350362
std::filesystem::path m_logDirectory;
363+
std::filesystem::path m_archiveLogDirectory;
351364
std::filesystem::path m_logArchivePattern;
352365
std::filesystem::path m_logFileName;
353366

@@ -374,6 +387,7 @@ namespace mtconnect {
374387
std::string m_devicesFile;
375388
std::filesystem::path m_exePath;
376389
std::filesystem::path m_working;
390+
std::filesystem::path m_configPath;
377391

378392
std::list<std::filesystem::path> m_configPaths;
379393
std::list<std::filesystem::path> m_dataPaths;

src/mtconnect/device_model/configuration/coordinate_systems.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ namespace mtconnect {
3333
Requirement("Rotation", ValueType::VECTOR, 3, false),
3434
Requirement("TranslationDataSet", ValueType::DATA_SET, false),
3535
Requirement("RotationDataSet", ValueType::DATA_SET, false)});
36-
transformation->setOrder({"Translation", "TranslationDataSet", "Rotation", "RotationDataSet"});
36+
transformation->setOrder(
37+
{"Translation", "TranslationDataSet", "Rotation", "RotationDataSet"});
3738

3839
auto coordinateSystem = make_shared<Factory>(Requirements {
3940
Requirement("id", true), Requirement("name", false), Requirement("nativeName", false),

src/mtconnect/device_model/configuration/motion.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ namespace mtconnect {
3030
Requirement("Rotation", ValueType::VECTOR, 3, false),
3131
Requirement("TranslationDataSet", ValueType::DATA_SET, false),
3232
Requirement("RotationDataSet", ValueType::DATA_SET, false)});
33-
transformation->setOrder({"Translation", "TranslationDataSet", "Rotation", "RotationDataSet"});
33+
transformation->setOrder(
34+
{"Translation", "TranslationDataSet", "Rotation", "RotationDataSet"});
3435

3536
static auto motion = make_shared<Factory>(Requirements {
3637
Requirement("id", true), Requirement("parentIdRef", false),

src/mtconnect/device_model/configuration/solid_model.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ namespace mtconnect {
2929
if (!solidModel)
3030
{
3131
static auto transformation = make_shared<Factory>(
32-
Requirements {Requirement("Translation", ValueType::VECTOR, 3, false),
33-
Requirement("Rotation", ValueType::VECTOR, 3, false),
34-
Requirement("TranslationDataSet", ValueType::DATA_SET, false),
35-
Requirement("RotationDataSet", ValueType::DATA_SET, false)});
36-
transformation->setOrder({"Translation", "TranslationDataSet", "Rotation", "RotationDataSet"});
32+
Requirements {Requirement("Translation", ValueType::VECTOR, 3, false),
33+
Requirement("Rotation", ValueType::VECTOR, 3, false),
34+
Requirement("TranslationDataSet", ValueType::DATA_SET, false),
35+
Requirement("RotationDataSet", ValueType::DATA_SET, false)});
36+
transformation->setOrder(
37+
{"Translation", "TranslationDataSet", "Rotation", "RotationDataSet"});
3738

3839
solidModel = make_shared<Factory>(
3940
Requirements {{"id", true},

src/mtconnect/entity/xml_parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ namespace mtconnect::entity {
287287
}
288288
else
289289
{
290-
LOG(warning) << "Unexpected element: " << nodeQName(child);
290+
// LOG(warning) << "Unexpected element: " << nodeQName(child);
291291
errors.emplace_back(
292292
new EntityError("Invalid element '" + nodeQName(child) + "'", qname));
293293
}

src/mtconnect/parser/xml_parser.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,26 @@ namespace mtconnect::parser {
202202
{
203203
auto device =
204204
entity::XmlParser::parseXmlNode(Device::getRoot(), nodeset->nodeTab[i], errors);
205+
205206
if (device)
207+
{
206208
deviceList.emplace_back(dynamic_pointer_cast<Device>(device));
209+
}
210+
else
211+
{
212+
LOG(error) << "Failed to parse device, skipping";
213+
}
207214

208215
if (!errors.empty())
209216
{
210217
for (auto &e : errors)
211-
LOG(warning) << "Error parsing device: " << e->what();
218+
{
219+
if (device)
220+
LOG(warning) << "When loading device " << device->get<string>("name")
221+
<< ", A problem was skipped: " << e->what();
222+
else
223+
LOG(error) << "Failed to load device: " << e->what();
224+
}
212225
}
213226
}
214227
}

src/mtconnect/pipeline/shdr_token_mapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ namespace mtconnect {
166166
}
167167
catch (entity::PropertyError &e)
168168
{
169-
LOG(warning) << "Cannot convert value for data item id '" << dataItem->getId()
170-
<< "': " << *token << " - " << e.what();
169+
LOG(debug) << "Cannot convert value for data item id '" << dataItem->getId()
170+
<< "': " << *token << " - " << e.what();
171171
if (schemaVersion >= SCHEMA_VERSION(2, 5) && validation)
172172
{
173173
props.insert_or_assign("quality", "INVALID"s);

src/mtconnect/sink/rest_sink/parameter.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ namespace mtconnect::sink::rest_sink {
6464
Parameter(const std::string &n, ParameterType t = STRING, UrlPart p = PATH)
6565
: m_name(n), m_type(t), m_part(p)
6666
{}
67+
Parameter(const std::string_view &n, ParameterType t = STRING, UrlPart p = PATH)
68+
: m_name(n), m_type(t), m_part(p)
69+
{}
6770
Parameter(const Parameter &o) = default;
6871

6972
/// @brief to support std::set interface

0 commit comments

Comments
 (0)