Skip to content

Commit

Permalink
Refactor: Added App_Log() to the libdeng2 C wrapper, use it everywhere
Browse files Browse the repository at this point in the history
App_Log() works the same way as the LOG_* macros: a newline is
automatically added (if there isn’t one). Replaced applicable use of
LogBuffer_Printf() with App_Log() calls.
  • Loading branch information
skyjake committed Jan 12, 2014
1 parent cedf067 commit ad93df0
Show file tree
Hide file tree
Showing 30 changed files with 253 additions and 247 deletions.
2 changes: 1 addition & 1 deletion doomsday/client/src/con_main.cpp
Expand Up @@ -1023,7 +1023,7 @@ void Con_PrintRuler(void)
if(ConsoleSilent)
return;

LogBuffer_Msg(_E(R) "\n");
LOG_MSG(_E(R));
}

/// @param flags @ref consolePrintFlags
Expand Down
10 changes: 0 additions & 10 deletions doomsday/client/src/dd_plugin.cpp
Expand Up @@ -207,16 +207,6 @@ DENG_EXTERN_C int Plug_RemoveHook(int hookType, hookfunc_t hook)
{
int i, type = HOOKMASK(hookType);

/*
if(currentPlugin)
{
LogBuffer_Printf(DE2_LOG_WARNING,
"Plug_RemoveHook: Failed to remove hook %p of type %i; currently processing a hook.\n",
hook, hookType);
return false;
}
*/

// The type must be good.
if(type < 0 || type >= NUM_HOOK_TYPES)
return false;
Expand Down
8 changes: 4 additions & 4 deletions doomsday/client/src/library.cpp
Expand Up @@ -77,12 +77,12 @@ void Library_Shutdown(void)
void Library_ReleaseGames(void)
{
#ifdef UNIX
LOG_AS("Library_ReleaseGames");
foreach(Library* lib, loadedLibs)
{
if(lib->isGamePlugin)
{
LogBuffer_Printf(DE2_LOG_DEBUG,
"Library_ReleaseGames: Closing '%s'\n", Str_Text(lib->path));
LOGDEV_RES_VERBOSE("Closing '%s'") << Str_Text(lib->path);

// Close the Library.
DENG_ASSERT(lib->file);
Expand All @@ -99,8 +99,7 @@ static void reopenLibraryIfNeeded(Library* lib)

if(!lib->file->loaded())
{
LogBuffer_Printf(DE2_LOG_DEBUG,
"reopenLibraryIfNeeded: Opening '%s'\n", Str_Text(lib->path));
LOGDEV_RES_XVERBOSE("Re-opening '%s'") << Str_Text(lib->path);

// Make sure the Library gets opened again now.
lib->file->library();
Expand Down Expand Up @@ -227,6 +226,7 @@ void* Library_Symbol(Library* lib, const char* symbolName)
{
try
{
LOG_AS("Library_Symbol");
DENG_ASSERT(lib);
#ifdef UNIX
reopenLibraryIfNeeded(lib);
Expand Down
1 change: 0 additions & 1 deletion doomsday/client/src/render/rend_font.cpp
Expand Up @@ -174,7 +174,6 @@ void FR_SetFont(fontid_t num)
}
catch(ResourceSystem::UnknownFontIdError const &)
{}
//LogBuffer_Printf(DE2_LOG_WARNING, "Requested invalid font %i.\n", num);
}
else
{
Expand Down
26 changes: 13 additions & 13 deletions doomsday/libdeng1/src/memoryzone.c
Expand Up @@ -163,7 +163,7 @@ static memvolume_t *createVolume(size_t volumeSize)

unlockZone();

LogBuffer_Printf(DE2_LOG_MESSAGE,
App_Log(DE2_LOG_MESSAGE,
"Created a new %.1f MB memory volume.\n", vol->size / 1024.0 / 1024.0);

Z_CheckHeap();
Expand Down Expand Up @@ -208,7 +208,7 @@ void Z_Shutdown(void)
M_Free(vol);
}

LogBuffer_Printf(DE2_LOG_NOTE,
App_Log(DE2_LOG_NOTE,
"Z_Shutdown: Used %i volumes, total %u bytes.\n", numVolumes, totalMemory);

Sys_DestroyMutex(zoneMutex);
Expand Down Expand Up @@ -259,7 +259,7 @@ static void freeBlock(void *ptr, memblock_t **tracked)
{
unlockZone();
DENG_ASSERT(block->id == LIBDENG_ZONEID);
LogBuffer_Printf(DE2_LOG_WARNING,
App_Log(DE2_LOG_WARNING,
"Attempted to free pointer without ZONEID.\n");
return;
}
Expand Down Expand Up @@ -445,7 +445,7 @@ void *Z_Malloc(size_t size, int tag, void *user)

if(tag < PU_APPSTATIC || tag > PU_PURGELEVEL)
{
LogBuffer_Printf(DE2_LOG_WARNING, "Z_Malloc: Invalid purgelevel %i, cannot allocate memory.\n", tag);
App_Log(DE2_LOG_WARNING, "Z_Malloc: Invalid purgelevel %i, cannot allocate memory.\n", tag);
return NULL;
}
if(!size)
Expand Down Expand Up @@ -558,7 +558,7 @@ void *Z_Malloc(size_t size, int tag, void *user)
{
// Scanned all the way through, no suitable space found.
gotoNextVolume = true;
LogBuffer_Printf(DE2_LOG_DEBUG,
App_Log(DE2_LOG_DEBUG,
"Z_Malloc: gave up on volume after %i checks\n", numChecked);
break;
}
Expand Down Expand Up @@ -676,7 +676,7 @@ void Z_FreeTags(int lowTag, int highTag)
memvolume_t *volume;
memblock_t *block, *next;

LogBuffer_Printf(DE2_LOG_DEBUG,
App_Log(DE2_LOG_DEBUG,
"MemoryZone: Freeing all blocks in tag range:[%i, %i)\n",
lowTag, highTag+1);

Expand Down Expand Up @@ -711,7 +711,7 @@ void Z_CheckHeap(void)
memblock_t *block;
dd_bool isDone;

LogBuffer_Printf(DE2_LOG_TRACE, "Z_CheckHeap\n");
App_Log(DE2_LOG_TRACE, "Z_CheckHeap\n");

lockZone();

Expand All @@ -722,7 +722,7 @@ void Z_CheckHeap(void)
// Validate the counter.
if(allocatedMemoryInVolume(volume) != volume->allocatedBytes)
{
LogBuffer_Printf(DE2_LOG_CRITICAL,
App_Log(DE2_LOG_CRITICAL,
"Z_CheckHeap: allocated bytes counter is off (counter:%u != actual:%u)\n",
volume->allocatedBytes, allocatedMemoryInVolume(volume));
App_FatalError("Z_CheckHeap: zone book-keeping is wrong");
Expand All @@ -736,7 +736,7 @@ void Z_CheckHeap(void)
}
if(total != volume->size - sizeof(memzone_t))
{
LogBuffer_Printf(DE2_LOG_CRITICAL,
App_Log(DE2_LOG_CRITICAL,
"Z_CheckHeap: invalid total size of blocks (%u != %u)\n",
total, volume->size - sizeof(memzone_t));
App_FatalError("Z_CheckHeap: zone book-keeping is wrong");
Expand All @@ -746,7 +746,7 @@ void Z_CheckHeap(void)
block = volume->zone->blockList.prev;
if((byte *)block - ((byte *)volume->zone + sizeof(memzone_t)) + block->size != volume->size - sizeof(memzone_t))
{
LogBuffer_Printf(DE2_LOG_CRITICAL,
App_Log(DE2_LOG_CRITICAL,
"Z_CheckHeap: last block does not cover the end (%u != %u)\n",
(byte *)block - ((byte *)volume->zone + sizeof(memzone_t)) + block->size,
volume->size - sizeof(memzone_t));
Expand Down Expand Up @@ -820,7 +820,7 @@ void Z_ChangeTag2(void *ptr, int tag)

if(tag >= PU_PURGELEVEL && PTR2INT(block->user) < 0x100)
{
LogBuffer_Printf(DE2_LOG_ERROR,
App_Log(DE2_LOG_ERROR,
"Z_ChangeTag: An owner is required for purgable blocks.\n");
}
else
Expand Down Expand Up @@ -1038,7 +1038,7 @@ void Z_PrintStatus(void)
size_t allocated = Z_AllocatedMemory();
size_t wasted = Z_FreeMemory();

LogBuffer_Printf(DE2_LOG_DEBUG,
App_Log(DE2_LOG_DEBUG,
"Memory zone status: %u volumes, %u bytes allocated, %u bytes free (%f%% in use)\n",
Z_VolumeCount(), (uint)allocated, (uint)wasted, (float)allocated/(float)(allocated+wasted)*100.f);
}
Expand All @@ -1060,7 +1060,7 @@ static void addBlockToSet(zblockset_t *set)
set->_blockCount++;
set->_blocks = Z_Recalloc(set->_blocks, sizeof(zblockset_block_t) * set->_blockCount, set->_tag);

LogBuffer_Printf(DE2_LOG_DEBUG,
App_Log(DE2_LOG_DEBUG,
"addBlockToSet: set=%p blockCount=%u elemSize=%u elemCount=%u (total=%u)\n",
set, set->_blockCount, (uint)set->_elementSize, set->_elementsPerBlock,
(uint)(set->_blockCount * set->_elementSize * set->_elementsPerBlock));
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libdeng1/src/reader.c
Expand Up @@ -78,7 +78,7 @@ static dd_bool Reader_Check(Reader const *reader, size_t len)
}
if(reader->pos > reader->size - len)
{
LogBuffer_Printf(DE2_LOG_ERROR,
App_Log(DE2_LOG_ERROR,
"Reader_Check: Position %lu[+%lu] out of bounds, size=%lu.\n",
(unsigned long) reader->pos,
(unsigned long) len,
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libdeng1/src/stack.c
Expand Up @@ -75,7 +75,7 @@ void *Stack_Pop(ddstack_t *s)

if(!s->height)
{
LogBuffer_Printf(DE2_LOG_DEBUG, "Stack::Pop: Underflow.\n");
App_Log(DE2_LOG_DEBUG, "Stack::Pop: Underflow.\n");
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions doomsday/libdeng1/src/writer.c
Expand Up @@ -89,7 +89,7 @@ static dd_bool Writer_Check(Writer const *writer, size_t len)
if((int)writer->pos <= (int)writer->size - (int)len)
return true;
}
LogBuffer_Printf(DE2_LOG_ERROR,
App_Log(DE2_LOG_ERROR,
"Writer_Check: Position %lu[+%lu] out of bounds, size=%lu, dynamic=%i.\n",
(unsigned long) writer->pos,
(unsigned long) len,
Expand Down Expand Up @@ -326,7 +326,7 @@ void Writer_WritePackedUInt16(Writer *writer, uint16_t v)
{
if(v & 0x8000)
{
LogBuffer_Printf(DE2_LOG_ERROR,
App_Log(DE2_LOG_ERROR,
"Writer_WritePackedUInt16: Cannot write %i (%x).\n", v, v);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libdeng2/include/de/c_wrapper.h
Expand Up @@ -54,6 +54,7 @@ extern "C" {
*/
DENG2_PUBLIC void App_Timer(unsigned int milliseconds, void (*callback)(void));
DENG2_PUBLIC void App_FatalError(char const *msg);
DENG2_PUBLIC void App_Log(unsigned int metadata, char const *format, ...);

/*
* CommandLine
Expand Down Expand Up @@ -229,7 +230,6 @@ logentry_metadata_t;
DENG2_PUBLIC void LogBuffer_EnableStandardOutput(int enable);
DENG2_PUBLIC void LogBuffer_Flush(void);
DENG2_PUBLIC void LogBuffer_Clear(void);
DENG2_PUBLIC void LogBuffer_Msg(char const *text); // note: manual newlines
DENG2_PUBLIC void LogBuffer_Printf(unsigned int metadata, char const *format, ...); // note: manual newlines

/*
Expand Down
99 changes: 61 additions & 38 deletions doomsday/libdeng2/src/c_wrapper.cpp
Expand Up @@ -33,6 +33,40 @@

#define DENG2_COMMANDLINE() DENG2_APP->commandLine()

static bool checkLogEntryMetadata(unsigned int &metadata)
{
// Automatically apply the generic domain if not specified.
if(!(metadata & de::LogEntry::DomainMask))
{
metadata |= de::LogEntry::Generic;
}

// Validate the level.
de::LogEntry::Level logLevel = de::LogEntry::Level(metadata & de::LogEntry::LevelMask);
if(logLevel < de::LogEntry::XVerbose || logLevel > de::LogEntry::Critical)
{
metadata &= ~de::LogEntry::LevelMask;
metadata |= de::LogEntry::Message;
}

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

static void logFragmentPrinter(duint32 metadata, char const *fragment)
{
static std::string currentLogLine;

currentLogLine += fragment;

std::string::size_type pos;
while((pos = currentLogLine.find('\n')) != std::string::npos)
{
LOG().enter(metadata, currentLogLine.substr(0, pos).c_str());
currentLogLine.erase(0, pos + 1);
}
}

void App_Timer(unsigned int milliseconds, void (*callback)(void))
{
de::Loop::timer(de::TimeDelta::fromMilliSeconds(milliseconds), callback);
Expand All @@ -43,6 +77,28 @@ void App_FatalError(char const *msg)
DENG2_APP->handleUncaughtException(msg);
}

void App_Log(unsigned int metadata, char const *format, ...)
{
if(!checkLogEntryMetadata(metadata)) return;

char buffer[0x2000];
va_list args;
va_start(args, format);
size_t nc = vsprintf(buffer, format, args); /// @todo unsafe
va_end(args);
DENG2_ASSERT(nc < sizeof(buffer) - 2);
if(!nc) return;

// Make sure there's a newline in the end.
if(buffer[nc - 1] != '\n')
{
buffer[nc++] = '\n';
buffer[nc] = 0;
}

logFragmentPrinter(metadata, buffer);
}

void CommandLine_Alias(char const *longname, char const *shortname)
{
DENG2_COMMANDLINE().alias(longname, shortname);
Expand Down Expand Up @@ -129,51 +185,18 @@ void LogBuffer_EnableStandardOutput(int enable)
de::LogBuffer::appBuffer().enableStandardOutput(enable != 0);
}

static void logFragmentPrinter(duint32 metadata, char const *fragment)
{
static std::string currentLogLine;

currentLogLine += fragment;

std::string::size_type pos;
while((pos = currentLogLine.find('\n')) != std::string::npos)
{
LOG().enter(metadata, currentLogLine.substr(0, pos).c_str());
currentLogLine.erase(0, pos + 1);
}
}

void LogBuffer_Msg(char const *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;
}

// Validate the level.
de::LogEntry::Level logLevel = de::LogEntry::Level(metadata & de::LogEntry::LevelMask);
if(logLevel < de::LogEntry::XVerbose || logLevel > de::LogEntry::Critical)
{
metadata &= ~de::LogEntry::LevelMask;
metadata |= (logLevel = de::LogEntry::Message);
}

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

char buffer[2048];
char buffer[0x2000];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args); /// @todo unsafe
size_t nc = vsprintf(buffer, format, args); /// @todo unsafe
va_end(args);
DENG2_ASSERT(nc < sizeof(buffer) - 1);

logFragmentPrinter(logLevel | (metadata & de::LogEntry::ContextMask), buffer);
logFragmentPrinter(metadata, buffer);
}

Info *Info_NewFromString(char const *utf8text)
Expand Down
4 changes: 2 additions & 2 deletions doomsday/plugins/common/src/d_netsv.c
Expand Up @@ -1618,7 +1618,7 @@ D_CMD(MapCycle)

if(!IS_SERVER)
{
LogBuffer_Printf(DE2_LOG_SCR_ERROR, "Only allowed for a server.\n");
App_Log(DE2_LOG_SCR_ERROR, "Only allowed for a server.\n");
return false;
}

Expand All @@ -1628,7 +1628,7 @@ D_CMD(MapCycle)
map = NetSv_ScanCycle(cycleIndex = 0, 0);
if(map < 0)
{
LogBuffer_Printf(DE2_LOG_SCR_ERROR, "MapCycle \"%s\" is invalid.\n", mapCycle);
App_Log(DE2_LOG_SCR_ERROR, "MapCycle \"%s\" is invalid.\n", mapCycle);
return false;
}
for(i = 0; i < MAXPLAYERS; ++i)
Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/common/src/fi_lib.c
Expand Up @@ -654,7 +654,7 @@ D_CMD(StartFinale)
return false;
if(!Def_Get(DD_DEF_FINALE, argv[1], &fin))
{
LogBuffer_Printf(DE2_LOG_SCR_ERROR, "Script '%s' is not defined.\n", argv[1]);
App_Log(DE2_LOG_SCR_ERROR, "Script '%s' is not defined.\n", argv[1]);
return false;
}
G_SetGameAction(GA_NONE);
Expand Down

0 comments on commit ad93df0

Please sign in to comment.