Skip to content

Commit

Permalink
libbladeRF: Added syslog support (Linux, OSX)
Browse files Browse the repository at this point in the history
OpenBTS and YateBTS fork a transceiver sub-process and redirect stderr
and stdout, which effectively hide any bladerf logs. For debugging
time-critical code, printf debugging (or logging) is simple, yet
practically essential. libbladeRF logging via syslog is opt-in via the
CMake ENABLE_LIBBLADERF_SYSLOG option, which defaults to OFF.

To make this as transparent as possible, a generic mechanism for
initializing libraries without having to use any specific pre-known
function has been introduced (see init_fini.c). Currently its only real
purpose is to closelog() upon unloading library. Failing doing this is
an error affecting the case when the library is unloaded manually.

When the syslog support is enabled, the libbladeRF default log level is
reduced to WARNING, rather than INFO to avoid noise in logs.
  • Loading branch information
mambrus authored and jynik committed Jan 14, 2015
1 parent 8fab83f commit 344d497
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
43 changes: 43 additions & 0 deletions host/common/src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
*/
#ifdef LOGGING_ENABLED
#include <log.h>
#if !defined(WIN32) && !defined(__CYGWIN__) && defined(LOG_SYSLOG_ENABLED)
#include <syslog.h>
#endif
#include <stdio.h>
#include <stdarg.h>

Expand All @@ -38,7 +41,47 @@ void log_write(bladerf_log_level level, const char *format, ...)

/* Write the log message */
va_start(args, format);
#if defined(WIN32) || defined(__CYGWIN__)
vfprintf(stderr, format, args);
#else
# if defined (LOG_SYSLOG_ENABLED)
{
int syslog_level;

switch (level) {
case BLADERF_LOG_LEVEL_VERBOSE:
case BLADERF_LOG_LEVEL_DEBUG:
syslog_level = LOG_DEBUG;
break;

case BLADERF_LOG_LEVEL_INFO:
syslog_level = LOG_INFO;
break;

case BLADERF_LOG_LEVEL_WARNING:
syslog_level = LOG_WARNING;
break;

case BLADERF_LOG_LEVEL_ERROR:
syslog_level = LOG_ERR;
break;

case BLADERF_LOG_LEVEL_CRITICAL:
syslog_level = LOG_CRIT;
break;

default:
/* Shouldn't be used, so just route it to a low level */
syslog_level = LOG_DEBUG;
break;
}

vsyslog(syslog_level | LOG_USER, format, args);
}
# else
vfprintf(stderr, format, args);
# endif
#endif
va_end(args);
}
}
Expand Down
7 changes: 7 additions & 0 deletions host/libraries/libbladeRF/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ endif()

option(ENABLE_LIBBLADERF_LOGGING "Enable log messages in libbladeRF." ON)

option(ENABLE_LIBBLADERF_SYSLOG "Enable logging to syslog (Linux/OSX)" OFF)

option(BUILD_LIBBLADERF_DOCUMENTATION "Build libbladeRF documentation. Requries Doxygen." ${BUILD_DOCUMENTATION})
if(NOT ${BUILD_DOCUMENTATION})
set(BUILD_LIBBLADERF_DOCUMENTATION OFF)
Expand Down Expand Up @@ -116,6 +118,10 @@ if(ENABLE_LIBBLADERF_LOGGING)
add_definitions(-DLOGGING_ENABLED)
endif()

if(ENABLE_LIBBLADERF_SYSLOG)
add_definitions(-DLOG_SYSLOG_ENABLED)
endif()

