pyGAP / game / engine / common / log.cpp

#if defined(DEBUG)


#include "common/log.h"


#include <stdarg.h>
#include <cstring>
#include <cstdio>

namespace common {


#define LOG_BUFFER_SIZE     256


Log::log_map_t    Log::allocated_;


Log::Log(const char* context) :
    context_length_(0),
    threshold_(Log::LOG_DEBUG)
{
    buffer_ = new char [LOG_BUFFER_SIZE];

    context_length_ = strlen(context);
    if (context_length_ > LOG_BUFFER_SIZE)
        context_length_ = LOG_BUFFER_SIZE / 2;

    sprintf(buffer_, "[%s] ", context);
    context_length_ = strlen(buffer_);
}


Log::~Log() {
    delete buffer_;
}


Log* Log::get(const char* context) {
    log_map_t::iterator it = allocated_.find(context);
    if (it != allocated_.end())
        return it->second;

    Log* log = new Log(context);
    allocated_[context] = log;
    return log;
}


void Log::clear() {
    for (log_map_t::iterator it = allocated_.begin(); it != allocated_.end(); ++it)
        delete it->second;
    allocated_.clear();
}


void Log::write(Log::log_level_e log_level, const char* format, ...) {
    if (threshold_ > log_level)
        return;

    switch (log_level) {
        case LOG_DEBUG:
            printf("    ");
            break;
        case LOG_INFO:
            printf("-I- ");
            break;
        case LOG_WARNING:
            printf("+W+ ");
            break;
        case LOG_ERROR:
            printf("*E* ");
            break;
    }

    va_list arg_list;
    va_start(arg_list, format);
    vsnprintf(buffer_ + context_length_,
              LOG_BUFFER_SIZE - context_length_,
              format, arg_list);
    va_end(arg_list);

    printf("%s", buffer_);

    fflush(stdout);
}


} /* namespace common */


#endif // defined(DEBUG)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.