CUBRID Engine  latest
broker_log_util.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * broker_log_util.c -
21  */
22 
23 #ident "$Id$"
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 
30 #if defined(WINDOWS)
31 #include <sys/timeb.h>
32 #else
33 #include <sys/time.h>
34 #endif
35 
36 #include "cas_common.h"
37 #include "broker_log_util.h"
38 #include "cas_cci.h"
39 
40 static bool is_bind_with_size (char *buf, int *tot_val_size, int *info_size);
41 
42 char *
43 ut_trim (char *str)
44 {
45  char *p;
46  char *s;
47 
48  if (str == NULL)
49  return (str);
50 
51  for (s = str; *s != '\0' && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'); s++)
52  ;
53  if (*s == '\0')
54  {
55  *str = '\0';
56  return (str);
57  }
58 
59  /* *s must be a non-white char */
60  for (p = s; *p != '\0'; p++)
61  ;
62  for (p--; *p == ' ' || *p == '\t' || *p == '\n' || *p == '\r'; p--)
63  ;
64  *++p = '\0';
65 
66  if (s != str)
67  memmove (str, s, strlen (s) + 1);
68 
69  return (str);
70 }
71 
72 void
73 ut_tolower (char *str)
74 {
75  char *p;
76 
77  if (str == NULL)
78  return;
79 
80  for (p = str; *p; p++)
81  {
82  if (*p >= 'A' && *p <= 'Z')
83  *p = *p - 'A' + 'a';
84  }
85 }
86 
87 #if defined(BROKER_LOG_RUNNER)
88 static bool
89 is_bind_with_size (char *buf, int *tot_val_size, int *info_size)
90 {
91  char *p;
92  int type;
93  int size;
94 
95  if (tot_val_size)
96  {
97  *tot_val_size = 0;
98  }
99  if (info_size)
100  {
101  *info_size = 0;
102  }
103 
104  if (strncmp (buf, "B ", 1) != 0)
105  {
106  return false;
107  }
108 
109  type = atoi (buf + 2);
110  if ((type != CCI_U_TYPE_CHAR) && (type != CCI_U_TYPE_STRING) && (type != CCI_U_TYPE_NCHAR)
111  && (type != CCI_U_TYPE_VARNCHAR) && (type != CCI_U_TYPE_BIT) && (type != CCI_U_TYPE_VARBIT)
112  && (type != CCI_U_TYPE_ENUM) && (type != CCI_U_TYPE_JSON))
113  {
114  return false;
115  }
116 
117  p = strchr (buf + 2, ' ');
118  if (p == NULL)
119  {
120  goto error_on_val_size;
121  }
122 
123  size = atoi (p);
124  p = strchr (p + 1, ' ');
125  if (p == NULL)
126  {
127  goto error_on_val_size;
128  }
129 
130  if (info_size)
131  {
132  *info_size = (char *) (p + 1) - (char *) buf;
133  }
134 
135  switch (type)
136  {
137  case CCI_U_TYPE_CHAR:
138  case CCI_U_TYPE_STRING:
139  case CCI_U_TYPE_NCHAR:
140  case CCI_U_TYPE_VARNCHAR:
141  {
142  int len = strlen (p + 1);
143 
144  if (p[len] == '\n')
145  {
146  p[len] = 0;
147  len--;
148  }
149 
150  if (tot_val_size)
151  {
152  *tot_val_size = len + 1;
153  }
154  }
155  break;
156  default:
157  if (tot_val_size)
158  {
159  *tot_val_size = size;
160  }
161  break;
162  }
163 
164  return true;
165 
166 error_on_val_size:
167  if (tot_val_size)
168  {
169  *tot_val_size = -1;
170  }
171  return false;
172 }
173 #else /* BROKER_LOG_RUNNER */
174 static bool
175 is_bind_with_size (char *buf, int *tot_val_size, int *info_size)
176 {
177  char *msg;
178  char *p, *q;
179  char size[256] = { 0, };
180  char *value_p;
181  char *size_begin;
182  char *size_end;
183  char *info_end;
184  int len;
185 
186  if (info_size)
187  {
188  *info_size = 0;
189  }
190  if (tot_val_size)
191  {
192  *tot_val_size = 0;
193  }
194 
195  msg = get_msg_start_ptr (buf);
196  if (strncmp (msg, "bind ", 5) != 0)
197  {
198  return false;
199  }
200 
201  p = strchr (msg, ':');
202  if (p == NULL)
203  {
204  return false;
205  }
206  p += 2;
207 
208  if ((strncmp (p, "CHAR", 4) != 0) && (strncmp (p, "VARCHAR", 7) != 0) && (strncmp (p, "NCHAR", 5) != 0)
209  && (strncmp (p, "VARNCHAR", 8) != 0) && (strncmp (p, "BIT", 3) != 0) && (strncmp (p, "VARBIT", 6) != 0))
210  {
211  return false;
212  }
213 
214  q = strchr (p, ' ');
215  if (q == NULL)
216  {
217  /* log error case or NULL bind type */
218  return false;
219  }
220 
221  *q = '\0';
222  value_p = q + 1;
223 
224  size_begin = strstr (value_p, "(");
225  if (size_begin == NULL)
226  {
227  goto error_on_val_size;
228  }
229  size_begin += 1;
230  size_end = strstr (value_p, ")");
231  if (size_end == NULL)
232  {
233  goto error_on_val_size;
234  }
235 
236  info_end = size_end + 1;
237 
238  if (info_size)
239  {
240  *info_size = (char *) info_end - (char *) buf;
241  }
242 
243  if ((strncmp (p, "CHAR", 4) != 0) || (strncmp (p, "VARCHAR", 7) != 0) || (strncmp (p, "NCHAR", 5) != 0)
244  || (strncmp (p, "VARNCHAR", 8) != 0))
245  {
246  *tot_val_size = strlen (info_end);
247  }
248  else if (tot_val_size)
249  {
250  len = size_end - size_begin;
251  if (len > (int) sizeof (size))
252  {
253  goto error_on_val_size;
254  }
255  if (len > 0)
256  {
257  memcpy (size, size_begin, len);
258  size[len] = '\0';
259  }
260  *tot_val_size = atoi (size);
261  if (*tot_val_size < 0)
262  {
263  goto error_on_val_size;
264  }
265  }
266 
267  return true;
268 
269 error_on_val_size:
270  if (info_size)
271  {
272  *info_size = -1;
273  }
274  if (tot_val_size)
275  {
276  *tot_val_size = -1;
277  }
278  return false;
279 }
280 #endif /* BROKER_LOG_RUNNER */
281 
282 int
283 ut_get_line (FILE * fp, T_STRING * t_str, char **out_str, int *lineno)
284 {
285  char buf[1024];
286  int out_str_len;
287  bool is_first, bind_with_size = false;
288  int tot_val_size = 0, info_size = 0;
289  long position;
290 
291  t_string_clear (t_str);
292 
293  is_first = true;
294  while (1)
295  {
296  memset (buf, 0, sizeof (buf));
297  position = ftell (fp);
298  if (fgets (buf, sizeof (buf), fp) == NULL)
299  break;
300  /* if it is (debug) line, skip it */
301  if (strncmp (buf + 19, "(debug)", 7) == 0)
302  {
303  continue;
304  }
305  if (is_first)
306  {
307  bind_with_size = is_bind_with_size (buf, &tot_val_size, &info_size);
308  if (tot_val_size < 0 || info_size < 0 || (tot_val_size + info_size + 1) < 0)
309  {
310  fprintf (stderr, "log error\n");
311  return -1;
312  }
313  is_first = false;
314  }
315 
316  if (bind_with_size)
317  {
318  size_t rlen;
319  char *value = NULL;
320 
321  value = (char *) MALLOC (info_size + tot_val_size + 1);
322  if (value == NULL)
323  {
324  fprintf (stderr, "memory allocation error.\n");
325  return -1;
326  }
327  fseek (fp, position, SEEK_SET);
328  rlen = fread ((void *) value, sizeof (char), info_size + tot_val_size, fp);
329  if (t_bind_string_add (t_str, value, info_size + tot_val_size, tot_val_size) < 0)
330  {
331  fprintf (stderr, "memory allocation error.\n");
332  FREE_MEM (value);
333  return -1;
334  }
335  FREE_MEM (value);
336  break;
337  }
338  else
339  {
340  if (t_string_add (t_str, buf, (int) strlen (buf)) < 0)
341  {
342  fprintf (stderr, "memory allocation error.\n");
343  return -1;
344  }
345  if (buf[sizeof (buf) - 2] == '\0' || buf[sizeof (buf) - 2] == '\n')
346  break;
347  }
348  }
349 
350  out_str_len = t_string_len (t_str);
351  if (out_str)
352  *out_str = t_string_str (t_str);
353  if (lineno)
354  *lineno = *lineno + 1;
355  return out_str_len;
356 }
357 
358 int
359 is_cas_log (char *str)
360 {
361  if (strlen (str) < CAS_LOG_MSG_INDEX)
362  {
363  return 0;
364  }
365 
366  if (str[2] == '-' && str[5] == '-' && str[8] == ' ' && str[11] == ':' && str[14] == ':' && str[21] == ' ')
367  {
369  }
370  else if (str[2] == '/' && str[5] == ' ' && str[8] == ':' && str[11] == ':' && str[18] == ' ')
371  {
373  }
374 
375  return 0;
376 }
377 
378 char *
379 get_msg_start_ptr (char *linebuf)
380 {
381  char *tmp_ptr;
382 
383  if (is_cas_log (linebuf) == CAS_LOG_BEGIN_WITH_YEAR)
384  {
385  tmp_ptr = linebuf + CAS_LOG_MSG_INDEX;
386  }
387  else
388  {
389  tmp_ptr = linebuf + CAS_LOG_MSG_INDEX - 3;
390  }
391 
392  tmp_ptr = strchr (tmp_ptr, ' ');
393  if (tmp_ptr == NULL)
394  {
395  return (char *) "";
396  }
397  else
398  {
399  return tmp_ptr + 1;
400  }
401 }
402 
403 #define DATE_VALUE_COUNT 7
404 int
405 str_to_log_date_format (char *str, char *date_format_str)
406 {
407  char *startp;
408  char *endp;
409  int i;
410  int result = 0;
411  int val;
412  int date_val[DATE_VALUE_COUNT];
413 
414  for (i = 0; i < DATE_VALUE_COUNT; i++)
415  date_val[i] = 0;
416 
417  for (i = 0, startp = str; i < DATE_VALUE_COUNT; i++)
418  {
419  result = str_to_int32 (&val, &endp, startp, 10);
420  if (result != 0)
421  {
422  goto error;
423  }
424  if (val < 0)
425  {
426  val = 0;
427  }
428  else if (val > 999)
429  {
430  val = 999;
431  }
432  date_val[i] = val;
433  if (*endp == '\0')
434  {
435  break;
436  }
437  startp = endp + 1;
438  if (*startp == '\0')
439  {
440  break;
441  }
442  }
443 
444  sprintf (date_format_str, "%02d-%02d-%02d %02d:%02d:%02d.%03d", date_val[0], date_val[1], date_val[2], date_val[3],
445  date_val[4], date_val[5], date_val[6]);
446  return 0;
447 
448 error:
449  return -1;
450 }
451 
452 char *
453 ut_get_execute_type (char *msg_p, int *prepare_flag, int *execute_flag)
454 {
455  if (strncmp (msg_p, "execute ", 8) == 0)
456  {
457  *prepare_flag = 0;
458  *execute_flag = 0;
459  return (msg_p += 8);
460  }
461  else if (strncmp (msg_p, "execute_call ", 13) == 0)
462  {
463  *prepare_flag = 0x40; /* CCI_PREPARE_CALL */
464  *execute_flag = 0;
465  return (msg_p += 13);
466  }
467  else if (strncmp (msg_p, "execute_all ", 12) == 0)
468  {
469  *prepare_flag = 0;
470  *execute_flag = 2; /* CCI_EXEC_QUERY_ALL */
471  return (msg_p += 12);
472  }
473  else
474  {
475  return NULL;
476  }
477 }
478 
479 int
480 ut_check_log_valid_time (const char *log_date, const char *from_date, const char *to_date)
481 {
482  if (from_date[0])
483  {
484  if (strncmp (log_date, from_date, DATE_STR_LEN) < 0)
485  return -1;
486  }
487  if (to_date[0])
488  {
489  if (strncmp (to_date, log_date, DATE_STR_LEN) < 0)
490  return -1;
491  }
492 
493  return 0;
494 }
495 
496 double
497 ut_diff_time (struct timeval *begin, struct timeval *end)
498 {
499  double sec, usec;
500 
501  sec = (end->tv_sec - begin->tv_sec);
502  usec = (double) (end->tv_usec - begin->tv_usec) / 1000000;
503  return (sec + usec);
504 }
#define DATE_STR_LEN
#define CAS_LOG_BEGIN_WITH_YEAR
int t_string_add(T_STRING *t_str, char *str, int str_len)
char * get_msg_start_ptr(char *linebuf)
static void begin(char *test_name)
int t_string_len(T_STRING *t_str)
static char to_date[128]
#define CAS_LOG_BEGIN_WITH_MONTH
char * t_string_str(T_STRING *t_str)
int is_cas_log(char *str)
#define NULL
Definition: freelistheap.h:34
int str_to_int32(int *ret_p, char **end_p, const char *str_p, int base)
Definition: porting.c:2346
#define MALLOC(SIZE)
Definition: cas_common.h:53
#define FREE_MEM(PTR)
Definition: cas_common.h:58
#define DATE_VALUE_COUNT
int ut_check_log_valid_time(const char *log_date, const char *from_date, const char *to_date)
static void error(const char *msg)
Definition: gencat.c:331
#define strlen(s1)
Definition: intl_support.c:43
double ut_diff_time(struct timeval *begin, struct timeval *end)
int i
Definition: dynamic_load.c:954
static char from_date[128]
void ut_tolower(char *str)
char * ut_get_execute_type(char *msg_p, int *prepare_flag, int *execute_flag)
static bool is_bind_with_size(char *buf, int *tot_val_size, int *info_size)
char * ut_trim(char *str)
int t_bind_string_add(T_STRING *t_str, char *str, int str_len, int bind_len)
void t_string_clear(T_STRING *t_str)
int ut_get_line(FILE *fp, T_STRING *t_str, char **out_str, int *lineno)
#define CAS_LOG_MSG_INDEX
int str_to_log_date_format(char *str, char *date_format_str)
const char ** p
Definition: dynamic_load.c:945