Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Console.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_CORE_CONSOLE_H_
34#define DVD_CORE_CONSOLE_H_
35
38
39namespace Divide
40{
41
42constexpr int MAX_CONSOLE_ENTRIES = 128;
44{
45 template <typename... T>
46 NO_INLINE static void printfn(const char* format, T&&... args);
47 template <typename... T>
48 NO_INLINE static void printf(const char* format, T&&... args);
49 template <typename... T>
50 NO_INLINE static void warnfn(const char* format, T&&... args);
51 template <typename... T>
52 NO_INLINE static void warnf(const char* format, T&&... args);
53 template <typename... T>
54 NO_INLINE static void errorfn(const char* format, T&&... args);
55 template <typename... T>
56 NO_INLINE static void errorf(const char* format, T&&... args);
57
58 template <typename... T>
59 NO_INLINE static void d_printfn(const char* format, T&&... args);
60 template <typename... T>
61 NO_INLINE static void d_printf(const char* format, T&&... args);
62 template <typename... T>
63 NO_INLINE static void d_warnfn(const char* format, T&&... args);
64 template <typename... T>
65 NO_INLINE static void d_warnf(const char* format, T&&... args);
66 template <typename... T>
67 NO_INLINE static void d_errorfn(const char* format, T&&... args);
68 template <typename... T>
69 NO_INLINE static void d_errorf(const char* format, T&&... args);
70
71 template <typename... T>
72 NO_INLINE static void printfn(std::ofstream& outStream, const char* format, T&&... args);
73 template <typename... T>
74 NO_INLINE static void printf(std::ofstream& outStream, const char* format, T&&... args);
75 template <typename... T>
76 NO_INLINE static void warnfn(std::ofstream& outStream, const char* format, T&&... args);
77 template <typename... T>
78 NO_INLINE static void warnf(std::ofstream& outStream, const char* format, T&&... args);
79 template <typename... T>
80 NO_INLINE static void errorfn(std::ofstream& outStream, const char* format, T&&... args);
81 template <typename... T>
82 NO_INLINE static void errorf(std::ofstream& outStream, const char* format, T&&... args);
83 template <typename... T>
84 NO_INLINE static void d_printfn(std::ofstream& outStream, const char* format, T&&... args);
85 template <typename... T>
86 NO_INLINE static void d_printf(std::ofstream& outStream, const char* format, T&&... args);
87 template <typename... T>
88 NO_INLINE static void d_warnfn(std::ofstream& outStream, const char* format, T&&... args);
89 template <typename... T>
90 NO_INLINE static void d_warnf(std::ofstream& outStream, const char* format, T&&... args);
91 template <typename... T>
92 NO_INLINE static void d_errorfn(std::ofstream& outStream, const char* format, T&&... args);
93 template <typename... T>
94 NO_INLINE static void d_errorf(std::ofstream& outStream, const char* format, T&&... args);
95
96
97 enum class EntryType : U8
98 {
99 INFO = 0,
100 WARNING,
101 ERR,
102 COMMAND,
103 COUNT
104 };
105
106 enum class Flags : U8
107 {
111 DECORATE_FRAME = toBit( 4 ),
112 ENABLE_OUTPUT = toBit( 5 ),
114 PRINT_IMMEDIATE = toBit( 7 ),
115 COUNT = 7
116 };
117
119 {
120 string _text{};
122 };
123
124 using ConsolePrintCallback = std::function<void( const OutputEntry& )>;
126 {
128 size_t _id{ SIZE_MAX };
129 };
130
131 static void Flush();
132 static void Start( std::string_view logFilePath, std::string_view erroFilePath, bool printCopyright ) noexcept;
133 static void Stop();
134
135 static void ToggleFlag( const Flags flag, const bool state )
136 {
137 state ? s_flags |= to_base(flag) : s_flags &= ~to_base(flag);
138 }
139
140 [[nodiscard]] static bool IsFlagSet( const Flags flag ) { return s_flags & to_base(flag); }
141
142 [[nodiscard]] static size_t BindConsoleOutput(const ConsolePrintCallback& guiConsoleCallback);
143 [[nodiscard]] static bool UnbindConsoleOutput(size_t& index);
144
145 protected:
146 static void Output(std::string_view text, bool newline, EntryType type);
147 static void Output(std::ostream& outStream, std::string_view text, bool newline, EntryType type);
148 static void DecorateAndPrint(std::ostream& outStream, std::string_view text, bool newline, EntryType type);
149 static void PrintToFile(const OutputEntry& entry);
150
151 private:
152 static std::ofstream s_logStream;
153 static std::ofstream s_errorStream;
156 static U32 s_flags;
157 static std::atomic_bool s_running;
158};
159
160} // namespace Divide
161
162#endif //DVD_CORE_CONSOLE_H_
163
164#include "Console.inl"
#define NO_INLINE
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr int MAX_CONSOLE_ENTRIES
Definition: Console.h:42
uint8_t U8
std::shared_mutex SharedMutex
Definition: SharedMutex.h:43
eastl::vector< Type > vector
Definition: Vector.h:42
uint32_t U32
Project const SceneEntry & entry
Definition: DefaultScene.h:41
constexpr T toBit(const T X)
Converts an arbitrary positive integer value to a bitwise value used for masks.
constexpr auto to_base(const Type value) -> Type
Definition: Console.h:126
ConsolePrintCallback _cbk
Definition: Console.h:127
size_t _id
Definition: Console.h:128
Definition: Console.h:119
EntryType _type
Definition: Console.h:121
string _text
Definition: Console.h:120
static NO_INLINE void d_printfn(const char *format, T &&... args)
static NO_INLINE void d_printf(const char *format, T &&... args)
static NO_INLINE void d_errorf(std::ofstream &outStream, const char *format, T &&... args)
static NO_INLINE void printf(std::ofstream &outStream, const char *format, T &&... args)
static NO_INLINE void warnf(std::ofstream &outStream, const char *format, T &&... args)
static void ToggleFlag(const Flags flag, const bool state)
Definition: Console.h:135
static std::ofstream s_errorStream
Definition: Console.h:153
static NO_INLINE void errorfn(const char *format, T &&... args)
static NO_INLINE void d_errorfn(std::ofstream &outStream, const char *format, T &&... args)
static std::atomic_bool s_running
Definition: Console.h:157
static void DecorateAndPrint(std::ostream &outStream, std::string_view text, bool newline, EntryType type)
Definition: Console.cpp:41
static NO_INLINE void warnf(const char *format, T &&... args)
std::function< void(const OutputEntry &)> ConsolePrintCallback
Definition: Console.h:124
static NO_INLINE void d_printf(std::ofstream &outStream, const char *format, T &&... args)
static void PrintToFile(const OutputEntry &entry)
Definition: Console.cpp:106
static vector< ConsolePrintCallbackEntry > s_guiConsoleCallbacks
Definition: Console.h:155
static U32 s_flags
Definition: Console.h:156
static NO_INLINE void warnfn(const char *format, T &&... args)
static SharedMutex s_callbackLock
Definition: Console.h:154
static NO_INLINE void errorfn(std::ofstream &outStream, const char *format, T &&... args)
static void Flush()
Definition: Console.cpp:126
static NO_INLINE void d_warnf(std::ofstream &outStream, const char *format, T &&... args)
static NO_INLINE void printfn(const char *format, T &&... args)
static NO_INLINE void d_warnfn(const char *format, T &&... args)
static void Output(std::string_view text, bool newline, EntryType type)
Definition: Console.cpp:81
static NO_INLINE void d_errorf(const char *format, T &&... args)
static bool IsFlagSet(const Flags flag)
Definition: Console.h:140
static NO_INLINE void d_warnf(const char *format, T &&... args)
static bool UnbindConsoleOutput(size_t &index)
Definition: Console.cpp:206
static NO_INLINE void d_errorfn(const char *format, T &&... args)
static NO_INLINE void printfn(std::ofstream &outStream, const char *format, T &&... args)
static NO_INLINE void errorf(const char *format, T &&... args)
static NO_INLINE void d_printfn(std::ofstream &outStream, const char *format, T &&... args)
static NO_INLINE void printf(const char *format, T &&... args)
static size_t BindConsoleOutput(const ConsolePrintCallback &guiConsoleCallback)
Definition: Console.cpp:194
static NO_INLINE void d_warnfn(std::ofstream &outStream, const char *format, T &&... args)
static void Start(std::string_view logFilePath, std::string_view erroFilePath, bool printCopyright) noexcept
Definition: Console.cpp:144
static void Stop()
Definition: Console.cpp:179
static std::ofstream s_logStream
Definition: Console.h:152
static NO_INLINE void errorf(std::ofstream &outStream, const char *format, T &&... args)
static NO_INLINE void warnfn(std::ofstream &outStream, const char *format, T &&... args)