user2587779
user2587779

Reputation: 61

Stuck on GStreamer logging

I am trying to have my GStreamer application log information piped through GST_DEBUG and GST_ERROR to a log file. I have tried setting environment variables GST_DEBUG and GST_DEBUG_FILE both in CMakeLists.txt and in code, but have no luck.

Q-1: What do I need to do to fix logging?

Q-2: Can I have logging via GST_DEBUG, GST_ERROR, etc. and stdout at the same time?

When GST_DEBUG("THIS IS A TEST".") is executed, there is no output in the terminal nor the log file.

In `CMakeLists.txt:

find_package(PkgConfig)
pkg_check_modules(GST REQUIRED gstreamer-1.0 gstreamer-plugins-base-1.0 gstreamer-app-1.0)

set(GST_DEBUG "*:6")
set(GST_DEBUG_FILE /var/log/myapp.log)
set(GST_DEBUG_DUMP_DOT_DIR ${ROOT_DIR}/tmp)

add_executable(myapp, ${SOURCE_FILES})
target_compile_options(myapp, ${CMAKE_C_FLAGS} ${BUILD_FLAGS} ${GST_CFLAGS} ${GST_DEBUG_OPTIONS} -g -ggdb -Wall -pedantic -enable-gst-debug=yes)

Note that for some reason I cannot build when prefixing enable-gst-debug=yes with two "-", i.e., "--enable-gst-debug=yes" does not compile, but "-enable-gst-debug=yes" does.

Additionally, the GST_DEBUG_DUMP_DOT_DIR doesn't work from CMakeLists.txt, but if I run my application from a script with "GST_DEBUG_DUMP_DOT_DIR='../tmp' 'myapp'" then the dot files are generated. Figured this might be related.

In C:

GST_DEBUG_CATEGORY_STATIC(myapp)
#define GST_CAT_DEFAULT myapp

// Called previously when main parses args
g_option_context_add_group(pCntx, gst_init_get_option_group());

void init_myapp() {

    gst_init(NULL, NULL);// I know this is rather redundant because gst_init_get_option_group() has been called already.
    GST_DEBUG_CATEGORY_INIT(myapp, "myapp", 0, "Custom debugging cat.");
    setenv("GST_DEBUG", "*:6", 1);
    setenv("GST_DEBUG_FILE", "/var/log/myapp.log", 1);

    GST_DEBUG("THIS IS A TEST.");
}

Upvotes: 1

Views: 2431

Answers (1)

Pablo
Pablo

Reputation: 13580

Cmake is the building system, it has nothing to with environment variables for the running process. What you can do is set your default values with cmake like this:

# for escaped definitions
cmake_policy(SET CMP0005 NEW)

set(DEFAULT_GST_DEBUG "*:6" CACHE STRING "Default value for GST_DEBUG")
set(DEFAULT_GST_DEBUG_FILE /var/log/myapp.log CACHE STRING "Default value for GST_DEBUG_FILE")
set(DEFAULT_GST_DEBUG_DUMP_DOT_DIR ${ROOT_DIR}/tmp CACHE STRING "Default value for GST_DEBUG_DUMP_DOT_DIR")

add_definitions("-DDEFAULT_GST_DEBUG=\"${DEFAULT_GST_DEBUG}\"")
add_definitions("-D=DEFAULT_GST_DEBUG_FILE\"${DEFAULT_GST_DEBUG_FILE}\"")
add_definitions("-DDEFAULT_GST_DEBUG_DUMP_DOT_DIR=\"${DEFAULT_GST_DEBUG_DUMP_DOT_DIR}\"")

...

The correct way of setting the environment variable is by using

$ export VAR=VAL

or

$ VAR=VAL ./a.out

The user must be able to override your default values, don't take that ability from the user.

setenv should also work, but even though the documentation never mentions it, I suspect that these environment variables have to bet set before gst_init(NULL, NULL); is called, because I think this is the place where the environment variables are evaluated, so changing them afterwards would probably have no effect on the behaviour of the logging in GStreamer.

You have also the option to pass argc and argv to gst_init, but the documentation says that unknown command line options cause this function to abort program execution cite, and the API documentation shows how to deal with it.

So you could do it like this:

#include <stdlib.h>

GST_DEBUG_CATEGORY_STATIC(myapp)
#define GST_CAT_DEFAULT myapp

int init_myapp();

int main(int argc, char **argv)
{
    // the setenvs must come before the gst_init
    char *env = getenv("GST_DEBUG");
    if(env == NULL)
        setenv("GST_DEBUG", DEFAULT_GST_DEBUG, 1);

    env = getenv("GST_DEBUG_FILE");
    if(env == NULL)
        setenv("GST_DEBUG_FILE", DEFAULT_GST_DEBUG_FILE, 1);

    env = getenv("GST_DEBUG_DUMP_DOT_DIR");
    if(env == NULL)
        setenv("GST_DEBUG_DUMP_DOT_DIR", DEFAULT_GST_DEBUG_DUMP_DOT_DIR, 1);

    // if you must
    g_option_context_add_group(pCntx, gst_init_get_option_group());

    init_myapp();
    ...
}

int init_myapp()
{


    // this should be the first call!
    gst_init(NULL, NULL);

    GST_DEBUG_CATEGORY_INIT(myapp, "myapp", 0, "Custom debugging cat.");
}

Now you can change the default values in cmake with cmake -DDEFAULT_GST_DEBUG=value or with ccmake.

Hope that helps you.

Upvotes: 1

Related Questions