File: logtofile_filename.c

package info (click to toggle)
pgauditlogtofile 1.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 288 kB
  • sloc: ansic: 1,211; makefile: 21; sql: 11; sh: 7
file content (92 lines) | stat: -rw-r--r-- 2,553 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*-------------------------------------------------------------------------
 *
 * logtofile_filename.c
 *      Functions to calculate the filename of the log file
 *
 * Copyright (c) 2020-2025, Francisco Miguel Biete Banon
 *
 * This code is released under the PostgreSQL licence, as given at
 *  http://www.postgresql.org/about/licence/
 *-------------------------------------------------------------------------
 */
#include "logtofile_filename.h"

#include <pgtime.h>
#include <datatype/timestamp.h>
#include <utils/timestamp.h>

#include "logtofile_vars.h"

// Private functions
char *pgauditlogtofile_tm2filename(const struct pg_tm *tm);

/**
 * @brief Calculate the current filename of the log file
 * @param void
 * @return char * - the current filename
 */
char *
PgAuditLogToFile_current_filename(void)
{
  pg_time_t timet = timestamptz_to_time_t(GetCurrentTimestamp());
  struct pg_tm *tm = pg_localtime(&timet, log_timezone);

  return pgauditlogtofile_tm2filename(tm);
}

/**
 * @brief Set the next rotation time
 * @param void
 * @return void
 * @note Copied from src/backend/postmaster/syslogger.c
 */
void PgAuditLogToFile_set_next_rotation_time(void)
{
  pg_time_t now;
  struct pg_tm *tm;
  int rotinterval;

  /* nothing to do if time-based rotation is disabled */
  if (guc_pgaudit_ltf_log_rotation_age < 1)
    return;

  /*
   * The requirements here are to choose the next time > now that is a
   * "multiple" of the log rotation interval.  "Multiple" can be interpreted
   * fairly loosely.  In this version we align to log_timezone rather than
   * GMT.
   */
  rotinterval = guc_pgaudit_ltf_log_rotation_age * SECS_PER_MINUTE; /* convert to seconds */
  now = (pg_time_t)time(NULL);
  tm = pg_localtime(&now, log_timezone);
  now += tm->tm_gmtoff;
  now -= now % rotinterval;
  now += rotinterval;
  now -= tm->tm_gmtoff;
  LWLockAcquire(pgaudit_ltf_shm->lock, LW_EXCLUSIVE);
  pgaudit_ltf_shm->next_rotation_time = now;
  LWLockRelease(pgaudit_ltf_shm->lock);
}

/**
 * @brief Convert a pg_tm structure to a filename
 * @param tm - the pg_tm structure
 * @return char * - the filename
 */
char *
pgauditlogtofile_tm2filename(const struct pg_tm *tm)
{
  char *filename = NULL;
  int len;

  filename = palloc(MAXPGPATH * sizeof(char *));
  if (filename != NULL)
  {
    memset(filename, 0, sizeof(char) * MAXPGPATH);
    snprintf(filename, MAXPGPATH, "%s/", guc_pgaudit_ltf_log_directory);
    len = strlen(filename);
    pg_strftime(filename + len, MAXPGPATH - len, guc_pgaudit_ltf_log_filename, tm);
  }

  return filename;
}