logging.h
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <unordered_map>
12 #include <vector>
13 #include <boost/thread.hpp>
14 #include <boost/function.hpp>
15 #include <boost/shared_ptr.hpp>
16 #include <boost/asio.hpp>
17 #include <boost/make_shared.hpp>
18 #include <boost/enable_shared_from_this.hpp>
19 #include <boost/chrono.hpp>
20 #include <boost/asio/waitable_timer_service.hpp>
21 
22 #include <boost/asio.hpp>
23 #include <boost/bind.hpp>
24 #include <iostream>
25 #include <cstring>
26 #include <string>
27 
28 #ifdef TESTING
29 #include "gtest/gtest.h"
30 #endif
31 
32 #include "easylogging++.h"
33 #include "fileWatcher.h"
34 
35 #define ARM
36 
37 //active level of log
38 #define DPY_LOG_DEBUG_ON
39 #define DPY_LOG_INFO_ON
40 #define DPY_LOG_ERR_ON
41 #define DPY_LOG_WARN_ON
42 #define DPY_LOG_FATAL_ON
43 
44 /********************************************************************************
45  ********************************* LOGGING MACROS *******************************
46  ********************************************************************************/
47 
48 #ifdef DPY_LOG_DEBUG_ON
49 #define DPYLOG_DEBUG(formatString, ...)\
50  do{ Logging::getLogger()->debug(formatString, ##__VA_ARGS__); }while (0)
51 #else
52 #define DPYLOG_DEBUG(formatString, ...)
53 #endif
54 
55 #ifdef DPY_LOG_INFO_ON
56 #define DPYLOG_INFO(formatString, ...)\
57  do{ Logging::getLogger()->info(formatString, ##__VA_ARGS__); }while (0)
58 #else
59 #define DPYLOG_INFO(formatString, ...)
60 #endif
61 
62 #ifdef DPY_LOG_WARN_ON
63 #define DPYLOG_WARN(formatString, ...)\
64  do{ Logging::getLogger()->warn(formatString, ##__VA_ARGS__); }while (0)
65 #else
66 #define DPYLOG_WARN(formatString, ...)
67 #endif
68 
69 #ifdef DPY_LOG_ERR_ON
70 #define DPYLOG_ERR(formatString, ...)\
71  do{ Logging::getLogger()->error(formatString, ##__VA_ARGS__); }while (0)
72 #else
73 #define DPYLOG_ERR(formatString, ...)
74 
75 #endif
76 
77 #ifdef DPY_LOG_FATAL_ON
78 #define DPYLOG_FATAL(formatString, ...)\
79  do{ Logging::getLogger()->fatal(formatString, ##__VA_ARGS__); }while (0)
80 #else
81 #define DPYLOG_FATAL(formatString, ...)
82 #endif
83 
87 struct Level {
88  el::Level logLevel=el::Level::Debug;
89  bool enabled=false;
90 };
91 
92 typedef boost::asio::basic_waitable_timer<boost::chrono::steady_clock> steady_timer;
93 
101 class Logging
102 {
103  /********************************************************************************
104  ***************************** PRIVATE VARIABLES *********************************
105  ********************************************************************************/
106  friend class LogConfiguration;
107 private:
108  boost::asio::io_service mIo;
109  std::string m_appName;
110  std::string m_absPath;
111  std::time_t m_lastModificationTime;
112  steady_timer m_timer;
113  std::vector<std::string> m_insertOrder;
114  std::unordered_map<std::string, Level> m_mapStringLevel;
115  std::unordered_map<std::string, el::ConfigurationType> m_mapStringProperty;
116  boost::thread m_bt;
117  FileWatcher mFileWatcher;
118 
119  /********************************************************************************
120  ***************************** PUBLIC FUNCTIONS *********************************
121  ********************************************************************************/
122 public:
123  explicit Logging(std::string appName="default");
124  ~Logging();
125  bool start();
126  void stop();
127  static el::Logger* getLogger();
128 
129  /********************************************************************************
130  ***************************** PRIVATE FUNCTIONS *********************************
131  ********************************************************************************/
132 private:
133  void createLevelMap();
134  void createPropertyMap();
135  static bool openFile(boost::system::error_code& ec, const std::string& fileName, std::stringstream& ss);
136  bool writeToJson(const std::string& filename);
137  void checkModificationTime();
138  void easyLogConfig();
139  bool parseFromJson(boost::system::error_code& ec, const std::string& fileName);
140  bool isValid(std::string appname);
141  bool checkHierarchy();
142  void fileWatcherHandler(std::string path_to_watch, FileStatus status);
143 
144 #ifdef TESTING
145 public:
146  //Acordarse de hacer include de gtest if TESTING
147  FRIEND_TEST(Logging_test, isNameValidPASS);
148  FRIEND_TEST(Logging_test, isNameValidFAIL);
149  FRIEND_TEST(Logging_test, openFilePASS);
150  FRIEND_TEST(Logging_test, openFileFAIL);
151  FRIEND_TEST(Logging_test, writeToJsonPASS);
152  FRIEND_TEST(Logging_test, writeToJsonFAIL);
153  FRIEND_TEST(Logging_test, createPathPASS);
154  FRIEND_TEST(Logging_test, createPathFAIL);
155  FRIEND_TEST(Logging_test,checkCorrectHierarchy);
156  FRIEND_TEST(Logging_test,parseFromJsonCorrectly);
157 
158 #endif //TESTING
159 
160 };
el::Level logLevel
Easylogging level.
Definition: logging.h:93
bool enabled
boolean to set if it is enabled or not
Definition: logging.h:94
It stores info about the logging level.
Definition: logging.h:92
boost::asio::basic_waitable_timer< boost::chrono::steady_clock > steady_timer
Typedef to use a steady timer.
Definition: logging.h:97
boost::asio::basic_waitable_timer< boost::chrono::steady_clock > steady_timer
Typedef to use a steady timer.
Definition: dpyMwClient.h:52
Implement the interaction with a inotify tool.
Definition: fileWatcher.h:17
This class is used by Services and Apps to log messages.
Definition: logging.h:106
Debug
An enum definition which represents the different modes of the Automatic Power configuration.
Definition: iPower.h:83