Horizon Official Technical Documentation
Logger.hpp
Go to the documentation of this file.
1/***************************************************
2 * _ _ _ *
3 * | | | | (_) *
4 * | |_| | ___ _ __ _ _______ _ __ *
5 * | _ |/ _ \| '__| |_ / _ \| '_ \ *
6 * | | | | (_) | | | |/ / (_) | | | | *
7 * \_| |_/\___/|_| |_/___\___/|_| |_| *
8 ***************************************************
9 * This file is part of Horizon (c).
10 *
11 * Copyright (c) 2019 Sagun K. (sagunxp@gmail.com).
12 * Copyright (c) 2019 Horizon Dev Team.
13 *
14 * Base Author - Sagun K. (sagunxp@gmail.com)
15 *
16 * This library is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this library. If not, see <http://www.gnu.org/licenses/>.
28 **************************************************/
29
30#ifndef HORIZON_LOGGER_H
31#define HORIZON_LOGGER_H
32
33#include <cstring>
34#include <atomic>
35#include <mutex>
36
37#include <boost/log/core.hpp>
38#include <boost/log/trivial.hpp>
39#include <boost/log/sources/severity_logger.hpp>
40#include <boost/log/sinks.hpp>
41
42class Logger
43{
44private:
45 typedef boost::log::sources::severity_logger<boost::log::trivial::severity_level> logtype;
46
47public:
49 {
50 static Logger instance;
51
52 if (!instance._initialized)
53 instance.initialize();
54
55 return &instance;
56 }
57
58 void initialize();
59
61
62 boost::shared_ptr<boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend>> get_console_sink() { return _consoleSink; }
63
64 void colored_formatter(boost::log::record_view const& rec, boost::log::formatting_ostream& strm);
65
66 void shutdown();
67protected:
69
70 std::atomic<bool> _initialized;
71
72 boost::shared_ptr<boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend>> _consoleSink;
73 boost::shared_ptr<boost::log::sinks::asynchronous_sink<boost::log::sinks::text_file_backend>> _fileSink;
74};
75
77{
78public:
79 HLogStream(boost::log::trivial::severity_level level)
80 : level_(level) {}
81
82 template <typename T>
83 HLogStream& operator<<(const T& msg)
84 {
85 oss_ << msg;
86 return *this;
87 }
88
89 // Overload for std::ostream manipulators like std::hex, std::endl
90 HLogStream& operator<<(std::ostream& (*manip)(std::ostream&))
91 {
92 oss_ << manip;
93 return *this;
94 }
95
96 // Overload for std::string
97 HLogStream& operator<<(const std::string& msg)
98 {
99 oss_ << msg;
100 return *this;
101 }
102
103 // Overload for const char*
104 HLogStream& operator<<(const char* msg)
105 {
106 oss_ << msg;
107 return *this;
108 }
109
111 {
112 auto &lg = Logger().getInstance()->get_core_log();
113 lg.lock();
114 BOOST_LOG_SEV(lg, level_) << oss_.str();
115 lg.unlock();
116 }
117
118private:
119 boost::log::trivial::severity_level level_;
120 std::ostringstream oss_;
121};
122#define HLog(type) HLogStream(boost::log::trivial::type)
123#define HLogShutdown Logger().getInstance()->shutdown()
124#endif //HORIZON_LOGGER_H
Definition: Logger.hpp:77
HLogStream & operator<<(std::ostream &(*manip)(std::ostream &))
Definition: Logger.hpp:90
HLogStream & operator<<(const std::string &msg)
Definition: Logger.hpp:97
~HLogStream()
Definition: Logger.hpp:110
HLogStream & operator<<(const T &msg)
Definition: Logger.hpp:83
HLogStream & operator<<(const char *msg)
Definition: Logger.hpp:104
std::ostringstream oss_
Definition: Logger.hpp:120
boost::log::trivial::severity_level level_
Definition: Logger.hpp:119
HLogStream(boost::log::trivial::severity_level level)
Definition: Logger.hpp:79
Definition: Logger.hpp:43
logtype & get_core_log()
Definition: Logger.hpp:60
boost::shared_ptr< boost::log::sinks::asynchronous_sink< boost::log::sinks::text_file_backend > > _fileSink
Definition: Logger.hpp:73
logtype _core_log
Definition: Logger.hpp:68
boost::shared_ptr< boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend > > get_console_sink()
Definition: Logger.hpp:62
boost::log::sources::severity_logger< boost::log::trivial::severity_level > logtype
Definition: Logger.hpp:45
void shutdown()
Definition: Logger.cpp:133
static Logger * getInstance()
Definition: Logger.hpp:48
void colored_formatter(boost::log::record_view const &rec, boost::log::formatting_ostream &strm)
Definition: Logger.cpp:42
void initialize()
Definition: Logger.cpp:74
boost::shared_ptr< boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend > > _consoleSink
Definition: Logger.hpp:72
std::atomic< bool > _initialized
Definition: Logger.hpp:70