debug.c

Go to the documentation of this file.
00001 /*
00002  EasyDbus: DBUS Binding library.
00003  Copyright (C) 2007  Daniele Rondina aka Ge@@ru, geaaru@gmail.com 
00004 
00005  This program is free software; you can redistribute it and/or
00006  modify it under the terms of the GNU General Public License
00007  as published by the Free Software Foundation; either version 2
00008  of the License, or (at your option) any later version.
00009 
00010  This program is distributed in the hope that it will be useful,
00011  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  GNU General Public License for more details.
00014 
00015  You should have received a copy of the GNU General Public License
00016  along with this program; if not, write to the Free Software
00017  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 
00019   Filename:  debug.c
00020  
00021   Description:  
00022  
00023   Version:  1.0
00024   Created:  06/13/07 20:51:57 CEST
00025   Revision:  none
00026  
00027   Author:   Daniele Rondina aka Ge@@ru (geaaru@gmail.com) 
00028   License:  GPL 2.0
00029 */
00030 
00031 
00047 #define _GNU_SOURCE
00048 #include <stdio.h>
00049 #include <stdarg.h>
00050 #include <stdlib.h>
00051 #include <string.h>
00052 #include <unistd.h>
00053 
00054 #include "debug.h"
00055 #ifdef TOFILE_DEBUG
00056 
00060 int debug_file_fd = -1;
00061 #endif
00062 
00067 void
00068 _EasyDbusDebug (const char *file,
00069                 const char *function, int line, const char *fmt, ...)
00070 {
00071    char *ptr;
00072    char *pptr;
00073    va_list ap;
00074 
00075    va_start (ap, fmt);
00076    vasprintf (&ptr, fmt, ap);
00077    va_end (ap);
00078 
00079    pptr = ptr + strlen (ptr) - 1;
00080    if (*pptr == '\n')
00081       *pptr = ' ';
00082 
00083 #ifdef TOFILE_DEBUG
00084    char buffer[1024];
00085 
00086    snprintf (buffer, 1024, "[%s@%s:%d] %s\n", file, function, line, ptr);
00087    write (debug_file_fd, buffer, strlen (buffer));
00088 #else
00089    fprintf (stderr, "[%s@%s:%d] %s\n", file, function, line, ptr);
00090    fflush (stderr);
00091 #endif
00092    free (ptr);
00093 }
00094 
00095 #ifdef TOFILE_DEBUG
00096 #   include <sys/types.h>
00097 #   include <sys/stat.h>
00098 #   include <fcntl.h>
00099 
00110 int
00111 easydbus_init_logger (char *debug_file)
00112 {
00113    if (debug_file == NULL)
00114       return -1;
00115 
00116    debug_file_fd = open (debug_file, O_WRONLY | O_APPEND | O_CREAT,
00117                          S_IRGRP | S_IRWXU | S_IROTH);
00118 
00119    if (debug_file_fd == -1)
00120       return -1;
00121 
00122    return 0;
00123 }
00124 
00131 void
00132 easydbus_deinit_logger (void)
00133 {
00134    close (debug_file_fd);
00135 }
00136 
00137 #endif
00138 
00139 // vim: ts=3 shiftwidth=3 expandtab

Generated on Thu Apr 10 10:00:17 2008 for EasyDbus-0.2 by  doxygen 1.5.4