feat(probe): added probe namespace
Probe handles LogManager (for tracking multiple log files) as well as a few utility functions such as wait, pause, glVisView, and rayView (future)
This commit is contained in:
@@ -1,72 +0,0 @@
|
||||
#ifndef LOGGER_H
|
||||
#define LOGGER_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <queue>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <unordered_map>
|
||||
|
||||
enum class DiagnosticLevel {
|
||||
DEBUG,
|
||||
INFO,
|
||||
WARNING,
|
||||
ERROR,
|
||||
CRITICAL,
|
||||
NONE
|
||||
};
|
||||
|
||||
struct Record {
|
||||
std::string message;
|
||||
std::string context;
|
||||
std::chrono::time_point<std::chrono::system_clock> timestamp;
|
||||
DiagnosticLevel level;
|
||||
};
|
||||
|
||||
Record makeRecord(const std::string& message, const std::string& context, DiagnosticLevel level);
|
||||
|
||||
std::string timestepToString(const std::chrono::time_point<std::chrono::system_clock>& timestamp);
|
||||
|
||||
class Logger {
|
||||
private:
|
||||
static std::unordered_map<std::string, Logger*> instances;
|
||||
static std::mutex mapMutex;
|
||||
|
||||
std::queue<std::string> logQueue;
|
||||
std::mutex queueMutex;
|
||||
|
||||
std::condition_variable queueCondition;
|
||||
std::thread logThread;
|
||||
std::atomic<bool> running;
|
||||
std::string filename;
|
||||
std::ofstream logFile;
|
||||
|
||||
Logger(const std::string& filename);
|
||||
Logger(const Logger&) = delete;
|
||||
Logger& operator=(const Logger&) = delete;
|
||||
|
||||
void processesLogs();
|
||||
|
||||
std::string levelAsString(DiagnosticLevel level);
|
||||
|
||||
public:
|
||||
static Logger& getInstance(const std::string& filename);
|
||||
|
||||
void log(const Record& record);
|
||||
|
||||
void log(const std::string& message, const std::string& context, DiagnosticLevel level);
|
||||
|
||||
void log(const std::string& message, DiagnosticLevel level);
|
||||
|
||||
void log(const std::string& message);
|
||||
|
||||
~Logger();
|
||||
};
|
||||
|
||||
#endif
|
||||
99
src/probe/public/probe.h
Normal file
99
src/probe/public/probe.h
Normal file
@@ -0,0 +1,99 @@
|
||||
//=== Probe.h ===
|
||||
#ifndef PROBE_H
|
||||
#define PROBE_H
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#include "mfem.hpp"
|
||||
#include "quill/Logger.h"
|
||||
|
||||
/**
|
||||
* @brief The Probe namespace contains utility functions for debugging and logging.
|
||||
*/
|
||||
namespace Probe {
|
||||
/**
|
||||
* @brief Pause the execution and wait for user input.
|
||||
*/
|
||||
void pause();
|
||||
|
||||
/**
|
||||
* @brief Wait for a specified number of seconds.
|
||||
* @param seconds The number of seconds to wait.
|
||||
*/
|
||||
void wait(int seconds);
|
||||
|
||||
/**
|
||||
* @brief Visualize a solution using GLVis.
|
||||
* @param u The GridFunction to visualize.
|
||||
* @param mesh The mesh associated with the GridFunction.
|
||||
* @param windowTitle The title of the visualization window.
|
||||
*/
|
||||
void glVisView(mfem::GridFunction& u, mfem::Mesh& mesh,
|
||||
const std::string& windowTitle = "solution");
|
||||
|
||||
/**
|
||||
* @brief Class to manage logging operations.
|
||||
*/
|
||||
class LogManager {
|
||||
private:
|
||||
/**
|
||||
* @brief Private constructor for singleton pattern.
|
||||
*/
|
||||
LogManager();
|
||||
|
||||
/**
|
||||
* @brief Destructor.
|
||||
*/
|
||||
~LogManager();
|
||||
|
||||
// Map to store pointers to quill loggers (raw pointers as quill deals with its own memory managment in a seperated, detatched, thread)
|
||||
std::map<std::string, quill::Logger*> loggerMap;
|
||||
|
||||
// Prevent copying and assignment (Rule of Zero)
|
||||
LogManager(const LogManager&) = delete;
|
||||
LogManager& operator=(const LogManager&) = delete;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Get the singleton instance of LogManager.
|
||||
* @return The singleton instance of LogManager.
|
||||
*/
|
||||
static LogManager& getInstance() {
|
||||
static LogManager instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get a logger by name.
|
||||
* @param loggerName The name of the logger.
|
||||
* @return A pointer to the logger.
|
||||
*/
|
||||
quill::Logger* getLogger(const std::string& loggerName);
|
||||
|
||||
/**
|
||||
* @brief Get the names of all loggers.
|
||||
* @return A vector of logger names.
|
||||
*/
|
||||
std::vector<std::string> getLoggerNames();
|
||||
|
||||
/**
|
||||
* @brief Get all loggers.
|
||||
* @return A vector of pointers to the loggers.
|
||||
*/
|
||||
std::vector<quill::Logger*> getLoggers();
|
||||
|
||||
/**
|
||||
* @brief Create a new file logger.
|
||||
* @param filename The name of the log file.
|
||||
* @param loggerName The name of the logger.
|
||||
* @return A pointer to the new logger.
|
||||
*/
|
||||
quill::Logger* newFileLogger(const std::string& filename,
|
||||
const std::string& loggerName);
|
||||
};
|
||||
|
||||
} // namespace Probe
|
||||
#endif
|
||||
Reference in New Issue
Block a user