Fledge
An open source edge computing platform for industrial users
logger.h
1 #ifndef _LOGGER_H
2 #define _LOGGER_H
3 /*
4  * Fledge storage service.
5  *
6  * Copyright (c) 2017-2018 OSisoft, LLC
7  *
8  * Released under the Apache 2.0 Licence
9  *
10  * Author: Mark Riddoch, Massimiliano Pinto
11  */
12 
13 #include <string>
14 #include <functional>
15 #include <map>
16 #include <mutex>
17 #include <queue>
18 #include <thread>
19 #include <condition_variable>
20 #include <atomic>
21 #include <sys/socket.h>
22 #include <arpa/inet.h>
23 #define PRINT_FUNC Logger::getLogger()->info("%s:%d", __FUNCTION__, __LINE__);
24 
42 class Logger {
43  public:
44  enum class LogLevel
45  {
46  ERROR,
47  WARNING,
48  INFO,
49  DEBUG,
50  FATAL
51  };
52 
53  Logger(const std::string& application);
54  ~Logger();
55  static Logger *getLogger();
56  void debug(const std::string& msg, ...);
57  void printLongString(const std::string&, LogLevel = LogLevel::DEBUG);
58  void info(const std::string& msg, ...);
59  void warn(const std::string& msg, ...);
60  void error(const std::string& msg, ...);
61  void fatal(const std::string& msg, ...);
62  void setMinLevel(const std::string& level);
63  std::string& getMinLevel() { return levelString; }
64 
65  // LogInterceptor callback function signature
66  typedef void (*LogInterceptor)(LogLevel, const std::string&, void*);
67 
68  // Register an interceptor
69  bool registerInterceptor(LogLevel level, LogInterceptor callback, void* userData);
70 
71  // Unregister an interceptor
72  bool unregisterInterceptor(LogLevel level, LogInterceptor callback);
73 
74  private:
75  std::string *format(const std::string& msg, va_list ap);
76  static Logger *instance;
77  std::string levelString;
78  int m_level;
79 
80  struct InterceptorData {
81  LogInterceptor callback;
82  void* userData;
83  };
84 
85  std::multimap<LogLevel, InterceptorData> m_interceptors;
86  std::mutex m_interceptorMapMutex;
87 
88  struct LogTask {
89  LogLevel level;
90  std::string message;
91  LogInterceptor callback;
92  void* userData;
93  };
94 
95  std::queue<LogTask> m_taskQueue;
96  std::mutex m_queueMutex;
97  std::condition_variable m_condition;
98  std::atomic<bool> m_runWorker;
99  std::thread *m_workerThread;
100 
101  void log(int sysLogLvl, const char * lvlName, LogLevel appLogLvl, const std::string& msg, va_list args);
102  void sendToUdpSink(const std::string& msg);
103  void executeInterceptor(LogLevel level, const std::string& message);
104  void workerThread();
105  int m_UdpSockFD = -1;
106  struct sockaddr_in m_UdpServerAddr;
107  bool m_SyslogUdpEnabled = false;
108  std::string m_identifier;
109  std::string m_hostname;
110 };
111 
112 #endif
113 
Logger::getLogger
static Logger * getLogger()
Return the singleton instance of the logger class.
Definition: logger.cpp:184
Logger::debug
void debug(const std::string &msg,...)
Log a message at the level debug.
Definition: logger.cpp:346
Logger::fatal
void fatal(const std::string &msg,...)
Log a message at the level fatal.
Definition: logger.cpp:465
Logger::error
void error(const std::string &msg,...)
Log a message at the level error.
Definition: logger.cpp:447
Logger::printLongString
void printLongString(const std::string &, LogLevel=LogLevel::DEBUG)
Log a long string across multiple syslog entries.
Definition: logger.cpp:363
Logger::warn
void warn(const std::string &msg,...)
Log a message at the level warn.
Definition: logger.cpp:430
Logger::info
void info(const std::string &msg,...)
Log a message at the level info.
Definition: logger.cpp:413
Logger::registerInterceptor
bool registerInterceptor(LogLevel level, LogInterceptor callback, void *userData)
Register a callback function to be called when a log message is written that matches the specificatio...
Definition: logger.cpp:246
Logger::~Logger
~Logger()
Destructor for the logger class.
Definition: logger.cpp:138
Logger::Logger
Logger(const std::string &application)
Constructor for the Logger class.
Definition: logger.cpp:48
Logger::setMinLevel
void setMinLevel(const std::string &level)
Set the minimum level of logging to write to syslog.
Definition: logger.cpp:204
Logger
Fledge Logger class used to log to syslog.
Definition: logger.h:42
Logger::unregisterInterceptor
bool unregisterInterceptor(LogLevel level, LogInterceptor callback)
Remove the registration of a previously registered callback.
Definition: logger.cpp:274