Переглянути джерело

add logging infrastructure

Julius Pfrommer 9 роки тому
батько
коміт
5a8bd71664
3 змінених файлів з 85 додано та 6 видалено
  1. 9 6
      CMakeLists.txt
  2. 2 0
      src/ua_config.h.in
  3. 74 0
      src/util/ua_log.h

+ 9 - 6
CMakeLists.txt

@@ -41,11 +41,7 @@ set(lib_sources src/ua_types.c
                 ${headers}
                 ${generated_headers})
 
-# build settings
-set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules")
-set(generate_src_options) # the options for the tools that generate code from xml-schema definitions
-
-## compiler flags
+# compiler flags
 if(CMAKE_COMPILER_IS_GNUCC)
 add_definitions(-std=c99 -pedantic -pipe -fstack-protector -Wall -Wextra
                 -Wno-unused-parameter -Wno-unused-function -Wno-unused-label
@@ -58,6 +54,10 @@ add_definitions(-std=c99 -pedantic -pipe -fstack-protector -Wall -Wextra
 	endif(WIN32)
 endif(CMAKE_COMPILER_IS_GNUCC)
 
+# build settings
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules")
+set(generate_src_options) # the options for the tools that generate code from xml-schema definitions
+
 ## auto-generate all types or only the relevant subset?
 option(TYPES_ONLY_NEEDED "Include only compile-needed types" OFF)
 if(TYPES_ONLY_NEEDED)
@@ -102,7 +102,10 @@ else()
     list(APPEND lib_sources src/server/ua_namespace.c)
 endif(ENABLE_MULTITHREADING)
 
-add_library(open62541 ${lib_sources})
+add_library(open62541 ${lib_sources}) # we can add more files to lib_sources later on
+
+## logging
+set(UA_LOGLEVEL 400 CACHE STRING "Level at which logs shall be reported")
 
 ## coverage
 option(ENABLE_COVERAGE "Enable gcov coverage" OFF)

+ 2 - 0
src/ua_config.h.in

@@ -1,6 +1,8 @@
 /* Buid options and configuration (set by cmake) */
 #define UA_ENCODING_AMOUNT @UA_ENCODING_AMOUNT@
 #cmakedefine ENABLE_MULTITHREADING
+#cmakedefine ENABLE_LOGGING
+#define UA_LOGLEVEL @UA_LOGLEVEL@
 
 #cmakedefine MSVC
 #cmakedefine WIN32

+ 74 - 0
src/util/ua_log.h

@@ -0,0 +1,74 @@
+#ifndef UA_LOG_H_
+#define UA_LOG_H_
+
+/**
+   @defgroup logging Logging
+
+   @brief Logging functionality is externally provided to the open62541 libary.
+   That is, every thread that wants logging to take place fills a global
+   variable "logger" with the appropriate callback functions. The UA_LOGLEVEL
+   preprocessor definition indicates which severity of events (trance, debug,
+   info, warning, error, fatal) shall be reported. Enabling logs at a certain
+   level enables all logs at the higher levels also. Furthermore, every
+   log-message has a category that can be used for filtering or to select the
+   output medium (file, stdout, ..).
+*/
+
+enum UA_LoggerCategory {
+	UA_LOGGERCATEGORY_CONNECTION,
+	UA_LOGGERCATEGORY_SESSION,
+	UA_LOGGERCATEGORY_SUBSCRIPTION,
+	UA_LOGGERCATEGORY_MAINTENANCE,
+	UA_LOGGERCATEGORY_LOAD,
+}
+
+typedef struct UA_Logger {
+	log_trace(UA_LoggerCategory category, const char *msg, ...);
+	log_debug(UA_LoggerCategory category, const char *msg, ...);
+	log_info(UA_LoggerCategory category, const char *msg, ...);
+	log_warning(UA_LoggerCategory category, const char *msg, ...);
+	log_error(UA_LoggerCategory category, const char *msg, ...);
+	log_fatal(UA_LoggerCategory category, const char *msg, ...);
+} UA_Logger;
+
+/** The logger is a global variable on the stack. So every thread needs to
+	initialise its own logger. */
+static UA_Logger logger;
+
+#if UA_LOGLEVEL <= 100
+#define LOG_TRACE(CATEGORY, MSG, ...) do{ logger.log_trace(CATEGORY, MSG, __VA_ARGS__); } while
+#else
+#define LOG_TRACE(CATEGORY, MSG, ...) do {} while;
+#endif
+
+#if UA_LOGLEVEL <= 200
+#define LOG_DEBUG(CATEGORY, MSG, ...) do{ logger.log_debug(CATEGORY, MSG, __VA_ARGS__); } while
+#else
+#define LOG_DEBUG(CATEGORY, MSG, ...) do {} while;
+#endif
+
+#if UA_LOGLEVEL <= 300
+#define LOG_INFO(CATEGORY, MSG, ...) do{ logger.log_info(CATEGORY, MSG, __VA_ARGS__); } while
+#else
+#define LOG_INFO(CATEGORY, MSG, ...) do {} while;
+#endif
+
+#if UA_LOGLEVEL <= 400
+#define LOG_WARNING(CATEGORY, MSG, ...) do{ logger.log_warning(CATEGORY, MSG, __VA_ARGS__); } while
+#else
+#define LOG_WARNING(CATEGORY, MSG, ...) do {} while;
+#endif
+
+#if UA_LOGLEVEL <= 500
+#define LOG_ERROR(CATEGORY, MSG, ...) do{ logger.log_error(CATEGORY, MSG, __VA_ARGS__); } while
+#else
+#define LOG_ERROR(CATEGORY, MSG, ...) do {} while;
+#endif
+
+#if UA_LOGLEVEL <= 600
+#define LOG_FATAL(CATEGORY, MSG, ...) do{ logger.log_fatal(CATEGORY, MSG, __VA_ARGS__); } while
+#else
+#define LOG_FATAL(CATEGORY, MSG, ...) do {} while;
+#endif
+
+#endif /* UA_LOG_H_ */