]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Log.cc
port new sockets stuff to winsock
[chaz/yoink] / src / Moof / Log.cc
index a7cdae4a49e57604a6cdea4f678dba2d888aa2a7..da7985d60115bff1bde22cc9557131f7059c6742 100644 (file)
@@ -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 <cstdarg>
-#include <cstdio>              // snprintf
-#include <cstring>             // 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 <fstream>
 
 #include "Log.hh"
 #include "Script.hh"
 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));
 }
 
 
This page took 0.024076 seconds and 4 git commands to generate.