if(ENABLE_LOCK_CHECKS)
add_definitions(-DENABLE_LOCK_CHECKS)
endif()
Expand Down Expand Up @@ -269,6 +275,7 @@ set(LIBBLADERF_SOURCE
src/sync_worker.c
src/tuning.c
src/version_compat.c
src/init_fini.c
${BLADERF_HOST_COMMON_SOURCE_DIR}/sha256.c
${BLADERF_HOST_COMMON_SOURCE_DIR}/conversions.c
${BLADERF_HOST_COMMON_SOURCE_DIR}/log.c
Expand Down
1 change: 1 addition & 0 deletions host/libraries/libbladeRF/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ more information.
| -DENABLE_BACKEND_CYAPI=\<ON/OFF\>a | Enables (Windows-only) Cypress driver/library based backend. Default: ON if the FX3 SDK is available, OFF otherwise. |
| -DENABLE_BACKEND_DUMMY=\<ON/OFF\> | Enables dummy backend support. Only useful for some developers. Default: OFF |
| -DENABLE_LIBBLADERF_LOGGING=\<ON/OFF\> | Enable log messages. Default: ON |
| -DENABLE_LIBBLADERF_SYSLOG=\<ON/OFF\> | Enable log messages to syslog (Linux/OSX) if ENABLE_LIBBLADERF_LOGGING is enabled. Default: OFF |
| -DENABLE_LIBBLADERF_SYNC_LOG_VERBOSE=\<ON/OFF\> | Enable log_verbose() calls in the sync interface's data path. Note that this may harm performance. Default: OFF |
| -DENABLE_LOCK_CHECKS=\<ON/OFF\> | Enable checks for lock acquistion failures (e.g., deadlock). Default: OFF |
| -DENABLE_USB_DEV_RESET_ON_OPEN=\<ON/OFF\> | Enable USB port reset when opening a device. Defaults to ON for Linux, OFF otherwise. |
Expand Down
3 changes: 3 additions & 0 deletions host/libraries/libbladeRF/src/bladerf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,9 @@ void bladerf_version(struct bladerf_version *version)
void bladerf_log_set_verbosity(bladerf_log_level level)
{
log_set_verbosity(level);
#if defined(LOG_SYSLOG_ENABLED)
log_debug("Log verbosity has been set to: %d", level);
#endif
}

/*------------------------------------------------------------------------------
Expand Down
83 changes: 83 additions & 0 deletions host/libraries/libbladeRF/src/init_fini.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* This file is part of the bladeRF project:
* http://www.github.com/nuand/bladeRF
*
* Copyright (C) 2013 Nuand LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include "conversions.h"
#include "version.h"
#if !defined(WIN32) && !defined(__CYGWIN__) && defined(LOG_SYSLOG_ENABLED)
#include <syslog.h>
#endif
#include "log.h"

#if !defined(WIN32) && !defined(__CYGWIN__)
#if !defined(__clang__) && !defined(__GNUC__)
#error init/fini mechanism not known to work for your compiler.
#endif
#define __init __attribute__((constructor))
#define __fini __attribute__((destructor))
#else
/* Corresponding syntax for Windows (TBD) */
#define __init
#define __fini
#endif

#ifdef LOG_SYSLOG_ENABLED
# define DEF_LOG_LEVEL BLADERF_LOG_LEVEL_WARNING
#else
# define DEF_LOG_LEVEL BLADERF_LOG_LEVEL_INFO
#endif

/* Module initializers/deinitializers. When used as library (who don't have
* a natural entry/exit function) these are used to initialize
* deinitialize. Use to set predefined/default states and cleanup.
*
* This will work with shared libraries as well as with static as they get
* invoked by RTL load/unload, with or without C++ code (i.e. functions will
* play nice with C++ normal ctors/dtors).
*
* Keep log in to at least once per new build-/run-environment assert that
* the mechanism works.
*/


void __init __bladerf_init(void)
{
#if !defined(WIN32) && !defined(__CYGWIN__) && defined(LOG_SYSLOG_ENABLED)
openlog("bladeRF",
LOG_CONS | LOG_NDELAY | LOG_NOWAIT | LOG_PERROR | LOG_PID,
LOG_USER);
#endif

bladerf_log_set_verbosity(DEF_LOG_LEVEL);
log_debug("libbladeRF %s: initializing\n", LIBBLADERF_VERSION);
}

void __fini __bladerf_fini(void)
{
bladerf_log_set_verbosity(DEF_LOG_LEVEL);
log_debug("libbladeRF %s: deinitializing\n", LIBBLADERF_VERSION);
fflush(NULL);
#if !defined(WIN32) && !defined(__CYGWIN__) && defined(LOG_SYSLOG_ENABLED)
closelog();
#endif
}

0 comments on commit 344d497

Please sign in to comment.