X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FLog.cc;h=da7985d60115bff1bde22cc9557131f7059c6742;hp=a7cdae4a49e57604a6cdea4f678dba2d888aa2a7;hb=8c6a82feb045ac859b344cd7665a36ac00f4949e;hpb=e495074443d9fd7bc16137084cf9de3d031b75c4 diff --git a/src/Moof/Log.cc b/src/Moof/Log.cc index a7cdae4..da7985d 100644 --- a/src/Moof/Log.cc +++ b/src/Moof/Log.cc @@ -1,34 +1,15 @@ -/******************************************************************************* - - Copyright (c) 2009, Charles McGarvey - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -*******************************************************************************/ - -#include -#include // snprintf -#include // strcpy +/*] Copyright (c) 2009-2010, Charles McGarvey [************************** +**] All rights reserved. +* +* vi:ts=4 sw=4 tw=75 +* +* Distributable under the terms and conditions of the 2-clause BSD license; +* see the file COPYING for a complete text of the license. +* +**************************************************************************/ + +#include #include "Log.hh" #include "Script.hh" @@ -37,133 +18,56 @@ namespace Mf { -static LogLevel logLevel_ = LOG_WARNING; - -inline void printLog_(int logLevel, const char* fmt, va_list args) -{ - if (logLevel_ < logLevel) return; - - switch (logLevel) - { - case LOG_ERROR: - fprintf(stderr, " error: "); - break; - case LOG_WARNING: - fprintf(stderr, "warning: "); - break; - case LOG_SCRIPT: - fprintf(stderr, " script: "); - break; - case LOG_INFO: - fprintf(stderr, " info: "); - break; - case LOG_DEBUG: - fprintf(stderr, " debug: "); - break; - } - - vfprintf(stderr, fmt, args); - fprintf(stderr, "\n"); -} +Log::Level Log::gLevel = Log::INFO; -LogLevel setLogLevel(LogLevel level) +void Log::setLevel(Level level) { - if (level != 0) - logLevel_ = level; - - return logLevel_; + gLevel = level; } - -void logError(const char* fmt, ...) +Log::Level Log::level() { - va_list args; - va_start(args, fmt); - - printLog_(LOG_ERROR, fmt, args); - - va_end(args); + return gLevel; } -void logWarning(const char* fmt, ...) -{ - va_list args; - va_start(args, fmt); - printLog_(LOG_WARNING, fmt, args); +std::ostream& log(std::clog); - va_end(args); -} +static std::ofstream nullLog_; +std::ostream& nullLog(nullLog_); -void logInfo(const char* fmt, ...) -{ - va_list args; - va_start(args, fmt); - - printLog_(LOG_INFO, fmt, args); - - va_end(args); -} - -void logDebug(const char* fmt, ...) -{ - va_list args; - va_start(args, fmt); +Log logError( Log::ERRORR, " error: "); +Log logWarning(Log::WARNING, "warning: "); +Log logInfo( Log::INFO, " info: "); - printLog_(LOG_DEBUG, fmt, args); - va_end(args); -} - -void logScript(const char* fmt, ...) +static int logScript_(Script& script, Log::Level level) { - va_list args; - va_start(args, fmt); - - printLog_(LOG_SCRIPT, fmt, args); + static Log* logs[] = {0, &logError, &logWarning, &logInfo}; - va_end(args); -} - -int logScript(Script& script) -{ - Script::Value param = script[1]; + Script::Slot param = script[1]; while (!param.isNone()) { - if (param.isString()) - { - std::string str; - param.get(str); - logScript("%s", str.c_str()); - } - else if (param.isBoolean()) - { - if (param) logScript("true"); - else logScript("false"); - - } - else if (param.isNil()) - { - logScript("nil"); - } - else - { - logScript("%s (%X)", param.getTypeName().c_str(), - param.getIdentifier()); - } - - param.index++; + (*logs[level])(param); + ++param.index; } return 0; } -void importLogPrintFunction(Script& script) +void importLogFunctions(Script& script) { - script.importFunction("print", (int (*)(Script&))logScript); + script.importFunction("LogError", + boost::bind(logScript_, _1, Log::ERRORR)); + script.importFunction("LogWarning", + boost::bind(logScript_, _1, Log::WARNING)); + script.importFunction("LogInfo", + boost::bind(logScript_, _1, Log::INFO)); + script.importFunction("print", + boost::bind(logScript_, _1, Log::INFO)); }