tlog.c 20.8 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
slguan 已提交
16
#define _DEFAULT_SOURCE
H
hzcheng 已提交
17
#include "tutil.h"
S
log  
Shengliang Guan 已提交
18
#include "tlog.h"
L
Liu Jicong 已提交
19

S
ulog  
Shengliang Guan 已提交
20 21 22 23 24
#define LOG_MAX_LINE_SIZE              (1000)
#define LOG_MAX_LINE_BUFFER_SIZE       (LOG_MAX_LINE_SIZE + 10)
#define LOG_MAX_LINE_CONTENT_SIZE      (LOG_MAX_LINE_SIZE - 100)
#define LOG_MAX_LINE_DUMP_SIZE         (65 * 1024)
#define LOG_MAX_LINE_DUMP_BUFFER_SIZE  (LOG_MAX_LINE_DUMP_SIZE + 10)
S
Shengliang Guan 已提交
25
#define LOG_MAX_LINE_DUMP_CONTENT_SIZE (LOG_MAX_LINE_DUMP_SIZE - 100)
S
slguan 已提交
26

S
ulog  
Shengliang Guan 已提交
27 28
#define LOG_FILE_NAME_LEN    300
#define LOG_DEFAULT_BUF_SIZE (20 * 1024 * 1024)  // 20MB
H
hzcheng 已提交
29

S
ulog  
Shengliang Guan 已提交
30 31 32 33 34
#define LOG_DEFAULT_INTERVAL 25
#define LOG_INTERVAL_STEP    5
#define LOG_MIN_INTERVAL     5
#define LOG_MAX_INTERVAL     25
#define LOG_MAX_WAIT_MSEC    1000
D
fix bug  
dapan1121 已提交
35

S
slguan 已提交
36
#define LOG_BUF_BUFFER(x) ((x)->buffer)
S
ulog  
Shengliang Guan 已提交
37 38 39 40
#define LOG_BUF_START(x)  ((x)->buffStart)
#define LOG_BUF_END(x)    ((x)->buffEnd)
#define LOG_BUF_SIZE(x)   ((x)->buffSize)
#define LOG_BUF_MUTEX(x)  ((x)->buffMutex)
S
slguan 已提交
41

H
hzcheng 已提交
42
typedef struct {
L
Liu Jicong 已提交
43
  char           *buffer;
S
slguan 已提交
44 45 46
  int32_t         buffStart;
  int32_t         buffEnd;
  int32_t         buffSize;
D
fix bu  
dapan1121 已提交
47
  int32_t         minBuffSize;
48
  TdFilePtr       pFile;
S
slguan 已提交
49
  int32_t         stop;
H
hzcheng 已提交
50 51
  pthread_t       asyncThread;
  pthread_mutex_t buffMutex;
S
slguan 已提交
52
  tsem_t          buffNotEmpty;
H
hzcheng 已提交
53 54
} SLogBuff;

S
slguan 已提交
55
typedef struct {
L
Liu Jicong 已提交
56 57 58 59 60 61 62 63
  int32_t         fileNum;
  int32_t         maxLines;
  int32_t         lines;
  int32_t         flag;
  int32_t         openInProgress;
  pid_t           pid;
  char            logName[LOG_FILE_NAME_LEN];
  SLogBuff       *logHandle;
S
slguan 已提交
64 65 66
  pthread_mutex_t logMutex;
} SLogObj;

S
Shengliang Guan 已提交
67 68
static int8_t  tsLogInited = 0;
static SLogObj tsLogObj = {.fileNum = 1};
L
Liu Jicong 已提交
69

S
Shengliang Guan 已提交
70
int8_t  tscEmbeddedInUtil = 0;
71
int32_t tsLogKeepDays = 0;
S
Shengliang Guan 已提交
72
bool    tsAsyncLog = true;
S
Shengliang Guan 已提交
73 74 75
int32_t tsNumOfLogLines = 10000000;
int64_t tsAsyncLogLostLines = 0;
int32_t tsWriteInterval = LOG_DEFAULT_INTERVAL;
D
fix bug  
dapan1121 已提交
76

S
Shengliang Guan 已提交
77 78 79
// log
int32_t dDebugFlag = 135;
int32_t vDebugFlag = 135;
S
Shengliang Guan 已提交
80
int32_t mDebugFlag = 131;
S
Shengliang Guan 已提交
81 82
int32_t cDebugFlag = 131;
int32_t jniDebugFlag = 131;
S
Shengliang Guan 已提交
83
int32_t tmrDebugFlag = 131;
S
Shengliang Guan 已提交
84
int32_t uDebugFlag = 131;
S
Shengliang Guan 已提交
85 86
int32_t rpcDebugFlag = 131;
int32_t qDebugFlag = 131;
S
Shengliang Guan 已提交
87
int32_t wDebugFlag = 135;
S
Shengliang Guan 已提交
88
int32_t sDebugFlag = 135;
S
Shengliang Guan 已提交
89
int32_t tsdbDebugFlag = 131;
L
Liu Jicong 已提交
90
int32_t tqDebugFlag = 135;
S
Shengliang Guan 已提交
91
int32_t fsDebugFlag = 135;
D
dapan1121 已提交
92

D
fix bug  
dapan1121 已提交
93
int64_t dbgEmptyW = 0;
D
fix bug  
dapan1121 已提交
94 95 96
int64_t dbgWN = 0;
int64_t dbgSmallWN = 0;
int64_t dbgBigWN = 0;
D
fix bug  
dapan1121 已提交
97 98
int64_t dbgWSize = 0;

