Comments (4)
Oh, that's interesting. What's the best way to test it?
from plog.
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <plog/Formatters/TxtFormatter.h>
#include <plog/Initializers/ConsoleInitializer.h>
#include <plog/Log.h>
static plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender(plog::streamStdErr);
struct termios original_attributes;
void enable_raw_mode() {
tcgetattr(STDIN_FILENO, &original_attributes);
struct termios raw = {0};
cfmakeraw(&raw);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
void disable_raw_mode() {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original_attributes);
}
int main() {
plog::init(plog::verbose, &consoleAppender);
enable_raw_mode();
bool forceCharacterReturn = false;
while (1) {
char c;
// To test: type some text and press 't' to toggle between forcing an extra \r in to the logging statement.
// Press 'q' to quit. (Cannot use ctrl+c because we are in raw mode.)
if (read(STDIN_FILENO, &c, 1) == 1) {
if (c == 't') {
forceCharacterReturn = !forceCharacterReturn;
}
PLOG_INFO << "c=" << c << " forceCR=" << forceCharacterReturn << (forceCharacterReturn ? "\r" : "");
if (c == 'q') {
break;
}
}
}
disable_raw_mode();
return 0;
}
type: a, b, c, t, d, e, f, q
t toggles on injecting a CR
q quits
$ g++ -I/repos/plog/include example.cc
$ ./a.out
2023-09-22 12:00:11.594 INFO [507366] [main@41] c=a forceCR=0
2023-09-22 12:00:11.783 INFO [507366] [main@41] c=b forceCR=0
2023-09-22 12:00:12.043 INFO [507366] [main@41] c=c forceCR=0
2023-09-22 12:00:13.537 INFO [507366] [main@41] c=t forceCR=1
2023-09-22 12:00:14.198 INFO [507366] [main@41] c=d forceCR=1
2023-09-22 12:00:14.309 INFO [507366] [main@41] c=e forceCR=1
2023-09-22 12:00:14.387 INFO [507366] [main@41] c=f forceCR=1
2023-09-22 12:00:19.802 INFO [507366] [main@41] c=q forceCR=1
There's multiple cases, I think, where \n is translated to \r\n. https://man7.org/linux/man-pages/man3/termios.3.html including even O_POST.
I'd probably recommend against testing this with terminals though, since CR support might be useful for other cases such as Windows-style output or other systems that just happen to expect \r\n. It just happened to be that I noticed this with raw terminal mode enabled.
from plog.
I think PLOG_INFO << "first line \n second line"
is expected to output 2 lines if raw mode support is properly implemented, right? So it's not only about \n
at the end but also in the middle?
from plog.
Correct. But I could argue that we shouldn't rewrite the user's log text itself. That could produce unexpected results if users think they are getting verbatim output, and it may require another scan through memory plus additional logic to inject the \r
midstream.
FWIW, I'd think of this more as supporting environments that just happen to expect a \r
on a per-log-line basis, rather than thinking of it as 'raw mode support'.
from plog.
Related Issues (20)
- [Feature Request] Single logger, but each appender has a different level HOT 4
- [Question] How to use Chained for multiple Classes within many DLL's
- [Question] How to add custom prefix (record) information? HOT 2
- Can't add ConsoleAppender in a if statement HOT 2
- std::filesystem::path issue with new utf-8 mode on MSVC/Windows HOT 5
- Capture stdout and redirect to plog HOT 11
- How to use plog library in mac, its not working in mac for me HOT 5
- Please add instruction how to run samples HOT 3
- Features say doesnt require c++11 but code uses c++11 HOT 3
- Plog not initializing with too long of a file name? HOT 3
- License + create custom logger for open62541 HOT 2
- How to log an indented multiline text? HOT 2
- SyslogFormatter HOT 1
- write to /var/log/syslog HOT 1
- Cannot resolve symbol "init" HOT 2
- README does not talk about "Initializers" at https://github.com/SergiusTheBest/plog?tab=readme-ov-file#step-2-initialization HOT 2
- How to chain multiple shared lib's log, and identify log content using log header or instance id? HOT 2
- DynamicAppender can not work good HOT 2
- hexdump .to_string / ascdump .to_string HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from plog.