Go to the documentation of this file.
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;
void dumpToConsole() const
Dump the message contents to the standard output.
void writeToStream(std::ostream &out) const
Write the message contents to the specified stream.
virtual ~COutputLogger()
virtual dtor (so we can derive classes from this one)
void setVerbosityLevel(const VerbosityLevel level)
alias of setMinLoggingLevel()
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
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.
std::string getLoggerLastMsg() const
Return the last Tmsg instance registered in the logger history.
mrpt::vision::TStereoCalibResults out
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void writeLogToFile(const std::string *fname_in=nullptr) const
Write the contents of the COutputLogger instance to an external file.
std::string generateStringFromFormat(std::string_view fmt, va_list argp) const
Helper method for generating a std::string instance from printf-like arguments.
std::string getLoggerName() const
Return the name of the COutputLogger instance.
mrpt::Clock::time_point timestamp
Timestamp of the message.
void void logCond(const VerbosityLevel level, bool cond, const std::string &msg_str) const
Log the given message only if the condition is satisfied.
std::string getLogAsString() const
Get the history of COutputLogger instance in a string representation.
void logFmt(const VerbosityLevel level, const char *fmt,...) const MRPT_printf_format_check(3
Alternative logging method, which mimics the printf behavior.
std::deque< output_logger_callback_t > m_listCallbacks
static std::array< std::string, NUMBER_OF_VERBOSITY_LEVELS > & logging_levels_to_names()
Map from VerbosityLevels to their corresponding names.
VerbosityLevel
Enumeration of available verbosity levels.
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...
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.
void logStr(const VerbosityLevel level, std::string_view msg_str) const
Main method to add the specified message string to the logger.
std::string body
Actual content of the message.
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.
VerbosityLevel level
Verbosity level of the message.
Clock that is compatible with MRPT TTimeStamp representation.
std::string getAsString() const
Return a string representation of the underlying message.
void setMinLoggingLevel(const VerbosityLevel level)
Set the minimum logging level for which the incoming logs are going to be taken into account.
~TMsg()
Default Destructor.
void loggerReset()
Reset the contents of the logger instance.
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
bool logDeregisterCallback(output_logger_callback_t userFunc)
Versatile class for consistent logging and management of output messages.
static std::array< std::string, NUMBER_OF_VERBOSITY_LEVELS > logging_levels_to_names
void dumpLogToConsole() const
Dump the current contents of the COutputLogger instance in the terminal window.
TConsoleColor
For use in setConsoleColor.
Struct responsible of holding information relevant to the message (in std::string form) issued by the...
static std::array< mrpt::system::TConsoleColor, NUMBER_OF_VERBOSITY_LEVELS > & logging_levels_to_colors()
Map from VerbosityLevels to their corresponding mrpt::system::TConsoleColor.
void logRegisterCallback(output_logger_callback_t userFunc)
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...
void setLoggerName(const std::string &name)
Set the name of the COutputLogger instance.
void setConsoleColor(TConsoleColor color, bool changeStdErr=false)
Changes the text color in the console for the text written from now on.
std::string std::string format(std::string_view fmt, ARGS &&... args)
COutputLogger()
Default class constructor.
std::string name
Name of the COutputLogger instance that called registered the message.
Page generated by Doxygen 1.8.17 for MRPT 2.0.3 at Fri May 15 23:51:15 UTC 2020 | |