~ubuntu-branches/ubuntu/precise/gcompris/precise

« back to all changes in this revision

Viewing changes to src/chess_computer-activity/gnuchess/debug.c

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2010-06-27 22:51:30 UTC
  • mfrom: (1.1.16 upstream) (5.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100627225130-mf7h4m5r8m7bd9fb
Tags: 9.3-1
* New upstream release.
* Drop GTK_DISABLE_DEPRECATED patch, useless for now.
* Provide RELEASE_NOTE_9.3.txt downloaded from sourceforge.
* New voice package for Asturian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * debug.c - Debugging output
 
3
 */
 
4
 
 
5
#include <config.h>
 
6
#include "common.h"
 
7
 
 
8
#ifdef DEBUG
 
9
 
 
10
#include <unistd.h>
 
11
#include <sys/types.h>
 
12
#include <sys/stat.h>
 
13
#include <sys/time.h>
 
14
#include <fcntl.h>
 
15
#include <stdarg.h>
 
16
 
 
17
#define MAX_DEBUG 1000
 
18
#define DEBUG_FILENAME "gnuchess.debug"
 
19
 
 
20
/* Default to stderr */
 
21
static int debug_fd = 2;
 
22
 
 
23
int dbg_open(const char *name)
 
24
{
 
25
   int flags = O_WRONLY | O_CREAT | O_APPEND;
 
26
   int mode = 0777;
 
27
   
 
28
   if (name == NULL) {
 
29
      debug_fd = open(DEBUG_FILENAME, flags, mode);
 
30
   } else {
 
31
      debug_fd = open(name, flags, mode);
 
32
   }
 
33
   if (-1 == debug_fd) {
 
34
      debug_fd = 2;
 
35
      return -1;
 
36
   }
 
37
   dbg_printf("--- Opening debug log ---\n");
 
38
   return debug_fd;
 
39
}
 
40
 
 
41
int dbg_close(void)
 
42
{
 
43
   /* We don't want to close stderr */
 
44
   if (debug_fd == 2) {
 
45
      return -1;
 
46
   }
 
47
   dbg_printf("--- Closing debug log ---\n");
 
48
   return close(debug_fd);
 
49
}
 
50
 
 
51
int dbg_printf(const char *fmt, ...)
 
52
{
 
53
   va_list ap;
 
54
   char buf[MAX_DEBUG];
 
55
   struct timeval tv;
 
56
   
 
57
   gettimeofday(&tv, NULL);
 
58
   sprintf(buf, "%010ld.%06ld: ", tv.tv_sec, tv.tv_usec);
 
59
   write(debug_fd, buf, strlen(buf));
 
60
 
 
61
   va_start(ap, fmt);
 
62
   vsnprintf(buf, MAX_DEBUG, fmt, ap);
 
63
   va_end(ap);
 
64
 
 
65
   return write(debug_fd, buf, strlen(buf));
 
66
}
 
67
 
 
68
#else /* !DEBUG */
 
69
 
 
70
int dbg_open(const char *name __attribute__((unused)) ) { return 0; }
 
71
int dbg_printf(const char *fmt __attribute__((unused)), ...) { return 0; }
 
72
int dbg_close(void) { return 0; }
 
73
 
 
74
#endif /* DEBUG */