diff options
author | Martin Braun <martin.braun@ettus.com> | 2017-07-20 17:36:31 -0700 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2017-07-21 09:24:26 -0700 |
commit | 8223a289727bbda353bd7129512daf00d46d898c (patch) | |
tree | b4e170612b71209e922e72b8d1611d02b2c5e896 /host | |
parent | fabed5f21324473a5b1345ce5d4ff8807715e7eb (diff) | |
download | uhd-8223a289727bbda353bd7129512daf00d46d898c.tar.gz uhd-8223a289727bbda353bd7129512daf00d46d898c.tar.bz2 uhd-8223a289727bbda353bd7129512daf00d46d898c.zip |
C API: Added logging macros
Diffstat (limited to 'host')
-rw-r--r-- | host/include/uhd.h | 1 | ||||
-rw-r--r-- | host/include/uhd/utils/CMakeLists.txt | 1 | ||||
-rw-r--r-- | host/include/uhd/utils/log.h | 96 | ||||
-rw-r--r-- | host/lib/utils/CMakeLists.txt | 1 | ||||
-rw-r--r-- | host/lib/utils/log_c.cpp | 71 |
5 files changed, 170 insertions, 0 deletions
diff --git a/host/include/uhd.h b/host/include/uhd.h index 8647e25e4..4844f153b 100644 --- a/host/include/uhd.h +++ b/host/include/uhd.h @@ -37,5 +37,6 @@ #include <uhd/usrp_clock/usrp_clock.h> #include <uhd/utils/thread_priority.h> +#include <uhd/utils/log.h> #endif /* INCLUDED_UHD_H */ diff --git a/host/include/uhd/utils/CMakeLists.txt b/host/include/uhd/utils/CMakeLists.txt index e176f2c77..2f04b31ff 100644 --- a/host/include/uhd/utils/CMakeLists.txt +++ b/host/include/uhd/utils/CMakeLists.txt @@ -47,6 +47,7 @@ UHD_INSTALL(FILES IF(ENABLE_C_API) UHD_INSTALL(FILES thread_priority.h + log.h DESTINATION ${INCLUDE_DIR}/uhd/utils COMPONENT headers ) diff --git a/host/include/uhd/utils/log.h b/host/include/uhd/utils/log.h new file mode 100644 index 000000000..ce4c30ced --- /dev/null +++ b/host/include/uhd/utils/log.h @@ -0,0 +1,96 @@ +/* + * Copyright 2017 Ettus Research (National Instruments Corp.) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, 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, see <http://www.gnu.org/licenses/>. + */ + +#ifndef INCLUDED_UHD_UTILS_LOG_H +#define INCLUDED_UHD_UTILS_LOG_H + +#include <uhd/config.h> + +typedef enum { + UHD_LOG_LEVEL_TRACE, + UHD_LOG_LEVEL_DEBUG, + UHD_LOG_LEVEL_INFO, + UHD_LOG_LEVEL_WARNING, + UHD_LOG_LEVEL_ERROR, + UHD_LOG_LEVEL_FATAL +} uhd_log_severity_level_t; + +#ifdef __cplusplus +extern "C" { +#endif + + void UHD_API _uhd_log( + const uhd_log_severity_level_t log_level, + const char *filename, + const int lineno, + const char *comp, + const char *format, + ... + ); + +#ifdef __cplusplus +}; +#endif + + +#ifndef __cplusplus +// macro-style logging (compile-time determined) +#if UHD_LOG_MIN_LEVEL < 1 +#define UHD_LOG_TRACE(component, ...) \ + _uhd_log(UHD_LOG_LEVEL_TRACE, __FILE__, __LINE__, component, __VA_ARGS__); +#else +#define UHD_LOG_TRACE(component, ...) +#endif + +#if UHD_LOG_MIN_LEVEL < 2 +#define UHD_LOG_DEBUG(component, ...) \ + _uhd_log(UHD_LOG_LEVEL_DEBUG, __FILE__, __LINE__, component, __VA_ARGS__); +#else +#define UHD_LOG_DEBUG(component, ...) +#endif + +#if UHD_LOG_MIN_LEVEL < 3 +#define UHD_LOG_INFO(component, ...) \ + _uhd_log(UHD_LOG_LEVEL_INFO, __FILE__, __LINE__, component, __VA_ARGS__); +#else +#define UHD_LOG_INFO(component, ...) +#endif + +#if UHD_LOG_MIN_LEVEL < 4 +#define UHD_LOG_WARNING(component, ...) \ + _uhd_log(UHD_LOG_LEVEL_WARNING, __FILE__, __LINE__, component, __VA_ARGS__); +#else +#define UHD_LOG_WARNING(component, ...) +#endif + +#if UHD_LOG_MIN_LEVEL < 5 +#define UHD_LOG_ERROR(component, ...) \ + _uhd_log(UHD_LOG_LEVEL_ERROR, __FILE__, __LINE__, component, __VA_ARGS__); +#else +#define UHD_LOG_ERROR(component, ...) +#endif + +#if UHD_LOG_MIN_LEVEL < 6 +#define UHD_LOG_FATAL(component, ...) \ + _uhd_log(UHD_LOG_LEVEL_FATAL, __FILE__, __LINE__, component, __VA_ARGS__); +#else +#define UHD_LOG_FATAL(component, ...) +#endif + +#endif /* #ifndef __cplusplus */ + +#endif /* INCLUDED_UHD_UTILS_LOG_H */ diff --git a/host/lib/utils/CMakeLists.txt b/host/lib/utils/CMakeLists.txt index a053846c6..0b163c35f 100644 --- a/host/lib/utils/CMakeLists.txt +++ b/host/lib/utils/CMakeLists.txt @@ -181,6 +181,7 @@ LIBUHD_APPEND_SOURCES( IF(ENABLE_C_API) LIBUHD_APPEND_SOURCES( + ${CMAKE_CURRENT_SOURCE_DIR}/log_c.cpp ${CMAKE_CURRENT_SOURCE_DIR}/thread_priority_c.cpp ) ENDIF(ENABLE_C_API) diff --git a/host/lib/utils/log_c.cpp b/host/lib/utils/log_c.cpp new file mode 100644 index 000000000..bd74a4ecc --- /dev/null +++ b/host/lib/utils/log_c.cpp @@ -0,0 +1,71 @@ +/* + * Copyright 2017 Ettus Research (National Instruments Corp) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, 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, see <http://www.gnu.org/licenses/>. + */ + +#include <uhd/utils/log.h> +#include <uhd/utils/log.hpp> +#include <stdarg.h> + + +void UHD_API _uhd_log( + const uhd_log_severity_level_t log_level, + const char *filename, + const int lineno, + const char *component, + const char *format, + ... +) { + int size = 0; + char *c_str = NULL; + va_list ap; + + /* figure out size */ + va_start(ap, format); + size = vsnprintf(c_str, size, format, ap); + va_end(ap); + + if (size < 0) { + return; + } + + /* trailing '\0' */ + size++; + c_str = (char *)malloc(size); + if (!c_str) { + return; + } + + va_start(ap, format); + size = vsnprintf(c_str, size, format, ap); + if (size < 0) { + goto out_free; + } + va_end(ap); + + try { + uhd::_log::log( + static_cast<uhd::log::severity_level>(log_level), + filename, + unsigned(lineno), + component, + boost::this_thread::get_id() + ) << c_str; + } catch (...) {} + +out_free: + free(c_str); + return; +} |