Skip to content

Commit

Permalink
stdout: add new stdout() destination
Browse files Browse the repository at this point in the history
This destination is the same as using the file() destination with /dev/stdout
as a filename. The reason it makes sense to create a separate destination
is that /dev/stdout is not a portable filename that would work on all
UNIXes.

Signed-off-by: Balazs Scheidler <[email protected]>
  • Loading branch information
bazsi committed Sep 2, 2023
1 parent f2f2912 commit 7cb6135
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modules/affile/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set(AFFILE_SOURCES
"poll-file-changes.h"
"poll-multiline-file-changes.h"
"stdin.h"
"stdout.h"
"transport-prockmsg.h"
"wildcard-source.h"
"wildcard-file-reader.h"
Expand All @@ -38,6 +39,7 @@ set(AFFILE_SOURCES
"poll-multiline-file-changes.c"
"regular-files.c"
"stdin.c"
"stdout.c"
"transport-prockmsg.c"
"wildcard-source.c"
"wildcard-file-reader.c"
Expand Down
2 changes: 2 additions & 0 deletions modules/affile/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ modules_affile_libaffile_la_SOURCES = \
modules/affile/linux-kmsg.c \
modules/affile/stdin.c \
modules/affile/stdin.h \
modules/affile/stdout.c \
modules/affile/stdout.h \
modules/affile/affile-source.c \
modules/affile/affile-source.h \
modules/affile/affile-dest.c \
Expand Down
22 changes: 22 additions & 0 deletions modules/affile/affile-grammar.ym
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "plugin.h"
#include "cfg-grammar-internal.h"
#include "stdin.h"
#include "stdout.h"
#include "named-pipe.h"

#include <string.h>
Expand Down Expand Up @@ -102,6 +103,7 @@ affile_grammar_set_wildcard_file_source_driver(WildcardSourceDriver *sd)
%token KW_FORCE_DIRECTORY_POLLING

%token KW_STDIN
%token KW_STDOUT

%type <ptr> source_affile
%type <ptr> source_affile_params
Expand All @@ -110,6 +112,7 @@ affile_grammar_set_wildcard_file_source_driver(WildcardSourceDriver *sd)
%type <ptr> dest_affile
%type <ptr> dest_affile_params
%type <ptr> dest_afpipe_params
%type <ptr> dest_stdout_params

%%

Expand Down Expand Up @@ -246,6 +249,7 @@ multi_line_timeout
dest_affile
: KW_FILE '(' _inner_dest_context_push dest_affile_params _inner_dest_context_pop ')' { $$ = $4; }
| KW_PIPE '(' _inner_dest_context_push dest_afpipe_params _inner_dest_context_pop ')' { $$ = $4; }
| KW_STDOUT '(' _inner_dest_context_push dest_stdout_params _inner_dest_context_pop ')' { $$ = $4; }
;

dest_affile_params
Expand Down Expand Up @@ -301,6 +305,24 @@ dest_affile_common_option
| KW_CREATE_DIRS '(' yesno ')' { affile_dd_set_create_dirs(last_driver, $3); }
;

dest_stdout_params
:
{
last_driver = *instance = stdout_dd_new(configuration);
last_writer_options = &((AFFileDestDriver *) last_driver)->writer_options;
last_file_perm_options = &((AFFileDestDriver *) last_driver)->file_opener_options.file_perm_options;
} dest_stdout_options
;

dest_stdout_options
: dest_stdout_option dest_stdout_options
|
;

dest_stdout_option
: dest_writer_option
| dest_driver_option
;

/* INCLUDE_RULES */

Expand Down
1 change: 1 addition & 0 deletions modules/affile/affile-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static CfgLexerKeyword affile_keywords[] =
{ "fifo", KW_PIPE },
{ "pipe", KW_PIPE },
{ "stdin", KW_STDIN },
{ "stdout", KW_STDOUT },

{ "wildcard_file", KW_WILDCARD_FILE },
{ "base_dir", KW_BASE_DIR },
Expand Down
5 changes: 5 additions & 0 deletions modules/affile/affile-plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ static Plugin affile_plugins[] =
.name = "pipe",
.parser = &affile_parser,
},
{
.type = LL_CONTEXT_DESTINATION,
.name = "stdout",
.parser = &affile_parser,
},
};

gboolean
Expand Down
68 changes: 68 additions & 0 deletions modules/affile/stdout.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2023 Balazs Scheidler <[email protected]>
*
* 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 "file-specializations.h"
#include "transport/transport-file.h"
#include "affile-dest.h"
#include "logproto-file-writer.h"
#include <unistd.h>

static LogTransport *
_construct_transport(FileOpener *self, gint fd)
{
return log_transport_file_new(fd);
}

static LogProtoClient *
_construct_dst_proto(FileOpener *s, LogTransport *transport, LogProtoClientOptions *proto_options)
{
return log_proto_file_writer_new(transport, proto_options, 0, FALSE);
}

static gint
_open(FileOpener *self, const gchar *name, gint flags)
{
return dup(1);
}

FileOpener *
file_opener_for_stdout_new(void)
{
FileOpener *self = file_opener_new();

self->construct_transport = _construct_transport;
self->construct_dst_proto = _construct_dst_proto;
self->open = _open;
return self;
}

LogDriver *
stdout_dd_new(GlobalConfig *cfg)
{
LogTemplate *filename_template = log_template_new(cfg, NULL);

log_template_compile_literal_string(filename_template, "-");
AFFileDestDriver *self = affile_dd_new_instance(filename_template, cfg);

// self->file_reader_options.reader_options.super.stats_source = stats_register_type("stdout");
self->file_opener = file_opener_for_stdout_new();
return &self->super.super;
}
31 changes: 31 additions & 0 deletions modules/affile/stdout.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2023 Balazs Scheidler <[email protected]>
*
* 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.
*
*/

#ifndef AFFILE_STDOUT_H_INCLUDED
#define AFFILE_STDOUT_H_INCLUDED

#include "driver.h"
#include "cfg.h"

LogDriver *stdout_dd_new(GlobalConfig *cfg);

#endif
1 change: 1 addition & 0 deletions tests/copyright/policy
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ modules/timestamp/tests/test_format_date.*$
modules/add-contextual-data/add-contextual-data-glob-selector\.[ch]$
modules/add-contextual-data/tests/test_glob_selector\.c$
modules/affile/tests/test_file_writer\.c$
modules/affile/stdout\.[ch]
modules/afsocket/socket-options-unix\.[ch]$
modules/basicfuncs/vp-funcs\.[ch]$
modules/correlation/group-lines\.[ch]$
Expand Down

0 comments on commit 7cb6135

Please sign in to comment.