~leonerd/libtickit/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifdef __GLIBC__
#  define _XOPEN_SOURCE  // fdopen
#endif

#include "tickit.h"

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>  // getpid

#define streq(a,b) (!strcmp(a,b))

static void (*debug_func)(const char *str, void *data);
static void *debug_func_data;

static FILE *debug_fh;

/* This needs to default true on startup so the first debugging call gets
 * made, which then lets us inspect the env. vars. to set it correctly
 */
bool tickit_debug_enabled = true;

struct Flag {
  struct Flag *next;
  char *name;
};

static struct Flag *enabled_flags;

static bool init_done = false;
void tickit_debug_init(void)
{
  if(init_done)
    return;

  const char *flags_str = getenv("TICKIT_DEBUG_FLAGS");

  while(flags_str) {
    const char *endp = strchr(flags_str, ',');
    if(!endp)
      endp = flags_str + strlen(flags_str);

    struct Flag *newflag = malloc(sizeof *newflag);
    newflag->name = malloc(endp - flags_str + 1);
    strncpy(newflag->name, flags_str, endp - flags_str);
    newflag->name[endp - flags_str] = 0;

    newflag->next = enabled_flags;
    enabled_flags = newflag;

    flags_str = endp;
    if(flags_str[0] == ',')
      flags_str++;
    else
      break;
  }

  if(!debug_func) {
    const char *val;
    if((val = getenv("TICKIT_DEBUG_FD")) && val[0]) {
      int fd;
      if(sscanf(val, "%d", &fd))
        tickit_debug_set_fh(fdopen(fd, "a"));
    }
    else if((val = getenv("TICKIT_DEBUG_FILE")) && val[0]) {
      tickit_debug_open(val);
    }
    else if(enabled_flags) {
      char name[17];
      sprintf(name, "tickit-%d.log", getpid());
      tickit_debug_open(name);
    }
  }

  tickit_debug_enabled = !!enabled_flags && (debug_func || debug_fh);

  init_done = true;
}

static bool flag_enabled(const char *name)
{
  for(struct Flag *f = enabled_flags; f; f = f->next) {
    if(f->name[0] == '*')
      return true;

    if(name[0] != f->name[0])
      continue;

    if(!f->name[1])
      return true;
    if(streq(name+1, f->name+1))
      return true;
  }

  return false;
}

void tickit_debug_set_func(void (*func)(const char *str, void *data), void *data)
{
  debug_func      = func;
  debug_func_data = data;

  if(debug_fh)
    fclose(debug_fh);

  tickit_debug_enabled = !!enabled_flags && (debug_func || debug_fh);
}

void tickit_debug_set_fh(FILE *fh)
{
  if(debug_fh)
    fclose(debug_fh);

  debug_fh = fh;
  if(debug_fh)
    setvbuf(debug_fh, NULL, _IONBF, 0);

  if(debug_func)
    debug_func = NULL;

  tickit_debug_enabled = !!enabled_flags && (debug_func || debug_fh);
}

bool tickit_debug_open(const char *path)
{
  FILE *fh = fopen(path, "a");
  if(!fh)
    return false;

  tickit_debug_set_fh(fh);
  return true;
}

void tickit_debug_logf(const char *flag, const char *fmt, ...)
{
  va_list args;
  va_start(args, fmt);

  tickit_debug_vlogf(flag, fmt, args);

  va_end(args);
}

void tickit_debug_vlogf(const char *flag, const char *fmt, va_list args)
{
  if(!init_done)
    tickit_debug_init();

  if(!tickit_debug_enabled)
    return;
  if(!flag_enabled(flag))
    return;

  struct timeval now;
  gettimeofday(&now, NULL);

  char timestamp[9];
  strftime(timestamp, sizeof timestamp, "%H:%M:%S", localtime(&now.tv_sec));

#define LINE_PREFIX "%s.%03d [%-3s]: "

  if(debug_func) {
    size_t len;
    va_list args_copy;
    va_copy(args_copy, args);
    len = snprintf(NULL, 0, LINE_PREFIX, timestamp, 0, flag) +
      vsnprintf(NULL, 0, fmt, args_copy) +
      1;
    va_end(args_copy);

    char *buf = malloc(len + 1);
    {
      char *s = buf;

      s += sprintf(s, LINE_PREFIX, timestamp, (int)(now.tv_usec / 1000), flag);
      s += vsprintf(s, fmt, args);
      s += sprintf(s, "\n");
    }

    (*debug_func)(buf, debug_func_data);

    free(buf);
  }
  else if(debug_fh) {
    fprintf(debug_fh, LINE_PREFIX, timestamp, (int)(now.tv_usec / 1000), flag);
    vfprintf(debug_fh, fmt, args);
    fprintf(debug_fh, "\n");
  }

#undef LINE_PREFIX
}