Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uprobe_syslog pipe #16

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/upipe/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pkginclude_HEADERS = \
uprobe_prefix.h \
uprobe_select_flows.h \
uprobe_stdio.h \
uprobe_syslog.h \
uprobe_transfer.h \
uprobe_ubuf_mem.h \
uprobe_ubuf_mem_pool.h \
Expand Down
88 changes: 88 additions & 0 deletions include/upipe/uprobe_syslog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2015 Open Broadcast Systems Ltd
*
* Authors: Kieran Kunhya
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/** @file
* @short probe outputting all log events to syslog
*/

#ifndef _UPIPE_UPROBE_SYSLOG_H_
/** @hidden */
#define _UPIPE_UPROBE_SYSLOG_H_
#ifdef __cplusplus
extern "C" {
#endif

#include <upipe/uprobe.h>
#include <upipe/uprobe_helper_uprobe.h>

/** @This is a super-set of the uprobe structure with additional local
* members. */
struct uprobe_syslog {
/** syslog ident **/
char *ident;

/** minimum level of printed messages */
enum uprobe_log_level min_level;

/** structure exported to modules */
struct uprobe uprobe;
};

UPROBE_HELPER_UPROBE(uprobe_syslog, uprobe)

/** @This initializes an already allocated uprobe_syslog structure.
*
* @param uprobe_syslog pointer to the already allocated structure
* @param next next probe to test if this one doesn't catch the event
* @param stream syslog stream to which to log the messages
* @param level level at which to log the messages
* @return pointer to uprobe, or NULL in case of error
*/
struct uprobe *uprobe_syslog_init(struct uprobe_syslog *uprobe_syslog,
struct uprobe *next, char *ident,
int option, int facility,
enum uprobe_log_level min_level);

/** @This cleans a uprobe_syslog structure.
*
* @param uprobe_syslog structure to clean
*/
void uprobe_syslog_clean(struct uprobe_syslog *uprobe_syslog);

/** @This allocates a new uprobe syslog structure.
*
* @param next next probe to test if this one doesn't catch the event
* @param stream syslog stream to which to log the messages
* @param level level at which to log the messages
* @return pointer to uprobe, or NULL in case of error
*/
struct uprobe *uprobe_syslog_alloc(struct uprobe *next, char *ident,
int option, int facility,
enum uprobe_log_level min_level);

#ifdef __cplusplus
}
#endif
#endif
1 change: 1 addition & 0 deletions lib/upipe/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ libupipe_la_SOURCES = \
uprobe_output.c \
uprobe_select_flows.c \
uprobe_stdio.c \
uprobe_syslog.c \
uprobe_transfer.c \
uprobe_ubuf_mem.c \
uprobe_ubuf_mem_pool.c \
Expand Down
120 changes: 120 additions & 0 deletions lib/upipe/uprobe_syslog.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright (C) 2015 Open Broadcast Systems Ltd
*
* Authors: Kieran Kunhya
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/** @file
* @short probe outputting all log events to syslog
*/

#include <upipe/ubase.h>
#include <upipe/uprobe.h>
#include <upipe/uprobe_syslog.h>
#include <upipe/uprobe_helper_alloc.h>
#include <upipe/upipe.h>

#include <stdlib.h>
#include <syslog.h>
#include <string.h>
#include <stdarg.h>

/** @internal @This catches events thrown by pipes.
*
* @param uprobe pointer to probe
* @param upipe pointer to pipe throwing the event
* @param event event thrown
* @param args optional event-specific parameters
* @return an error code
*/
static int uprobe_syslog_throw(struct uprobe *uprobe, struct upipe *upipe,
int event, va_list args)
{
struct uprobe_syslog *uprobe_syslog = uprobe_syslog_from_uprobe(uprobe);
if (event != UPROBE_LOG)
return uprobe_throw_next(uprobe, upipe, event, args);

enum uprobe_log_level level = va_arg(args, enum uprobe_log_level);
if (uprobe_syslog->min_level > level)
return UBASE_ERR_NONE;
int priority;
switch (level) {
case UPROBE_LOG_VERBOSE: priority = LOG_DEBUG; break;
case UPROBE_LOG_DEBUG: priority = LOG_DEBUG; break;
case UPROBE_LOG_NOTICE: priority = LOG_NOTICE; break;
case UPROBE_LOG_WARNING: priority = LOG_WARNING; break;
case UPROBE_LOG_ERROR: priority = LOG_ERR; break;
default: priority = LOG_WARNING; break;
}

const char *msg = va_arg(args, const char *);
syslog(priority, "%s", msg);
return UBASE_ERR_NONE;
}

