24#ifndef __BITPIT_LOGGER_HPP__
25#define __BITPIT_LOGGER_HPP__
31#include <unordered_map>
34#include "bitpit_common.hpp"
36#define BITPIT_DEBUG_COUT(...) BITPIT_OVERLOAD_CALL(BITPIT_DEBUG_COUT, __VA_ARGS__)
37#if BITPIT_ENABLE_DEBUG
38#define BITPIT_DEBUG_COUT_0() bitpit::log::cout()
39#define BITPIT_DEBUG_COUT_1(NAME) bitpit::log::cout(NAME)
41#define BITPIT_DEBUG_COUT_0() while(0) bitpit::log::cout()
42#define BITPIT_DEBUG_COUT_1(NAME) while(0) bitpit::log::cout(NAME)
70 VISIBILITY_NOTSET = -1,
71 VISIBILITY_MASTER = 0,
73 MASTER = VISIBILITY_MASTER,
74 GLOBAL = VISIBILITY_GLOBAL,
90 bool consoleTimestampEnabled;
91 bool fileTimestampEnabled;
94 LoggerBuffer(
int nProcesses,
int rank, std::size_t bufferSize = 256);
112 void setSettings(
const std::shared_ptr<Settings> &settings);
114 int flush(
bool terminate);
117 std::vector<char> m_buffer;
121 std::string m_rankPrefix;
123 bool m_consoleEnabled;
124 std::ostream *m_console;
127 std::ofstream *m_file;
129 std::shared_ptr<Settings> m_settings;
131 std::string getTimestamp()
const;
133 int_type overflow(int_type ch)
override;
136 int flushLine(std::ostream &stream,
const char *begin,
const char *end,
137 const std::string ×tamp,
bool terminate);
185 void println(
const std::string &message);
187 void println(
const std::string &message, log::Visibility visibility);
188 void println(
const std::string &message,
const log::Level severity, log::Visibility visibility);
190 void print(
const std::string &message);
192 void print(
const std::string &message, log::Visibility visibility);
193 void print(
const std::string &message,
log::Level severity, log::Visibility visibility);
196 void print(
const T &value,
log::Level severity, log::Visibility visibility)
199 m_buffer->setSettings(m_bufferSettings);
202 enableBufferStreams(severity, visibility);
205 static_cast<std::ostream &
>(*this) << value;
211 std::shared_ptr<LoggerBuffer> m_buffer;
212 std::shared_ptr<LoggerBuffer::Settings> m_bufferSettings;
215 log::Visibility m_defaultVisibility;
223 Logger(
const std::string &name,
const std::shared_ptr<LoggerBuffer> &buffer);
228 void enableBufferStreams(
log::Level severity, log::Visibility visibility);
237 BITPIT_PUBLIC_API
static std::string BITPIT_LOG_NAME;
238 BITPIT_PUBLIC_API
static std::string BITPIT_LOG_DIRECTORY;
244 Logger &
cout(
log::Level defualtSeverity = log::Level::LEVEL_NOTSET, log::Visibility defualtVisibility = log::VISIBILITY_NOTSET);
245 Logger &
cout(
const std::string &name,
log::Level defualtSeverity = log::Level::LEVEL_NOTSET, log::Visibility defualtVisibility = log::VISIBILITY_NOTSET);
247 Logger &
critical(log::Visibility visibility = log::VISIBILITY_NOTSET);
248 Logger &
critical(
const std::string &name,log::Visibility visibility = log::VISIBILITY_NOTSET);
250 Logger &
error(log::Visibility visibility = log::VISIBILITY_NOTSET);
251 Logger &
error(
const std::string &name, log::Visibility visibility = log::VISIBILITY_NOTSET);
253 Logger &
warning(log::Visibility visibility = log::VISIBILITY_NOTSET);
254 Logger &
warning(
const std::string &name, log::Visibility visibility = log::VISIBILITY_NOTSET);
256 Logger &
info(log::Visibility visibility = log::VISIBILITY_NOTSET);
257 Logger &
info(
const std::string &name, log::Visibility visibility = log::VISIBILITY_NOTSET);
259 Logger &
debug(log::Visibility visibility = log::VISIBILITY_NOTSET);
260 Logger &
debug(
const std::string &name, log::Visibility visibility = log::VISIBILITY_NOTSET);
263 int nProcesses,
int rank);
266 const std::string &directory = BITPIT_LOG_DIRECTORY,
267 int nProcesses = 1,
int rank = 0);
270 bool reset =
false,
const std::string &directory = BITPIT_LOG_DIRECTORY,
271 int nProcesses = 1,
int rank = 0);
273 void create(
const std::string &name,
bool reset =
false,
274 int nProcesses = 1,
int rank = 0);
276 void create(
const std::string &name,
bool reset,
const std::string &directory,
277 int nProcesses = 1,
int rank = 0);
279 bool destroy(
const std::string &name,
bool force =
false);
281 bool exists(
const std::string &name)
const;
296 static std::unique_ptr<LoggerManager> m_manager;
298 std::string m_defaultName;
299 std::string m_defaultDirectory;
302 std::unordered_map<std::string, std::unique_ptr<Logger>> m_loggers;
303 std::unordered_map<std::string, int> m_loggerUsers;
304 std::unordered_map<std::string, std::unique_ptr<std::ofstream>> m_fileStreams;
311 void _create(
const std::string &name,
bool reset,
const std::string &directory,
int nProcesses,
int rank);
312 void _create(
const std::string &name, std::shared_ptr<LoggerBuffer> &buffer);
326 Logger &
cout(
log::Level defualtSeverity = log::Level::LEVEL_NOTSET, log::Visibility defualtVisibility = log::VISIBILITY_NOTSET);
327 Logger &
cout(
const std::string &name,
log::Level defualtSeverity = log::Level::LEVEL_NOTSET, log::Visibility defualtVisibility = log::VISIBILITY_NOTSET);
369 return m.f(logger, m.value);
373 LoggerManipulator<std::string>
context(
const std::string &
context);
397 LoggerManipulator<log::Level>
disable(
const log::Level &verbosity = log::Level::LEVEL_CRITICAL);
406 LoggerManipulator<int>
indent(
int delta);
Stream buffer for the message logger.
std::ofstream & getFileStream()
LoggerBuffer(int nProcesses, int rank, std::size_t bufferSize=256)
void setFileEnabled(bool enabled)
void setSettings(const std::shared_ptr< Settings > &settings)
int getProcessCount() const
const Settings * getSettings() const
void setFileStream(std::ofstream *file)
void setConsoleEnabled(bool enabled)
std::ostream & getConsoleStream()
int flush(bool terminate)
void setConsoleStream(std::ostream *console)
void create(const std::string &name, bool reset=false, int nProcesses=1, int rank=0)
bool setMode(log::Mode mode)
bool exists(const std::string &name) const
Logger & debug(log::Visibility visibility=log::VISIBILITY_NOTSET)
bool destroy(const std::string &name, bool force=false)
void setVerbosities(log::Level threshold)
Logger & critical(log::Visibility visibility=log::VISIBILITY_NOTSET)
std::string getDefaultDirectory() const
log::Mode getMode() const
Logger & cout(log::Level defualtSeverity=log::Level::LEVEL_NOTSET, log::Visibility defualtVisibility=log::VISIBILITY_NOTSET)
Logger & warning(log::Visibility visibility=log::VISIBILITY_NOTSET)
static LoggerManager & manager()
Logger & error(log::Visibility visibility=log::VISIBILITY_NOTSET)
void setFileVerbosity(log::Level threshold)
std::string getDefaultName() const
Logger & info(log::Visibility visibility=log::VISIBILITY_NOTSET)
bool isInitialized() const
void setConsoleVerbosity(log::Level threshold)
void initialize(log::Mode mode, bool reset, int nProcesses, int rank)
void setDefaultVisibility(log::Visibility visibility)
void setDefaultSeverity(log::Level severity)
void setVerbosities(log::Level threshold)
void setConsoleTimestampEnabled(bool enabled)
void setFileVerbosity(log::Level threshold)
log::Level getDefaultSeverity()
void setConsoleVerbosity(log::Level threshold)
void setPriority(log::Priority priority)
bool isConsoleTimestampEnabled() const
void disable(log::Level=log::Level::LEVEL_CRITICAL)
log::Visibility getDefaultVisibility()
log::Level getFileVerbosity()
void setIndentation(int delta)
void disableConsole(log::Level=log::Level::LEVEL_CRITICAL)
bool isFileTimestampEnabled() const
void setVisibility(log::Visibility visibility)
void setFileTimestampEnabled(bool enabled)
void println(const std::string &message)
void disableFile(log::Level=log::Level::LEVEL_CRITICAL)
std::string getName() const
void print(const std::string &message)
void setContext(const std::string &context)
void setTimestampEnabled(bool enabled)
log::Level getConsoleVerbosity()
log::Visibility getVisibility()
log::Priority getPriority()
#define BITPIT_DEPRECATED(func)
LoggerManipulator< log::Level > defaultSeverity(const log::Level &severity)
Logger & cout(log::Level defaultSeverity, log::Visibility defaultVisibility)
LoggerManipulator< log::Visibility > defaultVisibility(const log::Visibility &visibility)
LoggerManipulator< log::Level > verbosities(const log::Level &threshold)
Logger & setPriority(Logger &logger, const log::Priority &priority)
LoggerManipulator< log::Visibility > visibility(const log::Visibility &visibility)
Logger & critical(log::Visibility defaultVisibility)
Logger & operator<<(Logger &logger, LoggerManipulator< T > &&m)
Logger & disable(Logger &logger, const log::Level &level)
LoggerManipulator< log::Level > consoleVerbosity(const log::Level &threshold)
Logger & setDefaultSeverity(Logger &logger, const log::Level &severity)
LoggerManipulator< log::Priority > priority(const log::Priority &priority)
Logger & error(log::Visibility defaultVisibility)
Logger & warning(log::Visibility defaultVisibility)
LoggerManipulator< log::Level > fileVerbosity(const log::Level &threshold)
Logger & debug(log::Visibility defaultVisibility)
LoggerManipulator< int > indent(int delta)
Logger & setFileVerbosity(Logger &logger, const log::Level &threshold)
Logger & setVisibility(Logger &logger, const log::Visibility &visibility)
Logger & info(log::Visibility defaultVisibility)
Logger & disableConsole(Logger &logger, const log::Level &level)
Logger & setDefaultVisibility(Logger &logger, const log::Visibility &visibility)
LoggerManager & manager()
Logger & setVerbosities(Logger &logger, const log::Level &threshold)
Logger & setIndentation(Logger &logger, const int &delta)
LoggerManipulator< std::string > context(const std::string &context)
Logger & disableFile(Logger &logger, const log::Level &level)
Logger & setConsoleVerbosity(Logger &logger, const log::Level &threshold)
Logger & setContext(Logger &logger, const std::string &context)
LoggerManipulator(Logger &(*ff)(Logger &, const T &), const T &ss)