/* [PG_CUSTOM: DBMS_SCHEDULER] BEGIN * События пакета: журнал сервера и короткий файл data/log/dbms_scheduler.log. * Сбой записи в файл не роняет метод. Координатор сменяет файл, когда он больше 1 МиБ. */ extern "C" { #include "postgres.h" #include "miscadmin.h" #include "storage/fd.h" #include "utils/elog.h" } #include #include #include #include #include #include #include "dbms_scheduler_log.h" namespace DbmsScheduler { static const off_t LOG_LIMIT_BYTES = 1024 * 1024; static const int OUTPUT_MAX = 200; static void logPath(char *path, int pathSize, const char *suffix) { snprintf(path, pathSize, "%s/log/dbms_scheduler.log%s", DataDir, suffix); } static void appendLogFile(const char *context, const char *output) { char path[MAXPGPATH]; char block[OUTPUT_MAX + 160]; int blockLen; int fd; if (DataDir == NULL || DataDir[0] == '\0') return; logPath(path, sizeof(path), ""); fd = open(path, O_CREAT | O_WRONLY | O_APPEND, 0600); if (fd < 0 && errno == ENOENT) { char dir[MAXPGPATH]; snprintf(dir, sizeof(dir), "%s/log", DataDir); if (MakePGDirectory(dir) == 0) fd = open(path, O_CREAT | O_WRONLY | O_APPEND, 0600); } if (fd < 0) return; blockLen = snprintf(block, sizeof(block), "dbms_scheduler: %s\nOutput: %s\n", context, output); if (blockLen > 0) { if (blockLen >= (int) sizeof(block)) blockLen = (int) sizeof(block) - 1; (void) write(fd, block, (size_t) blockLen); } close(fd); } void logEventf(const char *context, const char *fmt, ...) { char output[OUTPUT_MAX + 1]; va_list args; if (context == NULL) context = ""; if (fmt == NULL) fmt = ""; va_start(args, fmt); vsnprintf(output, sizeof(output), fmt, args); va_end(args); ereport(LOG, (errmsg("dbms_scheduler: %s", context), errdetail_internal("Output: %s", output))); appendLogFile(context, output); } void logRotate() { char path[MAXPGPATH]; char previous[MAXPGPATH]; struct stat fileStat; if (DataDir == NULL || DataDir[0] == '\0') return; logPath(path, sizeof(path), ""); if (stat(path, &fileStat) != 0) return; if (fileStat.st_size <= LOG_LIMIT_BYTES) return; logPath(previous, sizeof(previous), ".1"); (void) unlink(previous); (void) rename(path, previous); } }