Skip to content

Commit

Permalink
Refactor|Log|libdeng2: Always require a context domain for log entries
Browse files Browse the repository at this point in the history
This makes it simpler to check which levels and enabled, etc.
  • Loading branch information
skyjake committed Jan 8, 2014
1 parent fb03a38 commit d3be5fc
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 46 deletions.
4 changes: 2 additions & 2 deletions doomsday/client/src/ui/dialogs/logsettingsdialog.cpp
Expand Up @@ -140,7 +140,7 @@ DENG2_PIMPL(LogSettingsDialog)
parent->add(wgt.alert =
new VariableToggleWidget(tr("Alerts"), App::config()[String("alert.") + dom.name]));
wgt.alert->setActiveValue(LogEntry::Warning);
wgt.alert->setInactiveValue(LogEntry::MAX_LOG_LEVELS);
wgt.alert->setInactiveValue(LogEntry::HighestLogLevel + 1);

// Lay out the folding panel's contents.
if(parent == &fold->content())
Expand Down Expand Up @@ -180,7 +180,7 @@ DENG2_PIMPL(LogSettingsDialog)
for(uint i = 0; i < NUM_DOMAINS; ++i)
{
char const *name = domainText[i].name;
cfg.set(String("alert.") + name, int(alerts? LogEntry::Warning : LogEntry::MAX_LOG_LEVELS));
cfg.set(String("alert.") + name, int(alerts? LogEntry::Warning : (LogEntry::HighestLogLevel + 1)));
}
}

Expand Down
56 changes: 33 additions & 23 deletions doomsday/libdeng2/include/de/core/log.h
Expand Up @@ -258,30 +258,41 @@ class DENG2_PUBLIC LogEntry : public Lockable, public ISerializable
{
public:
/**
* Entry domain (bits) and target audience. If not set, the entry is generic and intended for
* the end-user/player.
* Entry domain (bits) and target audience. If not set, the entry is generic and
* intended for the end-user/player.
*/
enum Context
{
// Domains
Generic = 0, ///< Global domain.
Resource = 0x10000, /**< Resource or resource pack domain (files, etc.).
"Resource" is here meant in a wider sense of all the
external data that Doomsday utilizes. */
Map = 0x20000, /**< Map domain: information pertaining to the map and its
elements, playsim, etc. */
Script = 0x40000, ///< Script domain
GL = 0x80000, ///< Graphics/renderer domain (shaders, etc.)
Audio = 0x100000, ///< Audio domain
Input = 0x200000, ///< Input domain: events, devices, etc.
Network = 0x400000, ///< Network domain: connections, packets, etc.
FirstDomainBit = 16,
GenericBit = FirstDomainBit,
ResourceBit,
MapBit,
ScriptBit,
GLBit,
AudioBit,
InputBit,
NetworkBit,
LastDomainBit = NetworkBit,

Generic = (1 << GenericBit), ///< Global domain (bit automatically set if no other domains).
Resource = (1 << ResourceBit), /**< Resource or resource pack domain (files, etc.).
"Resource" is here meant in a wider sense of all the
external data that Doomsday utilizes. */
Map = (1 << MapBit), /**< Map domain: information pertaining to the map and its
elements, playsim, etc. */
Script = (1 << ScriptBit), ///< Script domain
GL = (1 << GLBit), ///< Graphics/renderer domain (shaders, etc.)
Audio = (1 << AudioBit), ///< Audio domain
Input = (1 << InputBit), ///< Input domain: events, devices, etc.
Network = (1 << NetworkBit), ///< Network domain: connections, packets, etc.

// User groups
Dev = 0x8000000, /**< Native code developer (i.e., the programmer); can be
combined with other flags to mark the entry for devs.
If bit is not set, the entry is for the end-user. */
Dev = 0x8000000, /**< Native code developer (i.e., the programmer); can be
combined with other flags to mark the entry for devs.
If bit is not set, the entry is for the end-user. */

AllDomains = 0x7f0000,
AllDomains = 0xff0000,
DomainMask = AllDomains,
ContextMask = 0xfff0000
};
Expand Down Expand Up @@ -310,7 +321,7 @@ class DENG2_PUBLIC LogEntry : public Lockable, public ISerializable
val |= Dev;
text = text.remove(text.size() - 3);
}
for(int i = 16; i < 32; ++i)
for(int i = FirstDomainBit; i <= LastDomainBit; ++i)
{
if(!contextToText(LogEntry::Context(1 << i)).compareWithoutCase(text))
return LogEntry::Context((1 << i) | val);
Expand Down Expand Up @@ -361,10 +372,9 @@ class DENG2_PUBLIC LogEntry : public Lockable, public ISerializable
*/
Critical = 7,

MAX_LOG_LEVELS,

LOWEST_LOG_LEVEL = XVerbose,
LevelMask = 0x7
LowestLogLevel = XVerbose,
HighestLogLevel = Critical,
LevelMask = 0x7
};

