Skip to content

Commit 9797614

Browse files
committed
Make MonLogger independet from InfoLogger
1 parent 4528885 commit 9797614

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

src/Backends/InfoLoggerBackend.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ inline unsigned long InfoLoggerBackend::convertTimestamp(const std::chrono::time
2828

2929
InfoLoggerBackend::InfoLoggerBackend()
3030
{
31-
MonLogger::Get() << "InfoLogger backend initialized" << InfoLogger::endm;
31+
MonLogger::Get() << "InfoLogger backend initialized" << MonLogger::End();
3232
}
3333

3434
void InfoLoggerBackend::addGlobalTag(std::string name, std::string value)

src/Exceptions/MonitoringInternalException.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ MonitoringInternalException::MonitoringInternalException(const std::string& sour
1818
MonitoringInternalException::MonitoringInternalException(int code, const std::string& source, const std::string& message)
1919
: message(message)
2020
{
21-
MonLogger::Get() << "MonitoringInternalException["
21+
MonLogger::Get(Severity::Warn) << "MonitoringInternalException["
2222
<< source << "] (" << code << "): " << message << MonLogger::End();
2323
}
2424

src/MonLogger.h

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#ifndef MONITORING_MONINFOLOGGER_H
77
#define MONITORING_MONINFOLOGGER_H
88

9+
#include <chrono>
10+
#include <iomanip>
911
#include <iostream>
1012

1113
namespace AliceO2
@@ -14,40 +16,66 @@ namespace AliceO2
1416
namespace Monitoring
1517
{
1618

19+
/// List of possible log severities
20+
enum class Severity { Error = 31, Warn = 33, Info = 49 };
21+
1722
/// Simple Monitoring logging class
1823
class MonLogger
1924
{
2025
public:
26+
/// Appends value/message to the log
27+
/// \param log - message
28+
/// \return - this to allow chaining
2129
template <typename T>
2230
MonLogger &operator<<(const T &log) {
23-
mStream << log;
24-
return *this;
31+
mStream << log;
32+
return *this;
2533
}
2634

27-
MonLogger &operator<<(std::ostream& (*log) (std::ostream&)) {
28-
mStream << log;
29-
return *this;
30-
}
31-
static MonLogger &Get()
35+
/// Singleton
36+
/// Returns Logger instance with current date and given severity
37+
/// \param severity - severity level
38+
/// \return - logger instance
39+
static MonLogger &Get(Severity severity = Severity::Info)
3240
{
3341
static MonLogger loggerInstance;
42+
loggerInstance.setDate();
43+
loggerInstance.setSeverity(severity);
3444
return loggerInstance;
3545
}
36-
static auto End() -> decltype("\n")
46+
47+
/// Terminates log line
48+
/// return - string with color termination and new line
49+
static auto End() -> decltype("\033[0m\n")
3750
{
38-
return "\n";
51+
return "\033[0m\n";
3952
}
4053

4154
private:
55+
/// Log stream
4256
std::ostream &mStream;
43-
MonLogger(std::ostream &oStream = std::cout): mStream(oStream) {};
57+
58+
/// Sets cout as a log stream
59+
MonLogger(std::ostream &oStream = std::cout) : mStream(oStream)
60+
{}
61+
62+
/// Appends current datetime to log stream
63+
void setDate() {
64+
auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
65+
mStream << std::put_time(std::localtime(&now), "%Y-%m-%d %X") << "\t";
66+
}
4467

68+
/// Sets log color based on severity
69+
/// \param severity - log severity
70+
void setSeverity(Severity severity) {
71+
mStream << "\033[1;" << static_cast<int>(severity) << "m";
72+
}
73+
4574
/// Delete copy and move constructors
4675
MonLogger &operator=(const MonLogger &) = delete;
4776
MonLogger(const MonLogger &) = delete;
4877
MonLogger(MonLogger &&) = delete;
4978
MonLogger &operator=(MonLogger&&) = delete;
50-
5179
};
5280

5381
} // Monitoring

0 commit comments

Comments
 (0)