Andy's CarPlayer

/***************************************************************************
                          logging.c  -  description
                             -------------------
    begin                : Wed Feb 4 2004
    copyright            : (C) 2004 by Andy Clews
    email                : andy  clews  blueyonder  co  uk
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

 #include 
 #include 
 #include 
 
 #include "log.h"
 #include "main.h"
// #include "get_songs.h"

 int log_open(char *filename, int level)
 {
   log_close(); 

   if (filename)
   {
     log_file = fopen(filename,"w");
     if (!log_file)
       return -1;
     setlinebuf(log_file);
   }
   return 0;
 }

 void log_close(void)
 {
   if (log_file)
     fclose(log_file);
   log_file = NULL;
 }

 void log_printf(int level, char * text, ...)
 {
   time_t t = time(NULL);
   struct tm *tm = localtime(&t);
   va_list ap;

   int i;

   if (debug_level < level)
     return;

   // Log to console...
   if (!log_file)
   {
     if (!text || !*text)
       fprintf(stderr, "\n");
     else
     {
       if (debug_level >= LOG_DEBUG)
         fprintf(stderr, "%02d/%02d/%02d %02d:%02d:%02d %d:", tm->tm_mon+1, tm->tm_mday, tm->tm_year%100,\
          tm->tm_hour, tm->tm_min, tm->tm_sec, level);
       for (i = 0; i < level; i++)
         fprintf(stderr, " ");
       va_start(ap, text);
       vfprintf(stderr, text, ap);
       va_end(ap);
     }
     fflush(stderr);
   }

   // Log to file
   if (log_file)
   {
     if (!text || !*text)
       fprintf(log_file, "\n");
     else
     {
       if (debug_level >= LOG_DEBUG)
         fprintf(log_file, "%02d/%02d/%02d %02d:%02d:%02d %d:", tm->tm_mon+1, tm->tm_mday, tm->tm_year%100, \
         tm->tm_hour, tm->tm_min, tm->tm_sec, level);
       for (i = 0; i < level; i++)
         fprintf(log_file, " ");
       va_start(ap, text);
       vfprintf(log_file, text, ap);
       va_end(ap);
     }
   }
 }