L
Liu Jicong 已提交
99
static void     *taosAsyncOutputLog(void *param);
S
slguan 已提交
100 101
static int32_t   taosPushLogBuffer(SLogBuff *tLogBuff, char *msg, int32_t msgLen);
static SLogBuff *taosLogBuffNew(int32_t bufSize);
102
static void      taosCloseLogByFd(TdFilePtr pFile);
S
slguan 已提交
103
static int32_t   taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum);
S
Shengliang Guan 已提交
104
static int32_t   taosCompressFile(char *srcFileName, char *destFileName);
S
slguan 已提交
105 106

static int32_t taosStartLog() {
H
hzcheng 已提交
107 108
  pthread_attr_t threadAttr;
  pthread_attr_init(&threadAttr);
S
slguan 已提交
109
  if (pthread_create(&(tsLogObj.logHandle->asyncThread), &threadAttr, taosAsyncOutputLog, tsLogObj.logHandle) != 0) {
H
hzcheng 已提交
110 111 112 113 114 115
    return -1;
  }
  pthread_attr_destroy(&threadAttr);
  return 0;
}

S
Shengliang Guan 已提交
116
int32_t taosInitLog(const char *logName, int maxFiles) {
S
Shengliang Guan 已提交
117
  if (atomic_val_compare_exchange_8(&tsLogInited, 0, 1) != 0) return 0;
S
osenv  
Shengliang Guan 已提交
118
  osUpdate();
S
Shengliang Guan 已提交
119

S
Shengliang Guan 已提交
120
  char fullName[PATH_MAX] = {0};
S
os env  
Shengliang Guan 已提交
121
  snprintf(fullName, PATH_MAX, "%s" TD_DIRSEP "%s", tsLogDir, logName);
S
Shengliang Guan 已提交
122

S
ulog  
Shengliang Guan 已提交
123
  tsLogObj.logHandle = taosLogBuffNew(LOG_DEFAULT_BUF_SIZE);
S
slguan 已提交
124
  if (tsLogObj.logHandle == NULL) return -1;
S
Shengliang Guan 已提交
125
  if (taosOpenLogFile(fullName, tsNumOfLogLines, maxFiles) < 0) return -1;
H
hzcheng 已提交
126 127 128 129
  if (taosStartLog() < 0) return -1;
  return 0;
}

S
slguan 已提交
130 131 132 133
static void taosStopLog() {
  if (tsLogObj.logHandle) {
    tsLogObj.logHandle->stop = 1;
  }
H
hzcheng 已提交
134 135
}

S
slguan 已提交
136
void taosCloseLog() {
H
hzcheng 已提交
137
  taosStopLog();
L
Liu Jicong 已提交
138
  // tsem_post(&(tsLogObj.logHandle->buffNotEmpty));
S
ulog  
Shengliang Guan 已提交
139
  taosMsleep(LOG_MAX_INTERVAL / 1000);
S
slguan 已提交
140 141
  if (taosCheckPthreadValid(tsLogObj.logHandle->asyncThread)) {
    pthread_join(tsLogObj.logHandle->asyncThread, NULL);
S
slguan 已提交
142
  }
S
slguan 已提交
143 144 145
  // In case that other threads still use log resources causing invalid write in valgrind
  // we comment two lines below.
  // taosLogBuffDestroy(tsLogObj.logHandle);
H
hzcheng 已提交
146 147 148
  // taosCloseLog();
}

149 150
static bool taosLockLogFile(TdFilePtr pFile) {
  if (pFile == NULL) return false;
H
hzcheng 已提交
151

S
slguan 已提交
152
  if (tsLogObj.fileNum > 1) {
153
    int32_t ret = taosLockFile(pFile);
H
hzcheng 已提交
154 155 156 157 158 159 160 161
    if (ret == 0) {
      return true;
    }
  }

  return false;
}

162 163
static void taosUnLockLogFile(TdFilePtr pFile) {
  if (pFile == NULL) return;
H
hzcheng 已提交
164

S
slguan 已提交
165
  if (tsLogObj.fileNum > 1) {
166
    taosUnLockFile(pFile);
H
hzcheng 已提交
167 168 169
  }
}

S
TD-1263  
Shengliang Guan 已提交
170
static void taosKeepOldLog(char *oldName) {
S
TD-1574  
Shengliang Guan 已提交
171
  if (tsLogKeepDays == 0) return;
S
TD-1263  
Shengliang Guan 已提交
172

S
TD-1263  
Shengliang Guan 已提交
173
  int64_t fileSec = taosGetTimestampSec();
S
TD-1263  
Shengliang Guan 已提交
174
  char    fileName[LOG_FILE_NAME_LEN + 20];
S
TD-1263  
Shengliang Guan 已提交
175
  snprintf(fileName, LOG_FILE_NAME_LEN + 20, "%s.%" PRId64, tsLogObj.logName, fileSec);
S
TD-1263  
Shengliang Guan 已提交
176

S
Shengliang Guan 已提交
177
  taosRenameFile(oldName, fileName);
S
TD-1574  
Shengliang Guan 已提交
178 179 180 181 182 183 184 185
  if (tsLogKeepDays < 0) {
    char compressFileName[LOG_FILE_NAME_LEN + 20];
    snprintf(compressFileName, LOG_FILE_NAME_LEN + 20, "%s.%" PRId64 ".gz", tsLogObj.logName, fileSec);
    if (taosCompressFile(fileName, compressFileName) == 0) {
      (void)remove(fileName);
    }
  }

S
os env  
Shengliang Guan 已提交
186
  taosRemoveOldFiles(tsLogDir, TABS(tsLogKeepDays));
S
TD-1263  
Shengliang Guan 已提交
187 188
}

