23 #define WIN32_LEAN_AND_MEAN 38 static std::array<mrpt::system::TConsoleColor, NUMBER_OF_VERBOSITY_LEVELS>
46 std::array<mrpt::system::TConsoleColor, NUMBER_OF_VERBOSITY_LEVELS>&
52 static std::array<std::string, NUMBER_OF_VERBOSITY_LEVELS>
59 std::array<std::string, NUMBER_OF_VERBOSITY_LEVELS>&
75 if (level < m_min_verbosity_level)
return;
78 TMsg msg(level, msg_str, *
this);
79 if (logging_enable_keep_record) m_history.push_back(msg);
81 if (logging_enable_console_output)
87 for (
const auto& c : m_listCallbacks)
104 std::string str = this->generateStringFromFormat(fmt, argp);
107 this->logStr(level, str);
111 std::string_view fmt, va_list argp)
const 113 int result = -1, length = 1024;
114 std::vector<char> buffer;
118 buffer.resize(length + 10);
119 result =
os::vsnprintf(&buffer[0], length, fmt.data(), argp);
124 if (result >= length) result = -1;
129 return std::string(&buffer[0]);
132 const VerbosityLevel level,
bool cond,
const std::string& msg_str)
const 135 this->logStr(level, msg_str);
140 m_logger_name = name;
147 m_min_verbosity_level = level;
151 m_min_verbosity_level = level;
157 for (
const auto& h : m_history) fname += h.getAsString();
162 this->getLogAsString(str);
166 const std::string* fname_in )
const 176 fname = m_logger_name +
".log";
178 std::ofstream f(fname);
181 "[%s:] Could not open external file: %s",
182 m_logger_name.c_str(), fname.c_str()));
184 std::string hist_str;
185 this->getLogAsString(hist_str);
192 for (
const auto& h : m_history) h.dumpToConsole();
197 TMsg last_msg = m_history.back();
203 msg_str = this->getLoggerLastMsg();
208 m_logger_name =
"log";
211 logging_enable_console_output =
true;
212 logging_enable_keep_record =
false;
227 name(logger.getLoggerName()),
240 if (!body.empty() && *body.rbegin() !=
'\n')
out << std::endl;
246 *contents = this->getAsString();
250 const std::string str = getAsString();
253 OutputDebugStringA(str.c_str());
258 const std::string str = getAsString();
260 const bool dump_to_cerr = (level ==
LVL_ERROR);
269 (dump_to_cerr ? std::cerr : std::cout) << str;
283 template <
typename T,
typename... U>
286 using fnType = T (*)(U...);
287 auto** fnPointer = f.template target<fnType*>();
288 return (
size_t)*fnPointer;
Clock that is compatible with MRPT TTimeStamp representation.
std::string generateStringFromFormat(std::string_view fmt, va_list argp) const
Helper method for generating a std::string instance from printf-like arguments.
size_t getAddress(std::function< T(U...)> f)
static std::array< mrpt::system::TConsoleColor, NUMBER_OF_VERBOSITY_LEVELS > logging_levels_to_colors
Implementation file for the COutputLogger header class.
VerbosityLevel
Enumeration of available verbosity levels.
virtual ~COutputLogger()
virtual dtor (so we can derive classes from this one)
void logFmt(const VerbosityLevel level, const char *fmt,...) const MRPT_printf_format_check(3
Alternative logging method, which mimics the printf behavior.
std::string std::string format(std::string_view fmt, ARGS &&... args)
void dumpToConsole() const
Dump the message contents to the standard output.
void setConsoleColor(TConsoleColor color, bool changeStdErr=false)
Changes the text color in the console for the text written from now on.
std::string getLoggerLastMsg() const
Return the last Tmsg instance registered in the logger history.
static std::array< std::string, NUMBER_OF_VERBOSITY_LEVELS > & logging_levels_to_names()
Map from VerbosityLevels to their corresponding names.
void setMinLoggingLevel(const VerbosityLevel level)
Set the minimum logging level for which the incoming logs are going to be taken into account...
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
void writeToStream(std::ostream &out) const
Write the message contents to the specified stream.
std::deque< output_logger_callback_t > m_listCallbacks
VerbosityLevel level
Verbosity level of the message.
std::function< void(std::string_view msg, const mrpt::system::VerbosityLevel level, std::string_view loggerName, const mrpt::Clock::time_point timestamp)> output_logger_callback_t
Callback types for use with mrpt::system::COuputLogger.
static std::array< std::string, NUMBER_OF_VERBOSITY_LEVELS > logging_levels_to_names
static std::array< mrpt::system::TConsoleColor, NUMBER_OF_VERBOSITY_LEVELS > & logging_levels_to_colors()
Map from VerbosityLevels to their corresponding mrpt::system::TConsoleColor.
std::string getAsString() const
Return a string representation of the underlying message.
std::string body
Actual content of the message.
Versatile class for consistent logging and management of output messages.
std::string getLoggerName() const
Return the name of the COutputLogger instance.
void setLoggerName(const std::string &name)
Set the name of the COutputLogger instance.
void loggerReset()
Reset the contents of the logger instance.
TConsoleColor
For use in setConsoleColor.
void void logCond(const VerbosityLevel level, bool cond, const std::string &msg_str) const
Log the given message only if the condition is satisfied.
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
void dumpLogToConsole() const
Dump the current contents of the COutputLogger instance in the terminal window.
mrpt::Clock::time_point timestamp
Timestamp of the message.
TMsg(const mrpt::system::VerbosityLevel level, std::string_view msg, const COutputLogger &logger)
Class constructor that passes a message in std::string form as well as a reference to the COutputLogg...
std::string getLogAsString() const
Get the history of COutputLogger instance in a string representation.
void setVerbosityLevel(const VerbosityLevel level)
alias of setMinLoggingLevel()
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
int vsnprintf(char *buf, size_t bufSize, const char *format, va_list args) noexcept
An OS-independent version of vsnprintf (Notice the bufSize param, which may be ignored in some compil...
void logStr(const VerbosityLevel level, std::string_view msg_str) const
Main method to add the specified message string to the logger.
mrpt::vision::TStereoCalibResults out
COutputLogger()
Default class constructor.
Struct responsible of holding information relevant to the message (in std::string form) issued by the...
std::string timeLocalToString(const mrpt::system::TTimeStamp t, unsigned int secondFractionDigits=6)
Convert a timestamp into this textual form (in local time): HH:MM:SS.MMMMMM.
void logRegisterCallback(output_logger_callback_t userFunc)
std::string name
Name of the COutputLogger instance that called registered the message.
~TMsg()
Default Destructor.
bool logDeregisterCallback(output_logger_callback_t userFunc)
void writeLogToFile(const std::string *fname_in=nullptr) const
Write the contents of the COutputLogger instance to an external file.