Warning, file /include/JANA/JLogger.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005 #pragma once
0006
0007 #include <iostream>
0008 #include <sstream>
0009 #include <JANA/Compatibility/JStreamLog.h>
0010
0011
0012 struct JLogger {
0013 enum class Level { TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF };
0014 Level level;
0015 std::ostream *destination;
0016 std::string className;
0017 bool show_level = true;
0018 bool show_classname = false;
0019 bool show_timestamp = false;
0020 bool show_threadstamp = false;
0021
0022 explicit JLogger(JLogger::Level level = JLogger::Level::INFO,
0023 std::ostream* destination = &std::cout,
0024 std::string className = "")
0025 : level(level), destination(destination), className(std::move(className)) {};
0026
0027 JLogger(const JLogger&) = default;
0028 JLogger& operator=(const JLogger&) = default;
0029
0030 void SetTag(std::string tag) {className = tag; }
0031 void SetTimestampFlag() {show_timestamp = true; }
0032 void UnsetTimestampFlag() {show_timestamp = false; }
0033 void SetThreadstampFlag() {show_threadstamp = true; }
0034 void UnsetThreadstampFlag() {show_threadstamp = false; }
0035 };
0036
0037 static JLogger default_cout_logger = JLogger(JLogger::Level::TRACE, &std::cout, "JANA");
0038 static JLogger default_cerr_logger = JLogger(JLogger::Level::TRACE, &std::cerr, "JERR");
0039
0040 inline std::ostream& operator<<(std::ostream& s, JLogger::Level l) {
0041 switch (l) {
0042 case JLogger::Level::TRACE: return s << "TRACE";
0043 case JLogger::Level::DEBUG: return s << "DEBUG";
0044 case JLogger::Level::INFO: return s << "INFO";
0045 case JLogger::Level::WARN: return s << "WARN";
0046 case JLogger::Level::ERROR: return s << "ERROR";
0047 case JLogger::Level::FATAL: return s << "FATAL";
0048 default: return s << "OFF";
0049 }
0050 }
0051
0052
0053 struct JLogMessage {
0054
0055
0056 struct End {};
0057
0058 const JLogger& logger;
0059 JLogger::Level level;
0060 std::ostringstream builder;
0061
0062
0063 JLogMessage(const JLogger& logger = default_cout_logger,
0064 JLogger::Level level = JLogger::Level::INFO)
0065 : logger(logger), level(level) {
0066
0067 if (logger.show_level) {
0068 builder << "[" << level << "] ";
0069 }
0070 if (logger.show_classname) {
0071 builder << logger.className << ": ";
0072 }
0073
0074 }
0075
0076
0077
0078 std::string ltrunc(std::string original, size_t desired_length) {
0079 auto n = original.length();
0080 if (n <= desired_length) return original;
0081 return "\u2026" + original.substr(n - desired_length, desired_length - 1);
0082 }
0083 };
0084
0085
0086
0087
0088 template <typename T>
0089 inline JLogMessage operator<<(JLogger& l, const T& t) {
0090 JLogMessage m(l);
0091 m.builder << t;
0092 return m;
0093 }
0094
0095 template<typename T>
0096 inline JLogMessage& operator<<(JLogMessage& m, const T& t) {
0097 m.builder << t;
0098 return m;
0099 }
0100
0101 template<typename T>
0102 inline JLogMessage&& operator<<(JLogMessage&& m, const T& t) {
0103 m.builder << t;
0104 return std::move(m);
0105 }
0106
0107 inline void operator<<(JLogMessage&& m, JLogMessage::End const&) {
0108 std::ostream& dest = *m.logger.destination;
0109 m.builder << std::endl;
0110 dest << m.builder.str();
0111 dest.flush();
0112 }
0113
0114
0115
0116 #define LOG JLogMessage()
0117
0118 #define LOG_IF(predicate) if (predicate) JLogMessage()
0119
0120 #define LOG_END JLogMessage::End();
0121
0122 #define LOG_AT_LEVEL(logger, msglevel) if ((logger).level <= msglevel) JLogMessage((logger), msglevel)
0123
0124 #define LOG_FATAL(logger) LOG_AT_LEVEL(logger, JLogger::Level::FATAL)
0125 #define LOG_ERROR(logger) LOG_AT_LEVEL(logger, JLogger::Level::ERROR)
0126 #define LOG_WARN(logger) LOG_AT_LEVEL(logger, JLogger::Level::WARN)
0127 #define LOG_INFO(logger) LOG_AT_LEVEL(logger, JLogger::Level::INFO)
0128 #define LOG_DEBUG(logger) LOG_AT_LEVEL(logger, JLogger::Level::DEBUG)
0129 #define LOG_TRACE(logger) LOG_AT_LEVEL(logger, JLogger::Level::TRACE)
0130