S
slguan 已提交
189
static void *taosThreadToOpenNewFile(void *param) {
S
TD-1263  
Shengliang Guan 已提交
190 191
  char keepName[LOG_FILE_NAME_LEN + 20];
  sprintf(keepName, "%s.%d", tsLogObj.logName, tsLogObj.flag);
H
hzcheng 已提交
192

S
slguan 已提交
193 194
  tsLogObj.flag ^= 1;
  tsLogObj.lines = 0;
S
TD-1263  
Shengliang Guan 已提交
195
  char name[LOG_FILE_NAME_LEN + 20];
S
slguan 已提交
196
  sprintf(name, "%s.%d", tsLogObj.logName, tsLogObj.flag);
H
hzcheng 已提交
197

S
Shengliang Guan 已提交
198
  taosUmaskFile(0);
H
hzcheng 已提交
199

200 201
  TdFilePtr pFile = taosOpenFile(name, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) {
202 203
    tsLogObj.openInProgress = 0;
    tsLogObj.lines = tsLogObj.maxLines - 1000;
204
    uError("open new log file fail! reason:%s, reuse lastlog", strerror(errno));
H
Hui Li 已提交
205 206
    return NULL;
  }
S
TD-1263  
Shengliang Guan 已提交
207

208 209
  taosLockLogFile(pFile);
  (void)taosLSeekFile(pFile, 0, SEEK_SET);
H
hzcheng 已提交
210

211 212
  TdFilePtr pOldFile = tsLogObj.logHandle->pFile;
  tsLogObj.logHandle->pFile = pFile;
S
slguan 已提交
213 214
  tsLogObj.lines = 0;
  tsLogObj.openInProgress = 0;
215
  taosCloseLogByFd(pOldFile);
L
Liu Jicong 已提交
216

S
TD-1263  
Shengliang Guan 已提交
217 218
  uInfo("   new log file:%d is opened", tsLogObj.flag);
  uInfo("==================================");
S
TD-1263  
Shengliang Guan 已提交
219 220
  taosKeepOldLog(keepName);

H
hzcheng 已提交
221 222 223
  return NULL;
}

S
slguan 已提交
224 225
static int32_t taosOpenNewLogFile() {
  pthread_mutex_lock(&tsLogObj.logMutex);
H
hzcheng 已提交
226

S
slguan 已提交
227 228
  if (tsLogObj.lines > tsLogObj.maxLines && tsLogObj.openInProgress == 0) {
    tsLogObj.openInProgress = 1;
H
hzcheng 已提交
229

230
    uInfo("open new log file ......");
H
hzcheng 已提交
231 232 233 234 235 236 237 238 239
    pthread_t      thread;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

    pthread_create(&thread, &attr, taosThreadToOpenNewFile, NULL);
    pthread_attr_destroy(&attr);
  }

S
slguan 已提交
240
  pthread_mutex_unlock(&tsLogObj.logMutex);
H
hzcheng 已提交
241 242 243 244

  return 0;
}

S
slguan 已提交
245 246 247
void taosResetLog() {
  char lastName[LOG_FILE_NAME_LEN + 20];
  sprintf(lastName, "%s.%d", tsLogObj.logName, tsLogObj.flag);
H
hzcheng 已提交
248 249

  // force create a new log file
S
slguan 已提交
250
  tsLogObj.lines = tsLogObj.maxLines + 10;
H
hzcheng 已提交
251 252

  taosOpenNewLogFile();
H
Hui Li 已提交
253
  (void)remove(lastName);
H
hzcheng 已提交
254

255 256
  uInfo("==================================");
  uInfo("   reset log file ");
H
hzcheng 已提交
257 258
}

S
slguan 已提交
259
static bool taosCheckFileIsOpen(char *logFileName) {
260 261
  TdFilePtr pFile = taosOpenFile(logFileName, TD_FILE_WRITE);
  if (pFile == NULL) {
262 263 264 265 266 267
    if (errno == ENOENT) {
      return false;
    } else {
      printf("\nfailed to open log file:%s, reason:%s\n", logFileName, strerror(errno));
      return true;
    }
H
hzcheng 已提交
268 269
  }

270 271 272
  if (taosLockLogFile(pFile)) {
    taosUnLockLogFile(pFile);
    taosCloseFile(&pFile);
H
hzcheng 已提交
273 274
    return false;
  } else {
275
    taosCloseFile(&pFile);
H
hzcheng 已提交
276 277 278 279
    return true;
  }
}

S
slguan 已提交
280 281 282
static void taosGetLogFileName(char *fn) {
  if (tsLogObj.fileNum > 1) {
    for (int32_t i = 0; i < tsLogObj.fileNum; i++) {
H
hzcheng 已提交
283 284 285 286 287 288 289 290 291
      char fileName[LOG_FILE_NAME_LEN];

      sprintf(fileName, "%s%d.0", fn, i);
      bool file1open = taosCheckFileIsOpen(fileName);

      sprintf(fileName, "%s%d.1", fn, i);
      bool file2open = taosCheckFileIsOpen(fileName);

      if (!file1open && !file2open) {
S
slguan 已提交
292
        sprintf(tsLogObj.logName, "%s%d", fn, i);
H
hzcheng 已提交
293 294 295 296 297
        return;
      }
    }
  }

H
Hui Li 已提交
298 299 300
  if (strlen(fn) < LOG_FILE_NAME_LEN) {
    strcpy(tsLogObj.logName, fn);
  }
H
hzcheng 已提交
301 302
}

