abumq / easyloggingpp

C++ logging library. It is powerful, supports asynchronous low latency, extendable, light-weight, fast performing, thread and type safe and consists of many built-in features. It provides ability to write logs in your own customized format. It also provide support for logging your classes, third-party libraries, STL and third-party containers etc.
MIT License
3.81k stars 929 forks source link

Auto EOL #814

Open jbakosi opened 2 years ago

jbakosi commented 2 years ago

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 commented 2 years ago

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.

the-WINTERSOLDIER commented 2 years ago

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