Instrument Neutral Distributed Interface INDI  2.0.2
indilogger.h
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (C) 2012 Evidence Srl - www.evidence.eu.com
3 
4  Adapted to INDI Library by Jasem Mutlaq & Geehalel.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 *******************************************************************************/
20 
21 #pragma once
22 
23 #include "indiapi.h"
24 #include "defaultdevice.h"
25 
26 #include <stdarg.h>
27 #include <fstream>
28 #include <ostream>
29 #include <string>
30 #include <sstream>
31 #include <sys/time.h>
32 
38 #define DEBUG_CONF(outputFile, configuration, fileVerbosityLevel, screenVerbosityLevel) \
39  { \
40  Logger::getInstance().configure(outputFile, configuration, fileVerbosityLevel, screenVerbosityLevel); \
41  }
42 
48 /*
49 #define DEBUG(priority, msg) { \
50  std::ostringstream __debug_stream__; \
51  __debug_stream__ << msg; \
52  Logger::getInstance().print(priority, __FILE__, __LINE__, \
53  __debug_stream__.str()); \
54  }
55 */
56 #define DEBUG(priority, msg) INDI::Logger::getInstance().print(getDeviceName(), priority, __FILE__, __LINE__, msg)
57 #define DEBUGF(priority, msg, ...) \
58  INDI::Logger::getInstance().print(getDeviceName(), priority, __FILE__, __LINE__, msg, __VA_ARGS__)
59 
60 #define DEBUGDEVICE(device, priority, msg) INDI::Logger::getInstance().print(device, priority, __FILE__, __LINE__, msg)
61 #define DEBUGFDEVICE(device, priority, msg, ...) \
62  INDI::Logger::getInstance().print(device, priority, __FILE__, __LINE__, msg, __VA_ARGS__)
63 
72 #define LOG_ERROR(txt) DEBUG(INDI::Logger::DBG_ERROR, (txt))
73 #define LOG_WARN(txt) DEBUG(INDI::Logger::DBG_WARNING, (txt))
74 #define LOG_INFO(txt) DEBUG(INDI::Logger::DBG_SESSION, (txt))
75 #define LOG_DEBUG(txt) DEBUG(INDI::Logger::DBG_DEBUG, (txt))
76 #define LOG_EXTRA1(txt) DEBUG(INDI::Logger::DBG_EXTRA_1, (txt))
77 #define LOG_EXTRA2(txt) DEBUG(INDI::Logger::DBG_EXTRA_2, (txt))
78 #define LOG_EXTRA3(txt) DEBUG(INDI::Logger::DBG_EXTRA_3, (txt))
79 
80 #define LOGF_ERROR(fmt, ...) DEBUGF(INDI::Logger::DBG_ERROR, (fmt), __VA_ARGS__)
81 #define LOGF_WARN(fmt, ...) DEBUGF(INDI::Logger::DBG_WARNING, (fmt), __VA_ARGS__)
82 #define LOGF_INFO(fmt, ...) DEBUGF(INDI::Logger::DBG_SESSION, (fmt), __VA_ARGS__)
83 #define LOGF_DEBUG(fmt, ...) DEBUGF(INDI::Logger::DBG_DEBUG, (fmt), __VA_ARGS__)
84 #define LOGF_EXTRA1(fmt, ...) DEBUGF(INDI::Logger::DBG_EXTRA_1, (fmt), __VA_ARGS__)
85 #define LOGF_EXTRA2(fmt, ...) DEBUGF(INDI::Logger::DBG_EXTRA_2, (fmt), __VA_ARGS__)
86 #define LOGF_EXTRA3(fmt, ...) DEBUGF(INDI::Logger::DBG_EXTRA_3, (fmt), __VA_ARGS__)
87 
88 
89 namespace INDI
90 {
114 class Logger
115 {
117  enum loggerConf_
118  {
119  L_nofile_ = 1 << 0,
120  L_file_ = 1 << 1,
121  L_noscreen_ = 1 << 2,
122  L_screen_ = 1 << 3
123  };
124 
125 #ifdef LOGGER_MULTITHREAD
127  static pthread_mutex_t lock_;
128 #endif
129 
130  bool configured_ { false };
131 
133  static Logger *m_;
134 
139  static std::string logFile_;
140 
144  static std::string logDir_;
145 
152  static loggerConf_ configuration_;
153 
155  std::ofstream out_;
157  struct timeval initialTime_;
159  static unsigned int fileVerbosityLevel_;
161  static unsigned int screenVerbosityLevel_;
162  static unsigned int rememberscreenlevel_;
163 
172  Logger();
173 
178  ~Logger();
179 
181  inline static void lock();
182 
184  inline static void unlock();
185 
186  static INDI::DefaultDevice *parentDevice;
187 
188  public:
190  {
191  DBG_IGNORE = 0x0,
192  DBG_ERROR = 0x1,
193  DBG_WARNING = 0x2,
194  DBG_SESSION = 0x4,
195  DBG_DEBUG = 0x8,
196  DBG_EXTRA_1 = 0x10,
197  DBG_EXTRA_2 = 0X20,
198  DBG_EXTRA_3 = 0x40,
199  DBG_EXTRA_4 = 0x80
200  };
201 
202  struct switchinit
203  {
207  unsigned int levelmask;
208  };
209 
210  static const unsigned int defaultlevel = DBG_ERROR | DBG_WARNING | DBG_SESSION;
211  static const unsigned int nlevels = 8;
212  static struct switchinit LoggingLevelSInit[nlevels];
217  typedef loggerConf_ loggerConf;
218  static const loggerConf file_on = L_nofile_;
219  static const loggerConf file_off = L_file_;
220  static const loggerConf screen_on = L_noscreen_;
221  static const loggerConf screen_off = L_screen_;
222  static unsigned int customLevel;
223  static unsigned int nDevices;
224 
225  static std::string getLogFile()
226  {
227  return logFile_;
228  }
229  static loggerConf_ getConfiguration()
230  {
231  return configuration_;
232  }
233 
239  static Logger &getInstance();
240 
241  static bool saveConfigItems(FILE *fp);
242 
251  int addDebugLevel(const char *debugLevelName, const char *LoggingLevelName);
252 
253  void print(const char *devicename, const unsigned int verbosityLevel, const std::string &sourceFile,
254  const int codeLine,
255  //const std::string& message,
256  const char *message, ...);
257 
267  void configure(const std::string &outputFile, const loggerConf configuration, const int fileVerbosityLevel,
268  const int screenVerbosityLevel);
269 
270  static struct switchinit DebugLevelSInit[nlevels];
273  static bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n);
275  static bool updateProperties(bool enable);
276  static char Tags[nlevels][MAXINDINAME];
277 
283  static unsigned int rank(unsigned int l);
284 };
285 
286 inline Logger::loggerConf operator|(Logger::loggerConf __a, Logger::loggerConf __b)
287 {
288  return Logger::loggerConf(static_cast<int>(__a) | static_cast<int>(__b));
289 }
290 
291 inline Logger::loggerConf operator&(Logger::loggerConf __a, Logger::loggerConf __b)
292 {
293  return Logger::loggerConf(static_cast<int>(__a) & static_cast<int>(__b));
294 }
295 }
hid_device * device
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
Constants and Data structure definitions for the interface to the reference INDI C API implementation...
ISState
Switch state.
Definition: indiapi.h:150
#define MAXINDILABEL
Definition: indiapi.h:192
#define MAXINDINAME
Definition: indiapi.h:191
Namespace to encapsulate INDI client, drivers, and mediator classes.
Logger::loggerConf operator&(Logger::loggerConf __a, Logger::loggerConf __b)
Definition: indilogger.h:291
Logger::loggerConf operator|(Logger::loggerConf __a, Logger::loggerConf __b)
Definition: indilogger.h:286
unsigned int levelmask
Definition: indilogger.h:207
char name[MAXINDINAME]
Definition: indilogger.h:204
char label[MAXINDILABEL]
Definition: indilogger.h:205
The Logger class is a simple logger to log messages to file and INDI clients. This is the implementat...
Definition: indilogger.cpp:37
static ISwitch DebugLevelS[nlevels]
Definition: indilogger.h:271
static ISwitch LoggingLevelS[nlevels]
Definition: indilogger.h:213
static unsigned int customLevel
Definition: indilogger.h:222
static unsigned int rank(unsigned int l)
Method used to print message called by the DEBUG() macro.
Definition: indilogger.cpp:348
static const loggerConf screen_on
Definition: indilogger.h:220
static struct switchinit DebugLevelSInit[nlevels]
Definition: indilogger.h:270
static const loggerConf file_off
Definition: indilogger.h:219
static ISwitchVectorProperty DebugLevelSP
Definition: indilogger.h:272
static const unsigned int nlevels
Definition: indilogger.h:211
static bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
Definition: indilogger.cpp:145
void configure(const std::string &outputFile, const loggerConf configuration, const int fileVerbosityLevel, const int screenVerbosityLevel)
Method to configure the logger. Called by the DEBUG_CONF() macro. To make implementation easier,...
Definition: indilogger.cpp:283
static ISwitchVectorProperty LoggingLevelSP
Definition: indilogger.h:214
void print(const char *devicename, const unsigned int verbosityLevel, const std::string &sourceFile, const int codeLine, const char *message,...)
Definition: indilogger.cpp:372
static Logger & getInstance()
Method to get a reference to the object (i.e., Singleton) It is a static method.
Definition: indilogger.cpp:339
static bool initProperties(INDI::DefaultDevice *device)
Definition: indilogger.cpp:85
static bool updateProperties(bool enable)
Definition: indilogger.cpp:113
static bool saveConfigItems(FILE *fp)
Definition: indilogger.cpp:136
loggerConf_ loggerConf
Definition: indilogger.h:217
static struct switchinit LoggingLevelSInit[nlevels]
Definition: indilogger.h:212
static const loggerConf file_on
Definition: indilogger.h:218
static ISwitch ConfigurationS[2]
Definition: indilogger.h:215
static std::string getLogFile()
Definition: indilogger.h:225
int addDebugLevel(const char *debugLevelName, const char *LoggingLevelName)
Adds a new debugging level to the driver.
Definition: indilogger.cpp:72
static char Tags[nlevels][MAXINDINAME]
Definition: indilogger.h:276
static const unsigned int defaultlevel
Definition: indilogger.h:210
static loggerConf_ getConfiguration()
Definition: indilogger.h:229
static const loggerConf screen_off
Definition: indilogger.h:221
static unsigned int nDevices
Definition: indilogger.h:223
static ISwitchVectorProperty ConfigurationSP
Definition: indilogger.h:216
One switch descriptor.
Switch vector property descriptor.
Definition: indiapi.h:367