mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-21 19:30:03 +02:00
WIP
This commit is contained in:
parent
3431752649
commit
ff9c37701b
@ -5,7 +5,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef FIFODEBUG
|
#ifdef FIFODEBUG
|
||||||
@ -22,8 +22,8 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
// #define STRINGIFY(x) #x
|
#define STRINGIFY(x) #x
|
||||||
// #define TOSTRING(x) STRINGIFY(x)
|
#define TOSTRING(x) STRINGIFY(x)
|
||||||
#define MYCONCAT(x,y)
|
#define MYCONCAT(x,y)
|
||||||
#define __AT__ std::string(__FILE__) + std::string("::") + std::string(__func__) + std::string("(): ")
|
#define __AT__ std::string(__FILE__) + std::string("::") + std::string(__func__) + std::string("(): ")
|
||||||
#define __SHORT_FORM_OF_FILE__ \
|
#define __SHORT_FORM_OF_FILE__ \
|
||||||
@ -162,23 +162,13 @@ inline FILE*& Output2FILE::Stream()
|
|||||||
return pStream;
|
return pStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
// inline void Output2FILE::Output(const std::string& msg)
|
|
||||||
// {
|
|
||||||
// FILE* pStream = Stream();
|
|
||||||
// if (!pStream)
|
|
||||||
// return;
|
|
||||||
// fprintf(pStream, "%s", msg.c_str());
|
|
||||||
// fflush(pStream);
|
|
||||||
// }
|
|
||||||
|
|
||||||
inline void Output2FILE::Output(const std::string& msg)
|
inline void Output2FILE::Output(const std::string& msg)
|
||||||
{
|
{
|
||||||
std::cout << msg;
|
FILE* pStream = Stream();
|
||||||
// FILE* pStream = Stream();
|
if (!pStream)
|
||||||
// if (!pStream)
|
return;
|
||||||
// return;
|
fprintf(pStream, "%s", msg.c_str());
|
||||||
// fprintf(pStream, "%s", msg.c_str());
|
fflush(pStream);
|
||||||
// fflush(pStream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Output2FILE::Output(const std::string& msg, TLogLevel level)
|
inline void Output2FILE::Output(const std::string& msg, TLogLevel level)
|
||||||
|
87
slsSupportLib/include/logger2.h
Normal file
87
slsSupportLib/include/logger2.h
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
#pragma once
|
||||||
|
/*Utility to log to console*/
|
||||||
|
|
||||||
|
#include "ansi.h" //Colors
|
||||||
|
#include "logger.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <chrono>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
/*
|
||||||
|
Define the max level that is visible
|
||||||
|
The compiler should optimize away any calls below
|
||||||
|
this level
|
||||||
|
*/
|
||||||
|
#ifndef LOG_MAX_LEVEL
|
||||||
|
#define LOG_MAX_LEVEL logINFO
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace sls {
|
||||||
|
class Logger {
|
||||||
|
std::ostringstream os;
|
||||||
|
TLogLevel level = LOG_MAX_LEVEL;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Logger() = default;
|
||||||
|
Logger(TLogLevel level) : level(level){};
|
||||||
|
~Logger() {
|
||||||
|
// output happens in the destructor to allow for <<
|
||||||
|
os << Reset() << '\n';
|
||||||
|
std::clog << os.str(); // Single write
|
||||||
|
}
|
||||||
|
|
||||||
|
static TLogLevel &ReportingLevel() { // singelton eeh
|
||||||
|
static TLogLevel reportingLevel = logINFO;
|
||||||
|
return reportingLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Danger this buffer need as many elements as TLogLevel
|
||||||
|
static const char *Color(TLogLevel level) {
|
||||||
|
static const char *const colors[] = {
|
||||||
|
RED BOLD, YELLOW BOLD, RESET, BLUE, RED, RESET,
|
||||||
|
RESET, RESET, RESET, RESET, RESET, RESET};
|
||||||
|
return colors[level];
|
||||||
|
}
|
||||||
|
static const char *Reset() {
|
||||||
|
static const char *reset = RESET;
|
||||||
|
return reset;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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",
|
||||||
|
"DEBUG", "DEBUG1", "DEBUG2", "DEBUG3", "DEBUG4", "DEBUG5"};
|
||||||
|
return buffer[level];
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostringstream &Get() {
|
||||||
|
os << Color(level);
|
||||||
|
os << "- " << Time();
|
||||||
|
os << " " << ToString(level) << ": ";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
std::string Time(decltype(std::chrono::system_clock::now()) now =
|
||||||
|
std::chrono::system_clock::now()) {
|
||||||
|
std::ostringstream oss;
|
||||||
|
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
|
now.time_since_epoch()) %
|
||||||
|
1000;
|
||||||
|
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
|
||||||
|
oss << std::put_time(std::localtime(&now_time), "%H:%M:%S") << "."
|
||||||
|
<< std::setw(3) << std::setfill('0') << ms.count();
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#define LOG(level) \
|
||||||
|
if (level > LOG_MAX_LEVEL) \
|
||||||
|
; \
|
||||||
|
else if (level > sls::Logger::ReportingLevel()) \
|
||||||
|
; \
|
||||||
|
else \
|
||||||
|
sls::Logger(level).Get()
|
||||||
|
|
||||||
|
} // namespace sls
|
@ -1,13 +1,56 @@
|
|||||||
#include "catch.hpp"
|
#include "catch.hpp"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
|
#include "logger2.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
TEST_CASE("Get time"){
|
||||||
|
auto now = std::chrono::system_clock::now();
|
||||||
|
sls::Logger log;
|
||||||
|
auto time = log.Time(now);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("fail"){
|
TEST_CASE("fail"){
|
||||||
|
|
||||||
FILE_LOG(logINFO) << "A message";
|
FILE_LOG(logINFO) << "A message";
|
||||||
|
FILE_LOG(logWARNING) << "An error";
|
||||||
|
FILE_LOG(logERROR) << "A warning";
|
||||||
|
|
||||||
|
// sls::Logger::ReportingLevel() = logERROR;
|
||||||
|
// std::cout << sls::Logger::ReportingLevel() << '\n';
|
||||||
|
LOG(logINFO) << "A new message";
|
||||||
|
LOG(logERROR) << "A new error";
|
||||||
|
LOG(logWARNING) << "A new warning";
|
||||||
|
|
||||||
|
LOG(logDEBUG3) << "This should not be printed";
|
||||||
|
|
||||||
|
std::ostringstream local;
|
||||||
|
auto clog_buff = std::clog.rdbuf();
|
||||||
|
std::clog.rdbuf(local.rdbuf());
|
||||||
|
LOG(logERROR) << "This should also not be printed";
|
||||||
|
|
||||||
|
std::clog.rdbuf(clog_buff); // restore
|
||||||
|
LOG(logERROR) << "But this should";
|
||||||
|
|
||||||
|
std::cout << "we got: " << local.str() << '\n';
|
||||||
|
|
||||||
|
|
||||||
|
// sls::Logger::ReportingLevel() = logDEBUG1;
|
||||||
|
// std::cout << sls::Logger::ReportingLevel() << '\n';
|
||||||
|
// std::ostream& os = std::cout;
|
||||||
|
// Output2FILE::Stream();
|
||||||
|
|
||||||
|
// os << "hej pa dig\n";
|
||||||
|
|
||||||
|
// std::ostringstream oss;
|
||||||
|
// std::ostream& os = oss;
|
||||||
|
// auto& out = Output2FILE::Stream();
|
||||||
|
// out = os;
|
||||||
|
// Output2FILE::Stream() = std::cout;
|
||||||
|
// FILE_LOG(logERROR) << "An error message";
|
||||||
|
|
||||||
|
|
||||||
// CHECK(false);
|
// CHECK(false);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user