view mcabber/src/utils.c @ 76:a8f8492abd44

[/trunk] Changeset 90 by mikael * Cleaning * Fix a bug when the log file cannot be opened.
author mikael
date Sat, 16 Apr 2005 18:13:24 +0000
parents e88b15cbf2de
children 32f54ad6d729
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>

/* Variables globales a UTILS.C */
static int DebugEnabled;

void ut_InitDebug(int level)
{
  FILE *fp = fopen("/tmp/mcabberlog", "w");

  DebugEnabled = level;

  if (!fp) return;
  fprintf(fp, "Debugging mode started...\n"
	  "-----------------------------------\n");
  fclose(fp);
}

void ut_WriteLog(const char *fmt, ...)
{
  FILE *fp = NULL;
  time_t ahora;
  va_list ap;
  char *buffer = NULL;

  if (DebugEnabled) {
    fp = fopen("/tmp/mcabberlog", "a+");
    if (!fp) return;
    buffer = (char *) calloc(1, 64);

    ahora = time(NULL);
    strftime(buffer, 64, "[%H:%M:%S] ", localtime(&ahora));
    fprintf(fp, "%s", buffer);

    va_start(ap, fmt);
    vfprintf(fp, fmt, ap);
    va_end(ap);

    free(buffer);
    fclose(fp);
  }
}