Skip to content

Commit

Permalink
Initial implementation of filters for LogDebugTrace
Browse files Browse the repository at this point in the history
  • Loading branch information
azonenberg committed Mar 8, 2017
1 parent ad35060 commit a0efde1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
69 changes: 54 additions & 15 deletions log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ vector<unique_ptr<LogSink>> g_log_sinks;
//set this for STDLogSink to only write to stdout even for error/warning severity
bool g_logToStdoutAlways = false;

//Only print trace messages from classes in this set
set<string> g_trace_filters;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// String formatting

Expand Down Expand Up @@ -149,7 +152,8 @@ bool ParseLoggerArguments(
s == "-L" || s == "--logfile-lines")
{
bool line_buffered = (s == "-L" || s == "--logfile-lines");
if(i+1 < argc) {
if(i+1 < argc)
{
FILE *log = fopen(argv[++i], "wt");
g_log_sinks.emplace_back(new FILELogSink(log, line_buffered, console_verbosity));
}
Expand All @@ -158,6 +162,20 @@ bool ParseLoggerArguments(
printf("%s requires an argument\n", s.c_str());
}
}
else if(s == "--trace")
{
if(i+1 < argc)
{
string sfilter = argv[++i];
if(sfilter == "::")
sfilter = "";
g_trace_filters.emplace(sfilter);
}
else
{
printf("%s requires an argument\n", s.c_str());
}
}
else if(s == "--stdout-only")
g_logToStdoutAlways = true;

Expand Down Expand Up @@ -267,39 +285,60 @@ void LogDebugTrace(const char* function, const char *format, ...)
{
lock_guard<mutex> lock(g_log_mutex);

//Early out (for performance) if we don't have any debug-level sinks
bool has_debug_sinks = false;
for(auto &sink : g_log_sinks)
{
if(sink->GetSeverity() >= Severity::DEBUG)
{
has_debug_sinks = true;
break;
}
}
if(!has_debug_sinks)
return;

string sfunc(function);

//Class and function names
string cls;
string name = sfunc;

//Member function?
//Parse out "class::function" from PRETTY_FUNCTION which includes the return type and full arg list
//This normally gives us zillions of templates we dont need to see!
string sfunc(function);
size_t colpos = sfunc.find("::");
size_t poff = sfunc.find("(", colpos);
size_t coff = sfunc.rfind(" ", colpos);
if( (colpos != string::npos) && (poff != string::npos) && (coff != string::npos) )
{
//Get the function name
size_t namelen = poff - colpos - 2;
string name = sfunc.substr(colpos+2, namelen);
name = sfunc.substr(colpos+2, namelen);

//Get the class name
size_t clen = colpos - coff - 1;
string cls = sfunc.substr(coff + 1, clen);

//Format final result
sfunc = cls + "::" + name;
cls = sfunc.substr(coff + 1, clen);
}

//Global function
size_t soff = sfunc.find(" ");
poff = sfunc.find("(", soff);
if( (soff != string::npos) && (poff != string::npos) )
else
{
size_t namelen = poff - soff - 1;
string name = sfunc.substr(soff+1, namelen);

sfunc = string("::") + name;
size_t soff = sfunc.find(" ");
poff = sfunc.find("(", soff);
if( (soff != string::npos) && (poff != string::npos) )
{
size_t namelen = poff - soff - 1;
name = sfunc.substr(soff+1, namelen);
}
}

//TODO: Check if we match a global "things we want to log" filter
//Format final function name
sfunc = cls + "::" + name;

//Check if class name is in the "to log" list
if(g_trace_filters.find(cls) == g_trace_filters.end())
return;

va_list va;
for(auto &sink : g_log_sinks)
Expand Down
6 changes: 6 additions & 0 deletions log.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
#include <memory>
#include <string>
#include <vector>
#include <set>
#include <mutex>

#if defined(__MINGW32__) && !defined(__WINPTHREADS_VERSION)
// Include mingw-std-threads extra header
#include <mingw.mutex.h>
Expand Down Expand Up @@ -65,6 +67,9 @@ class LogSink

virtual ~LogSink() {}

Severity GetSeverity()
{ return m_min_severity; }

/**
@brief Increase the indentation level
*/
Expand Down Expand Up @@ -169,6 +174,7 @@ class FILELogSink : public LogSink

extern std::mutex g_log_mutex;
extern std::vector<std::unique_ptr<LogSink>> g_log_sinks;
extern std::set<std::string> g_trace_filters;

/**
@brief Scoping wrapper for log indentation
Expand Down

0 comments on commit a0efde1

Please sign in to comment.