Git Product home page Git Product logo

Comments (4)

SergiusTheBest avatar SergiusTheBest commented on August 12, 2024

Oh, that's interesting. What's the best way to test it?

from plog.

davidfiala avatar davidfiala commented on August 12, 2024
#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.

SergiusTheBest avatar SergiusTheBest commented on August 12, 2024

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.

davidfiala avatar davidfiala commented on August 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.