S
slguan 已提交
303
static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum) {
S
slguan 已提交
304 305
#ifdef WINDOWS
  /*
L
Liu Jicong 已提交
306 307 308
   * always set maxFileNum to 1
   * means client log filename is unique in windows
   */
S
slguan 已提交
309 310 311
  maxFileNum = 1;
#endif

S
Shengliang Guan 已提交
312 313 314
  char    name[LOG_FILE_NAME_LEN + 50] = "\0";
  int32_t logstat0_mtime, logstat1_mtime;
  int32_t size;
H
hzcheng 已提交
315

S
slguan 已提交
316 317
  tsLogObj.maxLines = maxLines;
  tsLogObj.fileNum = maxFileNum;
H
hzcheng 已提交
318 319
  taosGetLogFileName(fn);

H
Hui Li 已提交
320 321 322 323
  if (strlen(fn) < LOG_FILE_NAME_LEN + 50 - 2) {
    strcpy(name, fn);
    strcat(name, ".0");
  }
S
Shengliang Guan 已提交
324
  bool log0Exist = taosStatFile(name, NULL, &logstat0_mtime) >= 0;
H
hzcheng 已提交
325

S
TD-1263  
Shengliang Guan 已提交
326 327 328 329
  if (strlen(fn) < LOG_FILE_NAME_LEN + 50 - 2) {
    strcpy(name, fn);
    strcat(name, ".1");
  }
S
Shengliang Guan 已提交
330 331
  bool log1Exist = taosStatFile(name, NULL, &logstat1_mtime) >= 0;

H
hzcheng 已提交
332
  // if none of the log files exist, open 0, if both exists, open the old one
S
TD-1263  
Shengliang Guan 已提交
333 334 335
  if (!log0Exist && !log1Exist) {
    tsLogObj.flag = 0;
  } else if (!log1Exist) {
S
slguan 已提交
336
    tsLogObj.flag = 0;
S
TD-1263  
Shengliang Guan 已提交
337 338
  } else if (!log0Exist) {
    tsLogObj.flag = 1;
H
hzcheng 已提交
339
  } else {
S
Shengliang Guan 已提交
340
    tsLogObj.flag = (logstat0_mtime > logstat1_mtime) ? 0 : 1;
H
hzcheng 已提交
341 342
  }

H
Hui Li 已提交
343 344
  char fileName[LOG_FILE_NAME_LEN + 50] = "\0";
  sprintf(fileName, "%s.%d", tsLogObj.logName, tsLogObj.flag);
S
slguan 已提交
345
  pthread_mutex_init(&tsLogObj.logMutex, NULL);
H
hzcheng 已提交
346

S
Shengliang Guan 已提交
347
  taosUmaskFile(0);
348
  tsLogObj.logHandle->pFile = taosOpenFile(fileName, TD_FILE_CTEATE | TD_FILE_WRITE);
H
hzcheng 已提交
349

350
  if (tsLogObj.logHandle->pFile == NULL) {
H
Hui Li 已提交
351
    printf("\nfailed to open log file:%s, reason:%s\n", fileName, strerror(errno));
H
hzcheng 已提交
352 353
    return -1;
  }
354
  taosLockLogFile(tsLogObj.logHandle->pFile);
H
hzcheng 已提交
355 356

  // only an estimate for number of lines
S
Shengliang Guan 已提交
357
  int64_t filesize = 0;
358
  if (taosFStatFile(tsLogObj.logHandle->pFile, &filesize, NULL) < 0) {
H
Hui Li 已提交
359
    printf("\nfailed to fstat log file:%s, reason:%s\n", fileName, strerror(errno));
H
Hui Li 已提交
360 361
    return -1;
  }
S
Shengliang Guan 已提交
362
  size = (int32_t)filesize;
S
slguan 已提交
363
  tsLogObj.lines = size / 60;
H
hzcheng 已提交
364

365
  taosLSeekFile(tsLogObj.logHandle->pFile, 0, SEEK_END);
H
hzcheng 已提交
366 367

  sprintf(name, "==================================================\n");
368
  taosWriteFile(tsLogObj.logHandle->pFile, name, (uint32_t)strlen(name));
H
hzcheng 已提交
369
  sprintf(name, "                new log file                      \n");
370
  taosWriteFile(tsLogObj.logHandle->pFile, name, (uint32_t)strlen(name));
H
hzcheng 已提交
371
  sprintf(name, "==================================================\n");
372
  taosWriteFile(tsLogObj.logHandle->pFile, name, (uint32_t)strlen(name));
H
hzcheng 已提交
373 374 375 376

  return 0;
}

