cis_config
tools.h
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <errno.h>
6 #include <time.h>
7 
8 #ifndef CISTOOLS_H_
9 #define CISTOOLS_H_
10 
11 // Platform specific
12 #ifdef _WIN32
13 #include "regex_win32.h"
14 #include "stdint.h" // Use local copy for MSVC support
15 // Prevent windows.h from including winsock.h
16 #ifndef WIN32_LEAN_AND_MEAN
17 #define WIN32_LEAN_AND_MEAN
18 #endif
19 #include <windows.h>
20 #include "getline_win32.h"
21 #include <process.h>
22 #define cis_getpid _getpid
23 #define sleep(tsec) Sleep(1000*tsec)
24 #define usleep(usec) Sleep(usec/1000)
25 #else
26 #include "regex_posix.h"
27 #include <stdint.h>
28 #include <unistd.h>
29 #define cis_getpid getpid
30 #endif
31 
33 #ifdef IPCDEF
34 #define CIS_MSG_MAX 2048
35 #else
36 #define CIS_MSG_MAX 1048576
37 #endif
38 
39 #define CIS_MSG_EOF "EOF!!!"
40 
41 #define CIS_MSG_BUF 2048
42 
43 #define CIS_SLEEP_TIME 250000
44 
46 #define PSI_MSG_MAX CIS_MSG_MAX
47 #define PSI_MSG_BUF CIS_MSG_BUF
48 #define PSI_MSG_EOF CIS_MSG_EOF
49 #ifdef PSI_DEBUG
50 #define CIS_DEBUG PSI_DEBUG
51 #endif
52 static int _cis_error_flag = 0;
53 
54 
60 static inline
61 unsigned long ptr2seed(void *ptr) {
62  uint64_t v = (uint64_t)ptr;
63  unsigned long seed = (unsigned long)(v & 0xFFFFFFFFLL);
64  return seed;
65 };
66 
67 
68 //==============================================================================
80 //==============================================================================
81 
91 static inline
92 void cisLog(const char* prefix, const char* fmt, va_list ap) {
93  fprintf(stdout, "%s: %d: ", prefix, cis_getpid());
94  vfprintf(stdout, fmt, ap);
95  fprintf(stdout, "\n");
96  fflush(stdout);
97 };
98 
106 static inline
107 void cisInfo(const char* fmt, ...) {
108  va_list ap;
109  va_start(ap, fmt);
110  cisLog("INFO", fmt, ap);
111  va_end(ap);
112 };
113 
121 static inline
122 void cisDebug(const char* fmt, ...) {
123  va_list ap;
124  va_start(ap, fmt);
125  cisLog("DEBUG", fmt, ap);
126  va_end(ap);
127 };
128 
136 static inline
137 void cisError(const char* fmt, ...) {
138  va_list ap;
139  va_start(ap, fmt);
140  cisLog("ERROR", fmt, ap);
141  va_end(ap);
142  _cis_error_flag = 1;
143 };
144 
145 #ifdef CIS_DEBUG
146 #if CIS_DEBUG <= 10
147 #define cislog_error cisError
148 #define cislog_info cisInfo
149 #define cislog_debug cisDebug
150 #elif CIS_DEBUG <= 20
151 #define cislog_error cisError
152 #define cislog_info cisInfo
153 #define cislog_debug while (0) cisDebug
154 #elif CIS_DEBUG <= 40
155 #define cislog_error cisError
156 #define cislog_info while (0) cisInfo
157 #define cislog_debug while (0) cisDebug
158 #else
159 #define cislog_error while (0) cisError
160 #define cislog_info while (0) cisInfo
161 #define cislog_debug while (0) cisDebug
162 #endif
163 #else
164 #define cislog_error cisError
165 #define cislog_info while (0) cisInfo
166 #define cislog_debug while (0) cisDebug
167 #endif
168 
175 static inline
176 int not_empty_match(const char *pattern, const char *buf) {
177  if (buf == NULL)
178  return 0;
179  if (buf[0] == '\0')
180  return 0;
181  if (strcmp(buf, pattern) == 0) {
182  return 1;
183  } else {
184  return 0;
185  }
186 };
187 
193 static inline
194 int is_eof(const char *buf) {
195  return not_empty_match(CIS_MSG_EOF, buf);
196 };
197 
203 static inline
204 int is_recv(const char *buf) {
205  return not_empty_match("recv", buf);
206 };
207 
213 static inline
214 int is_send(const char *buf) {
215  return not_empty_match("send", buf);
216 };
217 
218 
219 
220 #endif /*CISTOOLS_H_*/