Selaa lähdekoodia

added a simple logger (writes to stdout)

Julius Pfrommer 9 vuotta sitten
vanhempi
commit
0caf71abc7
5 muutettua tiedostoa jossa 64 lisäystä ja 12 poistoa
  1. 1 0
      CMakeLists.txt
  2. 44 0
      examples/logger_stdout.c
  3. 7 0
      examples/logger_stdout.h
  4. 4 4
      examples/opcuaServer.c
  5. 8 8
      src/util/ua_log.h

+ 1 - 0
CMakeLists.txt

@@ -134,6 +134,7 @@ add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_namespace_0.c
 
 # build example server
 add_executable(exampleServer examples/opcuaServer.c
+                             examples/logger_stdout.c
                              examples/networklayer.c)
 target_link_libraries(exampleServer open62541)
 if(WIN32)

+ 44 - 0
examples/logger_stdout.c

@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <stdarg.h>
+
+#include "logger_stdout.h"
+#include "ua_log.h"
+#include "ua_types.h"
+
+void print_time() {
+	UA_DateTime now = UA_DateTime_now();
+	UA_ByteString str;
+	UA_DateTime_toString(now, &str);
+	printf("\"%.*s\"}", str.length, str.data);
+	UA_ByteString_deleteMembers(&str);
+}
+
+#define LOG_FUNCTION(LEVEL) \
+	void log_##LEVEL(UA_LoggerCategory category, const char *msg, ...) { \
+		va_list args;												   \
+		puts("##LEVEL - ");											   \
+		print_time();												   \
+		puts(" - ");												   \
+		va_start(args, msg);										   \
+		vprintf(msg, args);											   \
+		puts("\n");													   \
+		va_end(args);												   \
+	}
+
+LOG_FUNCTION(trace)
+LOG_FUNCTION(debug)
+LOG_FUNCTION(info)
+LOG_FUNCTION(warning)
+LOG_FUNCTION(error)
+LOG_FUNCTION(fatal)
+
+void Logger_Stdout_init(void *config) {
+	logger = (UA_Logger){
+		.log_trace = log_trace,
+		.log_debug = log_debug,
+		.log_info = log_info,
+		.log_warning = log_warning,
+		.log_error = log_error,
+		.log_fatal = log_fatal
+	};
+}

+ 7 - 0
examples/logger_stdout.h

@@ -0,0 +1,7 @@
+#ifndef LOGGER_STDOUT_H_
+#define LOGGER_STDOUT_H_
+
+/** Initialises the logger for the current thread. */
+void Logger_Stdout_init(void *config);
+
+#endif /* LOGGER_STDOUT_H_ */

+ 4 - 4
examples/opcuaServer.c

@@ -1,9 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "networklayer.h"
-#include "ua_application.h"
-
 #ifndef WIN32
 #include <sys/mman.h>
 #include <sys/wait.h>
@@ -12,8 +9,11 @@
 #include <sys/types.h>
 #include <time.h>
 #include <fcntl.h>
-
 #include <signal.h>
+
+#include "logger_stdout.h"
+#include "networklayer.h"
+#include "ua_application.h"
 #include "ua_channel_manager.h"
 #include "ua_session_manager.h"
 #include "ua_server.h"

+ 8 - 8
src/util/ua_log.h

@@ -14,21 +14,21 @@
    output medium (file, stdout, ..).
 */
 
-enum UA_LoggerCategory {
+typedef enum UA_LoggerCategory {
 	UA_LOGGERCATEGORY_CONNECTION,
 	UA_LOGGERCATEGORY_SESSION,
 	UA_LOGGERCATEGORY_SUBSCRIPTION,
 	UA_LOGGERCATEGORY_MAINTENANCE,
 	UA_LOGGERCATEGORY_LOAD,
-}
+} UA_LoggerCategory;
 
 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, ...);
+	void (*log_trace)(UA_LoggerCategory category, const char *msg, ...);
+	void (*log_debug)(UA_LoggerCategory category, const char *msg, ...);
+	void (*log_info)(UA_LoggerCategory category, const char *msg, ...);
+	void (*log_warning)(UA_LoggerCategory category, const char *msg, ...);
+	void (*log_error)(UA_LoggerCategory category, const char *msg, ...);
+	void (*log_fatal)(UA_LoggerCategory category, const char *msg, ...);
 } UA_Logger;
 
 /** The logger is a global variable on the stack. So every thread needs to