B
Bomin Zhang 已提交
377
void taosPrintLog(const char *flags, int32_t dflag, const char *format, ...) {
S
os env  
Shengliang Guan 已提交
378
  if (!osLogSpaceAvailable()) return;
S
slguan 已提交
379

H
hzcheng 已提交
380
  va_list        argpointer;
S
Shengliang Guan 已提交
381
  char           buffer[LOG_MAX_LINE_BUFFER_SIZE] = {0};
S
slguan 已提交
382
  int32_t        len;
H
hzcheng 已提交
383 384 385 386
  struct tm      Tm, *ptm;
  struct timeval timeSecs;
  time_t         curTime;

S
Shengliang Guan 已提交
387
  taosGetTimeOfDay(&timeSecs);
H
hzcheng 已提交
388 389
  curTime = timeSecs.tv_sec;
  ptm = localtime_r(&curTime, &Tm);
S
slguan 已提交
390

391
  len = sprintf(buffer, "%02d/%02d %02d:%02d:%02d.%06d %08" PRId64 " ", ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour,
S
TD-2616  
Shengliang Guan 已提交
392
                ptm->tm_min, ptm->tm_sec, (int32_t)timeSecs.tv_usec, taosGetSelfPthreadId());
H
hzcheng 已提交
393 394 395
  len += sprintf(buffer + len, "%s", flags);

  va_start(argpointer, format);
S
Shengliang Guan 已提交
396
  int32_t writeLen = vsnprintf(buffer + len, LOG_MAX_LINE_CONTENT_SIZE, format, argpointer);
S
slguan 已提交
397
  if (writeLen <= 0) {
S
Shengliang Guan 已提交
398 399 400 401 402 403
    char tmp[LOG_MAX_LINE_DUMP_BUFFER_SIZE] = {0};
    writeLen = vsnprintf(tmp, LOG_MAX_LINE_DUMP_CONTENT_SIZE, format, argpointer);
    strncpy(buffer + len, tmp, LOG_MAX_LINE_CONTENT_SIZE);
    len += LOG_MAX_LINE_CONTENT_SIZE;
  } else if (writeLen >= LOG_MAX_LINE_CONTENT_SIZE) {
    len += LOG_MAX_LINE_CONTENT_SIZE;
S
slguan 已提交
404 405 406
  } else {
    len += writeLen;
  }
H
hzcheng 已提交
407 408
  va_end(argpointer);

S
Shengliang Guan 已提交
409
  if (len > LOG_MAX_LINE_SIZE) len = LOG_MAX_LINE_SIZE;
H
hzcheng 已提交
410 411 412 413

  buffer[len++] = '\n';
  buffer[len] = 0;

414
  if ((dflag & DEBUG_FILE) && tsLogObj.logHandle && tsLogObj.logHandle->pFile != NULL) {
H
hzcheng 已提交
415
    if (tsAsyncLog) {
S
slguan 已提交
416
      taosPushLogBuffer(tsLogObj.logHandle, buffer, len);
H
hzcheng 已提交
417
    } else {
418
      taosWriteFile(tsLogObj.logHandle->pFile, buffer, len);
H
hzcheng 已提交
419 420
    }

S
slguan 已提交
421 422
    if (tsLogObj.maxLines > 0) {
      atomic_add_fetch_32(&tsLogObj.lines, 1);
H
hzcheng 已提交
423

S
slguan 已提交
424
      if ((tsLogObj.lines > tsLogObj.maxLines) && (tsLogObj.openInProgress == 0)) taosOpenNewLogFile();
H
hzcheng 已提交
425 426 427
    }
  }

428
  if (dflag & DEBUG_SCREEN) write(1, buffer, (uint32_t)len);
H
hzcheng 已提交
429 430
}

S
slguan 已提交
431
void taosDumpData(unsigned char *msg, int32_t len) {
S
os env  
Shengliang Guan 已提交
432
  if (!osLogSpaceAvailable()) return;
S
slguan 已提交
433

L
Liu Jicong 已提交
434 435
  char    temp[256];
  int32_t i, pos = 0, c = 0;
H
hzcheng 已提交
436 437 438 439 440 441 442

  for (i = 0; i < len; ++i) {
    sprintf(temp + pos, "%02x ", msg[i]);
    c++;
    pos += 3;
    if (c >= 16) {
      temp[pos++] = '\n';
443
      taosWriteFile(tsLogObj.logHandle->pFile, temp, (uint32_t)pos);
H
hzcheng 已提交
444 445 446 447 448 449 450
      c = 0;
      pos = 0;
    }
  }

  temp[pos++] = '\n';

451
  taosWriteFile(tsLogObj.logHandle->pFile, temp, (uint32_t)pos);
H
hzcheng 已提交
452 453
}

B
Bomin Zhang 已提交
454
void taosPrintLongString(const char *flags, int32_t dflag, const char *format, ...) {
S
os env  
Shengliang Guan 已提交
455
  if (!osLogSpaceAvailable()) return;
S
slguan 已提交
456

H
hzcheng 已提交
457
  va_list        argpointer;
S
Shengliang Guan 已提交
458
  char           buffer[LOG_MAX_LINE_DUMP_BUFFER_SIZE];
S
TD-1364  
Shengliang Guan 已提交
459
  int32_t        len;
H
hzcheng 已提交
460 461 462 463
  struct tm      Tm, *ptm;
  struct timeval timeSecs;
  time_t         curTime;

S
Shengliang Guan 已提交
464
  taosGetTimeOfDay(&timeSecs);
H
hzcheng 已提交
465 466
  curTime = timeSecs.tv_sec;
  ptm = localtime_r(&curTime, &Tm);
S
slguan 已提交
467

468
  len = sprintf(buffer, "%02d/%02d %02d:%02d:%02d.%06d %08" PRId64 " ", ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour,
S
TD-2616  
Shengliang Guan 已提交
469
                ptm->tm_min, ptm->tm_sec, (int32_t)timeSecs.tv_usec, taosGetSelfPthreadId());
H
hzcheng 已提交
470 471 472
  len += sprintf(buffer + len, "%s", flags);

  va_start(argpointer, format);
S
Shengliang Guan 已提交
473
  len += vsnprintf(buffer + len, LOG_MAX_LINE_DUMP_CONTENT_SIZE, format, argpointer);
H
hzcheng 已提交
474 475
  va_end(argpointer);

S
Shengliang Guan 已提交
476
  if (len > LOG_MAX_LINE_DUMP_SIZE) len = LOG_MAX_LINE_DUMP_SIZE;
H
hzcheng 已提交
477 478 479 480

  buffer[len++] = '\n';
  buffer[len] = 0;

481
  if ((dflag & DEBUG_FILE) && tsLogObj.logHandle && tsLogObj.logHandle->pFile != NULL) {
482 483 484
    if (tsAsyncLog) {
      taosPushLogBuffer(tsLogObj.logHandle, buffer, len);
    } else {
485
      taosWriteFile(tsLogObj.logHandle->pFile, buffer, len);
486
    }
L
Liu Jicong 已提交
487

S
slguan 已提交
488 489
    if (tsLogObj.maxLines > 0) {
      atomic_add_fetch_32(&tsLogObj.lines, 1);
H
hzcheng 已提交
490

S
slguan 已提交
491
      if ((tsLogObj.lines > tsLogObj.maxLines) && (tsLogObj.openInProgress == 0)) taosOpenNewLogFile();
H
hzcheng 已提交
492 493 494
    }
  }

495
  if (dflag & DEBUG_SCREEN) write(1, buffer, (uint32_t)len);
H
hzcheng 已提交
496 497
}

