Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
print.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
17#include "shambase/print.hpp"
18#include <iostream>
19#include <ostream>
20
21namespace shambase {
22
23 using printer_t = void (*)(std::string_view);
24 using flush_t = void (*)();
25
26 static printer_t _printer = nullptr;
27 static printer_t _printerln = nullptr;
28 static flush_t _flush = nullptr;
29
30 void print(std::string_view s) {
31 if (_printer == nullptr) {
32 std::cout << s;
33 } else {
34 _printer(s);
35 }
36 }
37 void println(std::string_view s) {
38 if (_printerln == nullptr) {
39 std::cout << s << "\n";
40 } else {
41 _printerln(s);
42 }
43 }
44 void flush() {
45 if (_flush == nullptr) {
46 std::cout << std::flush;
47 } else {
48 _flush();
49 }
50 }
51
53 void (*func_printer_normal)(std::string_view),
54 void (*func_printer_ln)(std::string_view),
55 void (*func_flush_func)()) {
56 _printer = func_printer_normal;
57 _printerln = func_printer_ln;
58 _flush = func_flush_func;
59 }
60
61 void reset_std_behavior() { change_printer(nullptr, nullptr, nullptr); }
62
63} // namespace shambase
namespace for basic c++ utilities
void println(std::string_view sv)
Prints a string to the console followed by a newline.
Definition print.cpp:37
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
void print(std::string_view sv)
Prints a string to the console.
Definition print.cpp:30
void flush()
Flushes the output buffer.
Definition print.cpp:44
void reset_std_behavior()
Restores the default behavior of the print and println functions.
Definition print.cpp:61
void change_printer(void(*func_printer_normal)(std::string_view), void(*func_printer_ln)(std::string_view), void(*func_flush_func)())
Changes the behavior of the print, println and flush functions.
Definition print.cpp:52