mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2026-05-15 21:35:35 +02:00
bb1a73d718
Build and Deploy on local RHEL9 / build (push) Successful in 2m0s
Build on RHEL9 docker image / build (push) Successful in 3m34s
Build on RHEL8 docker image / build (push) Successful in 4m46s
Build and Deploy on local RHEL8 / build (push) Successful in 5m3s
Run Simulator Tests on local RHEL9 / build (push) Successful in 14m43s
Run Simulator Tests on local RHEL8 / build (push) Successful in 18m15s
* added fetch fmt server library * added first draft of matterhorn * added enum ReturnCode * added cpp TCP Interface to slsDetectorServer * added fmt to workflows * bug: added std::signal for proper handling of ctr+c * added compile option to set log level * WIP * dont use c project settings when building matterhornserver * updated logger * WIP * WIP * linked fmt to slsProjectOptions * solved merge conflict * some refactoring * cleaned up logs * added fmt to workflow * WIP * generated register defs from csv file * oops given in hex * properly added fmt as a dependency * add fmt to conda recipe * some format changes * dont use public headers of fmt * WIP * used CRTP for virtual detector * WIP * added udp functions to matterhornserver * Matterhorn in tostring * warning unused variable from other PR * fixed build * updated cmake * added Server class usable for all detectors * removed stopserver * added some more functions * wrong overload * porper cleanup of matterhorn app * PR Review * refactored directory structure * used pause insetad of sleep --------- Co-authored-by: Dhanya Thattil <dhanya.thattil@psi.ch>
116 lines
3.7 KiB
C++
116 lines
3.7 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-or-other
|
|
// Copyright (C) 2021 Contributors to the SLS Detector Package
|
|
#pragma once
|
|
/*Utility to log to console*/
|
|
|
|
#include "sls/ansi.h" //Colors
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <sys/time.h>
|
|
|
|
namespace sls {
|
|
|
|
enum TLogLevel {
|
|
logERROR,
|
|
logWARNING,
|
|
logINFOBLUE,
|
|
logINFOGREEN,
|
|
logINFORED,
|
|
logINFOCYAN,
|
|
logINFOMAGENTA,
|
|
logINFO,
|
|
logDEBUG,
|
|
logDEBUG1,
|
|
logDEBUG2,
|
|
logDEBUG3,
|
|
logDEBUG4,
|
|
logDEBUG5
|
|
};
|
|
|
|
#define __AT__ \
|
|
std::string(__FILE__) + std::string("::") + std::string(__func__) + \
|
|
std::string("(): ")
|
|
#define __SHORT_FORM_OF_FILE__ \
|
|
(strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
|
|
#define __SHORT_AT__ \
|
|
std::string(__SHORT_FORM_OF_FILE__) + std::string("::") + \
|
|
std::string(__func__) + std::string("(): ")
|
|
|
|
class Logger {
|
|
std::ostringstream os;
|
|
TLogLevel level = ReportingLevel();
|
|
|
|
public:
|
|
Logger() = default;
|
|
explicit Logger(TLogLevel level) : level(level){};
|
|
~Logger() {
|
|
// output in the destructor to allow for << syntax
|
|
os << RESET << '\n';
|
|
std::clog << os.str() << std::flush; // Single write
|
|
}
|
|
|
|
static TLogLevel &ReportingLevel() { // singelton eeh
|
|
static TLogLevel reportingLevel = LOG_MAX_REPORTING_LEVEL;
|
|
return reportingLevel;
|
|
}
|
|
|
|
// Danger this buffer need as many elements as TLogLevel
|
|
static const char *Color(TLogLevel level) noexcept {
|
|
static const char *const colors[] = {
|
|
RED BOLD, YELLOW BOLD, BLUE, GREEN, RED, CYAN, MAGENTA,
|
|
RESET, RESET, RESET, RESET, RESET, RESET, RESET};
|
|
// out of bounds
|
|
if (level < 0 || level >= sizeof(colors) / sizeof(colors[0])) {
|
|
return RESET;
|
|
}
|
|
return colors[level];
|
|
}
|
|
|
|
// Danger this buffer need as many elements as TLogLevel
|
|
static std::string ToString(TLogLevel level) {
|
|
static const char *const buffer[] = {
|
|
"ERROR", "WARNING", "INFO", "INFO", "INFO",
|
|
"INFO", "INFO", "INFO", "DEBUG", "DEBUG1",
|
|
"DEBUG2", "DEBUG3", "DEBUG4", "DEBUG5"};
|
|
// out of bounds
|
|
if (level < 0 || level >= sizeof(buffer) / sizeof(buffer[0])) {
|
|
return "UNKNOWN";
|
|
}
|
|
return buffer[level];
|
|
}
|
|
|
|
std::ostringstream &Get() {
|
|
os << Color(level) << "- " << Timestamp() << " " << ToString(level)
|
|
<< ": ";
|
|
return os;
|
|
}
|
|
|
|
static std::string Timestamp() {
|
|
constexpr size_t buffer_len = 12;
|
|
char buffer[buffer_len];
|
|
time_t t;
|
|
::time(&t);
|
|
tm r;
|
|
strftime(buffer, buffer_len, "%X", localtime_r(&t, &r));
|
|
buffer[buffer_len - 1] = '\0';
|
|
struct timeval tv;
|
|
gettimeofday(&tv, nullptr);
|
|
constexpr size_t result_len = 100;
|
|
char result[result_len];
|
|
snprintf(result, result_len, "%s.%03ld", buffer,
|
|
(long)tv.tv_usec / 1000);
|
|
result[result_len - 1] = '\0';
|
|
return result;
|
|
}
|
|
};
|
|
|
|
#define LOG(level) \
|
|
if (level > LOG_MAX_REPORTING_LEVEL) \
|
|
; \
|
|
else if (level > sls::Logger::ReportingLevel()) \
|
|
; \
|
|
else \
|
|
sls::Logger(level).Get()
|
|
|
|
} // namespace sls
|