easyloggingpp icon indicating copy to clipboard operation
easyloggingpp copied to clipboard

Auto EOL

Open jbakosi opened this issue 2 years ago • 2 comments

Is there an automatic way of processing long strings (containing EOL '\n') to appear correctly-prefixed with the FORMAT prefix in the log?

That is, when logging

std::string longone( "Results: 14 results found.\nMatches 1-10: long string\nanother long string")
LOG(DEBUG) << longone;

instead of

2022-07-06 18:22:35: DEBUG: Results: 14 results found.
Matches 1-10: long string
another long string

I would like

2022-07-06 18:22:35: DEBUG: Results: 14 results found.
2022-07-06 18:22:35: DEBUG: Matches 1-10: long string
2022-07-06 18:22:35: DEBUG: another long string

jbakosi avatar Jul 06 '22 17:07 jbakosi

Answering my own question, with

void longlog( std::string& s, int width = 70 ) {
  auto logline = []( char*& e, char* b ){
    while (*e == ' ') ++e;
    char tmp = *e;
    *e = 0;
    NLOG(INFO) << b;
    *e = tmp;
  };
  char* b = &s[0];
  char* e = b;
  while (*(++e) != 0) {
    if (*e == '\n' || (e-b > width && *e == ' ')) {
      logline( e, b );
      b = e;
    }
  }
  if (e > b) logline( e, b );
}

I can do

longlog( longone );

Now if I could call that consistent with other log comands, e.g.,

LOG(INFO) << longone;

that would be great.

jbakosi avatar Jul 07 '22 10:07 jbakosi

@jbakosi May i work on this issue ? I'm new to open source and I'd like to contribute

the-WINTERSOLDIER avatar Oct 15 '22 12:10 the-WINTERSOLDIER