ealogger
ealogger is a c++ library that provides a blazing fast and easy to use logging mechanism
ealogger.h
Go to the documentation of this file.
1 // ealogger is a simple, asynchronous and powerful logger library for c++
2 // Copyright 2013 - 2016 Christian Rapp
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef EALOGGER_H
17 #define EALOGGER_H
18 
23 #include <string>
24 /*
25  * Mutual exclusion for threadsafe logger
26  */
27 #include <csignal>
28 #include <iostream>
29 #include <mutex>
30 #include <stdexcept>
31 /*
32  * Background logger thread
33  */
34 #include <map>
35 #include <thread>
36 #include <vector>
37 
38 #include <ealogger/global.h>
39 #include <ealogger/logmessage.h>
40 #include <ealogger/logqueue.h>
41 #include <ealogger/sink_console.h>
42 #include <ealogger/sink_file.h>
43 #include <ealogger/sink_syslog.h>
44 #include "config.h"
45 
49 namespace ealogger
50 {
61 // Define macros for all log levels and call public member write_log()
66 #define eal_debug(msg) \
67  write_log(msg, ealogger::constants::LOG_LEVEL::EAL_DEBUG, __FILE__, \
68  __LINE__, __func__)
69 
73 #define eal_info(msg) \
74  write_log(msg, ealogger::constants::LOG_LEVEL::EAL_INFO, __FILE__, \
75  __LINE__, __func__)
76 
80 #define eal_warn(msg) \
81  write_log(msg, ealogger::constants::LOG_LEVEL::EAL_WARNING, __FILE__, \
82  __LINE__, __func__)
83 
87 #define eal_error(msg) \
88  write_log(msg, ealogger::constants::LOG_LEVEL::EAL_ERROR, __FILE__, \
89  __LINE__, __func__)
90 
94 #define eal_fatal(msg) \
95  write_log(msg, ealogger::constants::LOG_LEVEL::EAL_FATAL, __FILE__, \
96  __LINE__, __func__)
97 
101 #define eal_stack() \
102  write_log("", ealogger::constants::LOG_LEVEL::EAL_STACK, __FILE__, \
103  __LINE__, __func__)
104 
145 class Logger
146 {
147 public:
159  Logger(bool async = true);
160  ~Logger();
161 
182  void write_log(std::string msg, ealogger::constants::LOG_LEVEL lvl,
183  std::string file, int lnumber, std::string func);
184 
185 
186  // overload void write_log(std::string, ealogger::constants::LOG_LEVEL, std::string, int, std::string)
187 
199  void write_log(std::string msg, ealogger::constants::LOG_LEVEL lvl);
200 
219  void init_syslog_sink(bool enabled = true,
222  std::string msg_template = "%s: %m",
223  std::string datetime_pattern = "%F %T");
239  void init_console_sink(bool enabled = true,
242  std::string msg_template = "%d %s: %m",
243  std::string datetime_pattern = "%F %T");
244 
268  void init_file_sink(bool enabled = true,
271  std::string msg_template = "%d %s [%f:%l] %m",
272  std::string datetime_pattern = "%F %T",
273  std::string logfile = "ealogger_logfile.log",
274  bool flush_buffer = false);
275  // void init_file_sink_rotating(bool enabled,
276  // ealogger::constants::LOG_LEVEL min_lvl,
277  // std::string msg_template,
278  // std::string datetime_pattern,
279  // std::string logfile);
290 
299 
313  std::string msg_template);
325  std::string datetime_pattern);
332  void set_enabled(ealogger::constants::LOGGER_SINK sink, bool enabled);
341 
347  bool queue_empty();
348 
349 private:
351  std::mutex mtx_logger_stop;
352 
353  static bool signal_SIGUSR1;
354 
355  bool async;
356 
358  LogQueue log_msg_queue;
360  std::thread logger_thread;
362  bool logger_thread_stop;
363 
364  std::map<ealogger::constants::LOGGER_SINK, std::shared_ptr<Sink>>
365  logger_sink_map;
366  std::map<ealogger::constants::LOGGER_SINK, std::unique_ptr<std::mutex>>
367  logger_mutex_map;
368 
370  static void logrotate(int signo);
371 
372  void thread_entry_point();
373 
379  void internal_log_routine(std::shared_ptr<LogMessage> m);
380 
381  /*
382  * So far controlling the background logger thread is only possible for the
383  * logger object itself.
384  */
385  bool get_logger_thread_stop();
386  void set_logger_thread_stop(bool stop);
387 };
389 }
390 
391 #endif // EALOGGER_H
void set_msg_template(ealogger::constants::LOGGER_SINK sink, std::string msg_template)
Set message template for a Sink.
Definition: ealogger.cpp:155
void init_file_sink(bool enabled=true, ealogger::constants::LOG_LEVEL min_lvl=ealogger::constants::LOG_LEVEL::EAL_DEBUG, std::string msg_template="%d %s [%f:%l] %m", std::string datetime_pattern="%F %T", std::string logfile="ealogger_logfile.log", bool flush_buffer=false)
Initialize the simple file Sink.
Definition: ealogger.cpp:131
Logger(bool async=true)
Logger constructor.
Definition: ealogger.cpp:25
void set_enabled(ealogger::constants::LOGGER_SINK sink, bool enabled)
Activate or deactivate a Sink.
Definition: ealogger.cpp:177
ealogger main class
Definition: ealogger.h:145
void set_datetime_pattern(ealogger::constants::LOGGER_SINK sink, std::string datetime_pattern)
Set datetime conversion pattern for a Sink.
Definition: ealogger.cpp:166
Global ealogger constants.
Main namespace for ealogger.
Definition: conversion_pattern.h:28
void discard_sink(ealogger::constants::LOGGER_SINK sink)
Discard a Sink and delete the object.
Definition: ealogger.cpp:196
The LogQueue class represents a threadsafe queue ealogger uses to store log messages.
Definition: logqueue.h:49
LOGGER_SINK
Supported logger Sinks.
Definition: global.h:44
void init_syslog_sink(bool enabled=true, ealogger::constants::LOG_LEVEL min_lvl=ealogger::constants::LOG_LEVEL::EAL_DEBUG, std::string msg_template="%s: %m", std::string datetime_pattern="%F %T")
Init a syslog Sink.
Definition: ealogger.cpp:103
void write_log(std::string msg, ealogger::constants::LOG_LEVEL lvl, std::string file, int lnumber, std::string func)
Write a log message.
Definition: ealogger.cpp:73
void set_min_lvl(ealogger::constants::LOGGER_SINK sink, ealogger::constants::LOG_LEVEL min_level)
Set the minimum log message severity for a Sink.
Definition: ealogger.cpp:186
void init_console_sink(bool enabled=true, ealogger::constants::LOG_LEVEL min_lvl=ealogger::constants::LOG_LEVEL::EAL_DEBUG, std::string msg_template="%d %s: %m", std::string datetime_pattern="%F %T")
Initialize the console Sink.
Definition: ealogger.cpp:117
bool is_initialized(ealogger::constants::LOGGER_SINK sink)
Check if a Sink has been initialized already.
Definition: ealogger.cpp:209
bool queue_empty()
Check if the message queue is empty.
Definition: ealogger.cpp:222
LOG_LEVEL
An enumaration representing the supported loglevels.
Definition: global.h:58