From 3ccabd9ff2c99e7f4db386726795d194532bc34b Mon Sep 17 00:00:00 2001 From: Pavel Kulik Date: Tue, 19 Nov 2024 16:59:03 -0800 Subject: [PATCH] Refactor plugin logging --- Plugins/ArduinoOutput/ArduinoOutput.h | 2 - Plugins/BandpassFilter/BandpassFilter.h | 2 - Plugins/ChannelMap/ChannelMap.h | 2 - Plugins/CommonAvgRef/CommonAvgRef.h | 2 - Plugins/Headers/Logging.h | 59 ------------------------ Plugins/Headers/ProcessorHeaders.h | 3 +- Plugins/PhaseDetector/PhaseDetector.h | 2 - Plugins/RecordControl/RecordControl.h | 2 - Plugins/SpikeDetector/SpikeDetector.h | 2 - Source/Utils/Utils.h | 61 +++++++++++++++++++------ 10 files changed, 47 insertions(+), 90 deletions(-) delete mode 100644 Plugins/Headers/Logging.h diff --git a/Plugins/ArduinoOutput/ArduinoOutput.h b/Plugins/ArduinoOutput/ArduinoOutput.h index 4a4623a50..deda84f9b 100644 --- a/Plugins/ArduinoOutput/ArduinoOutput.h +++ b/Plugins/ArduinoOutput/ArduinoOutput.h @@ -24,8 +24,6 @@ #ifndef __ARDUINOOUTPUT_H_F7BDA585__ #define __ARDUINOOUTPUT_H_F7BDA585__ -#define PROCESSOR_NAME "Arduino Output" - #include #include diff --git a/Plugins/BandpassFilter/BandpassFilter.h b/Plugins/BandpassFilter/BandpassFilter.h index d15e2b7ea..8fcc8bc07 100644 --- a/Plugins/BandpassFilter/BandpassFilter.h +++ b/Plugins/BandpassFilter/BandpassFilter.h @@ -24,8 +24,6 @@ #ifndef __BANDPASSFILTER_H_CED428E__ #define __BANDPASSFILTER_H_CED428E__ -#define PROCESSOR_NAME "Bandpass Filter" - #include #include diff --git a/Plugins/ChannelMap/ChannelMap.h b/Plugins/ChannelMap/ChannelMap.h index d96746641..1e4892618 100644 --- a/Plugins/ChannelMap/ChannelMap.h +++ b/Plugins/ChannelMap/ChannelMap.h @@ -24,8 +24,6 @@ #ifndef __CHANNELMAP_H_330E50E0__ #define __CHANNELMAP_H_330E50E0__ -#define PROCESSOR_NAME "Channel Map" - #include /** Holds channel map settings for one data stream*/ diff --git a/Plugins/CommonAvgRef/CommonAvgRef.h b/Plugins/CommonAvgRef/CommonAvgRef.h index eb825f77f..e10605659 100644 --- a/Plugins/CommonAvgRef/CommonAvgRef.h +++ b/Plugins/CommonAvgRef/CommonAvgRef.h @@ -24,8 +24,6 @@ #ifndef CAR_H_INCLUDED #define CAR_H_INCLUDED -#define PROCESSOR_NAME "Common Avg Ref" - #include /** Holds settings for one stream's CAR*/ diff --git a/Plugins/Headers/Logging.h b/Plugins/Headers/Logging.h deleted file mode 100644 index 35a45366c..000000000 --- a/Plugins/Headers/Logging.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - ------------------------------------------------------------------ - - This file is part of the Open Ephys GUI - Copyright (C) 2024 Open Ephys - - ------------------------------------------------------------------ - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -*/ - -#ifndef PROCESSOR_NAME - #define PROCESSOR_NAME "Unknown" -#endif - -#undef LOGA -#define LOGA(...) \ - getOELogger().LOGFile("[" PROCESSOR_NAME "][action] ", __VA_ARGS__) - -#undef LOGB -#define LOGB(...) \ - getOELogger().LOGFile("[" PROCESSOR_NAME "][buffer] ", __VA_ARGS__) - -#undef LOGC -#define LOGC(...) \ - getOELogger().LOGConsole("[" PROCESSOR_NAME "] ", __VA_ARGS__) - -#undef LOGD -#ifdef DEBUG -#define LOGD(...) \ - getOELogger().LOGConsole("[" PROCESSOR_NAME "][debug] ", __VA_ARGS__) -#else -#define LOGD(...) \ - getOELogger().LOGFile("[" PROCESSOR_NAME "][debug] ", __VA_ARGS__) -#endif - -#undef LOGDD -#define LOGDD(...) \ - getOELogger().LOGFile("[" PROCESSOR_NAME "][ddebug] ", __VA_ARGS__) - -#undef LOGE -#define LOGE(...) \ - getOELogger().LOGError("[" PROCESSOR_NAME "] ***ERROR*** ", __VA_ARGS__) - -// Not applicable for plugins -#undef LOGF -#undef LOGG \ No newline at end of file diff --git a/Plugins/Headers/ProcessorHeaders.h b/Plugins/Headers/ProcessorHeaders.h index 9e7a921d7..1d6bd3a3c 100644 --- a/Plugins/Headers/ProcessorHeaders.h +++ b/Plugins/Headers/ProcessorHeaders.h @@ -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" \ No newline at end of file +#include "DspLib.h" \ No newline at end of file diff --git a/Plugins/PhaseDetector/PhaseDetector.h b/Plugins/PhaseDetector/PhaseDetector.h index 36d855cdb..03631086e 100644 --- a/Plugins/PhaseDetector/PhaseDetector.h +++ b/Plugins/PhaseDetector/PhaseDetector.h @@ -24,8 +24,6 @@ #ifndef __PHASEDETECTOR_H_F411F29D__ #define __PHASEDETECTOR_H_F411F29D__ -#define PROCESSOR_NAME "Phase Detector" - #include enum PhaseType diff --git a/Plugins/RecordControl/RecordControl.h b/Plugins/RecordControl/RecordControl.h index 0292e23cf..c2878ca47 100644 --- a/Plugins/RecordControl/RecordControl.h +++ b/Plugins/RecordControl/RecordControl.h @@ -26,8 +26,6 @@ #include "RecordControlEditor.h" -#define PROCESSOR_NAME "Record Control" - #include /** diff --git a/Plugins/SpikeDetector/SpikeDetector.h b/Plugins/SpikeDetector/SpikeDetector.h index 2ecfb49bf..f96345807 100644 --- a/Plugins/SpikeDetector/SpikeDetector.h +++ b/Plugins/SpikeDetector/SpikeDetector.h @@ -24,8 +24,6 @@ #ifndef __SPIKEDETECTOR_H_3F920F95__ #define __SPIKEDETECTOR_H_3F920F95__ -#define PROCESSOR_NAME "Spike Detector" - #include class SpikeDetectorSettings diff --git a/Source/Utils/Utils.h b/Source/Utils/Utils.h index 5a4af860a..84f69d7d0 100644 --- a/Source/Utils/Utils.h +++ b/Source/Utils/Utils.h @@ -31,17 +31,13 @@ #include #include #include +#include #include "../Processors/PluginManager/OpenEphysPlugin.h" /* Thread-safe logger */ class OELogger { - std::ofstream logFile; - -protected: - OELogger() {} - public: static OELogger& instance() { @@ -49,9 +45,6 @@ class OELogger return lg; } - OELogger (OELogger const&) = delete; - OELogger& operator= (OELogger const&) = delete; - template void LOGConsole (Args&&... args) { @@ -90,8 +83,46 @@ class OELogger logFile << "[open-ephys] Session start time: " << ctime (&now); } + std::string getModuleName() const { + Dl_info info; + if (dladdr(reinterpret_cast(__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 */ @@ -99,15 +130,15 @@ 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 @@ -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