File: tf-format-date.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (155 lines) | stat: -rw-r--r-- 4,529 bytes parent folder | download | duplicates (2)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * Copyright (c) 2022 Balazs Scheidler <bazsi77@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * 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.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */
#include "tf-format-date.h"
#include "timeutils/conv.h"


/*
 * $(format-date [options] format-string [timestamp])
 *
 * $(format-date) takes a timestamp in the DATETIME representation and
 * formats it according to an strftime() format string.  The DATETIME
 * representation in syslog-ng is a UNIX timestamp formatted as a decimal
 * number, with an optional fractional part, where the seconds and the
 * fraction of seconds are separated by a dot.
 *
 * If the timestamp argument is missing, the timestamp of the message is
 * used.
 *
 * Options:
 *   --time-zone <TZstring>
 */
typedef struct _TFFormatDate
{
  TFSimpleFuncState super;
  TimeZoneInfo *time_zone_info;
  gchar *format;
} TFFormatDate;

static gboolean
tf_format_date_prepare(LogTemplateFunction *self, gpointer s, LogTemplate *parent, gint argc, gchar *argv[],
                       GError **error)
{
  TFFormatDate *state = (TFFormatDate *) s;
  gchar *time_zone = NULL;
  GOptionContext *ctx;
  GOptionEntry format_date_options[] =
  {
    { "time-zone", 'Z', 0, G_OPTION_ARG_STRING, &time_zone, NULL, NULL },
    { NULL }
  };
  gboolean result = FALSE;

  ctx = g_option_context_new("format-date");
  g_option_context_add_main_entries(ctx, format_date_options, NULL);

  if (!g_option_context_parse(ctx, &argc, &argv, error))
    {
      g_option_context_free(ctx);
      goto exit;
    }
  g_option_context_free(ctx);

  if (time_zone)
    {
      state->time_zone_info = time_zone_info_new(time_zone);
      if (!state->time_zone_info)
        {
          g_set_error(error, LOG_TEMPLATE_ERROR, LOG_TEMPLATE_ERROR_COMPILE,
                      "$(format-date): Error loading timezone information for %s", time_zone);
          goto exit;
        }
    }

  if (argc > 1)
    {
      /* take off format string if that our next argument */
      state->format = g_strdup(argv[1]);

      /* fill up slot in argv array */
      argv[1] = argv[0];
      argv++;
      argc--;
    }

  /* skip the extracted format argument */
  if (!tf_simple_func_prepare(self, state, parent, argc, argv, error))
    {
      goto exit;
    }
  result = TRUE;

exit:
  /* glib supplies us with duplicated strings that we are responsible for! */
  g_free(time_zone);
  return result;
}

static void
tf_format_date_call(LogTemplateFunction *self, gpointer s, const LogTemplateInvokeArgs *args, GString *result,
                    LogMessageValueType *type)
{
  TFFormatDate *state = (TFFormatDate *) s;
  UnixTime ut;

  *type = LM_VT_STRING;

  if (state->super.argc != 0)
    {
      const gchar *ts = args->argv[0]->str;
      if (!type_cast_to_datetime_unixtime(ts, -1, &ut, NULL))
        {
          ut.ut_sec = 0;
          ut.ut_usec = 0;
          ut.ut_gmtoff = -1;
        }
    }
  else
    {
      ut = args->messages[0]->timestamps[LM_TS_STAMP];
    }

  WallClockTime wct;

  gint tz_override = state->time_zone_info ? time_zone_info_get_offset(state->time_zone_info, ut.ut_sec) : -1;
  convert_unix_time_to_wall_clock_time_with_tz_override(&ut, &wct, tz_override);

  gchar buf[64];
  gint len = strftime(buf, sizeof(buf), state->format, &wct.tm);

  if (len > 0)
    g_string_append_len(result, buf, len);
}

static void
tf_format_date_free_state(gpointer s)
{
  TFFormatDate *state = (TFFormatDate *) s;

  g_free(state->format);
  if (state->time_zone_info)
    time_zone_info_free(state->time_zone_info);
  tf_simple_func_free_state(&state->super);
}

TEMPLATE_FUNCTION(TFFormatDate, tf_format_date, tf_format_date_prepare, tf_simple_func_eval, tf_format_date_call,
                  tf_format_date_free_state, NULL);