Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
Console.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Strings/StringFormat.h"
5
6namespace SC
7{
8struct String;
9
12
24struct SC_COMPILER_EXPORT Console
25{
28 Console(Span<char> conversionBuffer = {});
29
32 template <typename... Types>
33 bool print(StringSpan fmt, Types&&... args)
34 {
35 return printInternal(true, fmt, forward<Types>(args)...);
36 }
37
40 template <typename... Types>
41 bool printError(StringSpan fmt, Types&&... args)
42 {
43 return printInternal(false, fmt, forward<Types>(args)...);
44 }
45
47 void print(const StringSpan str);
48
50 void printError(const StringSpan str);
51
53 void flush();
54
57
59 void printLine(const StringSpan str);
60
62 void printErrorLine(const StringSpan str);
63
67
68 private:
69 template <typename... Types>
70 bool printInternal(bool useStdOut, StringSpan fmt, Types&&... args)
71 {
72 StringFormatOutput output(fmt.getEncoding(), *this, useStdOut);
73 if (fmt.getEncoding() == StringEncoding::Ascii || fmt.getEncoding() == StringEncoding::Utf8)
74 {
75 // It's ok parsing format string '{' and '}' both for utf8 and ascii with StringIteratorASCII
76 // because on a valid UTF8 string, these chars are unambiguously recognizable
77 return StringFormat<StringIteratorASCII>::format(output, fmt, forward<Types>(args)...);
78 }
79 return false; // UTF16/32 format strings are not supported
80 }
81 Span<char> conversionBuffer;
82#if SC_PLATFORM_WINDOWS
83 void printWindows(const StringSpan str, void* handle);
84
85 void* hStdOut;
86 void* hStdErr;
87 bool isConsole = true;
88 bool isDebugger = true;
89#endif
90};
91
93
94} // namespace SC
95extern SC::Console* globalConsole;
96#if !defined(SC_LOG_MESSAGE)
97#define SC_LOG_MESSAGE(fmt, ...) \
98 if (globalConsole) \
99 globalConsole->print(fmt, ##__VA_ARGS__)
100#endif
Writes to console using SC::StringFormat.
Definition Console.h:25
void flush()
Flushes stdout.
void print(const StringSpan str)
Prints a string to console.
void printLine(const StringSpan str)
Prints a string to stdout and adds a newline at the end of it.
void flushStdErr()
Flushes stderr.
void printError(const StringSpan str)
Prints a string to stderr.
bool print(StringSpan fmt, Types &&... args)
Prints a formatted string using SC::StringFormat to stdout.
Definition Console.h:33
bool printError(StringSpan fmt, Types &&... args)
Prints a formatted string using SC::StringFormat to stderr.
Definition Console.h:41
void printErrorLine(const StringSpan str)
Prints a string to stderr 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)
Console(Span< char > conversionBuffer={})
Constructs a console with an OPTIONAL conversion buffer used for UTF encoding conversions on Windows.
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
Definition StringFormat.h:26
Formats String with a simple DSL embedded in the format string.
Definition StringFormat.h:83
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37
constexpr StringEncoding getEncoding() const
Get encoding of this StringView.
Definition StringSpan.h:98