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

Avoiding iostream? #187

Open
alecazam opened this issue Sep 9, 2023 · 1 comment
Open

Avoiding iostream? #187

alecazam opened this issue Sep 9, 2023 · 1 comment

Comments

@alecazam
Copy link

alecazam commented Sep 9, 2023

I really like your work on the language. But your samples use a lot of cout. I find any use of cin/cout/cerr to be flawed in C++. Just raising this, since you don't cite iostream as a C++ flaw.

iostream use is not atomic, requires endl or \n, requires multiple complex formatting functions, can't be stripped, and gets interleaved since the flush can occur at an time. Plus iostream use spreads to other libraries that then implement operator << >>, instead of simply returning a string operator.

I've seen then logging system adopt iostream code, since they have operator << >> defined, so why not. Then it's a nightmare to try and lock and ensure non-interleaved logs across an app. Leave support in for committee members who wrote/think it is an amazing library, but my code is much smaller leaving out of all of iostream for fprintf/std::format type constructs.

Cleaning it up with format helps a bit, but there's no way to skip all this formatting work when this moves to a log system. Use of raw stdout/stderr, and cout/cerr should really be discouraged. This needs the filtering/formatting of a log system. That can add threadName, file, line, level and report specific data to system specific loggers like Android logcat. These systems need the individual fields, and not data formatted into the message of the log.

std::cout<< "Got index "<< i<< "\n";
std::cerr<< "Got index "<< i<< "\n"; // when does this go out?
std::cerr<< "Got index "<< i<< " and " << obj.toString() << "\n";

cleanup

std::cerr << "Got index {} and {}\n".format(i, obj.toString());

I'd much prefer these kinds of constructs instead.

fprintf( stdout, "Got index %d\n", i );

fprintf( stderr, "Got index %d and string %s\n", obj.toString().c_str() );

// can easily strip, early out on  debug level, no need to add "\n"
// varargs don't do any formatting if debug level or group is filtered (or macro stripped)
LOGD( "GroupName", "Got index %d", i ); 

LOGD( "GroupName", "Got string %s", obj.toString().c_str() );

@alecazam
Copy link
Author

alecazam commented Sep 9, 2023

Here are two examples of logging systems from my project kram for reference. I need to unify to one set of macros to one set. fmt has object support and the system sprintf is typically non-extensible in that regard.

https://github.com/alecazam/kram/blob/main/libkram/kram/KramLog.h
https://github.com/alecazam/kram/blob/main/libkram/kram/KramFmt.h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant