-#ifdef CONFIG_FEATURE_IPC_SYSLOG
- if ((option_mask & OPT_circularlog) && shbuf) {
- char b[1024];
-
- va_start(arguments, fmt);
- vsnprintf(b, sizeof(b) - 1, fmt, arguments);
- va_end(arguments);
- circ_message(b);
-
- } else
-#endif
- fd = device_open(logFilePath, O_WRONLY | O_CREAT
- | O_NOCTTY | O_APPEND | O_NONBLOCK);
- if (fd >= 0) {
- fl.l_type = F_WRLCK;
- fcntl(fd, F_SETLKW, &fl);
-
-#ifdef CONFIG_FEATURE_ROTATE_LOGFILE
- if (ENABLE_FEATURE_ROTATE_LOGFILE && logFileSize > 0 ) {
- struct stat statf;
- int r = fstat(fd, &statf);
- if (!r && (statf.st_mode & S_IFREG)
- && (lseek(fd,0,SEEK_END) > logFileSize)) {
- if (logFileRotate > 0) {
- int i = strlen(logFilePath) + 4;
- char oldFile[i];
- char newFile[i];
- for (i=logFileRotate-1; i>0; i--) {
- sprintf(oldFile, "%s.%d", logFilePath, i-1);
- sprintf(newFile, "%s.%d", logFilePath, i);
- rename(oldFile, newFile);
- }
- sprintf(newFile, "%s.%d", logFilePath, 0);
- fl.l_type = F_UNLCK;
- fcntl(fd, F_SETLKW, &fl);
- close(fd);
- rename(logFilePath, newFile);
- fd = device_open(logFilePath,
- O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
- O_NONBLOCK);
- fl.l_type = F_WRLCK;
- fcntl(fd, F_SETLKW, &fl);
- } else {
- ftruncate(fd, 0);
- }
+#if ENABLE_FEATURE_ROTATE_LOGFILE
+ if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) {
+ if (G.logFileRotate) { /* always 0..99 */
+ int i = strlen(G.logFilePath) + 3 + 1;
+ char oldFile[i];
+ char newFile[i];
+ i = G.logFileRotate - 1;
+ /* rename: f.8 -> f.9; f.7 -> f.8; ... */
+ while (1) {
+ sprintf(newFile, "%s.%d", G.logFilePath, i);
+ if (i == 0) break;
+ sprintf(oldFile, "%s.%d", G.logFilePath, --i);
+ rename(oldFile, newFile);