Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update logging #397

Merged
36 changes: 15 additions & 21 deletions src/FPGA_common/FPGA_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ using namespace std;
#ifndef NDEBUG
#define ASSERT_WARNING(cond, message) \
if (!cond) \
printf("W: %s (%s)\n", message, #cond)
lime::warning("W: %s (%s)", message, #cond)
#else
#define ASSERT_WARNING(cond, message)
#endif

#if 0
#define verbose_printf(...) printf(__VA_ARGS__);
#else
#define verbose_printf(...)
#endif

namespace lime {

/** @brief A class for writing a batch of registers into the FPGA. */
Expand Down Expand Up @@ -119,7 +113,7 @@ FPGA::FPGA(std::shared_ptr<ISPI> fpgaSPI, std::shared_ptr<ISPI> lms7002mSPI)

void FPGA::EnableValuesCache(bool enabled)
{
verbose_printf("Enable FPGA registers cache: %s\n", enabled ? "true" : "false");
lime::debug("Enable FPGA registers cache: %s", enabled ? "true" : "false");
useCache = enabled;
if (!useCache)
regsCache.clear();
Expand Down Expand Up @@ -309,7 +303,7 @@ int FPGA::ReadRegisters(const uint32_t* addrs, uint32_t* data, unsigned cnt)

int FPGA::StartStreaming()
{
verbose_printf("%s\n", __func__);
lime::debug("%s", __func__);
int interface_ctrl_000A = ReadRegister(0x000A);
if (interface_ctrl_000A < 0)
return -1;
Expand All @@ -319,7 +313,7 @@ int FPGA::StartStreaming()

int FPGA::StopStreaming()
{
verbose_printf("%s\n", __func__);
lime::debug("%s", __func__);
int interface_ctrl_000A = ReadRegister(0x000A);
if (interface_ctrl_000A < 0)
return -1;
Expand All @@ -329,7 +323,7 @@ int FPGA::StopStreaming()

int FPGA::ResetTimestamp()
{
verbose_printf("%s\n", __func__);
lime::debug("%s", __func__);
#ifndef NDEBUG
int interface_ctrl_000A = ReadRegister(0x000A);
if (interface_ctrl_000A < 0)
Expand All @@ -356,7 +350,7 @@ int FPGA::WaitTillDone(uint16_t pollAddr, uint16_t doneMask, uint16_t errorMask,
uint16_t error = 0;
if (!title.empty())
{
verbose_printf("%s\n", title.c_str());
lime::debug("%s", title.c_str());
}
do
{
Expand All @@ -382,7 +376,7 @@ int FPGA::WaitTillDone(uint16_t pollAddr, uint16_t doneMask, uint16_t errorMask,
} while (!done);
if (!title.empty())
{
verbose_printf("%s done\n", title.c_str());
lime::debug(title + " done"s);
}
return 0;
}
Expand Down Expand Up @@ -445,7 +439,7 @@ int FPGA::SetPllClock(uint8_t clockIndex, int nSteps, bool waitLock, bool doPhas
*/
int FPGA::SetPllFrequency(const uint8_t pllIndex, const double inputFreq, FPGA_PLL_clock* clocks, const uint8_t clockCount)
{
verbose_printf("FPGA SetPllFrequency: PLL[%i] input:%.3f MHz clockCount:%i\n", pllIndex, inputFreq / 1e6, clockCount);
lime::debug("FPGA SetPllFrequency: PLL[%i] input:%.3f MHz clockCount:%i", pllIndex, inputFreq / 1e6, clockCount);
WriteRegistersBatch batch(this);
const auto timeout = chrono::seconds(3);
if (not fpgaPort)
Expand All @@ -463,7 +457,7 @@ int FPGA::SetPllFrequency(const uint8_t pllIndex, const double inputFreq, FPGA_P
return ReportError(ERANGE, "FPGA SetPllFrequency: PLL[%i] input frequency must be >=%g MHz", pllIndex, PLLlowerLimit / 1e6);
for (int i = 0; i < clockCount; ++i)
{
verbose_printf("CLK[%i] Fout:%.3f MHz bypass:%i phase:%g findPhase: %i\n",
lime::debug("CLK[%i] Fout:%.3f MHz bypass:%i phase:%g findPhase: %i",
clocks[i].index,
clocks[i].outFrequency / 1e6,
clocks[i].bypass,
Expand Down Expand Up @@ -528,7 +522,7 @@ int FPGA::SetPllFrequency(const uint8_t pllIndex, const double inputFreq, FPGA_P
}
}
if (desiredVCO.size() == 0)
return ReportError(EINVAL, "FPGA SetPllFrequency: no suitable VCO frequencies found for requested clocks\n");
return ReportError(EINVAL, "FPGA SetPllFrequency: no suitable VCO frequencies found for requested clocks");

// Find VCO that satisfies most outputs with integer dividers
uint64_t bestFreqVCO = std::max_element(
Expand Down Expand Up @@ -938,7 +932,7 @@ int FPGA::UploadWFM(const void* const* samples, uint8_t chCount, size_t sample_c
*/
int FPGA::SetInterfaceFreq(double txRate_Hz, double rxRate_Hz, double txPhase, double rxPhase, int channel)
{
verbose_printf("FPGA::SetInterfaceFreq tx:%.3f MHz rx:%.3f MHz txPhase:%g rxPhase:%g ch:%i\n",
lime::debug("FPGA::SetInterfaceFreq tx:%.3f MHz rx:%.3f MHz txPhase:%g rxPhase:%g ch:%i",
txRate_Hz / 1e6,
rxRate_Hz / 1e6,
txPhase,
Expand Down Expand Up @@ -985,7 +979,7 @@ int FPGA::SetInterfaceFreq(double txRate_Hz, double rxRate_Hz, double txPhase, d
*/
int FPGA::SetInterfaceFreq(double txRate_Hz, double rxRate_Hz, int chipIndex)
{
verbose_printf("FPGA::SetInterfaceFreq tx:%.3f MHz rx:%.3f MHz chipIndex:%i\n", txRate_Hz / 1e6, rxRate_Hz / 1e6, chipIndex);
lime::debug("FPGA::SetInterfaceFreq tx:%.3f MHz rx:%.3f MHz chipIndex:%i", txRate_Hz / 1e6, rxRate_Hz / 1e6, chipIndex);
//PrintStackTrace();
const int pll_ind = (chipIndex == 1) ? 2 : 0;
int status = 0;
Expand Down Expand Up @@ -1083,7 +1077,7 @@ int FPGA::SetInterfaceFreq(double txRate_Hz, double rxRate_Hz, int chipIndex)
}
else
{
verbose_printf("Retry%i: SetPllFrequency\n", i);
lime::debug("Retry%i: SetPllFrequency", i);
std::this_thread::sleep_for(busyPollPeriod);
}
}
Expand Down Expand Up @@ -1133,7 +1127,7 @@ int FPGA::SetInterfaceFreq(double txRate_Hz, double rxRate_Hz, int chipIndex)
}
else
{
verbose_printf("Retry%i: SetPllFrequency\n", i);
lime::debug("Retry%i: SetPllFrequency", i);
std::this_thread::sleep_for(busyPollPeriod);
}
}
Expand Down Expand Up @@ -1188,7 +1182,7 @@ int FPGA::ReadRawStreamData(char* buffer, unsigned length, int epIndex, int time

double FPGA::DetectRefClk(double fx3Clk)
{
verbose_printf("FPGA::DetectRefClk fx3Clk:%g\n", fx3Clk);
lime::debug("FPGA::DetectRefClk fx3Clk:%g", fx3Clk);
const double fx3Cnt = 16777210; //fixed fx3 counter in FPGA
const double clkTbl[] = { 10e6, 30.72e6, 38.4e6, 40e6, 52e6 };
const uint32_t addr[] = { 0x61, 0x63 };
Expand Down
4 changes: 3 additions & 1 deletion src/GUI/ILMS7002MTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include "lms7002_gui_utilities.h"

#include "limesuite/LMS7002M.h"
#include "Logger.h"

using namespace lime;
using namespace std::literals::string_literals;

ILMS7002MTab::ILMS7002MTab(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style)
: wxPanel(parent, id, pos, size, style)
Expand Down Expand Up @@ -34,7 +36,7 @@ void ILMS7002MTab::ParameterChangeHandler(wxCommandEvent& event)
parameter = wndId2Enum.at(reinterpret_cast<wxWindow*>(event.GetEventObject()));
} catch (std::exception& e)
{
std::cout << "Control element(ID = " << event.GetId() << ") don't have assigned LMS parameter." << std::endl;
lime::error("Control element(ID = "s + std::to_string(event.GetId()) + ") don't have assigned LMS parameter."s);
return;
}
WriteParam(parameter, event.GetInt());
Expand Down
2 changes: 1 addition & 1 deletion src/LMS_Programing/LMS_Programing_wxgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void LMS_Programing_wxgui::DoProgramming()
evt.SetEventObject(this);
evt.SetId(ID_PROGRAMING_FINISHED_EVENT);
evt.SetEventType(wxEVT_COMMAND_THREAD);
evt.SetString(status == 0 ? _("Programming Completed!") : _("Programming failed!\n"));
evt.SetString(status == 0 ? _("Programming Completed!") : _("Programming failed!"));

wxPostEvent(this, evt);
mProgrammingInProgress.store(false);
Expand Down
120 changes: 84 additions & 36 deletions src/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ enum class LogLevel : uint8_t {
DEBUG, //!< A debugging message, only shown in Debug configuration.
};

// C-string versions
//! Log a critical error message with formatting
static inline void critical(const char* format, ...);

Expand All @@ -42,6 +43,25 @@ static inline void debug(const char* format, ...);
//! Log a message with formatting and specified logging level
static inline void log(const LogLevel level, const char* format, ...);

// C++ std::string versions
//! Log a critical error message
static inline void critical(const std::string& text);

//! Log an error message
static inline int error(const std::string& text);

//! Log a warning message
static inline void warning(const std::string& text);

//! Log an information message
static inline void info(const std::string& text);

//! Log a debug message
static inline void debug(const std::string& text);

//! Log a message with specified logging level
static inline void log(const LogLevel level, const std::string& text);

/*!
* Send a message to the registered logger.
* \param level a possible logging level
Expand All @@ -64,6 +84,44 @@ LIME_API void registerLogHandler(const LogHandler handler);
//! Convert log level to a string name for printing
LIME_API const char* logLevelToName(const LogLevel level);

/*!
* Get the error code to string + any optional message reported.
*/
LIME_API const char* GetLastErrorMessage(void);

/*!
* Report a typical errno style error.
* The resulting error message comes from strerror().
* \param errnum a recognized error code
* \return a non-zero status code to return
*/
LIME_API int ReportError(const int errnum);

/*!
* Report an error as an integer code and a formatted message string.
* \param errnum a recognized error code
* \param format a format string followed by args
* \return a non-zero status code to return
*/
inline int ReportError(const int errnum, const char* format, ...);

/*!
* Report an error as a formatted message string.
* The reported errnum is 0 - no relevant error code.
* \param format a format string followed by args
* \return a non-zero status code to return
*/
inline int ReportError(const char* format, ...);

/*!
* Report an error as an integer code and message format arguments
* \param errnum a recognized error code
* \param format a printf-style format string
* \param argList the format string args as a va_list
* \return a non-zero status code to return
*/
LIME_API int ReportError(const int errnum, const char* format, va_list argList);

} // namespace lime

static inline void lime::log(const LogLevel level, const char* format, ...)
Expand Down Expand Up @@ -115,47 +173,37 @@ static inline void lime::debug(const char* format, ...)
va_end(args);
}

namespace lime {

/*!
* Get the error code to string + any optional message reported.
*/
LIME_API const char* GetLastErrorMessage(void);
static inline void lime::critical(const std::string& text)
{
lime::log(lime::LogLevel::CRITICAL, text);
}

/*!
* Report a typical errno style error.
* The resulting error message comes from strerror().
* \param errnum a recognized error code
* \return a non-zero status code to return
*/
LIME_API int ReportError(const int errnum);
static inline int lime::error(const std::string& text)
{
lime::log(lime::LogLevel::ERROR, text);
return -1;
}

/*!
* Report an error as an integer code and a formatted message string.
* \param errnum a recognized error code
* \param format a format string followed by args
* \return a non-zero status code to return
*/
inline int ReportError(const int errnum, const char* format, ...);
static inline void lime::warning(const std::string& text)
{
lime::log(lime::LogLevel::WARNING, text);
}

/*!
* Report an error as a formatted message string.
* The reported errnum is 0 - no relevant error code.
* \param format a format string followed by args
* \return a non-zero status code to return
*/
inline int ReportError(const char* format, ...);
static inline void lime::info(const std::string& text)
{
lime::log(lime::LogLevel::INFO, text);
}

/*!
* Report an error as an integer code and message format arguments
* \param errnum a recognized error code
* \param format a printf-style format string
* \param argList the format string args as a va_list
* \return a non-zero status code to return
*/
LIME_API int ReportError(const int errnum, const char* format, va_list argList);
static inline void lime::debug(const std::string& text)
{
lime::log(lime::LogLevel::DEBUG, text);
}

} // namespace lime
//! Log a message with formatting and specified logging level
static inline void lime::log(const LogLevel level, const std::string& text)
{
lime::log(level, "%s", text.c_str());
}

inline int lime::ReportError(const int errnum, const char* format, ...)
{
Expand Down
12 changes: 4 additions & 8 deletions src/Si5351C/Si5351C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ Si5351C::Status Si5351C::UploadConfiguration()
return Status::SUCCESS;
} catch (std::runtime_error& e)
{
printf("Si5351C configuration failed %s\n", e.what());
lime::error("Si5351C configuration failed %s", e.what());
return Status::FAILED;
}
}
Expand Down Expand Up @@ -746,9 +746,7 @@ void Si5351C::FindVCO(Si5351_Channel* clocks, Si5351_PLL* plls, const unsigned l
bestVCOA = it->first;
}
}
//scores calculated
//cout << "PLLA stage: " << endl;
//cout << "best score: " << bestScore << " best VCO: " << bestVCOA << endl;

plls[0].VCO_Hz = bestVCOA;
plls[0].feedbackDivider = (double)bestVCOA / plls[0].inputFreqHz;

Expand Down Expand Up @@ -815,9 +813,7 @@ void Si5351C::FindVCO(Si5351_Channel* clocks, Si5351_PLL* plls, const unsigned l
bestVCOB = it->first;
}
}
//scores calculated
// cout << "PLLB stage: " << endl;
// cout << "best score: " << bestScore << " best VCO: " << bestVCOB << endl;

if (bestVCOB == 0) //just in case if pllb is not used make it the same frequency as plla
bestVCOB = bestVCOA;
plls[1].VCO_Hz = bestVCOB;
Expand Down Expand Up @@ -1091,7 +1087,7 @@ Si5351C::Status Si5351C::ClearStatus()
return Status::SUCCESS;
} catch (std::runtime_error& e)
{
printf("Si5351C configuration failed %s\n", e.what());
lime::error("Si5351C configuration failed %s", e.what());
return Status::FAILED;
}
}
Loading
Loading