2016-09-05 12:00:36 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:16:20 -08:00
|
|
|
/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
|
2016-09-05 12:00:36 -07:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-10-31 20:22:58 -07:00
|
|
|
#include "Instance.h"
|
2016-09-05 12:00:36 -07:00
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/Path.h>
|
|
|
|
|
#include <wpi/SmallString.h>
|
|
|
|
|
#include <wpi/StringRef.h>
|
|
|
|
|
#include <wpi/raw_ostream.h>
|
2016-09-05 12:00:36 -07:00
|
|
|
|
|
|
|
|
using namespace cs;
|
|
|
|
|
|
|
|
|
|
static void def_log_func(unsigned int level, const char* file,
|
|
|
|
|
unsigned int line, const char* msg) {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallString<128> buf;
|
|
|
|
|
wpi::raw_svector_ostream oss(buf);
|
2016-09-05 12:00:36 -07:00
|
|
|
if (level == 20) {
|
2017-09-07 00:16:26 -07:00
|
|
|
oss << "CS: " << msg << '\n';
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::errs() << oss.str();
|
2016-09-05 12:00:36 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringRef levelmsg;
|
2016-09-05 12:00:36 -07:00
|
|
|
if (level >= 50)
|
2017-09-07 00:16:26 -07:00
|
|
|
levelmsg = "CRITICAL: ";
|
2016-09-05 12:00:36 -07:00
|
|
|
else if (level >= 40)
|
2017-09-07 00:16:26 -07:00
|
|
|
levelmsg = "ERROR: ";
|
2016-09-05 12:00:36 -07:00
|
|
|
else if (level >= 30)
|
2017-09-07 00:16:26 -07:00
|
|
|
levelmsg = "WARNING: ";
|
2016-09-05 12:00:36 -07:00
|
|
|
else
|
|
|
|
|
return;
|
2018-04-29 23:33:19 -07:00
|
|
|
oss << "CS: " << levelmsg << msg << " (" << wpi::sys::path::filename(file)
|
2017-09-07 00:16:26 -07:00
|
|
|
<< ':' << line << ")\n";
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::errs() << oss.str();
|
2016-09-05 12:00:36 -07:00
|
|
|
}
|
|
|
|
|
|
2018-10-31 20:22:58 -07:00
|
|
|
Instance::Instance() : telemetry(notifier), network_listener(logger, notifier) {
|
|
|
|
|
SetDefaultLogger();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Instance::~Instance() {}
|
|
|
|
|
|
|
|
|
|
Instance& Instance::GetInstance() {
|
|
|
|
|
static Instance inst;
|
|
|
|
|
return inst;
|
|
|
|
|
}
|
2016-09-05 12:00:36 -07:00
|
|
|
|
2018-10-31 20:22:58 -07:00
|
|
|
void Instance::SetDefaultLogger() { logger.SetLogger(def_log_func); }
|
2017-02-17 01:12:16 -05:00
|
|
|
|
2018-10-31 20:22:58 -07:00
|
|
|
std::pair<CS_Source, std::shared_ptr<SourceData>> Instance::FindSource(
|
|
|
|
|
const SourceImpl& source) {
|
|
|
|
|
return sources.FindIf(
|
|
|
|
|
[&](const SourceData& data) { return data.source.get() == &source; });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::pair<CS_Sink, std::shared_ptr<SinkData>> Instance::FindSink(
|
|
|
|
|
const SinkImpl& sink) {
|
|
|
|
|
return sinks.FindIf(
|
|
|
|
|
[&](const SinkData& data) { return data.sink.get() == &sink; });
|
|
|
|
|
}
|