fixed bug in jpeg2000 decoding
[swftools.git] / lib / log.c
index ace2987..e75148f 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -6,22 +6,31 @@
    
    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org> 
 
-   This file is distributed under the GPL, see file COPYING for details */
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
-#ifdef __NT__
-#include "stdafx.h"
-#include <string.h>
-#include <winsock2.h>
 #include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#ifdef WIN32
+//#include "stdafx.h"
 #include <malloc.h>
 #if _MSC_VER > 1000
 #pragma once
 #endif // _MSC_VER > 1000
 #else
 #include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <string.h>
 #include <unistd.h>
 #endif
 
 
 static int screenloglevel = 1;
 static int fileloglevel = -1;
-static int socketloglevel = -1;
-static int maxloglevel = 1;
 static FILE *logFile = 0;
-#ifdef __NT__
-static SOCKET logSocket;
-#else
-static int logSocket = 0;
-#endif
-
-static char bLogToSock = 0;
-
-static void initlogSocket(char* servAddr, char* logPort);
+static int maxloglevel = 1;
 
-void initLog(char* pLogName, int fileloglevel, char* servAddr, char* logPort, int serverlevel, int screenlevel)
+int getScreenLogLevel()
 {
-   screenloglevel = screenlevel;
-   fileloglevel = fileloglevel;
-   socketloglevel = screenlevel;
-
-   maxloglevel=screenloglevel;
-   if(fileloglevel>maxloglevel && pLogName)
-       maxloglevel=fileloglevel;
-   if(serverlevel>maxloglevel && servAddr)
-       maxloglevel=serverlevel;
-
-   if(!servAddr)
-       serverlevel = -1;
-   if(!pLogName)
-       fileloglevel = -1;
-
-   logFile = NULL;
-   bLogToSock = 0;
-
-   if (pLogName && fileloglevel>=0)
-   logFile = fopen(pLogName, "a+");
-   bLogToSock = (servAddr && logPort && (serverlevel>=0));
-   if(bLogToSock)
-       initlogSocket(servAddr, logPort);
+    return screenloglevel;
 }
-
-static void initlogSocket(char* servAddr, char* logPort)
+int getLogLevel()
 {
-#ifndef __NT__
-   bLogToSock = 0;
-#else
-   // init winsock
-   // check and prepare WinSock DLL
-   WORD wVersionRequested = MAKEWORD( 2, 2 );
-   WSADATA wsaData;
-   if ( WSAStartup(wVersionRequested, &wsaData) != 0 )
-   {
-      bLogToSock = false;
-      return;
-   }
-   // Confirm that the WinSock DLL supports 2.2.
-   // Note that if the DLL supports versions greater
-   // than 2.2 in addition to 2.2, it will still return
-   // 2.2 in wVersion since that is the version we
-   // requested.
-
-   if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 )
-   {
-      bLogToSock = false;
-      return;
-   }
-
-   struct hostent *hp;
-   hp = gethostbyname(servAddr);
-   if (hp == NULL) // we don't know who this host is
-   {
-      bLogToSock = false;
-      return;
-   }
-
-   // connect socket
-   sockaddr_in SocketAddress;
-
-   memset(&SocketAddress, 0, sizeof(SocketAddress));
-   memcpy((char*)&SocketAddress.sin_addr, hp->h_addr, hp->h_length); // set address
-   SocketAddress.sin_family = hp->h_addrtype;
-   SocketAddress.sin_port = htons((u_short)atoi(logPort));
-
-   logSocket = socket(hp->h_addrtype, SOCK_STREAM, 0);
-   if (logSocket == INVALID_SOCKET)
-   {
-      bLogToSock = false;
-      return;
-   }
+    return maxloglevel;
+}
 
-   // try to connect to the specified socket
-   if ( connect(logSocket, (struct sockaddr*)&SocketAddress, sizeof (SocketAddress)) == SOCKET_ERROR) {
-      bLogToSock = false;
-      return;
-   }
-   bLogToSock = true;
-#endif
+void setConsoleLogging(int level)
+{
+    if(level>maxloglevel)
+        maxloglevel=level;
+    screenloglevel = level;
+}
+void setFileLogging(char*filename, int level, char append)
+{
+    if(level>maxloglevel)
+        maxloglevel=level;
+    if(logFile) {
+        fclose(logFile);logFile=0;
+    }
+    if(filename && level>=0) {
+        logFile = fopen(filename, append?"ab+":"wb");
+        fileloglevel = level;
+    } else {
+        logFile = 0;
+        fileloglevel = 0;
+    }
+}
+/* deprecated */
+void initLog(char* filename, int filelevel, char* s00, char* s01, int s02, int screenlevel)
+{
+    setFileLogging(filename, filelevel, 0);
+    setConsoleLogging(screenlevel);
 }
 
 void exitLog()
 {
-   // close socket communication
-   if(bLogToSock)
-#ifndef __NT__
-     close(logSocket);
-#else
-     closesocket(logSocket);
-#endif
    // close file
-   if(logFile != NULL)
+   if(logFile != NULL) {
      fclose(logFile);
+     logFile = 0;
+     fileloglevel = -1;
+     screenloglevel = 1;
+     maxloglevel = 1;
+   }
 }
 
+static char * logimportance[]= {"Fatal","Error","Warning","Notice","Verbose","Debug","Trace"};
+static int loglevels=7;
+static char * logimportance2[]= {"       ","FATAL  ","ERROR  ","WARNING","NOTICE ","VERBOSE","DEBUG  ", "TRACE  "};
 
-static char * logimportance[]= {"Fatal","Error","Warning","Notice","Verbose","Debug"};
-static int loglevels=6;
-static char * logimportance2[]= {"       ","FATAL  ","ERROR  ","WARNING","NOTICE ","VERBOSE","DEBUG  "};
-static inline void log(char* logString)
+static inline void log_str(const char* logString)
 {
    char timebuffer[32];
    char* logBuffer;
@@ -224,27 +173,25 @@ static inline void log(char* logString)
    {
        if (logFile != NULL)
        {
-         fprintf(logFile, "%s\n", logBuffer); 
+         fprintf(logFile, "%s\r\n", logBuffer); 
          fflush(logFile);
        }
    }
 
-   if (level <= socketloglevel)
-   {
-       if (bLogToSock)
-       {
-         // send data
-#ifndef __NT__
-         write(logSocket, logBuffer, strlen(logBuffer));
-#else
-         send(logSocket, logBuffer, strlen(logBuffer), 0);
-#endif
-       }
-   }
    free (logBuffer);
 }
 
-void logf(const char* format, ...)
+void msg_str(const char* buf)
+{
+    if(buf[0]=='<') {
+       char*z = "fewnvdt";
+       char*x = strchr(z,buf[1]);
+       if(x && (x-z)>maxloglevel)
+               return;
+    }
+    log_str(buf);
+}
+void msg(const char* format, ...)
 {
     char buf[1024];
        va_list arglist;
@@ -252,15 +199,15 @@ void logf(const char* format, ...)
     
     /* speed up hack */
     if(format[0]=='<') {
-       char*z = "fewnvd";
+       char*z = "fewnvdt";
        char*x = strchr(z,format[1]);
        if(x && (x-z)>maxloglevel)
                return;
     }
 
-    vsprintf(buf, format, arglist);
+    vsnprintf(buf, sizeof(buf)-1, format, arglist);
        va_end(arglist);
     strcat(buf, "\n");
-    log(buf);
+    log_str(buf);
 }