Switchboard Extension SDK
Loading...
Searching...
No Matches
Logger.hpp
1//
2// Logger.hpp
3// SwitchboardSDK
4//
5// Created by Balázs Kiss on 2017. 10. 05..
6// Copyright © 2017. Synervoz Inc. All rights reserved.
7//
8
9#pragma once
10
11#include "LogDestination.hpp"
12#include "LogLevel.hpp"
13
14#include <string>
15#include <sstream>
16
17namespace switchboard {
18
23class Logger {
24public:
28 static void setLogDestination(LogDestination* destination);
29
36
42 static void setLogLevel(LogLevel level);
43
49 static LogLevel getLogLevel();
50
56 static void trace(const std::string& logMessage);
57
63 static void debug(const std::string& logMessage);
64
70 static void info(const std::string& logMessage);
71
77 static void warning(const std::string& logMessage);
78
84 static void error(const std::string& logMessage);
85
89 class LogStream {
90 public:
91 explicit LogStream(LogLevel level);
92 LogStream(const LogStream&) = delete;
93 LogStream& operator=(const LogStream&) = delete;
94 LogStream(LogStream&& other) noexcept;
95
96 LogStream& operator=(LogStream&& other) noexcept;
97
98 ~LogStream();
99
100 template<typename T>
101 LogStream& operator<<(const T& value) {
102 stream << value;
103 return *this;
104 }
105
106 // Handle std::endl and other manipulators
107 LogStream& operator<<(std::ostream& (*manip)(std::ostream&)) {
108 stream << manip;
109 return *this;
110 }
111
112 private:
113 LogLevel level;
114 std::ostringstream stream;
115 };
116
120 struct LogStreamFactory {
121 LogLevel level;
122 explicit LogStreamFactory(const LogLevel level) : level(level) {}
123
124 template<typename T>
125 LogStream operator<<(const T& value) {
126 LogStream stream(level);
127 stream << value;
128 return std::move(stream);
129 }
130 };
131
132 static LogStreamFactory traceStream;
133 static LogStreamFactory debugStream;
134 static LogStreamFactory infoStream;
135 static LogStreamFactory warningStream;
136 static LogStreamFactory errorStream;
137
138private:
139 static LogLevel logLevel;
140 static LogDestination* destination;
141};
142
143}
Provides an interface to implement platform-specific logging.
Definition LogDestination.hpp:21
Stream-like interface for logging at different levels.
Definition Logger.hpp:89
Provides logging functionality.
Definition Logger.hpp:23
static LogDestination * getLogDestination()
Gets the log destination of the logger.
static void error(const std::string &logMessage)
Logs an error level message.
static void setLogDestination(LogDestination *destination)
Sets the log destination of the logger.
static LogLevel getLogLevel()
Gets the log level of the logger.
static void debug(const std::string &logMessage)
Logs a debug level message.
static void info(const std::string &logMessage)
Logs an info level message.
static void setLogLevel(LogLevel level)
Sets the log level of the logger.
static void trace(const std::string &logMessage)
Logs a trace level message.
static void warning(const std::string &logMessage)
Logs a warning level message.
Helper factory for creating LogStream instances at different log levels.
Definition Logger.hpp:120