aboutsummaryrefslogtreecommitdiff
path: root/src/include/gauger.h
blob: 36dc055bb1d3ec1332e4dd3f480f368fd33a3075 (plain)
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
/** ---------------------------------------------------------------------------
 * This software is in the public domain, furnished "as is", without technical
 * support, and with no warranty, express or implied, as to its usefulness for
 * any purpose.
 *
 * gauger.h
 * Interface for C programs to log remotely to a gauger server
 *
 * Author: Bartlomiej Polot
 * -------------------------------------------------------------------------*/
#ifndef __GAUGER_H__
#define __GAUGER_H__

#ifndef WINDOWS

#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>

#define GAUGER(category, counter, value, unit) \
  { \
    const char *__gauger_v[10];      \
    char __gauger_s[32]; \
    pid_t __gauger_p; \
    if (! (__gauger_p = fork ())) { \
      if (! fork ()) { \
        sprintf (__gauger_s,"%Lf", (long double) (value)); \
        __gauger_v[0] = "gauger"; \
        __gauger_v[1] = "-n"; \
        __gauger_v[2] = (char *) counter; \
        __gauger_v[3] = "-d"; \
        __gauger_v[4] = __gauger_s; \
        __gauger_v[5] = "-u"; \
        __gauger_v[6] = (char *) unit;  \
        __gauger_v[7] = "-c"; \
        __gauger_v[8] = (char *) category;  \
        __gauger_v[9] = (char *) NULL; \
        execvp ("gauger", (char *const *) __gauger_v); \
        perror ("gauger"); \
        _exit (1); \
      }else{ \
        _exit (0); \
      } \
    }else{ \
      waitpid (__gauger_p,NULL,0); \
    } \
  }

#define GAUGER_ID(category, counter, value, unit, id) \
  { \
    char*__gauger_v[12]; \
    char __gauger_s[32]; \
    pid_t __gauger_p; \
    if (! (__gauger_p = fork ())) { \
      if (! fork ()) { \
        sprintf (__gauger_s,"%Lf", (long double) (value)); \
        __gauger_v[0] = "gauger"; \
        __gauger_v[1] = "-n"; \
        __gauger_v[2] = (char *) counter; \
        __gauger_v[3] = "-d"; \
        __gauger_v[4] = __gauger_s; \
        __gauger_v[5] = "-u"; \
        __gauger_v[6] = (char *) unit;  \
        __gauger_v[7] = "-i"; \
        __gauger_v[8] = id; \
        __gauger_v[9] = "-c"; \
        __gauger_v[10] = (char *) category; \
        __gauger_v[11] = (char *) NULL; \
        execvp ("gauger",__gauger_v); \
        perror ("gauger"); \
        _exit (1); \
      }else{ \
        _exit (0); \
      } \
    }else{ \
      waitpid (__gauger_p,NULL,0); \
    } \
  }

#else /* WINDOWS */

#include <stdlib.h>
#include <stdio.h>
#include <windef.h>

#define GAUGER(category, counter, value, unit) \
  { \
    char __gauger_commandline[MAX_PATH]; \
    \
    snprintf (__gauger_commandline, MAX_PATH, \
              "gauger.py -n \"%s\" -d \"%Lf\" -u \"%s\" -c \"%s\"", \
              counter, (long double) (value), unit, category); \
    __gauger_commandline[MAX_PATH - 1] = '\0'; \
    system (__gauger_commandline); \
  }

#define GAUGER_ID(category, counter, value, unit, id) \
  { \
    char __gauger_commandline[MAX_PATH]; \
    \
    snprintf (__gauger_commandline, MAX_PATH, \
              "gauger.py -n \"%s\" -d \"%Lf\" -u \"%s\" -i \"%s\" -c \"%s\"", \
              counter, (long double) (value), unit, id, category); \
    __gauger_commandline[MAX_PATH - 1] = '\0'; \
    system (__gauger_commandline); \
  }

#endif // WINDOWS

#endif