Instrument Neutral Distributed Interface INDI  1.9.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
126  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;
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() { return logFile_; }
226  static loggerConf_ getConfiguration() { return configuration_; }
227 
233  static Logger &getInstance();
234 
235  static bool saveConfigItems(FILE *fp);
236 
245  int addDebugLevel(const char *debugLevelName, const char *LoggingLevelName);
246 
247  void print(const char *devicename, const unsigned int verbosityLevel, const std::string &sourceFile,
248  const int codeLine,
249  //const std::string& message,
250  const char *message, ...);
251 
261  void configure(const std::string &outputFile, const loggerConf configuration, const int fileVerbosityLevel,
262  const int screenVerbosityLevel);
263 
267  static bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n);
269  static bool updateProperties(bool enable);
270  static char Tags[nlevels][MAXINDINAME];
271 
277  static unsigned int rank(unsigned int l);
278 };
279 
280 inline Logger::loggerConf operator|(Logger::loggerConf __a, Logger::loggerConf __b)
281 {
282  return Logger::loggerConf(static_cast<int>(__a) | static_cast<int>(__b));
283 }
284 
285 inline Logger::loggerConf operator&(Logger::loggerConf __a, Logger::loggerConf __b)
286 {
287  return Logger::loggerConf(static_cast<int>(__a) & static_cast<int>(__b));
288 }
289 }
INDI::Logger::VerbosityLevel
VerbosityLevel
Definition: indilogger.h:189
INDI::Logger::print
void print(const char *devicename, const unsigned int verbosityLevel, const std::string &sourceFile, const int codeLine, const char *message,...)
Definition: indilogger.cpp:387
INDI::Logger::getInstance
static Logger & getInstance()
Method to get a reference to the object (i.e., Singleton) It is a static method.
Definition: indilogger.cpp:354
INDI::Logger::DBG_EXTRA_2
@ DBG_EXTRA_2
Definition: indilogger.h:197
ISwitch
One switch descriptor.
INDI::Logger::Tags
static char Tags[nlevels][MAXINDINAME]
Definition: indilogger.h:270
INDI::Logger::DBG_WARNING
@ DBG_WARNING
Definition: indilogger.h:193
indiapi.h
Constants and Data structure definitions for the interface to the reference INDI C API implementation...
MAXINDINAME
#define MAXINDINAME
Definition: indiapi.h:190
INDI::Logger::switchinit::state
ISState state
Definition: indilogger.h:206
INDI::Logger::switchinit::label
char label[MAXINDILABEL]
Definition: indilogger.h:205
INDI::Logger::DBG_EXTRA_1
@ DBG_EXTRA_1
Definition: indilogger.h:196
INDI::Logger::DBG_ERROR
@ DBG_ERROR
Definition: indilogger.h:192
INDI::Logger::file_on
static const loggerConf file_on
Definition: indilogger.h:218
INDI::Logger::DBG_SESSION
@ DBG_SESSION
Definition: indilogger.h:194
INDI::Logger::LoggingLevelS
static ISwitch LoggingLevelS[nlevels]
Definition: indilogger.h:213
INDI::Logger::updateProperties
static bool updateProperties(bool enable)
Definition: indilogger.cpp:128
INDI::Logger::switchinit::name
char name[MAXINDINAME]
Definition: indilogger.h:204
INDI::Logger::DBG_DEBUG
@ DBG_DEBUG
Definition: indilogger.h:195
INDI::Logger::LoggingLevelSInit
static struct switchinit LoggingLevelSInit[nlevels]
Definition: indilogger.h:212
INDI::Logger::file_off
static const loggerConf file_off
Definition: indilogger.h:219
device
hid_device * device
Definition: activefocuser_utils.cpp:92
INDI::Logger::initProperties
static bool initProperties(INDI::DefaultDevice *device)
Definition: indilogger.cpp:100
INDI::Logger::switchinit::levelmask
unsigned int levelmask
Definition: indilogger.h:207
INDI::operator|
Logger::loggerConf operator|(Logger::loggerConf __a, Logger::loggerConf __b)
Definition: indilogger.h:280
INDI::Logger::DebugLevelS
static ISwitch DebugLevelS[nlevels]
Definition: indilogger.h:265
INDI::Logger::saveConfigItems
static bool saveConfigItems(FILE *fp)
Definition: indilogger.cpp:151
INDI::Logger::configure
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:298
INDI::Logger::DBG_EXTRA_3
@ DBG_EXTRA_3
Definition: indilogger.h:198
INDI::Logger::ConfigurationSP
static ISwitchVectorProperty ConfigurationSP
Definition: indilogger.h:216
INDI::Logger::DBG_EXTRA_4
@ DBG_EXTRA_4
Definition: indilogger.h:199
INDI::Logger::ConfigurationS
static ISwitch ConfigurationS[2]
Definition: indilogger.h:215
INDI::Logger::getLogFile
static std::string getLogFile()
Definition: indilogger.h:225
defaultdevice.h
INDI::Logger
The Logger class is a simple logger to log messages to file and INDI clients. This is the implementat...
Definition: indilogger.cpp:54
INDI::Logger::nDevices
static unsigned int nDevices
Definition: indilogger.h:223
INDI::Logger::defaultlevel
static const unsigned int defaultlevel
Definition: indilogger.h:210
INDI::Logger::switchinit
Definition: indilogger.h:202
INDI::Logger::DebugLevelSInit
static struct switchinit DebugLevelSInit[nlevels]
Definition: indilogger.h:264
INDI
Namespace to encapsulate INDI client, drivers, and mediator classes.
Definition: AlignmentSubsystemForClients.cpp:11
INDI::Logger::addDebugLevel
int addDebugLevel(const char *debugLevelName, const char *LoggingLevelName)
Adds a new debugging level to the driver.
Definition: indilogger.cpp:87
INDI::operator&
Logger::loggerConf operator&(Logger::loggerConf __a, Logger::loggerConf __b)
Definition: indilogger.h:285
INDI::Logger::screen_off
static const loggerConf screen_off
Definition: indilogger.h:221
INDI::Logger::loggerConf
loggerConf_ loggerConf
Definition: indilogger.h:217
MAXINDILABEL
#define MAXINDILABEL
Definition: indiapi.h:191
ISState
ISState
Switch state.
Definition: indiapi.h:148
INDI::DefaultDevice
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
Definition: defaultdevice.h:118
INDI::Logger::rank
static unsigned int rank(unsigned int l)
Method used to print message called by the DEBUG() macro.
Definition: indilogger.cpp:363
INDI::Logger::LoggingLevelSP
static ISwitchVectorProperty LoggingLevelSP
Definition: indilogger.h:214
INDI::Logger::DBG_IGNORE
@ DBG_IGNORE
Definition: indilogger.h:191
INDI::Logger::ISNewSwitch
static bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
Definition: indilogger.cpp:160
INDI::Logger::screen_on
static const loggerConf screen_on
Definition: indilogger.h:220
INDI::Logger::DebugLevelSP
static ISwitchVectorProperty DebugLevelSP
Definition: indilogger.h:266
INDI::Logger::customLevel
static unsigned int customLevel
Definition: indilogger.h:222
_ISwitchVectorProperty
Switch vector property descriptor.
Definition: indiapi.h:365
INDI::Logger::getConfiguration
static loggerConf_ getConfiguration()
Definition: indilogger.h:226
INDI::Logger::nlevels
static const unsigned int nlevels
Definition: indilogger.h:211