Instrument Neutral Distributed Interface INDI  1.5.0
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 #define DEBUGDEVICE(device, priority, msg) INDI::Logger::getInstance().print(device, priority, __FILE__, __LINE__, msg)
60 #define DEBUGFDEVICE(device, priority, msg, ...) \
61  INDI::Logger::getInstance().print(device, priority, __FILE__, __LINE__, msg, __VA_ARGS__)
62 
63 namespace INDI
64 {
88 class Logger
89 {
91  enum loggerConf_
92  {
93  L_nofile_ = 1 << 0,
94  L_file_ = 1 << 1,
95  L_noscreen_ = 1 << 2,
96  L_screen_ = 1 << 3
97  };
98 
99 #ifdef LOGGER_MULTITHREAD
100  static pthread_mutex_t lock_;
102 #endif
103 
104  bool configured_ { false };
105 
107  static Logger *m_;
108 
113  static std::string logFile_;
114 
118  static std::string logDir_;
119 
126  static loggerConf_ configuration_;
127 
129  std::ofstream out_;
131  struct timeval initialTime_;
133  static unsigned int fileVerbosityLevel_;
135  static unsigned int screenVerbosityLevel_;
136  static unsigned int rememberscreenlevel_;
137 
146  Logger();
147 
152  ~Logger();
153 
155  inline static void lock();
156 
158  inline static void unlock();
159 
160  static INDI::DefaultDevice *parentDevice;
161 
162  public:
164  {
165  DBG_ERROR = 0x1,
166  DBG_WARNING = 0x2,
167  DBG_SESSION = 0x4,
168  DBG_DEBUG = 0x8,
169  DBG_EXTRA_1 = 0x10,
170  DBG_EXTRA_2 = 0X20,
171  DBG_EXTRA_3 = 0x40,
172  DBG_EXTRA_4 = 0x80
173  };
174 
175  struct switchinit
176  {
180  unsigned int levelmask;
181  };
182 
183  static const unsigned int defaultlevel = DBG_ERROR | DBG_WARNING | DBG_SESSION;
184  static const unsigned int nlevels = 8;
185  static struct switchinit LoggingLevelSInit[nlevels];
190  typedef loggerConf_ loggerConf;
191  static const loggerConf file_on = L_nofile_;
192  static const loggerConf file_off = L_file_;
193  static const loggerConf screen_on = L_noscreen_;
194  static const loggerConf screen_off = L_screen_;
195  static unsigned int customLevel;
196  static unsigned int nDevices;
197 
198  static std::string getLogFile() { return logFile_; }
199  static loggerConf_ getConfiguration() { return configuration_; }
200 
206  static Logger &getInstance();
207 
208  static bool saveConfigItems(FILE *fp);
209 
218  int addDebugLevel(const char *debugLevelName, const char *LoggingLevelName);
219 
220  void print(const char *devicename, const unsigned int verbosityLevel, const std::string &sourceFile,
221  const int codeLine,
222  //const std::string& message,
223  const char *message, ...);
224 
234  void configure(const std::string &outputFile, const loggerConf configuration, const int fileVerbosityLevel,
235  const int screenVerbosityLevel);
236 
237  static struct switchinit DebugLevelSInit[nlevels];
240  static bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n);
242  static bool updateProperties(bool enable);
243  static char Tags[nlevels][MAXINDINAME];
244 
250  static unsigned int rank(unsigned int l);
251 };
252 
253 inline Logger::loggerConf operator|(Logger::loggerConf __a, Logger::loggerConf __b)
254 {
255  return Logger::loggerConf(static_cast<int>(__a) | static_cast<int>(__b));
256 }
257 
258 inline Logger::loggerConf operator&(Logger::loggerConf __a, Logger::loggerConf __b)
259 {
260  return Logger::loggerConf(static_cast<int>(__a) & static_cast<int>(__b));
261 }
262 }
static ISwitch LoggingLevelS[nlevels]
Definition: indilogger.h:186
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
static struct switchinit LoggingLevelSInit[nlevels]
Definition: indilogger.h:185
static const unsigned int nlevels
Definition: indilogger.h:184
#define MAXINDINAME
Definition: indiapi.h:162
static const unsigned int defaultlevel
Definition: indilogger.h:183
IndiDevice * device
Definition: indidevice.cpp:26
char name[MAXINDINAME]
Definition: indilogger.h:177
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:304
static ISwitchVectorProperty LoggingLevelSP
Definition: indilogger.h:187
char label[MAXINDILABEL]
Definition: indilogger.h:178
static Logger & getInstance()
Method to get a reference to the object (i.e., Singleton) It is a static method.
Definition: indilogger.cpp:360
static bool updateProperties(bool enable)
Definition: indilogger.cpp:134
Switch vector property descriptor.
Definition: indiapi.h:337
Namespace to encapsulate INDI client, drivers, and mediator classes. Developers can subclass the base...
static const loggerConf screen_off
Definition: indilogger.h:194
Logger::loggerConf operator|(Logger::loggerConf __a, Logger::loggerConf __b)
Definition: indilogger.h:253
static unsigned int customLevel
Definition: indilogger.h:195
void print(const char *devicename, const unsigned int verbosityLevel, const std::string &sourceFile, const int codeLine, const char *message,...)
Definition: indilogger.cpp:393
One switch descriptor.
Definition: indiapi.h:319
unsigned int levelmask
Definition: indilogger.h:180
static struct switchinit DebugLevelSInit[nlevels]
Definition: indilogger.h:237
Constants and Data structure definitions for the interface to the reference INDI C API implementation...
static ISwitchVectorProperty ConfigurationSP
Definition: indilogger.h:189
static const loggerConf file_off
Definition: indilogger.h:192
static char Tags[nlevels][MAXINDINAME]
Definition: indilogger.h:243
static bool initProperties(INDI::DefaultDevice *device)
Definition: indilogger.cpp:106
static loggerConf_ getConfiguration()
Definition: indilogger.h:199
static std::string getLogFile()
Definition: indilogger.h:198
static bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
Definition: indilogger.cpp:166
static ISwitch ConfigurationS[2]
Definition: indilogger.h:188
static ISwitchVectorProperty DebugLevelSP
Definition: indilogger.h:239
static const loggerConf file_on
Definition: indilogger.h:191
static bool saveConfigItems(FILE *fp)
Definition: indilogger.cpp:157
ISState
Switch state.
Definition: indiapi.h:124
The Logger class is a simple logger to log messages to file and INDI clients. This is the implementat...
Definition: indilogger.cpp:35
static unsigned int nDevices
Definition: indilogger.h:196
static unsigned int rank(unsigned int l)
Method used to print message called by the DEBUG() macro.
Definition: indilogger.cpp:369
#define MAXINDILABEL
Definition: indiapi.h:163
static const loggerConf screen_on
Definition: indilogger.h:193
int addDebugLevel(const char *debugLevelName, const char *LoggingLevelName)
Adds a new debugging level to the driver.
Definition: indilogger.cpp:93
Logger::loggerConf operator&(Logger::loggerConf __a, Logger::loggerConf __b)
Definition: indilogger.h:258
loggerConf_ loggerConf
Definition: indilogger.h:190
static ISwitch DebugLevelS[nlevels]
Definition: indilogger.h:238