From e43cf6b553f748d8ad118d408c740a4f9cba767f Mon Sep 17 00:00:00 2001 From: David Sidrane Date: Tue, 12 May 2015 13:18:59 -1000 Subject: [PATCH] Added POSIX File event tracer --- libuavcan_drivers/posix_tools/include.mk | 7 ++ .../include/posix_tools/file_event_tracer.hpp | 76 +++++++++++++++++++ .../posix_tools/file_storage_backend.hpp | 10 +-- 3 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 libuavcan_drivers/posix_tools/include.mk create mode 100644 libuavcan_drivers/posix_tools/include/posix_tools/file_event_tracer.hpp rename libuavcan_drivers/{ => posix_tools}/include/posix_tools/file_storage_backend.hpp (93%) diff --git a/libuavcan_drivers/posix_tools/include.mk b/libuavcan_drivers/posix_tools/include.mk new file mode 100644 index 0000000000..84ad8e1744 --- /dev/null +++ b/libuavcan_drivers/posix_tools/include.mk @@ -0,0 +1,7 @@ +# +# Copyright (C) 2015 David Sidrane +# + +LIBUAVCAN_POSIX_DIR := $(dir $(lastword $(MAKEFILE_LIST))) + +LIBUAVCAN_POSIX_INC := $(LIBUAVCAN_POSIX_DIR)include/ diff --git a/libuavcan_drivers/posix_tools/include/posix_tools/file_event_tracer.hpp b/libuavcan_drivers/posix_tools/include/posix_tools/file_event_tracer.hpp new file mode 100644 index 0000000000..1d69bd932e --- /dev/null +++ b/libuavcan_drivers/posix_tools/include/posix_tools/file_event_tracer.hpp @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2015 Pavel Kirienko + */ + +#pragma once + +#include +#include + +namespace uavcan_posix +{ +namespace dynamic_node_id_server +{ +/** + * This interface implements a POSIX compliant file based IEventTracer interface + */ +class FileEventTracer : public uavcan::dynamic_node_id_server::IEventTracer +{ + /** + * Maximum length of full path to log file + */ + + enum { MaxPathLength = 128, FormatBufferLength = 64 }; + + + /** + * This type is used for the path + */ + typedef uavcan::Array, + uavcan::ArrayModeDynamic, MaxPathLength> PathString; + + + PathString path_; + + +public: + + FileEventTracer() { } + + virtual void onEvent(uavcan::dynamic_node_id_server::TraceCode code, uavcan::int64_t argument) + { + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + int fd = open(path_.c_str(), O_WRONLY | O_CREAT | O_APPEND); + if (fd >= 0 ) + { + char buffer[FormatBufferLength + 1]; + int n = snprintf(buffer, FormatBufferLength, "%d.%ld,%d,%lld\n", ts.tv_sec, ts.tv_nsec, code, argument); + write(fd, buffer, n); + close(fd); + } + } + /** + * Initializes the File based event trace + * + */ + + int init(const PathString & path) + { + using namespace std; + + int rv = -uavcan::ErrInvalidParam; + + if (path.size() > 0) + { + path_ = path.c_str(); + rv = open(path_.c_str(), O_WRONLY | O_CREAT | O_TRUNC); + close(rv); + } + return rv; + } + +}; + +} +} diff --git a/libuavcan_drivers/include/posix_tools/file_storage_backend.hpp b/libuavcan_drivers/posix_tools/include/posix_tools/file_storage_backend.hpp similarity index 93% rename from libuavcan_drivers/include/posix_tools/file_storage_backend.hpp rename to libuavcan_drivers/posix_tools/include/posix_tools/file_storage_backend.hpp index 67762606ac..5595f3b478 100644 --- a/libuavcan_drivers/include/posix_tools/file_storage_backend.hpp +++ b/libuavcan_drivers/posix_tools/include/posix_tools/file_storage_backend.hpp @@ -94,16 +94,16 @@ public: * Initializes the File based back end storage by passing to a path to * the directory where the key named files will be stored. * This the return result should be 0 on success. - * If it is -EFBIG then the the path name is too long to + * If it is -ErrInvalidConfiguration then the the path name is too long to * Accommodate the trailing slash and max key length; * */ - int init(const String & path) + int init(const PathString & path) { using namespace std; - int rv = -EINVAL; + int rv = -uavcan::ErrInvalidParam; if (path.size() > 0) { @@ -115,8 +115,8 @@ public: base_path.pop_back(); } + rv = 0; struct stat sb; - if (stat(base_path.c_str(), &sb) != 0 || !S_ISDIR(sb.st_mode)) { rv = mkdir(base_path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO); @@ -124,7 +124,7 @@ public: base_path.push_back('/'); if ((base_path.size() + MaxStringLength) > MaxPathLength) { - rv = -EFBIG; + rv = -uavcan::ErrInvalidConfiguration; } } return rv;