summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/KM_log.cpp138
-rwxr-xr-xsrc/KM_log.h65
2 files changed, 129 insertions, 74 deletions
diff --git a/src/KM_log.cpp b/src/KM_log.cpp
index 0c4a3bf..b2e9bde 100755
--- a/src/KM_log.cpp
+++ b/src/KM_log.cpp
@@ -76,6 +76,19 @@ Kumu::DefaultLogSink()
return *s_DefaultLogSink;
}
+
+//------------------------------------------------------------------------------------------
+//
+
+void
+Kumu::EntryListLogSink::WriteEntry(const LogEntry& Entry)
+{
+ AutoMutex L(m_Lock);
+
+ if ( Entry.TestFilter(m_filter) )
+ m_Target.push_back(Entry);
+}
+
//------------------------------------------------------------------------------------------
//
@@ -84,8 +97,12 @@ Kumu::StdioLogSink::WriteEntry(const LogEntry& Entry)
{
AutoMutex L(m_Lock);
std::string buf;
- if ( Entry.CreateStringWithFilter(m_filter, buf) )
- fputs(buf.c_str(), m_stream);
+
+ if ( Entry.TestFilter(m_filter) )
+ {
+ Entry.CreateStringWithOptions(buf, m_options);
+ fputs(buf.c_str(), m_stream);
+ }
}
//---------------------------------------------------------------------------------
@@ -97,8 +114,12 @@ Kumu::WinDbgLogSink::WriteEntry(const LogEntry& Entry)
{
AutoMutex L(m_Lock);
std::string buf;
- if ( Entry.CreateStringWithFilter(m_filter, buf) )
- ::OutputDebugString(buf.c_str());
+
+ if ( Entry.TestFilter(m_filter) )
+ {
+ Entry.CreateStringWithOptions(buf, m_options);
+ ::OutputDebugString(buf.c_str());
+ }
}
#endif
@@ -112,84 +133,109 @@ Kumu::StreamLogSink::WriteEntry(const LogEntry& Entry)
{
AutoMutex L(m_Lock);
std::string buf;
- if ( Entry.CreateStringWithFilter(m_filter, buf) )
- write(m_fd, buf.c_str(), buf.size());
+
+ if ( Entry.TestFilter(m_filter) )
+ {
+ Entry.CreateStringWithOptions(buf, m_options);
+ write(m_fd, buf.c_str(), buf.size());
+ }
}
#endif
//------------------------------------------------------------------------------------------
+//
bool
-Kumu::LogEntry::CreateStringWithFilter(i32_t filter, std::string& out_buf) const
+Kumu::LogEntry::TestFilter(i32_t filter) const
{
- const char* p = 0;
-
- switch ( Type )
+ switch ( Type )
{
case LOG_CRIT:
- if ( (filter & LOG_ALLOW_CRIT) != 0 )
- p = " CRT";
+ if ( (filter & LOG_ALLOW_CRIT) == 0 )
+ return false;
break;
case LOG_ALERT:
- if ( (filter & LOG_ALLOW_ALERT) != 0 )
- p = " ALR";
+ if ( (filter & LOG_ALLOW_ALERT) == 0 )
+ return false;
break;
case LOG_NOTICE:
- if ( (filter & LOG_ALLOW_NOTICE) != 0 )
- p = " NTC";
+ if ( (filter & LOG_ALLOW_NOTICE) == 0 )
+ return false;
break;
case LOG_ERROR:
- if ( (filter & LOG_ALLOW_ERROR) != 0 )
- p = " ERR";
+ if ( (filter & LOG_ALLOW_ERROR) == 0 )
+ return false;
break;
case LOG_WARN:
- if ( (filter & LOG_ALLOW_WARN) != 0 )
- p = " WRN";
+ if ( (filter & LOG_ALLOW_WARN) == 0 )
+ return false;
break;
case LOG_INFO:
- if ( (filter & LOG_ALLOW_INFO) != 0 )
- p = " INF";
+ if ( (filter & LOG_ALLOW_INFO) == 0 )
+ return false;
break;
case LOG_DEBUG:
- if ( (filter & LOG_ALLOW_DEBUG) != 0 )
- p = " DBG";
+ if ( (filter & LOG_ALLOW_DEBUG) == 0 )
+ return false;
break;
- default:
- if ( (filter & LOG_ALLOW_DEFAULT) != 0 )
- p = " DFL";
- break;
}
- if ( p == 0 )
- return false;
-
- char buf[64];
- out_buf = "[";
-
- if ( (filter & LOG_ALLOW_TIMESTAMP) != 0 )
- {
- Timestamp Now;
- out_buf += Now.EncodeString(buf, 64);
+ return true;
+}
- if ( (filter & LOG_ALLOW_PID) != 0 )
- out_buf += " ";
- }
+//
+std::string&
+Kumu::LogEntry::CreateStringWithOptions(std::string& out_buf, i32_t opt) const
+{
+ out_buf.clear();
- if ( (filter & LOG_ALLOW_PID) != 0 )
+ if ( opt != 0 )
{
- snprintf(buf, 64, "%d", PID);
- out_buf += buf;
+ char buf[64];
+
+ if ( (opt & LOG_OPTION_TIMESTAMP) != 0 )
+ {
+ Timestamp Now;
+ out_buf += Now.EncodeString(buf, 64);
+ }
+
+ if ( (opt & LOG_OPTION_PID) != 0 )
+ {
+ if ( ! out_buf.empty() ) out_buf += " ";
+ snprintf(buf, 64, "%d", PID);
+ out_buf += buf;
+ }
+
+ if ( (opt & LOG_OPTION_TYPE) != 0 )
+ {
+ if ( ! out_buf.empty() ) out_buf += " ";
+
+ switch ( Type )
+ {
+ case LOG_CRIT: out_buf += "CRT"; break;
+ case LOG_ALERT: out_buf += "ALR"; break;
+ case LOG_NOTICE: out_buf += "NTC"; break;
+ case LOG_ERROR: out_buf += "ERR"; break;
+ case LOG_WARN: out_buf += "WRN"; break;
+ case LOG_INFO: out_buf += "INF"; break;
+ case LOG_DEBUG: out_buf += "DBG"; break;
+ default: out_buf += "DFL"; break;
+ }
+ }
+
+ out_buf.insert(0, "[");
+ out_buf += "]: ";
}
- out_buf += "] " + Msg;
- return true;
+ out_buf += Msg;
+ return out_buf;
}
diff --git a/src/KM_log.h b/src/KM_log.h
index 58c1b8f..6da77df 100755
--- a/src/KM_log.h
+++ b/src/KM_log.h
@@ -90,22 +90,24 @@ namespace Kumu
// OR these values together to come up with sink filter flags.
- // The default mask is 0x0000ffff (no time stamp, no pid, all messages).
- const i32_t LOG_ALLOW_TIMESTAMP = 0x01000000;
- const i32_t LOG_ALLOW_PID = 0x02000000;
-
- const i32_t LOG_ALLOW_DEBUG = 0x00000001;
- const i32_t LOG_ALLOW_INFO = 0x00000002;
- const i32_t LOG_ALLOW_WARN = 0x00000004;
- const i32_t LOG_ALLOW_ERROR = 0x00000008;
- const i32_t LOG_ALLOW_NOTICE = 0x00000010;
- const i32_t LOG_ALLOW_ALERT = 0x00000020;
- const i32_t LOG_ALLOW_CRIT = 0x00000040;
-
- const i32_t LOG_ALLOW_DEFAULT = 0x00008000; // show messages having an unknown type
- const i32_t LOG_ALLOW_NONE = 0x00000000;
- const i32_t LOG_ALLOW_MESSAGES = 0x0000ffff;
- const i32_t LOG_ALLOW_ANY = 0xffffffff;
+ // The default mask is LOG_ALLOW_ALL (all messages).
+ const i32_t LOG_ALLOW_DEBUG = 0x00000001;
+ const i32_t LOG_ALLOW_INFO = 0x00000002;
+ const i32_t LOG_ALLOW_WARN = 0x00000004;
+ const i32_t LOG_ALLOW_ERROR = 0x00000008;
+ const i32_t LOG_ALLOW_NOTICE = 0x00000010;
+ const i32_t LOG_ALLOW_ALERT = 0x00000020;
+ const i32_t LOG_ALLOW_CRIT = 0x00000040;
+ const i32_t LOG_ALLOW_NONE = 0x00000000;
+ const i32_t LOG_ALLOW_ALL = 0x000fffff;
+
+ // options are used to control display format
+ // default is 0 (
+ const i32_t LOG_OPTION_TYPE = 0x01000000;
+ const i32_t LOG_OPTION_TIMESTAMP = 0x02000000;
+ const i32_t LOG_OPTION_PID = 0x04000000;
+ const i32_t LOG_OPTION_NONE = 0x00000000;
+ const i32_t LOG_OPTION_ALL = 0xfff00000;
// A log message with environmental metadata
class LogEntry : public IArchive
@@ -120,7 +122,12 @@ namespace Kumu
LogEntry(ui32_t pid, LogType_t t, const char* m) : PID(pid), Type(t), Msg(m) { assert(m); }
virtual ~LogEntry() {}
- bool CreateStringWithFilter(i32_t, std::string&) const;
+ // returns true if the message Type is present in the mask
+ bool TestFilter(i32_t mask_value) const;
+
+ // renders the message into outstr using the given dispaly options
+ // returns outstr&
+ std::string& CreateStringWithOptions(std::string& outstr, i32_t mask_value) const;
// IArchive
bool HasValue() const { return ! Msg.empty(); }
@@ -137,15 +144,19 @@ namespace Kumu
{
protected:
i32_t m_filter;
+ i32_t m_options;
public:
+ ILogSink() : m_filter(LOG_ALLOW_ALL), m_options(LOG_OPTION_NONE) {}
virtual ~ILogSink() {}
- void SetFilterFlags(i32_t f) { m_filter = f; }
- i32_t GetFilterFlags() const { return m_filter; }
void SetFilterFlag(i32_t f) { m_filter |= f; }
void UnsetFilterFlag(i32_t f) { m_filter &= ~f; }
- bool TestFilterFlag(i32_t f) const { return ((m_filter & f) != 0); }
+ bool TestFilterFlag(i32_t f) const { return ((m_filter & f) == f); }
+
+ void SetOptionFlag(i32_t o) { m_options |= o; }
+ void UnsetOptionFlag(i32_t o) { m_options &= ~o; }
+ bool TestOptionFlag(i32_t o) const { return ((m_options & o) == o); }
// library messages
void Error(const char* fmt, ...) { LOG_MSG_IMPL(LOG_ERROR); }
@@ -216,21 +227,19 @@ namespace Kumu
}
};
- // collect log messages into the given list
+ // collect log messages into the given list, does not test filter
class EntryListLogSink : public ILogSink
{
+ Mutex m_Lock;
+ LogEntryList_t& m_Target;
KM_NO_COPY_CONSTRUCT(EntryListLogSink);
EntryListLogSink();
- LogEntryList_t& m_Target;
-
public:
EntryListLogSink(LogEntryList_t& target) : m_Target(target) {}
virtual ~EntryListLogSink() {}
- void WriteEntry(const LogEntry& Entry) {
- m_Target.push_back(Entry);
- }
+ void WriteEntry(const LogEntry& Entry);
};
@@ -242,8 +251,8 @@ namespace Kumu
KM_NO_COPY_CONSTRUCT(StdioLogSink);
public:
- StdioLogSink() : m_stream(stderr) {};
- StdioLogSink(FILE* stream) : m_stream(stream) {}
+ StdioLogSink() : m_stream(stderr) {}
+ StdioLogSink(FILE* stream) : m_stream(stream) {}
virtual ~StdioLogSink() {}
void WriteEntry(const LogEntry&);