CUBRID Engine  latest
cas_sql_log2.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 /*
21  * cas_sql_log2.c -
22  */
23 
24 #ident "$Id$"
25 
26 #include <stdio.h>
27 #include <time.h>
28 #include <stdarg.h>
29 #include <string.h>
30 
31 #if !defined(WINDOWS)
32 #include <unistd.h>
33 #include <sys/time.h>
34 #endif
35 
36 #include "cas_common.h"
37 #if !defined(CAS_FOR_ORACLE) && !defined(CAS_FOR_MYSQL)
38 #include "dbi.h"
39 #endif
40 #include "cas_execute.h"
41 #include "cas_sql_log2.h"
42 #include "broker_filename.h"
43 #include "broker_util.h"
44 
45 #if !defined(WINDOWS)
46 static char sql_log2_file[256] = "";
47 static FILE *sql_log2_fp = NULL;
48 static int log_count = 0;
49 static int saved_fd1;
50 #endif
51 
52 void
53 sql_log2_init (char *br_name, int index, int sql_log_value, bool log_reuse_flag)
54 {
55 #if !defined(WINDOWS)
56  char filename[BROKER_PATH_MAX], dirname[BROKER_PATH_MAX];
57 
58  if (!sql_log_value)
59  {
60  return;
61  }
62 
63  if (log_reuse_flag == false || sql_log2_file[0] == '\0')
64  {
65  sprintf (sql_log2_file, "%s/%s.%d.%d.%d", get_cubrid_file (FID_SQL_LOG2_DIR, dirname, BROKER_PATH_MAX), br_name,
66  index + 1, (int) time (NULL), log_count++);
67  }
69  if (snprintf (filename, sizeof (filename) - 1, "%s%s", dirname, sql_log2_file) < 0)
70  {
71  sql_log2_file[0] = '\0';
72  return;
73  }
74 
75  sql_log2_fp = fopen (filename, "a");
76  if (sql_log2_fp == NULL)
77  {
78  sql_log2_file[0] = '\0';
79  }
80 #endif
81 }
82 
83 char *
85 {
86 #if defined(WINDOWS)
87  return "";
88 #else
89  return (sql_log2_file == NULL) ? (char *) "" : (char *) sql_log2_file;
90 #endif
91 }
92 
93 void
95 {
96 #if !defined(WINDOWS)
97  if (sql_log2_fp)
98  {
99  saved_fd1 = dup (1);
100  dup2 (fileno (sql_log2_fp), 1);
101  }
102 #endif
103 }
104 
105 void
107 {
108 #if !defined(WINDOWS)
109  if (sql_log2_fp)
110  {
111  dup2 (saved_fd1, 1);
112  close (saved_fd1);
113  }
114 #endif
115 }
116 
117 void
118 sql_log2_end (bool reset_filename_flag)
119 {
120 #if !defined(WINDOWS)
121  if (sql_log2_fp)
122  {
123  fclose (sql_log2_fp);
124  }
125  sql_log2_fp = NULL;
126  if (reset_filename_flag == true)
127  sql_log2_file[0] = '\0';
128 #endif
129 }
130 
131 void
133 {
134 #if !defined(WINDOWS)
135  fflush (stdout);
136 #endif
137 }
138 
139 void
140 sql_log2_write (const char *fmt, ...)
141 {
142 #if !defined(WINDOWS)
143  va_list ap;
144  time_t t;
145  struct tm lt;
146  struct timeval tv;
147 
148  if (sql_log2_fp)
149  {
150  gettimeofday (&tv, NULL);
151  t = tv.tv_sec;
152  localtime_r (&t, &lt);
153  va_start (ap, fmt);
154  fprintf (sql_log2_fp, "%02d/%02d %02d:%02d:%02d.%03ld ", lt.tm_mon + 1, lt.tm_mday, lt.tm_hour, lt.tm_min,
155  lt.tm_sec, tv.tv_usec / 1000);
156  vfprintf (sql_log2_fp, fmt, ap);
157  fprintf (sql_log2_fp, "\n");
158  fflush (sql_log2_fp);
159  va_end (ap);
160  }
161 #endif
162 }
163 
164 void
165 sql_log2_append_file (char *file_name)
166 {
167 #if !defined(WINDOWS)
168  FILE *in_fp;
169  size_t read_len;
170  char read_buf[1024];
171 
172  if (sql_log2_fp == NULL)
173  return;
174 
175  in_fp = fopen (file_name, "r");
176  if (in_fp == NULL)
177  return;
178 
179  fflush (sql_log2_fp);
180  while ((read_len = fread (read_buf, 1, sizeof (read_buf), in_fp)) > 0)
181  {
182  if (read_len > sizeof (read_buf))
183  {
184  read_len = sizeof (read_buf);
185  }
186  fwrite (read_buf, 1, read_len, sql_log2_fp);
187  }
188  fclose (in_fp);
189  fflush (sql_log2_fp);
190 #endif
191 }
void sql_log2_end(bool reset_filename_flag)
Definition: cas_sql_log2.c:118
void sql_log2_append_file(char *file_name)
Definition: cas_sql_log2.c:165
char * dirname(const char *path)
Definition: porting.c:1066
static char sql_log2_file[256]
Definition: cas_sql_log2.c:46
void sql_log2_flush()
Definition: cas_sql_log2.c:132
static FILE * sql_log2_fp
Definition: cas_sql_log2.c:47
#define BROKER_PATH_MAX
Definition: broker_config.h:91
char * get_cubrid_file(T_CUBRID_FILE_ID fid, char *buf, size_t len)
void sql_log2_write(const char *fmt,...)
Definition: cas_sql_log2.c:140
static int log_count
Definition: cas_sql_log2.c:48
void sql_log2_dup_stdout()
Definition: cas_sql_log2.c:94
char * sql_log2_get_filename()
Definition: cas_sql_log2.c:84
void sql_log2_restore_stdout()
Definition: cas_sql_log2.c:106
#define NULL
Definition: freelistheap.h:34
void sql_log2_init(char *br_name, int index, int sql_log_value, bool log_reuse_flag)
Definition: cas_sql_log2.c:53
static int saved_fd1
Definition: cas_sql_log2.c:49