S
slguan 已提交
498 499
#if 0
void taosCloseLog() { 
500
  taosCloseLogByFd(tsLogObj.logHandle->pFile); 
S
slguan 已提交
501 502
}
#endif
H
hzcheng 已提交
503

504 505 506 507
static void taosCloseLogByFd(TdFilePtr pFile) {
  if (pFile != NULL) {
    taosUnLockLogFile(pFile);
    taosCloseFile(&pFile);
H
hzcheng 已提交
508 509 510
  }
}

S
slguan 已提交
511
static SLogBuff *taosLogBuffNew(int32_t bufSize) {
H
hzcheng 已提交
512 513 514 515 516 517 518 519 520 521
  SLogBuff *tLogBuff = NULL;

  tLogBuff = calloc(1, sizeof(SLogBuff));
  if (tLogBuff == NULL) return NULL;

  LOG_BUF_BUFFER(tLogBuff) = malloc(bufSize);
  if (LOG_BUF_BUFFER(tLogBuff) == NULL) goto _err;

  LOG_BUF_START(tLogBuff) = LOG_BUF_END(tLogBuff) = 0;
  LOG_BUF_SIZE(tLogBuff) = bufSize;
D
fix bu  
dapan1121 已提交
522
  tLogBuff->minBuffSize = bufSize / 10;
H
hzcheng 已提交
523 524 525
  tLogBuff->stop = 0;

  if (pthread_mutex_init(&LOG_BUF_MUTEX(tLogBuff), NULL) < 0) goto _err;
L
Liu Jicong 已提交
526
  // tsem_init(&(tLogBuff->buffNotEmpty), 0, 0);
H
hzcheng 已提交
527 528 529 530

  return tLogBuff;

_err:
S
TD-1848  
Shengliang Guan 已提交
531 532
  tfree(LOG_BUF_BUFFER(tLogBuff));
  tfree(tLogBuff);
H
hzcheng 已提交
533 534 535
  return NULL;
}

S
slguan 已提交
536 537
#if 0
static void taosLogBuffDestroy(SLogBuff *tLogBuff) {
S
slguan 已提交
538
  tsem_destroy(&(tLogBuff->buffNotEmpty));
H
hzcheng 已提交
539 540
  pthread_mutex_destroy(&(tLogBuff->buffMutex));
  free(tLogBuff->buffer);
S
TD-1848  
Shengliang Guan 已提交
541
  tfree(tLogBuff);
H
hzcheng 已提交
542
}
S
slguan 已提交
543
#endif
H
hzcheng 已提交
544

D
fix bug  
dapan1121 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558
static void taosCopyLogBuffer(SLogBuff *tLogBuff, int32_t start, int32_t end, char *msg, int32_t msgLen) {
  if (start > end) {
    memcpy(LOG_BUF_BUFFER(tLogBuff) + end, msg, msgLen);
  } else {
    if (LOG_BUF_SIZE(tLogBuff) - end < msgLen) {
      memcpy(LOG_BUF_BUFFER(tLogBuff) + end, msg, LOG_BUF_SIZE(tLogBuff) - end);
      memcpy(LOG_BUF_BUFFER(tLogBuff), msg + LOG_BUF_SIZE(tLogBuff) - end, msgLen - LOG_BUF_SIZE(tLogBuff) + end);
    } else {
      memcpy(LOG_BUF_BUFFER(tLogBuff) + end, msg, msgLen);
    }
  }
  LOG_BUF_END(tLogBuff) = (LOG_BUF_END(tLogBuff) + msgLen) % LOG_BUF_SIZE(tLogBuff);
}