/** @This initializes an already allocated uprobe_syslog structure.
*
* @param uprobe_syslog pointer to the already allocated structure
* @param next next probe to test if this one doesn't catch the event
* @param ident syslog ident string
* @param option syslog option (see syslog(3))
* @param facility syslog facility (see syslog(3))
* @param level level at which to log the messages
* @return pointer to uprobe, or NULL in case of error
*/
struct uprobe *uprobe_syslog_init(struct uprobe_syslog *uprobe_syslog,
struct uprobe *next, char *ident,
int option, int facility,
enum uprobe_log_level min_level)
{
assert(uprobe_syslog != NULL);
struct uprobe *uprobe = uprobe_syslog_to_uprobe(uprobe_syslog);
uprobe_syslog->ident = strdup(ident);
if (!uprobe_syslog->ident)
return NULL;
uprobe_syslog->min_level = min_level;

openlog(uprobe_syslog->ident, option, facility);

uprobe_init(uprobe, uprobe_syslog_throw, next);
return uprobe;
}

/** @This cleans a uprobe_syslog structure.
*
* @param uprobe_syslog structure to clean
*/
void uprobe_syslog_clean(struct uprobe_syslog *uprobe_syslog)
{
assert(uprobe_syslog != NULL);
struct uprobe *uprobe = uprobe_syslog_to_uprobe(uprobe_syslog);
closelog();
free(uprobe_syslog->ident);
uprobe_syslog->ident = NULL;
uprobe_clean(uprobe);
}

#define ARGS_DECL struct uprobe *next, char *ident, int option, int facility, enum uprobe_log_level min_level
#define ARGS next, ident, option, facility, min_level
UPROBE_HELPER_ALLOC(uprobe_syslog)
#undef ARGS
#undef ARGS_DECL
2 changes: 2 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dist_check_DATA = \

check_PROGRAMS = \
uprobe_stdio_test \
uprobe_syslog_test \
uprobe_prefix_test \
uprobe_dejitter_test \
uprobe_select_flows_test \
Expand Down Expand Up @@ -67,6 +68,7 @@ TESTS = \
ubuf_pic_mem_test \
ubuf_sound_mem_test \
uprobe_stdio_test.sh \
uprobe_syslog_test.sh \
uprobe_prefix_test.sh \
uprobe_dejitter_test \
uprobe_select_flows_test \
Expand Down
60 changes: 60 additions & 0 deletions tests/uprobe_syslog_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2015 Open Broadcast Systems Ltd
*
* Authors: Kieran Kunhya
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/** @file
* @short unit tests for uprobe syslog implementation
*/

#undef NDEBUG

#include <upipe/uprobe.h>
#include <upipe/uprobe_syslog.h>

#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <assert.h>

int main(int argc, char **argv)
{
char *ident = "upipe-test";

struct uprobe *uprobe1 = uprobe_syslog_alloc(NULL, ident, LOG_NDELAY | LOG_PID, LOG_LOCAL0, UPROBE_LOG_DEBUG);
assert(uprobe1 != NULL);

uprobe_err(uprobe1, NULL, "This is an error");
uprobe_warn_va(uprobe1, NULL, "This is a %s warning with %d", "composite",
0x42);
uprobe_notice(uprobe1, NULL, "This is a notice");
uprobe_dbg(uprobe1, NULL, "This is a debug");
uprobe_release(uprobe1);

struct uprobe *uprobe2 = uprobe_syslog_alloc(NULL, ident, LOG_NDELAY | LOG_PID, LOG_LOCAL0, UPROBE_LOG_ERROR);
assert(uprobe2 != NULL);
uprobe_err_va(uprobe2, NULL, "This is another error with %d", 0x43);
uprobe_warn(uprobe2, NULL, "This is a warning that you shouldn't see");
uprobe_release(uprobe2);
return 0;
}
26 changes: 26 additions & 0 deletions tests/uprobe_syslog_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

srcdir="$1"
DIR="`mktemp -d tmp.XXXXXXXXXX`"
./uprobe_stdio_test > "$DIR"/logs
RET=$?
if test $RET -ne 0; then
rm -rf "$DIR"
exit $RET
fi

if ! which valgrind >/dev/null 2>&1; then
echo "#### Please install valgrind for unit tests"
exit 1
fi

unset DIR
FILE="`mktemp tmp.XXXXXXXXXX`"
libtool --mode=execute valgrind -q --leak-check=full ./uprobe_syslog_test > /dev/null 2> "$FILE"
RET=$?
if test -s "$FILE"; then
cat "$FILE" >&2
RET=1
fi
rm -f "$FILE"
exit $RET