Sane C++ Libraries
C++ Platform Abstraction Libraries
Console.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Containers/Vector.h"
5#include "../Strings/StringFormat.h"
6#include "../Strings/StringView.h"
7
8namespace SC
9{
10struct String;
11
14
27{
30 Console(Vector<char>& encodingConversionBuffer);
31
37 template <typename... Types>
38 bool print(StringView fmt, Types&&... args)
39 {
40 StringFormatOutput output(fmt.getEncoding(), *this);
42 {
43 // It's ok parsing format string '{' and '}' both for utf8 and ascii with StringIteratorASCII
44 // because on a valid UTF8 string, these chars are unambiguously recognizable
45 return StringFormat<StringIteratorASCII>::format(output, fmt, forward<Types>(args)...);
46 }
47 return false; // UTF16/32 format strings are not supported
48 }
49
52 void print(const StringView str);
53
56 void printLine(const StringView str);
57
61
63 static bool isAttachedToConsole();
64
65 private:
66 Vector<char>& encodingConversionBuffer;
67#if SC_PLATFORM_WINDOWS
68 void* handle;
69 bool isConsole = true;
70 bool isDebugger = true;
71#endif
72};
73
75
76} // namespace SC
77extern SC::Console* globalConsole;
78#if !defined(SC_LOG_MESSAGE)
79#define SC_LOG_MESSAGE(fmt, ...) \
80 if (globalConsole) \
81 globalConsole->print(fmt, ##__VA_ARGS__)
82#endif
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition: Compiler.h:78
@ Ascii
Encoding is ASCII.
@ Utf8
Encoding is UTF8.
Writes to console using SC::StringFormat.
Definition: Console.h:27
bool print(StringView fmt, Types &&... args)
Prints a formatted string using SC::StringFormat.
Definition: Console.h:38
Console(Vector< char > &encodingConversionBuffer)
Constructs a console with a conversion buffer used for string conversions (UTF8 / UTF16)
static bool isAttachedToConsole()
Returns true if current process has an active console (Windows only, returns true elsewhere)
void printLine(const StringView str)
Prints a StringView to console and adds a newline at the end of it.
static bool tryAttachingToParentConsole()
Tries attaching current process to parent console (Windows only, has no effect elsewhere)
void print(const StringView str)
Prints a StringView to console.
static bool format(StringFormatOutput &data, StringView fmt, Types &&... args)
Formats fmt StringView using simple DSL where {} are replaced with args.
Definition: StringFormat.h:203
Allows pushing results of StringFormat to a buffer or to the console.
Definition: StringFormat.h:22
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47
constexpr StringEncoding getEncoding() const
Get encoding of this StringView.
Definition: StringView.h:93
A contiguous sequence of heap allocated elements.
Definition: Vector.h:51