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 <switchboard/export.h>
12#include <switchboard_core/LogDestination.hpp>
13#include <switchboard_core/LogLevel.hpp>
14
15#include <string>
16#include <sstream>
17
18namespace switchboard {
19
24class SWITCHBOARDSDK_EXPORT Logger {
25public:
29 static void setLogDestination(LogDestination* destination);
30
37
43 static void setLogLevel(LogLevel level);
44
50 static LogLevel getLogLevel();
51
57 static void trace(const std::string& logMessage);
58
64 static void debug(const std::string& logMessage);
65
71 static void info(const std::string& logMessage);
72
78 static void warning(const std::string& logMessage);
79
85 static void error(const std::string& logMessage);
86
90 class LogStream {
91 public:
92 explicit LogStream(LogLevel level);
93 LogStream(const LogStream&) = delete;
94 LogStream& operator=(const LogStream&) = delete;
95 LogStream(LogStream&& other) noexcept;
96
97 LogStream& operator=(LogStream&& other) noexcept;
98
99 ~LogStream();
100
101 template<typename T>
102 LogStream& operator<<(const T& value) {
103 stream << value;
104 return *this;
105 }
106
107 // Handle std::endl and other manipulators
108 LogStream& operator<<(std::ostream& (*manip)(std::ostream&)) {
109 stream << manip;
110 return *this;
111 }
112
113 private:
114 LogLevel level;
115 std::ostringstream stream;
116 };
117
121 struct LogStreamFactory {
122 LogLevel level;
123 explicit LogStreamFactory(const LogLevel level) : level(level) {}
124
125 template<typename T>
126 LogStream operator<<(const T& value) {
127 LogStream stream(level);
128 stream << value;
129 return stream;
130 }
131 };
132
133 static LogStreamFactory traceStream;
134 static LogStreamFactory debugStream;
135 static LogStreamFactory infoStream;
136 static LogStreamFactory warningStream;
137 static LogStreamFactory errorStream;
138
139private:
140 static LogLevel logLevel;
141 static LogDestination* destination;
142};
143
144}
Provides an interface to implement platform-specific logging.
Definition LogDestination.hpp:22
Stream-like interface for logging at different levels.
Definition Logger.hpp:90
Provides logging functionality.
Definition Logger.hpp:24
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:121