CUBRID Engine  latest
cas_error_log.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_error_log.c -
22  */
23 
24 #ident "$Id$"
25 
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <time.h>
30 #include <string.h>
31 #include <errno.h>
32 #if defined(WINDOWS)
33 #include <sys/timeb.h>
34 #include <process.h>
35 #include <io.h>
36 #else
37 #include <unistd.h>
38 #include <sys/time.h>
39 #endif
40 #include <assert.h>
41 
42 #include "cas_common.h"
43 #include "cas_error_log.h"
44 #include "cas_util.h"
45 #include "broker_config.h"
46 #include "cas.h"
47 #include "cas_execute.h"
48 
49 #include "broker_env_def.h"
50 #include "broker_filename.h"
51 #include "broker_util.h"
52 
53 static char *make_error_log_filename (char *filename_buf, size_t buf_size, const char *br_name);
54 static void cas_error_log_backup (void);
55 static void cas_log_write_internal (const char *fmt, ...);
56 
57 static FILE *error_log_fp = NULL;
59 static long saved_error_log_fpos = 0;
60 
61 static int eid = 0;
62 
63 static char *
64 make_error_log_filename (char *filename_buf, size_t buf_size, const char *br_name)
65 {
67 
68  assert (filename_buf != NULL);
69 
71 
72  if (cas_shard_flag == ON)
73  {
74  snprintf (filename_buf, buf_size, "%s%s_CAS_%d_%d_%d.err", dirname, br_name, shm_proxy_id + 1, shm_shard_id,
75  shm_shard_cas_id + 1);
76  }
77  else
78  {
79  snprintf (filename_buf, buf_size, "%s%s_CAS_%d.err", dirname, br_name, shm_as_index + 1);
80  }
81  return filename_buf;
82 }
83 
84 
85 void
86 cas_error_log_open (char *br_name)
87 {
88  if (error_log_fp != NULL)
89  {
90  cas_error_log_close (true);
91  }
92 
93  if (br_name != NULL)
94  {
96  }
97 
98  /* note: in "a+" mode, output is always appended */
99  error_log_fp = fopen (error_log_filepath, "r+");
100  if (error_log_fp != NULL)
101  {
102  fseek (error_log_fp, 0, SEEK_END);
104  }
105  else
106  {
107  error_log_fp = fopen (error_log_filepath, "w");
109  }
110 }
111 
112 void
114 {
115  if (error_log_fp != NULL)
116  {
117  if (flag)
118  {
119  fseek (error_log_fp, saved_error_log_fpos, SEEK_SET);
120  ftruncate (fileno (error_log_fp), saved_error_log_fpos);
121  }
122  fclose (error_log_fp);
123  error_log_fp = NULL;
125  }
126 }
127 
128 static void
130 {
131  char backup_filepath[BROKER_PATH_MAX];
132 
133  assert (error_log_filepath[0] != '\0');
134 
135  snprintf (backup_filepath, BROKER_PATH_MAX, "%s.bak", error_log_filepath);
136 
137  unlink (backup_filepath);
138  rename (error_log_filepath, backup_filepath);
139 }
140 
141 static void
142 cas_log_write_internal (const char *fmt, ...)
143 {
144  va_list ap;
145  char buf[LINE_MAX], *p;
146  int len, n;
147 
148  va_start (ap, fmt);
149  p = buf;
150  len = LINE_MAX;
151  n = ut_time_string (p, NULL);
152  len -= n;
153  p += n;
154  if (len > 0)
155  {
156  n = vsnprintf (p, len, fmt, ap);
157  len -= n;
158  p += n;
159  }
160  fwrite (buf, (p - buf), 1, error_log_fp);
161 
162  fflush (error_log_fp);
163  va_end (ap);
164 }
165 
166 void
167 cas_error_log_write (int dbms_errno, const char *dbms_errmsg)
168 {
169  if (error_log_fp == NULL)
170  {
171  return;
172  }
173 
174  if (eid >= INT_MAX)
175  {
176  eid = 0;
177  }
178  cas_log_write_internal (" DBMS ERROR [ERR_CODE : %d, ERR_MSG : %s] EID = %d", dbms_errno,
179  (dbms_errmsg != NULL) ? dbms_errmsg : "-", ++eid);
180  fputc ('\n', error_log_fp);
181 
183 
185  {
186  cas_error_log_close (true);
189  }
190 }
191 
192 char *
193 cas_error_log_get_eid (char *buf, size_t bufsz)
194 {
195  char *buf_p;
196 
197  if (buf == NULL || bufsz <= 0)
198  {
199  return NULL;
200  }
201 
202  if (bufsz < 32)
203  {
204  buf_p = malloc (32);
205 
206  if (buf_p == NULL)
207  {
208  return NULL;
209  }
210  }
211  else
212  {
213  buf_p = buf;
214  }
215 
216  /* actual print */
217  snprintf (buf_p, 32, ", EID = %u", eid);
218 
219  return buf_p;
220 }
static FILE * error_log_fp
Definition: cas_error_log.c:57
void cas_error_log_open(char *br_name)
Definition: cas_error_log.c:86
char * dirname(const char *path)
Definition: porting.c:1066
static void cas_error_log_backup(void)
#define BROKER_PATH_MAX
Definition: broker_config.h:91
static long saved_error_log_fpos
Definition: cas_error_log.c:59
static char * make_error_log_filename(char *filename_buf, size_t buf_size, const char *br_name)
Definition: cas_error_log.c:64
static int eid
Definition: cas_error_log.c:61
int ut_time_string(char *buf, struct timeval *time_val)
Definition: broker_util.c:421
char * get_cubrid_file(T_CUBRID_FILE_ID fid, char *buf, size_t len)
int shm_proxy_id
Definition: cas.c:154
int cas_shard_flag
Definition: cas.c:143
#define assert(x)
static void cas_log_write_internal(const char *fmt,...)
static char error_log_filepath[BROKER_PATH_MAX]
Definition: cas_error_log.c:58
#define NULL
Definition: freelistheap.h:34
static T_SHM_APPL_SERVER * shm_appl
Definition: broker.c:311
int shm_as_index
Definition: cas.c:151
int shm_shard_id
Definition: cas.c:144
char * cas_error_log_get_eid(char *buf, size_t bufsz)
const char ** p
Definition: dynamic_load.c:945
void cas_error_log_write(int dbms_errno, const char *dbms_errmsg)
int shm_shard_cas_id
Definition: cas.c:155
void cas_error_log_close(bool flag)