S
slguan 已提交
559
static int32_t taosPushLogBuffer(SLogBuff *tLogBuff, char *msg, int32_t msgLen) {
L
Liu Jicong 已提交
560 561 562
  int32_t        start = 0;
  int32_t        end = 0;
  int32_t        remainSize = 0;
D
fix bug  
dapan1121 已提交
563
  static int64_t lostLine = 0;
L
Liu Jicong 已提交
564 565
  char           tmpBuf[40] = {0};
  int32_t        tmpBufLen = 0;
H
hzcheng 已提交
566 567 568 569 570 571 572

  if (tLogBuff == NULL || tLogBuff->stop) return -1;

  pthread_mutex_lock(&LOG_BUF_MUTEX(tLogBuff));
  start = LOG_BUF_START(tLogBuff);
  end = LOG_BUF_END(tLogBuff);

D
fix bug  
dapan1121 已提交
573
  remainSize = (start > end) ? (start - end - 1) : (start + LOG_BUF_SIZE(tLogBuff) - end - 1);
H
hzcheng 已提交
574

D
fix bug  
dapan1121 已提交
575
  if (lostLine > 0) {
L
Liu Jicong 已提交
576
    sprintf(tmpBuf, "...Lost %" PRId64 " lines here...\n", lostLine);
D
dapan1121 已提交
577
    tmpBufLen = (int32_t)strlen(tmpBuf);
D
fix bug  
dapan1121 已提交
578 579 580 581
  }

  if (remainSize <= msgLen || ((lostLine > 0) && (remainSize <= (msgLen + tmpBufLen)))) {
    lostLine++;
S
Shengliang Guan 已提交
582
    tsAsyncLogLostLines++;
H
hzcheng 已提交
583 584 585 586
    pthread_mutex_unlock(&LOG_BUF_MUTEX(tLogBuff));
    return -1;
  }

D
fix bug  
dapan1121 已提交
587 588 589
  if (lostLine > 0) {
    taosCopyLogBuffer(tLogBuff, start, end, tmpBuf, tmpBufLen);
    lostLine = 0;
H
hzcheng 已提交
590
  }
D
fix bug  
dapan1121 已提交
591 592

  taosCopyLogBuffer(tLogBuff, LOG_BUF_START(tLogBuff), LOG_BUF_END(tLogBuff), msg, msgLen);
H
hzcheng 已提交
593

L
Liu Jicong 已提交
594
  // int32_t w = atomic_sub_fetch_32(&waitLock, 1);
D
fix bug  
dapan1121 已提交
595
  /*
D
fix bug  
dapan1121 已提交
596 597
  if (w <= 0 || ((remainSize - msgLen - tmpBufLen) < (LOG_BUF_SIZE(tLogBuff) * 4 /5))) {
    tsem_post(&(tLogBuff->buffNotEmpty));
D
fix bug  
dapan1121 已提交
598 599 600
    dbgPostN++;
  } else {
    dbgNoPostN++;
D
fix bug  
dapan1121 已提交
601
  }
D
fix bug  
dapan1121 已提交
602
  */
H
hzcheng 已提交
603 604 605 606 607 608

  pthread_mutex_unlock(&LOG_BUF_MUTEX(tLogBuff));

  return 0;
}

D
fix bu  
dapan1121 已提交
609
static int32_t taosGetLogRemainSize(SLogBuff *tLogBuff, int32_t start, int32_t end) {
D
fix bug  
dapan1121 已提交
610
  int32_t rSize = end - start;
H
hzcheng 已提交
611

D
fix bug  
dapan1121 已提交
612 613
  return rSize >= 0 ? rSize : LOG_BUF_SIZE(tLogBuff) + rSize;
}
H
hzcheng 已提交
614

D
fix bug  
dapan1121 已提交
615
static void taosWriteLog(SLogBuff *tLogBuff) {
D
fix bu  
dapan1121 已提交
616
  static int32_t lastDuration = 0;
L
Liu Jicong 已提交
617 618 619
  int32_t        remainChecked = 0;
  int32_t        start, end, pollSize;

D
fix bug  
dapan1121 已提交
620
  do {
D
fix bu  
dapan1121 已提交
621 622 623 624 625 626
    if (remainChecked == 0) {
      start = LOG_BUF_START(tLogBuff);
      end = LOG_BUF_END(tLogBuff);

      if (start == end) {
        dbgEmptyW++;
S
Shengliang Guan 已提交
627
        tsWriteInterval = LOG_MAX_INTERVAL;
D
fix bu  
dapan1121 已提交
628 629
        return;
      }
H
hzcheng 已提交
630

D
fix bu  
dapan1121 已提交
631 632
      pollSize = taosGetLogRemainSize(tLogBuff, start, end);
      if (pollSize < tLogBuff->minBuffSize) {
S
Shengliang Guan 已提交
633
        lastDuration += tsWriteInterval;
D
fix bug  
dapan1121 已提交
634
        if (lastDuration < LOG_MAX_WAIT_MSEC) {
D
fix bu  
dapan1121 已提交
635 636 637
          break;
        }
      }
D
fix bug  
dapan1121 已提交
638

D
fix bu  
dapan1121 已提交
639 640 641 642
      lastDuration = 0;
    }

    if (start < end) {
643
      taosWriteFile(tLogBuff->pFile, LOG_BUF_BUFFER(tLogBuff) + start, pollSize);
H
hzcheng 已提交
644
    } else {
L
Liu Jicong 已提交
645
      int32_t tsize = LOG_BUF_SIZE(tLogBuff) - start;
646
      taosWriteFile(tLogBuff->pFile, LOG_BUF_BUFFER(tLogBuff) + start, tsize);
D
fix bug  
dapan1121 已提交
647

648
      taosWriteFile(tLogBuff->pFile, LOG_BUF_BUFFER(tLogBuff), end);
H
hzcheng 已提交
649
    }
D
fix bug  
dapan1121 已提交
650 651

    dbgWN++;
L
Liu Jicong 已提交
652 653
    dbgWSize += pollSize;

D
fix bu  
dapan1121 已提交
654
    if (pollSize < tLogBuff->minBuffSize) {
D
fix bug  
dapan1121 已提交
655
      dbgSmallWN++;
S
Shengliang Guan 已提交
656 657
      if (tsWriteInterval < LOG_MAX_INTERVAL) {
        tsWriteInterval += LOG_INTERVAL_STEP;
D
fix bug  
dapan1121 已提交
658
      }
L
Liu Jicong 已提交
659
    } else if (pollSize > LOG_BUF_SIZE(tLogBuff) / 3) {
D
fix bug  
dapan1121 已提交
660
      dbgBigWN++;
S
Shengliang Guan 已提交
661
      tsWriteInterval = LOG_MIN_INTERVAL;
L
Liu Jicong 已提交
662
    } else if (pollSize > LOG_BUF_SIZE(tLogBuff) / 4) {
S
Shengliang Guan 已提交
663 664
      if (tsWriteInterval > LOG_MIN_INTERVAL) {
        tsWriteInterval -= LOG_INTERVAL_STEP;
D
fix bug  
dapan1121 已提交
665
      }
D
fix bug  
dapan1121 已提交
666 667 668 669
    }

    LOG_BUF_START(tLogBuff) = (LOG_BUF_START(tLogBuff) + pollSize) % LOG_BUF_SIZE(tLogBuff);

D
fix bu  
dapan1121 已提交
670 671 672
    start = LOG_BUF_START(tLogBuff);
    end = LOG_BUF_END(tLogBuff);

D
fix bug  
dapan1121 已提交
673
    pollSize = taosGetLogRemainSize(tLogBuff, start, end);
D
fix bu  
dapan1121 已提交
674
    if (pollSize < tLogBuff->minBuffSize) {
D
fix bug  
dapan1121 已提交
675 676 677
      break;
    }

S
Shengliang Guan 已提交
678
    tsWriteInterval = LOG_MIN_INTERVAL;
D
fix bu  
dapan1121 已提交
679 680

    remainChecked = 1;
L
Liu Jicong 已提交
681
  } while (1);
H
hzcheng 已提交
682 683
}

