MatLogger2  1.0.0
Library for logging of numeric data to HDF5 MAT-files, which is RT-safe and multithreaded.
matlogger2_backend.cpp
Go to the documentation of this file.
1 /* Part of this code is taken from ADVRHumanoids/XBotInterface, SoLib.h
2  *
3  * Copyright (C) 2017 IIT-ADVR
4  * Author: Giuseppe Rigano
5  * email: giuseppe.rigano@iit.it
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>
19 */
20 
21 
22 
23 #include <map>
24 #include <string>
25 #include <iostream>
26 #include <cstdio>
27 #include <dlfcn.h>
28 
29 #include <boost/algorithm/string.hpp>
30 
31 #include "matlogger2_backend.h"
32 
33 
34 namespace
35 {
36 
37 template <class T>
38 static std::unique_ptr<T> GetFactory(const std::string& lib_name)
39 {
40 
41  char * error;
42  void * lib_handle;
43 
44  lib_handle = dlopen(lib_name.c_str(), RTLD_NOW);
45 
46  if(!lib_handle)
47  {
48  return nullptr;
49  }
50  else
51  {
52 
53  T*(*create)();
54  create = (T*(*)()) dlsym(lib_handle, "create_instance");
55  if((error = dlerror()) != NULL)
56  {
57  fprintf(stderr, "Error in loading library '%s':\n%s", lib_name.c_str(), error);
58  return nullptr;
59  }
60 
61  T* instance = ( T* ) create();
62  if(instance != nullptr)
63  {
64  return std::unique_ptr<T>(instance);
65  }
66 
67  fprintf(stderr, "Error in loading library '%s': obtained pointer is null\n", lib_name.c_str());
68 
69  }
70 
71  return nullptr;
72 
73  }
74 
75 }
76 
78 {
79 public:
80 
81 virtual bool close(){return true;}
82 virtual bool init(std::string logger_name, bool compression){return true;}
83 virtual bool write(const char* name, const double* data, int rows, int cols, int slices){return true;}
84 
85 };
86 
88 {
89  if(type == "dummy")
90  {
91  return std::make_unique<DummyBackend>();
92  }
93 
94  return GetFactory<Backend>("libmatlogger2-backend-" + type + ".so");
95 }
96 
97 
virtual bool write(const char *name, const double *data, int rows, int cols, int slices)
virtual bool init(std::string logger_name, bool compression)
static UniquePtr MakeInstance(std::string type)
virtual bool close()
std::unique_ptr< Backend > UniquePtr