cis_config: /root/cis_interface/cis_interface/cis_interface/tools.h Source File

cis_config

tools.h
1 #ifndef CISTOOLS_H_
2 #define CISTOOLS_H_
3
4 #include <string.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdarg.h>
8 #include <errno.h>
9 #include <time.h>
10
11 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
12 extern "C" {
13 #endif
14
15 // Platform specific
16 #ifdef _WIN32
17 #include "regex_win32.h"
18 #include "stdint.h" // Use local copy for MSVC support
19 // Prevent windows.h from including winsock.h
20 #ifndef WIN32_LEAN_AND_MEAN
21 #define WIN32_LEAN_AND_MEAN
22 #endif
23 #include <windows.h>
24 #include "getline_win32.h"
25 #include <process.h>
26 #define cis_getpid _getpid
27 #define sleep(tsec) Sleep(1000*tsec)
28 #define usleep(usec) Sleep(usec/1000)
29 #else
30 #include "regex_posix.h"
31 #include <stdint.h>
32 #include <unistd.h>
33 #define cis_getpid getpid
34 #endif
35
37 #ifdef IPCDEF
38 #define CIS_MSG_MAX 2048
39 #else
40 #define CIS_MSG_MAX 1048576
41 #endif
42
43 #define CIS_MSG_EOF "EOF!!!"
44
45 #define CIS_MSG_BUF 2048
46
47 #define CIS_SLEEP_TIME 250000
48
50 #define PSI_MSG_MAX CIS_MSG_MAX
51 #define PSI_MSG_BUF CIS_MSG_BUF
52 #define PSI_MSG_EOF CIS_MSG_EOF
53 #ifdef PSI_DEBUG
54 #define CIS_DEBUG PSI_DEBUG
55 #endif
56 static int _cis_error_flag = 0;
57
58
64 static inline
65 unsigned long ptr2seed(void *ptr) {
66 uint64_t v = (uint64_t)ptr;
67 unsigned long seed = (unsigned long)(v & 0xFFFFFFFFLL);
68 return seed;
69 };
70
71
72 //==============================================================================
84 //==============================================================================
85
95 static inline
96 void cisLog(const char* prefix, const char* fmt, va_list ap) {
97 fprintf(stdout, "%s: %d: ", prefix, cis_getpid());
98 vfprintf(stdout, fmt, ap);
99 fprintf(stdout, "\n");
100 fflush(stdout);
101 };
102
110 static inline
111 void cisInfo(const char* fmt, ...) {
112 va_list ap;
113 va_start(ap, fmt);
114 cisLog("INFO", fmt, ap);
115 va_end(ap);
116 };
117
125 static inline
126 void cisDebug(const char* fmt, ...) {
127 va_list ap;
128 va_start(ap, fmt);
129 cisLog("DEBUG", fmt, ap);
130 va_end(ap);
131 };
132
140 static inline
141 void cisError(const char* fmt, ...) {
142 va_list ap;
143 va_start(ap, fmt);
144 cisLog("ERROR", fmt, ap);
145 va_end(ap);
146 _cis_error_flag = 1;
147 };
148
149 #ifdef CIS_DEBUG
150 #if CIS_DEBUG <= 10
151 #define cislog_error cisError
152 #define cislog_info cisInfo
153 #define cislog_debug cisDebug
154 #elif CIS_DEBUG <= 20
155 #define cislog_error cisError
156 #define cislog_info cisInfo
157 #define cislog_debug while (0) cisDebug
158 #elif CIS_DEBUG <= 40
159 #define cislog_error cisError
160 #define cislog_info while (0) cisInfo
161 #define cislog_debug while (0) cisDebug
162 #else
163 #define cislog_error while (0) cisError
164 #define cislog_info while (0) cisInfo
165 #define cislog_debug while (0) cisDebug
166 #endif
167 #else
168 #define cislog_error cisError
169 #define cislog_info while (0) cisInfo
170 #define cislog_debug while (0) cisDebug
171 #endif
172
179 static inline
180 int not_empty_match(const char *pattern, const char *buf) {
181 if (buf == NULL)
182 return 0;
183 if (buf[0] == '\0')
184 return 0;
185 if (strcmp(buf, pattern) == 0) {
186 return 1;
187 } else {
188 return 0;
189 }
190 };
191
197 static inline
198 int is_eof(const char *buf) {
199 return not_empty_match(CIS_MSG_EOF, buf);
200 };
201
207 static inline
208 int is_recv(const char *buf) {
209 return not_empty_match("recv", buf);
210 };
211
217 static inline
218 int is_send(const char *buf) {
219 return not_empty_match("send", buf);
220 };
221
222 #ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
223 }
224 #endif
225
226 #endif /*CISTOOLS_H_*/