35 #include <sys/timeb.h> 36 #if !defined (WINDOWS) 47 #define UTIL_LOG_MAX_HEADER_LEN (40) 48 #define UTIL_LOG_MAX_MSG_SIZE (1024) 49 #define UTIL_LOG_BUFFER_SIZE \ 50 (UTIL_LOG_MAX_MSG_SIZE + UTIL_LOG_MAX_HEADER_LEN) 52 #define UTIL_LOG_FILENAME "cubrid_utility.log" 65 template <
typename Duration>
89 for (h = 0; *s !=
'\0'; ++s)
113 #if defined (WINDOWS) 114 char path1[PATH_MAX], path2[PATH_MAX];
117 if (GetLongPathName (file1, path1,
sizeof (path1)) == 0 || GetLongPathName (file2, path2,
sizeof (path2)) == 0)
119 return (stricmp (file1, file2));
122 for (p = path1; *
p; p++)
125 for (p = path2; *
p; p++)
129 return (stricmp (path1, path2));
131 return (strcmp (file1, file2));
164 (*user_interrupt_handler) ();
179 #if !defined(WINDOWS) 182 (*user_quit_handler) ();
202 #if !defined(WINDOWS) 227 if (sigint_handler !=
NULL)
232 #if !defined(WINDOWS) 233 if (sigquit_handler !=
NULL)
265 for (t = o;; t =
NULL)
267 v = strtok_r (t, delim, &save);
272 char **
const realloc_r = (
char **) realloc (r,
sizeof (
char *) * (count + 1));
273 if (realloc_r ==
NULL)
282 r[count - 1] =
strdup (v);
296 for (i = 0; array[
i] !=
NULL; i++)
314 char *save_ptr, *token, *date_string;
315 const char *delim =
"-: ";
316 struct tm time_data, tmp_time_data;
324 date_string =
strdup (str);
325 if (date_string ==
NULL)
330 token = strtok_r (date_string, delim, &save_ptr);
336 time_data.tm_year = atoi (token) - 1900;
337 if (time_data.tm_year < 0)
343 time_data.tm_mon = atoi (token) - 1;
344 if (time_data.tm_mon < 0 || time_data.tm_mon > 11)
350 time_data.tm_mday = atoi (token);
351 if (time_data.tm_mday < 1 || time_data.tm_mday > 31)
357 time_data.tm_hour = atoi (token);
358 if (time_data.tm_hour < 0 || time_data.tm_hour > 23)
364 time_data.tm_min = atoi (token);
365 if (time_data.tm_min < 0 || time_data.tm_min > 59)
371 time_data.tm_sec = atoi (token);
372 if (time_data.tm_sec < 0 || time_data.tm_sec > 59)
382 token = strtok_r (
NULL, delim, &save_ptr);
384 time_data.tm_isdst = -1;
388 if (date_index != 6 || status !=
NO_ERROR)
393 tmp_time_data = time_data;
395 result_time = mktime (&tmp_time_data);
396 if (result_time < (time_t) 0)
401 time_data.tm_isdst = tmp_time_data.tm_isdst;
402 result_time = mktime (&time_data);
403 if (result_time < (time_t) 0)
417 struct drand48_data buf;
420 gettimeofday (&t,
NULL);
424 #if defined (WINDOWS) 425 srand48_r (t.tv_usec / 1000, &buf);
427 srand48_r (t.tv_usec, &buf);
431 for (i = count - 1; i > 0; i--)
433 drand48_r (&buf, &r);
434 j = (int) ((i + 1) * r);
476 va_start (arg_list, message_id);
499 va_start (arg_list, format);
522 va_start (arg_list, format);
543 size_t remained_buf_length, str_len;
551 for (i = 0; i < argc && remained_buf_length > 0; i++)
553 str_len =
strlen (argv[i]);
554 if (str_len > remained_buf_length)
559 remained_buf_length -= str_len;
561 if (i < argc - 1 && remained_buf_length > 0)
566 remained_buf_length--;
568 else if (i == argc - 1 && remained_buf_length > 0)
572 remained_buf_length--;
608 n = snprintf (p, len,
"%s%s", (prefix_str ? prefix_str :
""), msg);
651 tm_p = localtime_r (&sec, &tm);
653 len = (int) strftime (buf, buf_len,
"%y-%m-%d %H:%M:%S", tm_p);
658 len += snprintf (p, buf_len,
".%03d (%s) ", millisec, ((pid ==
NULL) ?
" " : pid));
681 fseek (fp, 0, SEEK_END);
700 char backup_file[PATH_MAX];
706 sprintf (backup_file,
"%s.bak", path);
707 (void) unlink (backup_file);
708 (void) rename (path, backup_file);
721 #define MAX_RETRY_COUNT 10 727 fp = fopen (path,
"a+");
730 if (lockf (fileno (fp), F_TLOCK, 0) < 0)
764 util_bsearch (
const void *key,
const void *base,
int n_elems,
unsigned int sizeof_elem,
765 int (*func_compare) (
const void *,
const void *),
bool * out_found)
768 int max = n_elems - 1;
781 mid = (min +
max) >> 1;
784 elem = (
char *) base + (mid * sizeof_elem);
787 compare = func_compare (elem, key);
816 template <
typename Duration>
820 using clock_t = std::chrono::system_clock;
821 using timept_secs = std::chrono::time_point<clock_t, std::chrono::seconds>;
822 auto now_timepoint = clock_t::now ();
823 timept_secs now_in_secs = std::chrono::time_point_cast<std::chrono::seconds> (now_timepoint);
824 secs = now_in_secs.time_since_epoch ();
825 rest = std::chrono::duration_cast<Duration> (now_timepoint - now_in_secs);
832 std::chrono::seconds secs_since_epoch;
833 std::chrono::milliseconds rest_in_msec;
834 util_get_seconds_and_rest_since_epoch<std::chrono::milliseconds> (secs_since_epoch, rest_in_msec);
835 *secs =
static_cast<time_t
> (secs_since_epoch.count ());
836 *msec =
static_cast<int> (rest_in_msec.count ());
char * envvar_logdir_file(char *path, size_t size, const char *filename)
#define SLEEP_MILISEC(sec, msec)
static FILE * fopen_and_lock(const char *path)
static FILE * util_log_file_backup(FILE *fp, const char *path)
static SIG_HANDLER user_interrupt_handler
void util_disarm_signal_handlers(void)
static void system_quit_handler(int sig)
int util_log_write_command(int argc, char *argv[])
void util_arm_signal_handlers(SIG_HANDLER sigint_handler, SIG_HANDLER sigquit_handler)
static void system_interrupt_handler(int sig)
#define UTIL_PID_ENVVAR_NAME
int util_log_write_errid(int message_id,...)
unsigned int hashpjw(const char *s)
int util_log_write_result(int error)
static int util_log_write_internal(const char *msg, const char *prefix_str)
#define UTIL_LOG_MAX_MSG_SIZE
int prm_get_integer_value(PARAM_ID prm_id)
void util_shuffle_string_array(char **array, int count)
static void util_get_seconds_and_rest_since_epoch(std::chrono::seconds &secs, Duration &rest)
void util_free_string_array(char **array)
int util_compare_filepath(const char *file1, const char *file2)
int count(int &result, const cub_regex_object ®, const std::string &src, const int position, const INTL_CODESET codeset)
#define UTIL_LOG_MAX_HEADER_LEN
int util_log_write_warnstr(const char *format,...)
char ** util_split_string(const char *str, const char *delim)
const char * utility_get_generic_message(int message_index)
void(* SIG_HANDLER)(void)
void util_get_second_and_ms_since_epoch(time_t *secs, int *msec)
const char * envvar_get(const char *name)
static void error(const char *msg)
static int util_log_header(char *buf, size_t buf_len)
static FILE * util_log_file_fopen(const char *path)
static char * util_Log_filename
int util_log_write_errstr(const char *format,...)
char * strdup(const char *str)
static SIG_HANDLER user_quit_handler
static char util_Log_filename_buf[PATH_MAX]
#define UTIL_LOG_FILENAME
time_t util_str_to_time_since_epoch(char *str)
#define UTIL_LOG_BUFFER_SIZE
SIGNAL_HANDLER_FUNCTION os_set_signal_handler(const int sig_no, SIGNAL_HANDLER_FUNCTION sig_handler)
int util_bsearch(const void *key, const void *base, int n_elems, unsigned int sizeof_elem, int(*func_compare)(const void *, const void *), bool *out_found)
static char util_Log_buffer[UTIL_LOG_BUFFER_SIZE]