Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect std::cout & std::cerr to vsg::Loggger #305

Merged
merged 5 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/app/vsgviewer/vsgviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ int main(int argc, char** argv)
// set up defaults and read command line arguments to override them
vsg::CommandLine arguments(&argc, argv);

// if we want to redirect std::cout and std::cerr to the vsg::Logger call vsg::Logger::redirect_stdout()
if (arguments.read({"--redirect-std", "-r"})) vsg::Logger::instance()->redirect_std();

// set up vsg::Options to pass in filepaths, ReaderWriters and other IO related options to use when reading and writing files.
auto options = vsg::Options::create();
options->sharedObjects = vsg::SharedObjects::create();
Expand Down
15 changes: 10 additions & 5 deletions examples/io/vsglog/vsglog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ class CustomLogger : public vsg::Inherit<vsg::Logger, CustomLogger>
protected:
void debug_implementation(const std::string_view& message) override
{
std::cout<<"custom debug : "<<message<<"\n";
fprintf(stdout, "custom debug: %.*s\n", static_cast<int>(message.length()), message.data());
}

void info_implementation(const std::string_view& message) override
{
std::cout<<"custom info : "<<message<<"\n";
fprintf(stdout, "custom info: %.*s\n", static_cast<int>(message.length()), message.data());
}

void warn_implementation(const std::string_view& message) override
{
std::cerr<<"custom warn : "<<message<<std::endl;
fprintf(stderr, "custom warn: %.*s\n", static_cast<int>(message.length()), message.data());
}

void error_implementation(const std::string_view& message) override
{
std::cerr<<"custom error : "<<message<<std::endl;
fprintf(stderr, "custom error: %.*s\n", static_cast<int>(message.length()), message.data());
}

void fatal_implementation(const std::string_view& message) override
{
std::cerr<<"custom error : "<<message<<std::endl;
fprintf(stderr, "custom error: %.*s\n", static_cast<int>(message.length()), message.data());
throw vsg::Exception{std::string(message)};
}
};
Expand All @@ -46,7 +46,12 @@ int main(int argc, char** argv)
// you can override the message verbosity by setting the minimum level that will be printed.
vsg::Logger::instance()->level = level;

// if we want to redirect std::cout and std::cerr to the vsg::Logger call vsg::Logger::redirect_stdout()
if (arguments.read({"--redirect-std", "-r"})) vsg::Logger::instance()->redirect_std();

// simplest form of messaging gets passed to the vsg::Logger::instance().
std::cout<<"cout cstring"<<std::endl;
std::cerr<<"cerr cstring"<<std::endl;
vsg::debug("debug string");
vsg::info("info cstring");
vsg::warn("warn cstring");
Expand Down
5 changes: 5 additions & 0 deletions examples/io/vsglog_mt/vsglog_mt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct MyOperation : public vsg::Inherit<vsg::Operation, MyOperation>
auto level = vsg::Logger::Level(1 + value % 4);
vsg::info("info() operation ",value);
vsg::log(level, "log() operation ",value);
std::cout<<"cout operation "<<value<<std::endl;
}
};

Expand All @@ -25,6 +26,10 @@ int main(int argc, char** argv)
mt_logger->setThreadPrefix(std::this_thread::get_id(), "main | ");

vsg::CommandLine arguments(&argc, argv);

// if we want to redirect std::cout and std::cerr to the vsg::Logger call vsg::Logger::redirect_stdout()
if (arguments.read({"--redirect-std", "-r"})) vsg::Logger::instance()->redirect_std();

auto numThreads = arguments.value<size_t>(16, "-t");
auto count = arguments.value<size_t>(100, "-n");
auto level = vsg::Logger::Level(arguments.value(0, "-l"));
Expand Down