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;
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 const char* fmt, va_list argp)
const
113 int result = -1,
length = 1024;
124 if (result >=
length) result = -1;
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);
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()));
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();
253 OutputDebugStringA(str.c_str());
268 (dump_to_cerr ? std::cerr : std::cout) << str;
282 template <
typename T,
typename... U>
285 using fnType = T (*)(U...);
286 fnType** fnPointer = f.template target<fnType*>();
287 return (
size_t)*fnPointer;
static std::array< std::string, NUMBER_OF_VERBOSITY_LEVELS > logging_levels_to_names
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.
Clock that is compatible with MRPT TTimeStamp representation.
Versatile class for consistent logging and management of output messages.
static std::array< std::string, NUMBER_OF_VERBOSITY_LEVELS > & logging_levels_to_names()
Map from VerbosityLevels to their corresponding names.
COutputLogger()
Default class constructor.
void logStr(const VerbosityLevel level, const std::string &msg_str) const
Main method to add the specified message string to the logger.
void dumpLogToConsole() const
Dump the current contents of the COutputLogger instance in the terminal window.
void writeLogToFile(const std::string *fname_in=NULL) const
Write the contents of the COutputLogger instance to an external file.
static std::array< mrpt::system::TConsoleColor, NUMBER_OF_VERBOSITY_LEVELS > & logging_levels_to_colors()
Map from VerbosityLevels to their corresponding mrpt::system::TConsoleColor.
void loggerReset()
Reset the contents of the logger instance.
void setMinLoggingLevel(const VerbosityLevel level)
Set the minimum logging level for which the incoming logs are going to be taken into account.
void logRegisterCallback(output_logger_callback_t userFunc)
std::string getLoggerName() const
Return the name of the COutputLogger instance.
std::deque< output_logger_callback_t > m_listCallbacks
bool logDeregisterCallback(output_logger_callback_t userFunc)
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.
void setVerbosityLevel(const VerbosityLevel level)
alias of setMinLoggingLevel()
std::string generateStringFromFormat(const char *fmt, va_list argp) const
Helper method for generating a std::string instance from printf-like arguments.
std::string getLoggerLastMsg() const
Return the last Tmsg instance registered in the logger history.
void setLoggerName(const std::string &name)
Set the name of the COutputLogger instance.
void void logCond(const VerbosityLevel level, bool cond, const std::string &msg_str) const
Log the given message only if the condition is satisfied.
virtual ~COutputLogger()
virtual dtor (so we can derive classes from this one)
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
GLuint GLsizei GLsizei * length
GLuint const GLchar * name
GLsizei const GLchar ** string
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
void setConsoleColor(TConsoleColor color, bool changeStdErr=false)
Changes the text color in the console for the text written from now on.
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...
TConsoleColor
For use in setConsoleColor.
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
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
Enumeration of available verbosity levels.
std::function< void(const std::string &msg, const mrpt::system::VerbosityLevel level, const std::string &loggerName, const mrpt::Clock::time_point ×tamp)> output_logger_callback_t
Callback types for use with mrpt::system::COuputLogger.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Struct responsible of holding information relevant to the message (in std::string form) issued by the...
~TMsg()
Default Destructor.
VerbosityLevel level
Verbosity level of the message.
std::string getAsString() const
Return a string representation of the underlying message.
TMsg(const mrpt::system::VerbosityLevel level, const std::string &msg, const COutputLogger &logger)
Class constructor that passes a message in std::string form as well as a reference to the COutputLogg...
std::string name
Name of the COutputLogger instance that called registered the message.
void writeToStream(std::ostream &out) const
Write the message contents to the specified stream.
void dumpToConsole() const
Dump the message contents to the standard output.
std::string body
Actual content of the message.
mrpt::Clock::time_point timestamp
Timestamp of the message.