24 #include "MCThreadLocalData.hpp" 26 #ifndef __AIBO_BUILD__ 31 #include <boost/algorithm/string/find.hpp> 32 #include <boost/algorithm/string/join.hpp> 33 #include <boost/algorithm/string/predicate.hpp> 34 #include <boost/algorithm/string/split.hpp> 36 #include <boost/scoped_ptr.hpp> 44 static boost::scoped_ptr<MCLogMessageHandler> GlobalMessageHandler;
46 static bool GlobalDebug =
false;
56 void CheckStaticDebugVariables()
58 if (unlikely(!Debug.get()))
60 Debug.reset(
new bool);
62 DebugFilter.reset(
new MC::StringList);
63 DebugColorFilters.reset(
new MC::StringList);
74 CheckStaticDebugVariables();
83 GlobalDebug = new_status;
85 CheckStaticDebugVariables();
93 CheckStaticDebugVariables();
94 return boost::algorithm::join(*DebugFilter,
",");
100 CheckStaticDebugVariables();
101 boost::algorithm::split(*DebugFilter, new_filter, boost::is_any_of(
","));
107 CheckStaticDebugVariables();
108 return boost::algorithm::join(*DebugColorFilters,
",");
114 CheckStaticDebugVariables();
115 boost::algorithm::split(*DebugColorFilters, new_color_filter, boost::is_any_of(
","));
122 GlobalMessageHandler.reset(handler);
124 MessageHandler.reset(handler);
130 if (filters.empty() || function_name.empty())
133 std::string FunctionName = function_name;
134 MC::StringList FunctionNameParts;
138 iter_split(FunctionNameParts, FunctionName, boost::algorithm::first_finder(
"("));
140 iter_split(FunctionNameParts, FunctionNameParts[0], boost::algorithm::first_finder(
" "));
142 iter_split(FunctionNameParts, FunctionNameParts[FunctionNameParts.size()-1], boost::algorithm::first_finder(
"::"));
143 int CutNumber = (int)FunctionNameParts.size()-2;
145 for (
int i = 0; i < CutNumber; ++i)
147 FunctionNameParts.erase(FunctionNameParts.begin());
149 FunctionName = boost::algorithm::join(FunctionNameParts,
"::");
151 if (std::find(filters.begin(), filters.end(), FunctionName) != filters.end())
155 iter_split(FunctionNameParts, FunctionName, boost::algorithm::first_finder(
"::"));
156 if (FunctionNameParts.size() > 1)
157 return std::find(filters.begin(), filters.end(), FunctionNameParts[0]) != filters.end();
159 printf(
"boost::algorithm::split in MCLog::IsAnyFilterMatched FAILED\n");
168 if (!
function || !format)
174 (!Debug.get() || !*Debug || (DebugFilter.get() && !DebugFilter->empty() &&
181 va_start(ap, format);
183 if (GlobalMessageHandler.get())
185 GlobalMessageHandler->Message(
MCLog::Normal,
function, format, ap);
189 if (MessageHandler.get())
191 MessageHandler->Message(
MCLog::Normal,
function, format, ap);
195 std::string TempStr(
function);
196 #if defined(__ANDROID__) 197 char OtherTempStr[512];
199 vsnprintf(OtherTempStr,
sizeof(OtherTempStr), format, ap);
200 qCritical(
"%s", OtherTempStr);
205 if (TempStr.find(
'(') != std::string::npos)
207 MC::StringList FunctionNameSplitParts;
210 boost::algorithm::split(FunctionNameSplitParts, TempStr, boost::is_any_of(
"("));
212 printf(
"boost::algorithm::split FAILED\n");
215 TempStr = FunctionNameSplitParts[0];
217 const char* color_str = MC_ANSI_BLACK;
220 color_str = MC_ANSI_GREEN;
222 fprintf(stdout,
"%s%45s%s - ", color_str, TempStr.c_str(), MC_ANSI_COLOR_END);
223 vfprintf(stdout, format, ap);
224 fprintf(stdout,
"\n");
231 if (!
function || !format)
236 va_start(ap, format);
237 if (MessageHandler.get())
239 MessageHandler->Message(
MCLog::Error,
function, format, ap);
241 #ifdef __AIBO_BUILD__ 247 std::string TempStr(
function);
248 #if defined(__ANDROID__) 249 char OtherTempStr[512];
252 vsnprintf(OtherTempStr,
sizeof(OtherTempStr), format, ap);
253 qCritical(
"%s", OtherTempStr);
258 if (TempStr.find(
'(') != std::string::npos)
260 MC::StringList FunctionNameSplitParts;
263 boost::algorithm::split(FunctionNameSplitParts, TempStr, boost::is_any_of(
"("));
265 printf(
"boost::algorithm::split FAILED\n");
268 TempStr = FunctionNameSplitParts[0];
271 fprintf(stdout,
"%s%45s%s - ", MC_ANSI_RED, TempStr.c_str(), MC_ANSI_COLOR_END);
272 vfprintf(stdout, format, ap);
273 fprintf(stdout,
"\n");
281 if (!
function || !format)
285 va_start(ap, format);
287 if (MessageHandler.get())
293 std::string TempStr(
function);
294 #if defined(__ANDROID__) 295 char OtherTempStr[512];
297 vsnprintf(OtherTempStr,
sizeof(OtherTempStr), format, ap);
298 qCritical(
"%s", OtherTempStr);
302 if (TempStr.find(
'(') != std::string::npos)
304 MC::StringList FunctionNameSplitParts;
307 boost::algorithm::split(FunctionNameSplitParts, TempStr, boost::is_any_of(
"("));
309 printf(
"boost::algorithm::split FAILED\n");
312 TempStr = FunctionNameSplitParts[0];
314 fprintf(stdout,
"%s%45s%s - ", MC_ANSI_PURPLE, TempStr.c_str(), MC_ANSI_COLOR_END);
315 vfprintf(stdout, format, ap);
316 fprintf(stdout,
"\n");
327 va_start(ap, format);
328 #if defined(__ANDROID__) 329 char OtherTempStr[4096];
331 vsnprintf(OtherTempStr,
sizeof(OtherTempStr), format, ap);
332 qCritical(
"%s", OtherTempStr);
337 vfprintf(stdout, format, ap);
342 std::string TempStr(format);
343 bool NewLine =
false;
345 if (TempStr.c_str()[TempStr.size()-1] ==
'\n')
348 TempStr.erase(TempStr.size()-1, 1);
353 char OtherTempStr[256];
357 snprintf(OtherTempStr,
sizeof(OtherTempStr),
"\033[1;%dm%s\033[0;39m", (
int)color, TempStr.c_str());
359 snprintf(OtherTempStr,
sizeof(OtherTempStr),
"\033[;%dm%s\033[0;39m", (
int)color, TempStr.c_str());
361 TempStr = OtherTempStr;
363 vfprintf(stdout, TempStr.c_str(), ap);
367 fprintf(stdout,
"\n");
373 #ifndef __AIBO_BUILD__ 376 if (!orig_str.contains(
'\033'))
379 QString NewStr(orig_str);
383 QString NormalCodeStr = QString(
"\033[;%1m").arg(QString::number(i));
384 QString BoldCodeStr = QString(
"\033[1;%1m").arg(QString::number(i));
388 NewStr.replace(NormalCodeStr,
"<span style=\"color:black\">");
389 NewStr.replace(BoldCodeStr,
"<span style=\"color:black;font-weight:bold\">");
393 NewStr.replace(NormalCodeStr,
"<span style=\"color:#ff363c\">");
394 NewStr.replace(BoldCodeStr,
"<span style=\"color:#ff363c;font-weight:bold\">");
398 NewStr.replace(NormalCodeStr,
"<span style=\"color:#389e0b\">");
399 NewStr.replace(BoldCodeStr,
"<span style=\"color:#389e0b;font-weight:bold\">");
403 NewStr.replace(NormalCodeStr,
"<span style=\"color:yellow\">");
404 NewStr.replace(BoldCodeStr,
"<span style=\"color:yellow;font-weight:bold\">");
408 NewStr.replace(NormalCodeStr,
"<span style=\"color:blue\">");
409 NewStr.replace(BoldCodeStr,
"<span style=\"color:blue;font-weight:bold\">");
413 NewStr.replace(NormalCodeStr,
"<span style=\"color:purple\">");
414 NewStr.replace(BoldCodeStr,
"<span style=\"color:purple;font-weight:bold\">");
418 NewStr.replace(NormalCodeStr,
"<span style=\"color:cyan\">");
419 NewStr.replace(BoldCodeStr,
"<span style=\"color:cyan;font-weight:bold\">");
423 NewStr.replace(NormalCodeStr,
"<span style=\"color:white\">");
424 NewStr.replace(BoldCodeStr,
"<span style=\"color:white;font-weight:bold\">");
427 NewStr.replace(
"\033[0;39m",
"</span>");
434 QString NewStr(orig_str);
437 NewStr.remove(
"<b>");
438 NewStr.remove(
"</b>");
439 NewStr.remove(
"</span>");
440 NewStr.remove(QRegExp(
"<*>", Qt::CaseInsensitive, QRegExp::Wildcard));
450 const int BufferSize = 1024;
451 char Buffer[BufferSize];
453 Position += vsnprintf(&Buffer[Position], BufferSize-Position, format, arg_list);
454 snprintf(&Buffer[Position], BufferSize-Position,
"\n");
455 printf(
"%s", Buffer);
458 #ifdef __AIBO_BUILD__ 467 #ifndef __AIBO_BUILD__ 471 const int BufferSize = 1024;
472 char Buffer[BufferSize];
474 vsnprintf(&Buffer[0], BufferSize, format, arg_list);
481 qWarning() << Buffer;
virtual void Message(MCLog::MessageType message_type, const char *function, const char *format, va_list arg_list) override
Print a log message.
static void ErrorMessage(const char *function, const char *format,...) CLANG_ANALYZER_NORETURN
Print error message with red color.
static QString RemoveMarkup(const QString &orig_str)
Remove the markup.
static QString ConvertFromAnsiToQtMarkup(const QString &orig_str)
Convert the ANSI codes to Qt markup format.
static void SetCustomHandler(MCLogMessageHandler *handler, bool global=false)
Set custom message handler.
static void SetDebugStatus(bool new_status, bool global=false)
Set debug status.
static std::string GetDebugColorFilters()
Get debug color filter.
#define MC_UNUSED(a)
Helper macro to avoid compiler warning about unused function parameters.
static void FormattedMessage(MCLog::ColorType color, bool bold, const char *format,...)
Print normal message.
static std::string GetDebugFilter()
Get debug filter.
static void LogMessage(const char *function, const char *format,...)
Print log message.
static bool GetDebugStatus(bool global=false)
Get debug status.
static void WarningMessage(const char *function, const char *format,...)
Print warning message with yellow color.
A wrapper class to cover boost::thread_specific_ptr/folly::ThreadLocal API on certain targets...
virtual void Message(MCLog::MessageType message_type, const char *function, const char *format, va_list arg_list) override
Print a log message.
static void SetDebugFilter(const std::string &new_filter)
Set debug filter.
static bool IsAnyFilterMatched(const MC::StringList &filters, const std::string &function_name)
Check if any filter matches with the function name.
static void SetDebugColorFilters(const std::string &new_color_filters)
Set debug color filters.