File: stardate.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 (136 lines) | stat: -rw-r--r-- 4,084 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2017 Balabit
 *
 * 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 "plugin.h"
#include "plugin-types.h"
#include "template/simple-function.h"
#include "messages.h"
#include "cfg.h"

#include "syslog-ng-config.h"

#include <stdlib.h>
#include <math.h>

static gboolean
_is_leap_year(int year)
{
  return ((year%4==0) && (year % 100!=0)) || (year%400==0);
}

static time_t
year_in_seconds(int year)
{
  if (_is_leap_year(year))
    return 31622400;
  else
    return 31536000;
}

typedef struct
{
  TFSimpleFuncState super;
  int precision;
} TFStardateState;

static guint64 power10[10] = { 1, 10, 100, 1000, 10000,
                               100000, 1000000, 10000000, 100000000, 1000000000
                             };

gboolean
tf_stardate_prepare(LogTemplateFunction *self, gpointer s, LogTemplate *parent,
                    gint argc, gchar *argv[], GError **error)
{
  TFStardateState *state = (TFStardateState *) s;
  state->precision = 2;

  GOptionEntry stardate_options[] =
  {
    { "digits", 'd', 0, G_OPTION_ARG_INT, &state->precision, "Precision: 0-9. Default: 2.", NULL },
    { NULL }
  };

  GOptionContext *ctx = g_option_context_new("stardate");
  g_option_context_add_main_entries(ctx, stardate_options, NULL);

  if (!g_option_context_parse(ctx, &argc, &argv, error))
    {
      g_option_context_free(ctx);
      return FALSE;
    }
  g_option_context_free(ctx);

  if (state->precision < 0 || state->precision > 9)
    {
      g_set_error(error, LOG_TEMPLATE_ERROR, LOG_TEMPLATE_ERROR_COMPILE,
                  "stardate: digits must be between 0-9.\n");
      return FALSE;
    }

  if (argc != 2)
    {
      g_set_error(error, LOG_TEMPLATE_ERROR, LOG_TEMPLATE_ERROR_COMPILE,
                  "stardate: format must be: $(stardate [--digits 2] $UNIXTIME)\n");
      return FALSE;
    }

  if (!tf_simple_func_prepare(self, state, parent, argc, argv, error))
    {
      g_set_error(error, LOG_TEMPLATE_ERROR, LOG_TEMPLATE_ERROR_COMPILE,
                  "stardate: stardate: prepare failed");
      return FALSE;
    }

  return TRUE;
}

static void
tf_stardate_call(LogTemplateFunction *self, gpointer s,
                 const LogTemplateInvokeArgs *args, GString *result, LogMessageValueType *type)
{
  TFStardateState  *state = (TFStardateState *) s;

  char *status;
  time_t time_to_convert = strtol(args->argv[0]->str, &status, 10);

  *type = LM_VT_STRING;
  if (*status)
    {
      msg_error("stardate: wrong format: expected unixtime like value",
                evt_tag_str("got:", args->argv[0]->str));
      return;
    }

  struct tm tm_time;
  localtime_r(&time_to_convert, &tm_time );

  struct tm secs_beginning_year = {.tm_year = tm_time.tm_year, .tm_mday = 1};
  time_t elapsed_seconds = time_to_convert - mktime(&secs_beginning_year);

  double fraction = (double)elapsed_seconds/year_in_seconds(tm_time.tm_year);
  double truncated = (double) floor(fraction * power10[state->precision]) / power10[state->precision];

  g_string_append_printf(result, "%0.*lf", state->precision, 1900 + tm_time.tm_year + truncated);
}

TEMPLATE_FUNCTION(TFStardateState, tf_stardate, tf_stardate_prepare,
                  tf_simple_func_eval, tf_stardate_call, tf_simple_func_free_state, NULL);