Skip to content

Commit

Permalink
Refactor plugin logging
Browse files Browse the repository at this point in the history
  • Loading branch information
medengineer committed Nov 20, 2024
1 parent 3d0fa51 commit 3ccabd9
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 90 deletions.
2 changes: 0 additions & 2 deletions Plugins/ArduinoOutput/ArduinoOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#ifndef __ARDUINOOUTPUT_H_F7BDA585__
#define __ARDUINOOUTPUT_H_F7BDA585__

#define PROCESSOR_NAME "Arduino Output"

#include <ProcessorHeaders.h>

#include <SerialLib.h>
Expand Down
2 changes: 0 additions & 2 deletions Plugins/BandpassFilter/BandpassFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#ifndef __BANDPASSFILTER_H_CED428E__
#define __BANDPASSFILTER_H_CED428E__

#define PROCESSOR_NAME "Bandpass Filter"

#include <ProcessorHeaders.h>
#include <DspLib.h>

Expand Down
2 changes: 0 additions & 2 deletions Plugins/ChannelMap/ChannelMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#ifndef __CHANNELMAP_H_330E50E0__
#define __CHANNELMAP_H_330E50E0__

#define PROCESSOR_NAME "Channel Map"

#include <ProcessorHeaders.h>

/** Holds channel map settings for one data stream*/
Expand Down
2 changes: 0 additions & 2 deletions Plugins/CommonAvgRef/CommonAvgRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#ifndef CAR_H_INCLUDED
#define CAR_H_INCLUDED

#define PROCESSOR_NAME "Common Avg Ref"

#include <ProcessorHeaders.h>

/** Holds settings for one stream's CAR*/
Expand Down
59 changes: 0 additions & 59 deletions Plugins/Headers/Logging.h

This file was deleted.

3 changes: 1 addition & 2 deletions Plugins/Headers/ProcessorHeaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ Should be included in the source files which declare a processor class.
#include "../../Source/Processors/GenericProcessor/GenericProcessor.h"
#include "../../Source/TestableExport.h"
#include "../../Source/Utils/BroadcastParser.h"
#include "DspLib.h"
#include "Logging.h"
#include "DspLib.h"
2 changes: 0 additions & 2 deletions Plugins/PhaseDetector/PhaseDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#ifndef __PHASEDETECTOR_H_F411F29D__
#define __PHASEDETECTOR_H_F411F29D__

#define PROCESSOR_NAME "Phase Detector"

#include <ProcessorHeaders.h>

enum PhaseType
Expand Down
2 changes: 0 additions & 2 deletions Plugins/RecordControl/RecordControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

#include "RecordControlEditor.h"

#define PROCESSOR_NAME "Record Control"

#include <ProcessorHeaders.h>

/**
Expand Down
2 changes: 0 additions & 2 deletions Plugins/SpikeDetector/SpikeDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#ifndef __SPIKEDETECTOR_H_3F920F95__
#define __SPIKEDETECTOR_H_3F920F95__

#define PROCESSOR_NAME "Spike Detector"

#include <ProcessorHeaders.h>

class SpikeDetectorSettings
Expand Down
61 changes: 46 additions & 15 deletions Source/Utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,20 @@
#include <map>
#include <mutex>
#include <string>
#include <dlfcn.h>

#include "../Processors/PluginManager/OpenEphysPlugin.h"

/* Thread-safe logger */
class OELogger
{
std::ofstream logFile;

protected:
OELogger() {}

public:
static OELogger& instance()
{
static OELogger lg;
return lg;
}

OELogger (OELogger const&) = delete;
OELogger& operator= (OELogger const&) = delete;

template <typename... Args>
void LOGConsole (Args&&... args)
{
Expand Down Expand Up @@ -90,24 +83,62 @@ class OELogger
logFile << "[open-ephys] Session start time: " << ctime (&now);
}

std::string getModuleName() const {
Dl_info info;
if (dladdr(reinterpret_cast<void*>(__builtin_return_address(0)), &info))
{
if (info.dli_fname) {
return formatModuleName(std::string(info.dli_fname));
}
}
return "[unknown]";
}

std::string formatModuleName(const std::string& path) const {
size_t lastSlash = path.find_last_of("/\\");
std::string basename = path.substr(lastSlash + 1);
std::string formatted;
for (size_t i = 0; i < basename.length(); ++i) {
char ch = basename[i];
if (std::isupper(ch)) {
if (i > 0) {
formatted += '-'; // Add hyphen before uppercase letters (except the first one)
}
formatted += std::tolower(ch); // Convert to lowercase
} else {
formatted += ch;
}
}
return "[" + formatted + "]";
}

private:
std::mutex mt;
std::ofstream logFile;

OELogger() = default;
~OELogger() = default;

// Disable copy and move
OELogger(const OELogger&) = delete;
OELogger& operator=(const OELogger&) = delete;

};

/* Expose the Logger instance to plugins */
extern "C" PLUGIN_API OELogger& getOELogger();

/* Log Action -- taken by user */
#define LOGA(...) \
getOELogger().LOGFile ("[open-ephys][action] ", __VA_ARGS__);
getOELogger().LOGFile (getOELogger().getModuleName(), "[action] ", __VA_ARGS__);

/* Log Buffer -- related logs i.e. inside process() method */
#define LOGB(...) \
getOELogger().LOGFile ("[open-ephys][buffer] ", __VA_ARGS__);
getOELogger().LOGFile (getOELogger().getModuleName(),"[buffer] ", __VA_ARGS__);

/* Log Console -- gets printed to the GUI Debug Console */
#define LOGC(...) \
getOELogger().LOGConsole ("[open-ephys] ", __VA_ARGS__);
getOELogger().LOGConsole (getOELogger().getModuleName(), " ", __VA_ARGS__);

/* Log Debug -- gets printed to the console in debug mode, to file otherwise */
#ifdef DEBUG
Expand All @@ -117,23 +148,23 @@ extern "C" PLUGIN_API OELogger& getOELogger();
#else
/* Log Debug -- gets printed to the log file */
#define LOGD(...) \
getOELogger().LOGFile ("[open-ephys][debug] ", __VA_ARGS__);
getOELogger().LOGFile (getOELogger().getModuleName(), "[debug] ", __VA_ARGS__);
#endif

/* Log Deep Debug -- gets printed to log file (e.g. enable after a crash to get more details) */
#define LOGDD(...) \
getOELogger().LOGFile ("[open-ephys][ddebug] ", __VA_ARGS__);
getOELogger().LOGFile (getOELogger().getModuleName(), "[ddebug] ", __VA_ARGS__);

/* Log Error -- gets printed to console with flare */
#define LOGE(...) \
getOELogger().LOGError ("[open-ephys] ***ERROR*** ", __VA_ARGS__);
getOELogger().LOGError (getOELogger().getModuleName(), "***ERROR*** ", __VA_ARGS__);

/* Log File -- gets printed directly to main output file */
#define LOGF(...) LOGD (...)

/* Log Graph -- gets logs related to processor graph generation/modification events */
#define LOGG(...) \
getOELogger().LOGFile ("[open-ephys][graph] ", __VA_ARGS__);
getOELogger().LOGFile (getOELogger().getModuleName(), "[graph] ", __VA_ARGS__);

/* Function Timer */
template <typename Time = std::chrono::microseconds, typename Clock = std::chrono::high_resolution_clock>
Expand Down

0 comments on commit 3ccabd9

Please sign in to comment.