21 #define WIN32_LEAN_AND_MEAN 39 COutputLogger::logging_levels_to_colors[NUMBER_OF_VERBOSITY_LEVELS] = {
46 std::string COutputLogger::logging_levels_to_names[NUMBER_OF_VERBOSITY_LEVELS] =
59 COutputLogger::COutputLogger() { this->loggerReset(); }
60 COutputLogger::~COutputLogger() {}
61 void COutputLogger::logStr(
64 if (
level < m_min_verbosity_level)
return;
67 TMsg msg(
level, msg_str, *
this);
68 if (logging_enable_keep_record) m_history.push_back(msg);
70 if (logging_enable_console_output)
76 for (
const auto&
c : m_listCallbacks)
77 (*
c.func)(msg.body, msg.level, msg.name, msg.timestamp,
c.userParam);
80 void COutputLogger::logFmt(
81 const VerbosityLevel
level,
const char* fmt, ...)
const 93 std::string str = this->generateStringFromFormat(fmt, argp);
96 this->logStr(
level, str);
99 std::string COutputLogger::generateStringFromFormat(
100 const char* fmt, va_list argp)
const 102 int result = -1,
length = 1024;
113 if (result >=
length) result = -1;
120 void COutputLogger::logCond(
124 this->logStr(
level, msg_str);
129 m_logger_name =
name;
132 std::string COutputLogger::getLoggerName()
const {
return m_logger_name; }
133 void COutputLogger::setMinLoggingLevel(
134 const VerbosityLevel
level )
136 m_min_verbosity_level =
level;
138 void COutputLogger::setVerbosityLevel(
const VerbosityLevel
level)
140 m_min_verbosity_level =
level;
143 void COutputLogger::getLogAsString(
std::string& fname)
const 146 for (
const auto& h : m_history) fname += h.getAsString();
151 this->getLogAsString(str);
154 void COutputLogger::writeLogToFile(
165 fname = m_logger_name +
".log";
169 fstream.fileOpenCorrectly(),
171 "\n[%s:] Could not open external file: %s", m_logger_name.c_str(),
175 this->getLogAsString(hist_str);
176 fstream.printf(
"%s", hist_str.c_str());
180 void COutputLogger::dumpLogToConsole()
const 182 for (
const auto& h : m_history) h.dumpToConsole();
185 std::string COutputLogger::getLoggerLastMsg()
const 187 TMsg last_msg = m_history.back();
188 return last_msg.getAsString();
191 void COutputLogger::getLoggerLastMsg(
std::string& msg_str)
const 193 msg_str = this->getLoggerLastMsg();
196 void COutputLogger::loggerReset()
198 m_logger_name =
"log";
201 logging_enable_console_output =
true;
202 logging_enable_keep_record =
false;
206 m_min_verbosity_level = LVL_INFO;
212 COutputLogger::TMsg::TMsg(
214 const COutputLogger& logger)
218 name = logger.getLoggerName();
223 COutputLogger::TMsg::~TMsg() {}
232 std::string COutputLogger::TMsg::getAsString()
const 236 out <<
"[" <<
name <<
"|" << COutputLogger::logging_levels_to_names[
level]
238 if (!body.empty() && *body.rbegin() !=
'\n') out << std::endl;
242 void COutputLogger::TMsg::getAsString(
std::string* contents)
const 244 *contents = this->getAsString();
249 out.
printf(
"%s", str.c_str());
251 OutputDebugStringA(str.c_str());
254 void COutputLogger::TMsg::dumpToConsole()
const 258 const bool dump_to_cerr = (
level == LVL_ERROR);
266 (dump_to_cerr ? std::cerr : std::cout) << str;
275 void COutputLogger::logRegisterCallback(
276 output_logger_callback_t userFunc,
void* userParam)
281 cbe.userParam = userParam;
282 m_listCallbacks.insert(cbe);
285 void COutputLogger::logDeregisterCallback(
286 output_logger_callback_t userFunc,
void* userParam)
291 cbe.userParam = userParam;
292 m_listCallbacks.erase(cbe);
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
mrpt::system::TTimeStamp getCurrentTime()
Returns the current (UTC) system time.
void setConsoleColor(TConsoleColor color, bool changeStdErr=false)
Changes the text color in the console for the text written from now on.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
This CStream derived class allow using a file as a write-only, binary stream.
TConsoleColor
For use in setConsoleColor.
GLsizei const GLchar ** string
#define INVALID_TIMESTAMP
Represents an invalid timestamp, where applicable.
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...
GLuint GLsizei GLsizei * length
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.
GLuint const GLchar * name
#define ASSERTMSG_(f, __ERROR_MSG)
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.