/* 
   Netdrive Samba client plugin
   logging functions
   Copyright (C) netlabs.org 2003-2012
   Copyright (C) bww bitwise works GmbH 2012-2016

   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., 675 Mass Ave, Cambridge, MA 02139, USA.  
*/

#define INCL_DOSERRORS
#define INCL_DOS
#include <os2emx.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include "nversion.h"
#include "smbwrp.h"

void debugInit(Resource *pRes, char *logfileext, int ifL)
{
        if (!pRes)
           return;

        pRes->firstLogLine = 1;
        pRes->ifL = ifL;
        APIRET rc = NO_ERROR;

        //create the logfile variable
        char *env = getenv("LOGFILES");
        if (env != NULL)
        {
           snprintf(pRes->logfile, sizeof(pRes->logfile), "%s/%s.%s", env, "log.ndpsmb", logfileext);
           snprintf(pRes->smb_logfile, sizeof(pRes->smb_logfile), "%s/%s.%s", env, "log.smbc", logfileext);
        }
        else
        {
           snprintf(pRes->logfile, sizeof(pRes->logfile), "%s.%s", "log.ndpsmb", logfileext);
           snprintf(pRes->smb_logfile, sizeof(pRes->smb_logfile), "%s.%s", "log.smbc", logfileext);
        }
        pRes->logfileFH = NULL;

        return;
}

void debugDelete()
{
        return;
}

void debugClose(Resource *pRes)
{
	if (pRes && pRes->logfileFH)
        {
           fclose(pRes->logfileFH);
           pRes->logfileFH = NULL;
        }
        return;
}

void debuglocal(Resource *pRes, int level, const char * fmt, ...)
{
        FILE *f = NULL;
        // do we have to log at all
	if (pRes && (level > pRes->loglevel))
           return;

        do {
           struct timeval tv;
           char buf[80] = {0};
           va_list args;
           if (pRes)
           {
              if (!pRes->logfileFH)
                 pRes->logfileFH = fopen(pRes->logfile, "a");
              if (!pRes->logfileFH)
                 break;
              else
                 f = pRes->logfileFH;
           }
           else
              f = stdout;

           // in the first log line we write our version of the client
           if (pRes && pRes->firstLogLine)
           {
              fprintf(f, "Samba client %s build %s based on %s\n", VERSION, BUILD, smbwrp_getVersion());
              fprintf(f, "This build is maintained by %s\n", VENDOR);
              fprintf(f, "Working with %s bit fileio NDFS\n", pRes->ifL ? "64" : "32");

              pRes->firstLogLine = 0;
           }

           gettimeofday(&tv, NULL);
           strftime(buf,sizeof(buf)-1,"%Y/%m/%d %H:%M:%S", localtime((time_t *)&tv.tv_sec));
           fprintf(f, "%s.%d: %d %d: ", buf, tv.tv_usec / 10000, level, (long)_gettid());
           va_start(args, fmt);
           vfprintf(f, fmt, args);
           va_end(args);
#ifdef _DEBUG
           fflush(f);
#endif

        } while (0);

        return;
}
