Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ngscopeclient/logtools
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 5b1cfa94b8f4
Choose a base ref
...
head repository: ngscopeclient/logtools
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a0efde10adb0
Choose a head ref
  • 3 commits
  • 4 files changed
  • 1 contributor

Commits on Mar 8, 2017

  1. Copy the full SHA
    9c48311 View commit details
  2. Copy the full SHA
    ad35060 View commit details
  3. Copy the full SHA
    a0efde1 View commit details
Showing with 72 additions and 17 deletions.
  1. +2 −2 FILELogSink.cpp
  2. +1 −1 STDLogSink.cpp
  3. +58 −9 log.cpp
  4. +11 −5 log.h
4 changes: 2 additions & 2 deletions FILELogSink.cpp
Original file line number Diff line number Diff line change
@@ -34,8 +34,8 @@ using namespace std;
// Construction / destruction

FILELogSink::FILELogSink(FILE *f, bool line_buffered, Severity min_severity)
: m_file(f)
, m_min_severity(min_severity)
: LogSink(min_severity)
, m_file(f)
{
if(line_buffered)
setvbuf(f, NULL, _IOLBF, 0);
2 changes: 1 addition & 1 deletion STDLogSink.cpp
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ extern bool g_logToStdoutAlways;
// Construction / destruction

STDLogSink::STDLogSink(Severity min_severity)
: m_min_severity(min_severity)
: LogSink(min_severity)
{
//Get the current display terminal width
#ifndef _WIN32
67 changes: 58 additions & 9 deletions log.cpp
Original file line number Diff line number Diff line change
@@ -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

@@ -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));
}
@@ -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;

@@ -267,29 +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) )
{
//C++ function. If we don't get here it's a C function, so use the entire function name in the log message.

//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);
cls = sfunc.substr(coff + 1, clen);
}

//Format final result
sfunc = cls + "::" + name;
//Global function
else
{
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)
16 changes: 11 additions & 5 deletions log.h
Original file line number Diff line number Diff line change
@@ -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>
@@ -55,15 +57,19 @@ enum class Severity
class LogSink
{
public:
LogSink()
LogSink(Severity min_severity = Severity::VERBOSE)
: m_indentSize(4)
, m_indentLevel(0)
, m_termWidth(120) //default if not using ioctls to check
, m_lastMessageWasNewline(true)
, m_min_severity(min_severity)
{}

virtual ~LogSink() {}

Severity GetSeverity()
{ return m_min_severity; }

/**
@brief Increase the indentation level
*/
@@ -111,6 +117,9 @@ class LogSink

/// @brief True if the last message ended in a \n character
bool m_lastMessageWasNewline;

/// @brief Minimum severity of messages to be printed
Severity m_min_severity;
};

/**
@@ -127,8 +136,6 @@ class STDLogSink : public LogSink

protected:
void Flush();

Severity m_min_severity;
};

/**
@@ -163,12 +170,11 @@ class FILELogSink : public LogSink

protected:
FILE *m_file;
Severity m_min_severity;

};

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