S
slguan 已提交
684
static void *taosAsyncOutputLog(void *param) {
H
hzcheng 已提交
685
  SLogBuff *tLogBuff = (SLogBuff *)param;
H
Haojun Liao 已提交
686
  setThreadName("log");
L
Liu Jicong 已提交
687

H
hzcheng 已提交
688
  while (1) {
S
Shengliang Guan 已提交
689
    taosMsleep(tsWriteInterval);
H
hzcheng 已提交
690 691

    // Polling the buffer
D
fix bug  
dapan1121 已提交
692
    taosWriteLog(tLogBuff);
H
hzcheng 已提交
693 694 695 696 697 698

    if (tLogBuff->stop) break;
  }

  return NULL;
}
S
Shengliang Guan 已提交
699 700 701 702 703

int32_t taosCompressFile(char *srcFileName, char *destFileName) {
  int32_t compressSize = 163840;
  int32_t ret = 0;
  int32_t len = 0;
L
Liu Jicong 已提交
704
  char   *data = malloc(compressSize);
S
ulog  
Shengliang Guan 已提交
705
  //  gzFile  dstFp = NULL;
S
Shengliang Guan 已提交
706

707 708 709
  // srcFp = fopen(srcFileName, "r");
  TdFilePtr pSrcFile = taosOpenFile(srcFileName, TD_FILE_READ);
  if (pSrcFile == NULL) {
S
Shengliang Guan 已提交
710 711 712 713
    ret = -1;
    goto cmp_end;
  }

714 715
  TdFilePtr pFile = taosOpenFile(destFileName, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) {
S
Shengliang Guan 已提交
716 717 718 719
    ret = -2;
    goto cmp_end;
  }

S
ulog  
Shengliang Guan 已提交
720 721 722 723 724 725 726 727 728 729 730
  //  dstFp = gzdopen(fd, "wb6f");
  //  if (dstFp == NULL) {
  //    ret = -3;
  //    close(fd);
  //    goto cmp_end;
  //  }
  //
  //  while (!feof(srcFp)) {
  //    len = (int32_t)fread(data, 1, compressSize, srcFp);
  //    (void)gzwrite(dstFp, data, len);
  //  }
S
Shengliang Guan 已提交
731 732

cmp_end:
733 734
  if (pSrcFile) {
    taosCloseFile(&pSrcFile);
S
Shengliang Guan 已提交
735
  }
S
ulog  
Shengliang Guan 已提交
736 737 738
  //  if (dstFp) {
  //    gzclose(dstFp);
  //  }
S
Shengliang Guan 已提交
739 740 741 742 743
  free(data);

  return ret;
}

S
Shengliang Guan 已提交
744 745
void taosSetAllDebugFlag(int32_t flag) {
  if (!(flag & DEBUG_TRACE || flag & DEBUG_DEBUG || flag & DEBUG_DUMP)) return;
S
Shengliang Guan 已提交
746 747

  dDebugFlag = flag;
S
Shengliang Guan 已提交
748 749
  vDebugFlag = flag;
  mDebugFlag = flag;
S
Shengliang Guan 已提交
750
  cDebugFlag = flag;
S
Shengliang Guan 已提交
751 752 753 754 755 756 757 758 759 760 761
  jniDebugFlag = flag;
  uDebugFlag = flag;
  rpcDebugFlag = flag;
  qDebugFlag = flag;
  wDebugFlag = flag;
  sDebugFlag = flag;
  tsdbDebugFlag = flag;
  tqDebugFlag = flag;
  fsDebugFlag = flag;

  uInfo("all debug flag are set to %d", flag);
S
Shengliang Guan 已提交
762
}