static String levelToText(duint32 level)
Expand All @@ -384,7 +394,7 @@ class DENG2_PUBLIC LogEntry : public Lockable, public ISerializable

static Level textToLevel(String text)
{
for(int i = XVerbose; i < MAX_LOG_LEVELS; ++i)
for(int i = XVerbose; i <= HighestLogLevel; ++i)
{
if(!levelToText(Level(i)).compareWithoutCase(text))
return Level(i);
Expand Down
8 changes: 7 additions & 1 deletion doomsday/libdeng2/src/c_wrapper.cpp
Expand Up @@ -145,11 +145,17 @@ static void logFragmentPrinter(duint32 metadata, char const *fragment)

void LogBuffer_Msg(char const *text)
{
logFragmentPrinter(de::LogEntry::Message, text);
logFragmentPrinter(de::LogEntry::Generic | de::LogEntry::Message, text);
}

void LogBuffer_Printf(unsigned int metadata, char const *format, ...)
{
// Automatically apply the generic domain if not specified.
if(!(metadata & de::LogEntry::DomainMask))
{
metadata |= de::LogEntry::Generic;
}

// If this level is not enabled, just ignore.
if(!de::LogBuffer::appBuffer().isEnabled(metadata)) return;

Expand Down
12 changes: 9 additions & 3 deletions doomsday/libdeng2/src/core/log.cpp
Expand Up @@ -260,7 +260,7 @@ String LogEntry::asText(Flags const &formattingFlags, int shortenSection) const
{
if(!flags.testFlag(Styled))
{
char const *levelNames[LogEntry::MAX_LOG_LEVELS] = {
char const *levelNames[] = {
"", // not used
"(vv)",
"(v)",
Expand All @@ -275,7 +275,7 @@ String LogEntry::asText(Flags const &formattingFlags, int shortenSection) const
}
else
{
char const *levelNames[LogEntry::MAX_LOG_LEVELS] = {
char const *levelNames[] = {
"", // not used
"XVerbose",
"Verbose",
Expand Down Expand Up @@ -571,8 +571,14 @@ void Log::disposeThreadLog()
LogEntryStager::LogEntryStager(duint32 metadata, String const &format)
: _metadata(metadata)
{
// Automatically set the Generic domain.
if(!(_metadata & LogEntry::DomainMask))
{
_metadata |= LogEntry::Generic;
}

_disabled = !LogBuffer::appBufferExists() ||
!LogBuffer::appBuffer().isEnabled(metadata);
!LogBuffer::appBuffer().isEnabled(_metadata);

if(!_disabled)
{
Expand Down
1 change: 1 addition & 0 deletions doomsday/libdeng2/src/core/logbuffer.cpp
Expand Up @@ -175,6 +175,7 @@ void LogBuffer::setEntryFilter(IFilter const *entryFilter)
bool LogBuffer::isEnabled(duint32 entryMetadata) const
{
DENG2_ASSERT(d->entryFilter != 0);
DENG2_ASSERT(entryMetadata & LogEntry::DomainMask); // must have a domain
return d->entryFilter->isLogEntryAllowed(entryMetadata);
}

Expand Down
23 changes: 9 additions & 14 deletions doomsday/libdeng2/src/core/logfilter.cpp
Expand Up @@ -53,22 +53,19 @@ DENG2_PIMPL_NOREF(LogFilter)
{
/// Filtering information for a domain.
struct Filter {
duint32 domainBit;
int domainBit;
LogEntry::Level minLevel;
bool allowDev;

Filter()
: domainBit(LogEntry::Generic)
: domainBit(LogEntry::GenericBit)
, minLevel(LogEntry::Message)
, allowDev(false)
{}

inline bool checkContextBit(duint32 md) const
{
int const mdDomain = md & LogEntry::DomainMask;
if(!domainBit && (!mdDomain || (mdDomain == LogEntry::AllDomains)))
return true; // Generic.
return (domainBit & md) != 0;
return (md & (1 << domainBit)) != 0;
}

void read(Record const &rec)
Expand All @@ -88,13 +85,10 @@ DENG2_PIMPL_NOREF(LogFilter)

Instance()
{
filterByContext[ResourceFilter].domainBit = LogEntry::Resource;
filterByContext[MapFilter].domainBit = LogEntry::Map;
filterByContext[ScriptFilter].domainBit = LogEntry::Script;
filterByContext[GLFilter].domainBit = LogEntry::GL;
filterByContext[AudioFilter].domainBit = LogEntry::Audio;
filterByContext[InputFilter].domainBit = LogEntry::Input;
filterByContext[NetworkFilter].domainBit = LogEntry::Network;
for(int i = 0; i < NUM_FILTERS; ++i)
{
filterByContext[i].domainBit = LogEntry::FirstDomainBit + i;
}
}

bool isLogEntryAllowed(duint32 md) const
Expand All @@ -116,7 +110,7 @@ DENG2_PIMPL_NOREF(LogFilter)

LogEntry::Level minLevel(duint32 md) const
{
int lev = LogEntry::MAX_LOG_LEVELS;
int lev = LogEntry::HighestLogLevel + 1;
for(uint i = 0; i < NUM_FILTERS; ++i)
{
Filter const &ftr = filterByContext[i];
Expand Down Expand Up @@ -203,6 +197,7 @@ LogFilter::LogFilter() : d(new Instance)

bool LogFilter::isLogEntryAllowed(duint32 metadata) const
{
DENG2_ASSERT(metadata & LogEntry::DomainMask); // must have a domain
return d->isLogEntryAllowed(metadata);
}

Expand Down
4 changes: 2 additions & 2 deletions doomsday/tests/test_log/main.cpp
Expand Up @@ -36,13 +36,13 @@ int main(int argc, char **argv)
bool devMode = j > 0;
app.logFilter().setAllowDev(devMode);

for(int i = LogEntry::LOWEST_LOG_LEVEL; i < LogEntry::MAX_LOG_LEVELS; ++i)
for(int i = LogEntry::LowestLogLevel; i <= LogEntry::HighestLogLevel; ++i)
{
LogEntry::Level level = LogEntry::Level(i);
app.logFilter().setMinLevel(level);
LOG_AT_LEVEL(level, "Enabled level %s with dev:%b") << LogEntry::levelToText(level) << devMode;

for(int k = LogEntry::LOWEST_LOG_LEVEL; k < LogEntry::MAX_LOG_LEVELS; ++k)
for(int k = LogEntry::LowestLogLevel; k <= LogEntry::HighestLogLevel; ++k)
{
for(int d = 0; d < 2; ++d)
{
Expand Down
2 changes: 1 addition & 1 deletion doomsday/tools/shell/shell-gui/src/linkwindow.cpp
Expand Up @@ -479,7 +479,7 @@ void LinkWindow::sendCommandToServer(de::String command)
if(d->link)
{
// Echo the command locally.
LogEntry *e = new LogEntry(LogEntry::Note, "", 0, ">",
LogEntry *e = new LogEntry(LogEntry::Generic | LogEntry::Note, "", 0, ">",
LogEntry::Args() << new LogEntry::Arg(command));
d->logBuffer.add(e);

Expand Down

0 comments on commit d3be5fc

Please sign in to comment.