DCSModulesAPI  1.0.0
DCS sub project containing all the control modules as libraries.
DCS_ModuleUtils.h
Go to the documentation of this file.
1 #ifndef _DCS_UTILS_H
2 #define _DCS_UTILS_H
3 
4 #pragma once
5 #include "../../config/exports.h"
6 #include <iostream>
7 #include <chrono>
8 #include <mutex>
9 #include <map>
10 #include <string>
11 #include <future>
12 
28 #ifdef SOURCE_PATH_SIZE
29 #define LOG_LVL(lvl, msg, ...) DCS::Utils::Logger::lvl(__FILE__ + SOURCE_PATH_SIZE, msg, __VA_ARGS__)
30 #else
31 #define LOG_LVL(lvl, msg, ...) DCS::Utils::Logger::lvl(__FILE__, msg, __VA_ARGS__)
32 #endif
33 
34 #define LOG_DEBUG(msg, ...) LOG_LVL(Debug, msg, __VA_ARGS__)
35 #define LOG_MESSAGE(msg, ...) LOG_LVL(Message, msg, __VA_ARGS__)
36 #define LOG_WARNING(msg, ...) LOG_LVL(Warning, msg, __VA_ARGS__)
37 #define LOG_ERROR(msg, ...) LOG_LVL(Error, msg, __VA_ARGS__)
38 #define LOG_CRITICAL(msg, ...) LOG_LVL(Critical, msg, __VA_ARGS__)
39 
40 #define ENUM_FLAG_OPERATOR(T,X) inline T operator X (T lhs, T rhs) { return (T) (static_cast<std::underlying_type_t <T>>(lhs) X static_cast<std::underlying_type_t <T>>(rhs)); }
41 #define ENUM_FLAGS(T) \
42 enum class T; \
43 inline T operator ~ (T t) { return (T) (~static_cast<std::underlying_type_t <T>>(t)); } \
44 ENUM_FLAG_OPERATOR(T,|) \
45 ENUM_FLAG_OPERATOR(T,^) \
46 ENUM_FLAG_OPERATOR(T,&) \
47 enum class T
48 
49 namespace DCS
50 {
51  typedef signed long long i64;
52  typedef unsigned long long u64;
53  typedef signed long i32;
54  typedef unsigned long u32;
55  typedef short i16;
56  typedef unsigned short u16;
57  typedef signed char i8;
58  typedef unsigned char u8;
59 
60  typedef float f32;
61  typedef double f64;
62 
66  typedef void* GenericHandle;
67 
68  namespace Utils
69  {
74  {
75  public:
76  String() = default;
77 
81  String(const char* text);
82  String(const String& s);
83 
84  ~String();
85 
86  String& operator=(const String& s) noexcept;
87 
92  inline const size_t size() const
93  {
94  return buffer_size;
95  }
96 
97  template<typename T>
98  static inline const String From(T val)
99  {
100  return String(std::to_string(val).c_str());
101  }
102 
107  inline const char* c_str() const
108  {
109  return buffer;
110  }
111 
112  private:
113  size_t buffer_size = 0;
114  char* buffer = nullptr;
115  };
116 
122  {
123  char buffer[512];
124  };
125 
132  class Logger
133  {
134  public:
135  enum class Verbosity : int;
136  private:
137 
138  typedef void (*WriteNotifyCallback)(DCS::Utils::String string, Verbosity v, void*);
139 
140  public:
147  enum class Verbosity
148  {
149  CRITICAL,
150  ERROR,
151  WARNING,
152  MESSAGE,
153  DEBUG
154  };
155 
159  enum class Options
160  {
161  ENABLE_COLOR,
162  DISABLE_COLOR
163  };
164 
168  static DCS_API void Init(Verbosity level, DCS::Utils::String file = "default_log.log");
169 
173  static DCS_API void Destroy();
174 
178  static DCS_API void SetLogWriteCallback(WriteNotifyCallback wnc, void* obj = nullptr);
179 
185  static DCS_API void Debug(const char* file, const char* msg, ...);
186 
192  static DCS_API void Message(const char* file, const char* msg, ...);
193 
199  static DCS_API void Warning(const char* file, const char* msg, ...);
200 
206  static DCS_API void Error(const char* file, const char* msg, ...);
207 
213  static DCS_API void Critical(const char* file, const char* msg, ...);
214 
218  static DCS_API void Settings(Options opt);
219 
223  static DCS_API void ChangeVerbosity(Verbosity v);
224 
225  private:
226  static void WriteData(std::string buffer[], Verbosity v);
227 
228  static WriteNotifyCallback writenotify;
229  static void* obj;
230 
231  static Verbosity verbosity_level;
232  static Options options;
233  static std::string timestamp();
234  static FILE* handle;
235  static std::mutex _log_mtx;
236  };
237 
245  template<typename T>
246  class AsyncItem
247  {
248  public:
249  AsyncItem(std::promise<T>& p) { f = p.get_future(); };
250  ~AsyncItem() = default;
251 
255  inline void wait() const
256  {
257  f.wait();
258  }
259 
267  inline T get() const
268  {
269  return f.get();
270  }
271 
272  private:
273  std::shared_future<T> f;
274  };
275  }
276 
280  namespace Timer
281  {
288  {
289  GenericHandle point;
290  };
291 
296  {
297  i16 hour = 0;
298  i16 min = 0;
299  i16 sec = 0;
300  i16 millis = 0;
301  i16 micros = 0;
302  i16 nanos = 0;
303 
308  const Utils::String to_string() const;
309  };
310 
315 
319  DCS_API void Delete(SystemTimer timer);
320 
328 
336 
344 
352  }
353 }
354 
355 #endif _DCS_UTILS_H
A class that holds possible future data.
Definition: DCS_ModuleUtils.h:246
#define DCS_API
Defines the export interface acessible via the dll-interface. Only important for SHARED LIB Compile M...
Definition: exports.h:116
DCS_API i64 GetNanoseconds(SystemTimer timer)
Gives number of nanoseconds passed relative to timer.
Definition: timer.cpp:116
const char * c_str() const
Converts a String object to const char* raw pointer.
Definition: DCS_ModuleUtils.h:107
double f64
Equivalent to double.
Definition: DCS_ModuleUtils.h:61
Options
Contains the possible Logger custom options.
Definition: DCS_ModuleUtils.h:159
A very simple string buffer to hold char values. Used for string information manipulations via tcp/ip...
Definition: DCS_ModuleUtils.h:121
unsigned long u32
Equivalent to uint_32t.
Definition: DCS_ModuleUtils.h:54
DCS_API SystemTimer New()
Creates a new SystemTimer.
Definition: timer.cpp:23
Holds data about when a timer was first created.
Definition: DCS_ModuleUtils.h:287
unsigned short u16
Equivalent to uint_16t.
Definition: DCS_ModuleUtils.h:56
void * GenericHandle
A generic opaque handle that is only meaningful for the API.
Definition: DCS_ModuleUtils.h:66
signed long i32
Equivalent to int_32t.
Definition: DCS_ModuleUtils.h:53
A wrapper class used to export a simple string to the client side.
Definition: DCS_ModuleUtils.h:73
signed char i8
Equivalent to int_8t.
Definition: DCS_ModuleUtils.h:57
DCS_API void Init()
Initializes the Network module.
Definition: socket.cpp:4
DCS_API void Delete(SystemTimer timer)
Deletes a SystemTimer.
Definition: timer.cpp:32
Represents a timestamp divided fieldwise.
Definition: DCS_ModuleUtils.h:295
DCS_API void Destroy()
Destroys the Network module.
Definition: socket.cpp:19
Definition: registry.h:70
float f32
Equivalent to float.
Definition: DCS_ModuleUtils.h:60
DCS_API Utils::String GetTimestampString(SystemTimer timer)
Gives a timestamp relative to timer in Utils::String format.
Definition: timer.cpp:66
DCS_API Timestamp GetTimestamp(SystemTimer timer)
Gives a timestamp relative to timer in Timestamp format.
Definition: timer.cpp:37
GenericHandle point
Represents a time point.
Definition: DCS_ModuleUtils.h:289
const size_t size() const
Returns size of the string.
Definition: DCS_ModuleUtils.h:92
unsigned long long u64
Equivalent to uint_64t.
Definition: DCS_ModuleUtils.h:52
unsigned char u8
Equivalent to uint_8t.
Definition: DCS_ModuleUtils.h:58
This class enables writing to a single (or multiple) buffer(s) for logging. Thread-safe.
Definition: DCS_ModuleUtils.h:132
void wait() const
Wait for item to be available.
Definition: DCS_ModuleUtils.h:255
DCS_API Utils::String GetTimestampStringSimple(SystemTimer timer)
Gives a timestamp relative to timer in Utils::String format (displays day/hour/minute duration only)...
Definition: timer.cpp:94
signed long long i64
Equivalent to int_64t.
Definition: DCS_ModuleUtils.h:51
short i16
Equivalent to int_16t.
Definition: DCS_ModuleUtils.h:55
Verbosity
Contains the possible verbosity levels for the Logger.
Definition: DCS_ModuleUtils.h:147