Skip to content

Commit

Permalink
syslog: closelog() upon unloading
Browse files Browse the repository at this point in the history
Not doing this is an error affecting the case library is unloaded
manually.

* Corrected build-error regarding init/fini mechanism for Windows. Mechanism
  will still not work for Windows (someone has to implement it) but change
  will assert not to break build. Note that -DDENABLE_LIBBLADERF_SYSLOG
  option is default OFF and init/fini is so far used only for non-windows
  initialization of logging (i.e. bisectors should be OK).

* Replaced all _linux_ conditionals with corresponding reversed logic
  for Windows, as Windows is really the odd-bird her not Linux. This
  improves transparency.

* Reverting Logging behaviour to before -DDENABLE_LIBBLADERF_SYSLOG was
  introduced to not break code that expect a certain output (bladeRF-CLI
  parsers e.t.a.)
  • Loading branch information
mambrus committed Nov 6, 2014
1 parent 760325f commit 6430e91
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
4 changes: 2 additions & 2 deletions host/common/src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
#ifdef LOGGING_ENABLED
#include <log.h>
#if defined(__linux__)
#if !defined(WIN32) && !defined(__CYGWIN__) && defined(LOG_SYSLOG_ENABLED)
#include <syslog.h>
#endif
#include <stdio.h>
Expand All @@ -41,7 +41,7 @@ void log_write(bladerf_log_level level, const char *format, ...)

/* Write the log message */
va_start(args, format);
#if !defined(__linux__)
#if defined(WIN32) || defined(__CYGWIN__)
vfprintf(stderr, format, args);
#else
# if defined (LOG_SYSLOG_ENABLED)
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 @@ -1099,6 +1099,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_info("Log verbosity has been set to: %d", level);
#endif
}

/*------------------------------------------------------------------------------
Expand Down
35 changes: 31 additions & 4 deletions host/libraries/libbladeRF/src/init_fini.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,49 @@
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#if defined(__linux__)
#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

/* Module initializers. Especially when used as library which doesn't have a
* natural entry function. Use to set predefined/default states and cleanup.
/* 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__)
#if !defined(WIN32) && !defined(__CYGWIN__) && defined(LOG_SYSLOG_ENABLED)
openlog("bladeRF",
LOG_CONS | LOG_NDELAY | LOG_NOWAIT | LOG_PERROR | LOG_PID, LOG_USER);
bladerf_log_set_verbosity(BLADERF_LOG_LEVEL_INFO);
#endif

log_info("BladeRF host software initializing");
}

void __fini __bladerf_fini(void) {
log_info("BladeRF host software deinitializing");
fflush(NULL);
#if !defined(WIN32) && !defined(__CYGWIN__) && defined(LOG_SYSLOG_ENABLED)
closelog();
#endif
}

0 comments on commit 6430e91

Please sign in to comment.