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#include "StringsExport.h"
6
7namespace SC
8{
9struct String;
10
13
25struct SC_STRINGS_EXPORT Console
26{
29 Console(Span<char> conversionBuffer = {});
30
33 template <typename... Types>
34 bool print(StringSpan fmt, Types&&... args)
35 {
36 return printInternal(true, fmt, forward<Types>(args)...);
37 }
38
41 template <typename... Types>
42 bool printError(StringSpan fmt, Types&&... args)
43 {
44 return printInternal(false, fmt, forward<Types>(args)...);
45 }
46
48 void print(const StringSpan str);
49
51 void printError(const StringSpan str);
52
54 void flush();
55
58
60 void printLine(const StringSpan str);
61
63 void printErrorLine(const StringSpan str);
64
68
69 private:
70 template <typename... Types>
71 bool printInternal(bool useStdOut, StringSpan fmt, Types&&... args)
72 {
73 StringFormatOutput output(fmt.getEncoding(), *this, useStdOut);
74 if (fmt.getEncoding() == StringEncoding::Ascii || fmt.getEncoding() == StringEncoding::Utf8)
75 {
76 // It's ok parsing format string '{' and '}' both for utf8 and ascii with StringIteratorASCII
77 // because on a valid UTF8 string, these chars are unambiguously recognizable
78 return StringFormat<StringIteratorASCII>::format(output, fmt, forward<Types>(args)...);
79 }
80 return false; // UTF16/32 format strings are not supported
81 }
82 Span<char> conversionBuffer;
83#if SC_PLATFORM_WINDOWS
84 void printWindows(const StringSpan str, void* handle);
85
86 void* hStdOut;
87 void* hStdErr;
88 bool isConsole = true;
89 bool isDebugger = true;
90#endif
91};
92
94
95} // namespace SC
96extern SC::Console* globalConsole;
97#if !defined(SC_LOG_MESSAGE)
98#define SC_LOG_MESSAGE(fmt, ...) \
99 if (globalConsole) \
100 globalConsole->print(fmt, ##__VA_ARGS__)
101#endif
Writes to console using SC::StringFormat.
Definition Console.h:26
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:34
bool printError(StringSpan fmt, Types &&... args)
Prints a formatted string using SC::StringFormat to stderr.
Definition Console.h:42
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:27
Formats String with a simple DSL embedded in the format string.
Definition StringFormat.h:84
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